tst-shm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Test program for POSIX shm_* functions.
  2. Copyright (C) 2000-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <error.h>
  17. #include <fcntl.h>
  18. #include <signal.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include <sys/mman.h>
  25. #include <sys/stat.h>
  26. #include <sys/wait.h>
  27. /* We want to see output immediately. */
  28. #define STDOUT_UNBUFFERED
  29. static char shm_test_name[sizeof "/glibc-shm-test-" + sizeof (pid_t) * 3];
  30. static char shm_escape_name[sizeof "/../escaped-" + sizeof (pid_t) * 3];
  31. static void
  32. init_shm_test_names (void)
  33. {
  34. snprintf (shm_test_name, sizeof (shm_test_name), "/glibc-shm-test-%u",
  35. getpid ());
  36. snprintf (shm_escape_name, sizeof (shm_escape_name), "/../escaped-%u",
  37. getpid ());
  38. }
  39. static void
  40. worker (int write_now)
  41. {
  42. struct timespec ts;
  43. struct stat64 st;
  44. int i;
  45. int fd = shm_open (shm_test_name, O_RDWR, 0600);
  46. if (fd == -1)
  47. error (EXIT_FAILURE, 0, "failed to open shared memory object: shm_open");
  48. char *mem;
  49. if (fd == -1)
  50. exit (fd);
  51. if (fstat64 (fd, &st) == -1)
  52. error (EXIT_FAILURE, 0, "stat failed");
  53. if (st.st_size != 4000)
  54. error (EXIT_FAILURE, 0, "size incorrect");
  55. mem = mmap (NULL, 4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  56. if (mem == MAP_FAILED)
  57. error (EXIT_FAILURE, 0, "mmap failed");
  58. ts.tv_sec = 0;
  59. ts.tv_nsec = 500000000;
  60. if (write_now)
  61. for (i = 0; i <= 4; ++i)
  62. mem[i] = i;
  63. else
  64. /* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4. */
  65. while (1)
  66. {
  67. for (i = 0; i <= 4; ++i)
  68. if (mem[i] != i)
  69. break;
  70. if (i > 4)
  71. /* OK, that's done. */
  72. break;
  73. nanosleep (&ts, NULL);
  74. }
  75. if (!write_now)
  76. for (i = 0; i <= 4; ++i)
  77. mem[i] = 4 + i;
  78. else
  79. /* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8. */
  80. while (1)
  81. {
  82. for (i = 0; i <= 4; ++i)
  83. if (mem[i] != 4 + i)
  84. break;
  85. if (i > 4)
  86. /* OK, that's done. */
  87. break;
  88. nanosleep (&ts, NULL);
  89. }
  90. if (munmap (mem, 4000) == -1)
  91. error (EXIT_FAILURE, errno, "munmap");
  92. close (fd);
  93. exit (0);
  94. }
  95. static int
  96. do_test (void)
  97. {
  98. int fd;
  99. pid_t pid1;
  100. pid_t pid2;
  101. int status1;
  102. int status2;
  103. struct stat64 st;
  104. init_shm_test_names ();
  105. fd = shm_open (shm_escape_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
  106. if (fd != -1)
  107. {
  108. perror ("read file outside of SHMDIR directory");
  109. return 1;
  110. }
  111. /* Create the shared memory object. */
  112. fd = shm_open (shm_test_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
  113. if (fd == -1)
  114. {
  115. /* If shm_open is unimplemented we skip the test. */
  116. if (errno == ENOSYS)
  117. {
  118. perror ("shm_open unimplemented. Test skipped.");
  119. return 0;
  120. }
  121. else
  122. error (EXIT_FAILURE, 0, "failed to create shared memory object: shm_open");
  123. }
  124. /* Size the object. We make it 4000 bytes long. */
  125. if (ftruncate (fd, 4000) == -1)
  126. {
  127. /* This failed. Must be a bug in the implementation of the
  128. shared memory itself. */
  129. perror ("failed to size of shared memory object: ftruncate");
  130. close (fd);
  131. shm_unlink (shm_test_name);
  132. return 0;
  133. }
  134. if (fstat64 (fd, &st) == -1)
  135. {
  136. shm_unlink (shm_test_name);
  137. error (EXIT_FAILURE, 0, "initial stat failed");
  138. }
  139. if (st.st_size != 4000)
  140. {
  141. shm_unlink (shm_test_name);
  142. error (EXIT_FAILURE, 0, "initial size not correct");
  143. }
  144. /* Spawn to processes which will do the work. */
  145. pid1 = fork ();
  146. if (pid1 == 0)
  147. worker (0);
  148. else if (pid1 == -1)
  149. {
  150. /* Couldn't create a second process. */
  151. perror ("fork");
  152. close (fd);
  153. shm_unlink (shm_test_name);
  154. return 0;
  155. }
  156. pid2 = fork ();
  157. if (pid2 == 0)
  158. worker (1);
  159. else if (pid2 == -1)
  160. {
  161. /* Couldn't create a second process. */
  162. int ignore;
  163. perror ("fork");
  164. kill (pid1, SIGTERM);
  165. waitpid (pid1, &ignore, 0);
  166. close (fd);
  167. shm_unlink (shm_test_name);
  168. return 0;
  169. }
  170. /* Wait until the two processes are finished. */
  171. waitpid (pid1, &status1, 0);
  172. waitpid (pid2, &status2, 0);
  173. /* Now we can unlink the shared object. */
  174. shm_unlink (shm_test_name);
  175. return (!WIFEXITED (status1) || WEXITSTATUS (status1) != 0
  176. || !WIFEXITED (status2) || WEXITSTATUS (status2) != 0);
  177. }
  178. static void
  179. cleanup_handler (void)
  180. {
  181. shm_unlink (shm_test_name);
  182. }
  183. #define CLEANUP_HANDLER cleanup_handler
  184. #include <support/test-driver.c>