unix.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* NOTE: Under Graphene, this test must be run only in fork mode.
  2. * This is due to Graphene restricting communication via Unix
  3. * domain sockets only for processes in same Graphene instance
  4. * (i.e. only between parent and its child in this test).
  5. */
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/socket.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include <sys/un.h>
  14. #include <sys/wait.h>
  15. #include <unistd.h>
  16. enum { SINGLE, PARALLEL } mode = PARALLEL;
  17. int do_fork = 0;
  18. int pipefds[2];
  19. int server_dummy_socket(void) {
  20. int create_socket;
  21. struct sockaddr_un address;
  22. if ((create_socket = socket(AF_UNIX, SOCK_STREAM, 0)) > 0)
  23. printf("Dummy socket was created\n");
  24. address.sun_family = AF_UNIX;
  25. strncpy(address.sun_path, "dummy", sizeof(address.sun_path));
  26. if (bind(create_socket, (struct sockaddr*)&address, sizeof(address)) < 0) {
  27. perror("bind");
  28. close(create_socket);
  29. exit(1);
  30. }
  31. if (listen(create_socket, 3) < 0) {
  32. perror("listen");
  33. close(create_socket);
  34. exit(1);
  35. }
  36. /* do not close this socket to test two sockets in parallel */
  37. return 0;
  38. }
  39. int server(void) {
  40. int create_socket, new_socket;
  41. socklen_t addrlen;
  42. int bufsize = 1024;
  43. char* buffer = malloc(bufsize);
  44. struct sockaddr_un address;
  45. if ((create_socket = socket(AF_UNIX, SOCK_STREAM, 0)) > 0)
  46. printf("The socket was created\n");
  47. address.sun_family = AF_UNIX;
  48. strncpy(address.sun_path, "u", sizeof(address.sun_path));
  49. if (bind(create_socket, (struct sockaddr*)&address, sizeof(address)) < 0) {
  50. perror("bind");
  51. close(create_socket);
  52. exit(1);
  53. }
  54. if (listen(create_socket, 3) < 0) {
  55. perror("listen");
  56. close(create_socket);
  57. exit(1);
  58. }
  59. if (mode == PARALLEL) {
  60. close(pipefds[0]);
  61. char byte = 0;
  62. if (write(pipefds[1], &byte, 1) != 1) {
  63. perror("write error");
  64. exit(1);
  65. }
  66. }
  67. addrlen = sizeof(address);
  68. new_socket = accept(create_socket, (struct sockaddr*)&address, &addrlen);
  69. if (new_socket < 0) {
  70. perror("accept");
  71. close(create_socket);
  72. exit(1);
  73. }
  74. close(create_socket);
  75. printf("The client is connected...\n");
  76. if (do_fork) {
  77. if (fork() > 0) {
  78. __asm__ volatile ("int $3");
  79. close(new_socket);
  80. wait(NULL);
  81. return 0;
  82. }
  83. }
  84. for (int i = 0; i < 10; i++) {
  85. sprintf(buffer, "Data: This is packet %d\n", i);
  86. if (sendto(new_socket, buffer, strlen(buffer), 0, 0, 0) == -1) {
  87. fprintf(stderr, "sendto() failed\n");
  88. exit(1);
  89. }
  90. }
  91. close(new_socket);
  92. if (do_fork)
  93. exit(0);
  94. return 0;
  95. }
  96. int client(void) {
  97. int count, create_socket;
  98. int bufsize = 1024;
  99. char* buffer = malloc(bufsize);
  100. struct sockaddr_un address;
  101. if (mode == PARALLEL) {
  102. close(pipefds[1]);
  103. char byte = 0;
  104. if (read(pipefds[0], &byte, 1) != 1) {
  105. perror("read error");
  106. return 1;
  107. }
  108. }
  109. if ((create_socket = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0)
  110. printf("The socket was created\n");
  111. address.sun_family = AF_UNIX;
  112. strncpy(address.sun_path, "u", sizeof(address.sun_path));
  113. if (connect(create_socket, (struct sockaddr*)&address, sizeof(address)) == 0)
  114. printf("The connection was accepted with the server\n");
  115. else {
  116. printf("The connection was not accepted with the server\n");
  117. exit(0);
  118. }
  119. if (do_fork) {
  120. if (fork() > 0) {
  121. close(create_socket);
  122. wait(NULL);
  123. return 0;
  124. }
  125. }
  126. puts("Receiving:");
  127. while ((count = recv(create_socket, buffer, bufsize, 0)) > 0) {
  128. fwrite(buffer, count, 1, stdout);
  129. }
  130. puts("Done");
  131. close(create_socket);
  132. if (do_fork)
  133. exit(0);
  134. return 0;
  135. }
  136. int main(int argc, char** argv) {
  137. if (argc > 1) {
  138. if (strcmp(argv[1], "client") == 0) {
  139. mode = SINGLE;
  140. return client();
  141. } else if (strcmp(argv[1], "server") == 0) {
  142. mode = SINGLE;
  143. server_dummy_socket();
  144. return server();
  145. } else if (strcmp(argv[1], "fork") == 0) {
  146. do_fork = 1;
  147. } else {
  148. printf("Invalid argument\n");
  149. return 1;
  150. }
  151. }
  152. if (pipe(pipefds) < 0) {
  153. perror("pipe error");
  154. return 1;
  155. }
  156. int pid = fork();
  157. if (pid < 0) {
  158. perror("fork error");
  159. return 1;
  160. } else if (pid == 0) {
  161. return client();
  162. } else {
  163. server_dummy_socket();
  164. return server();
  165. }
  166. }