unix.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. write(pipefds[1], &byte, 1);
  63. }
  64. addrlen = sizeof(address);
  65. new_socket = accept(create_socket, (struct sockaddr*)&address, &addrlen);
  66. if (new_socket < 0) {
  67. perror("accept");
  68. close(create_socket);
  69. exit(-1);
  70. }
  71. close(create_socket);
  72. printf("The client is connected...\n");
  73. if (do_fork) {
  74. if (fork() > 0) {
  75. asm volatile ("int $3");
  76. close(new_socket);
  77. wait(NULL);
  78. return 0;
  79. }
  80. }
  81. for (int i = 0; i < 10; i++) {
  82. sprintf(buffer, "Data: This is packet %d\n", i);
  83. if (sendto(new_socket, buffer, strlen(buffer), 0, 0, 0) == -1) {
  84. fprintf(stderr, "sendto() failed\n");
  85. exit(-1);
  86. }
  87. }
  88. close(new_socket);
  89. if (do_fork)
  90. exit(0);
  91. return 0;
  92. }
  93. int client(void) {
  94. int count, create_socket;
  95. int bufsize = 1024;
  96. char* buffer = malloc(bufsize);
  97. struct sockaddr_un address;
  98. if (mode == PARALLEL) {
  99. close(pipefds[1]);
  100. char byte = 0;
  101. read(pipefds[0], &byte, 1);
  102. }
  103. if ((create_socket = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0)
  104. printf("The socket was created\n");
  105. address.sun_family = AF_UNIX;
  106. strncpy(address.sun_path, "u", sizeof(address.sun_path));
  107. if (connect(create_socket, (struct sockaddr*)&address, sizeof(address)) == 0)
  108. printf("The connection was accepted with the server\n");
  109. else {
  110. printf("The connection was not accepted with the server\n");
  111. exit(0);
  112. }
  113. if (do_fork) {
  114. if (fork() > 0) {
  115. close(create_socket);
  116. wait(NULL);
  117. return 0;
  118. }
  119. }
  120. puts("Receiving:");
  121. while ((count = recv(create_socket, buffer, bufsize, 0)) > 0) {
  122. fwrite(buffer, count, 1, stdout);
  123. }
  124. puts("Done");
  125. close(create_socket);
  126. if (do_fork)
  127. exit(0);
  128. return 0;
  129. }
  130. int main(int argc, char** argv) {
  131. if (argc > 1) {
  132. if (strcmp(argv[1], "client") == 0) {
  133. mode = SINGLE;
  134. client();
  135. return 0;
  136. }
  137. if (strcmp(argv[1], "server") == 0) {
  138. mode = SINGLE;
  139. server_dummy_socket();
  140. server();
  141. return 0;
  142. }
  143. if (strcmp(argv[1], "fork") == 0) {
  144. do_fork = 1;
  145. goto old;
  146. }
  147. } else {
  148. old:
  149. pipe(pipefds);
  150. int pid = fork();
  151. if (pid == 0) {
  152. client();
  153. } else {
  154. server_dummy_socket();
  155. server();
  156. }
  157. }
  158. return 0;
  159. }