#include #include #include #include #include #include #include #include int main(int argc, char** argv) { void* map_shr; void* map_tab; int ret; int status; int s_to_c[2]; int c_to_s[2]; /* setup pipes for communication */ if(pipe(s_to_c) == -1) { perror("pipe"); exit(EXIT_FAILURE); } if(pipe(c_to_s) == -1) { perror("pipe"); exit(EXIT_FAILURE); } if (fork() != 0) /* Parent code*/ { uint32_t ref; char buf[1]; xc_gntshr *shr_h = xc_gntshr_open(NULL, 0); printf("I'm server, with pid %d\n", getpid()); if (shr_h == NULL) { perror("xc_gntshr_open"); exit(EXIT_FAILURE); } map_shr = xc_gntshr_share_pages(shr_h, 0, 1, &ref, 1); if (map_shr == NULL) { perror("xc_gntshr_share_pages"); exit(EXIT_FAILURE); } /* Send the gntref to the client. */ write(s_to_c[1], &ref, sizeof(uint32_t)); read(c_to_s[0], buf, 1); /* Now we unshare the page */ ret = xc_gntshr_munmap(shr_h, map_shr, 1); if (ret != 0) { perror("xc_gntshr_munmap"); exit(EXIT_FAILURE); } /* At this point, the kernel should complain… */ /* Waiting for the child to die. */ wait(&status); printf("Children died with status %d\n", status); return 0; } else /* Child code */ { uint32_t ref; xc_gnttab *tab_h = xc_gnttab_open(NULL, 0); printf("I'm client, with pid %d\n", getpid()); if (tab_h == NULL) { perror("xc_gnttab_open"); exit(EXIT_FAILURE); } /* Receive the ref from the server. */ read(s_to_c[0], &ref, sizeof(uint32_t)); /* Ready to map! */ map_tab = xc_gnttab_map_grant_ref(tab_h, 0, ref, PROT_READ|PROT_WRITE); if (map_tab == NULL) { perror("xc_gnttab_map_grant_ref"); exit(EXIT_FAILURE); } /* Sending a msg to server to indicate that he can now unshare. */ write(c_to_s[1], "\0", 1); return 0; } return 0; }