fork.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <stdio.h>
  2. #include <sys/mman.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include "../graphene-ipc.h"
  9. #define PAGE_SIZE 4096
  10. #define MAX_PIDBUF 7
  11. #define TEST_PAGES 20
  12. struct mapping {
  13. unsigned long addr;
  14. unsigned long len;
  15. unsigned long perms;
  16. unsigned long flags;
  17. };
  18. int main(int argc, char **argv)
  19. {
  20. char *pages[TEST_PAGES];
  21. int p0[2];
  22. int64_t token;
  23. if (argc == 1) {
  24. // Server
  25. int pid;
  26. struct gipc_send gs;
  27. unsigned long addr[TEST_PAGES];
  28. unsigned long len[TEST_PAGES];
  29. int gfd;
  30. int i;
  31. int rv;
  32. /* Map some anonymous memory, fill with crap */
  33. for (i = 0; i < TEST_PAGES; i++) {
  34. pages[i] = mmap(NULL, PAGE_SIZE * TEST_PAGES,
  35. PROT_READ|PROT_WRITE,
  36. MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  37. if (!pages[i]) {
  38. perror("mmap");
  39. return -1;
  40. }
  41. memset(pages[i], 'a' + i, 256);
  42. pages[i][256] = '\0';
  43. }
  44. /* Set up a communications channel */
  45. if (pipe(p0) == -1) {
  46. printf("Pipe failed %d\n", errno);
  47. return -1;
  48. }
  49. /* fork + exec this program with an arg */
  50. pid = fork();
  51. if (pid == -1) {
  52. perror("Fork");
  53. return -1;
  54. } else if (pid == 0) {
  55. char *args[3];
  56. args[0] = argv[0];
  57. args[1] = "quack";
  58. args[2] = NULL;
  59. close(p0[1]);
  60. dup2(p0[0], 0);
  61. execv(argv[0], args);
  62. printf("Failed to EXEC %s %s!, %d\n", argv[0], args[0], errno);
  63. return -1;
  64. }
  65. /* Parent */
  66. close (p0[0]);
  67. /* Open an IPC link */
  68. gfd = open (GIPC_FILE, O_RDWR);
  69. if (gfd < 0) {
  70. printf ("Fd is %d %d\n", gfd, errno);
  71. return -1;
  72. }
  73. token = ioctl(gfd, GIPC_CREATE, 0);
  74. if (token < 0) {
  75. printf ("Failed to create the gipc queue. %d\n", errno);
  76. return -1;
  77. }
  78. /* Write the token to the child */
  79. rv = write(p0[1], &token, sizeof(token));
  80. if ( rv != sizeof(token)) {
  81. perror("Failed to write map size\n");
  82. return -1;
  83. }
  84. /* Send each region */
  85. gs.entries = TEST_PAGES;
  86. gs.addr = addr;
  87. gs.len = len;
  88. i = TEST_PAGES;
  89. rv = write(p0[1], &i, sizeof(int));
  90. if ( rv != sizeof(int)) {
  91. perror("Failed to write map size\n");
  92. return -1;
  93. }
  94. for (i = 0; i < TEST_PAGES; i++) {
  95. struct mapping map = {(unsigned long) pages[i],
  96. TEST_PAGES * PAGE_SIZE,
  97. PROT_READ|PROT_WRITE,
  98. MAP_PRIVATE|MAP_ANONYMOUS};
  99. rv = write(p0[1], &map, sizeof(map));
  100. if ( rv != sizeof(map)) {
  101. perror("Failed to write map\n");
  102. return -1;
  103. }
  104. addr[i] = (unsigned long) pages[i];
  105. len[i] = TEST_PAGES * PAGE_SIZE;
  106. }
  107. rv = ioctl(gfd, GIPC_SEND, &gs);
  108. if (rv)
  109. printf ("[server] Bad rv %d (2)\n", errno);
  110. close (gfd);
  111. close (p0[1]);
  112. /* Exit */
  113. wait();
  114. } else {
  115. int rv, gfd;
  116. int max, i;
  117. struct gipc_recv gr;
  118. /* Read the token from parent */
  119. rv = read(0, &token, sizeof(token));
  120. if (rv != sizeof(token)) {
  121. perror ("Size read failed\n");
  122. return -1;
  123. }
  124. /* Open the gipc channel */
  125. gfd = open (GIPC_FILE, O_RDWR);
  126. if (gfd < 0) {
  127. printf ("Fd is %d %d\n", gfd, errno);
  128. return -1;
  129. }
  130. /* Join the queue */
  131. rv = ioctl(gfd, GIPC_JOIN, token);
  132. if (rv < 0) {
  133. printf ("Failed to join GIPC queue - %d\n", errno);
  134. return -1;
  135. }
  136. /* Read count of descriptors */
  137. rv = read(0, &max, sizeof(int));
  138. if (rv != sizeof(int)) {
  139. perror ("Size read failed\n");
  140. return -1;
  141. }
  142. /* For each memory descriptor on the pipe */
  143. for (i = 0; i < max; i++) {
  144. struct mapping map;
  145. rv = read(0, &map, sizeof(map));
  146. if (rv != sizeof(map)) {
  147. perror ("Size read failed\n");
  148. return -1;
  149. }
  150. gr.len = map.len;
  151. gr.addr = map.addr;
  152. // Perms?
  153. /* map the memory */
  154. rv = ioctl(gfd, GIPC_RECV, &gr);
  155. if (rv) {
  156. printf ("Bad map %p rv %d %d\n", (void *)gr.addr, rv, errno);
  157. return 1;
  158. }
  159. /* Do some sanity checks that it worked */
  160. if ('a' + i != ((char *) map.addr)[250]) {
  161. printf("Failed to map right shiz\n");
  162. return -1;
  163. }
  164. printf("%s\n", (char *) map.addr);
  165. }
  166. close(gfd);
  167. }
  168. return 0;
  169. }