1 /* Test program for POSIX shm_* functions.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
33 /* We want to see output immediately. */
34 #define STDOUT_UNBUFFERED
36 static char shm_test_name
[sizeof "/glibc-shm-test-" + sizeof (pid_t
) * 3];
37 static char shm_escape_name
[sizeof "/../escaped-" + sizeof (pid_t
) * 3];
40 init_shm_test_names (void)
42 snprintf (shm_test_name
, sizeof (shm_test_name
), "/glibc-shm-test-%u",
44 snprintf (shm_escape_name
, sizeof (shm_escape_name
), "/../escaped-%u",
49 worker (int write_now
)
55 int fd
= shm_open (shm_test_name
, O_RDWR
, 0600);
58 error (EXIT_FAILURE
, 0, "failed to open shared memory object: shm_open");
65 if (fstat64 (fd
, &st
) == -1)
66 error (EXIT_FAILURE
, 0, "stat failed");
67 if (st
.st_size
!= 4000)
68 error (EXIT_FAILURE
, 0, "size incorrect");
70 mem
= mmap (NULL
, 4000, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
71 if (mem
== MAP_FAILED
)
72 error (EXIT_FAILURE
, 0, "mmap failed");
75 ts
.tv_nsec
= 500000000;
78 for (i
= 0; i
<= 4; ++i
)
81 /* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4. */
84 for (i
= 0; i
<= 4; ++i
)
89 /* OK, that's done. */
92 nanosleep (&ts
, NULL
);
96 for (i
= 0; i
<= 4; ++i
)
99 /* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8. */
102 for (i
= 0; i
<= 4; ++i
)
107 /* OK, that's done. */
110 nanosleep (&ts
, NULL
);
113 if (munmap (mem
, 4000) == -1)
114 error (EXIT_FAILURE
, errno
, "munmap");
132 init_shm_test_names ();
134 fd
= shm_open (shm_escape_name
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
, 0600);
137 perror ("read file outside of SHMDIR directory");
142 /* Create the shared memory object. */
143 fd
= shm_open (shm_test_name
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
, 0600);
146 /* If shm_open is unimplemented we skip the test. */
149 perror ("shm_open unimplemented. Test skipped.");
153 error (EXIT_FAILURE
, 0, "failed to create shared memory object: shm_open");
156 /* Size the object. We make it 4000 bytes long. */
157 if (ftruncate (fd
, 4000) == -1)
159 /* This failed. Must be a bug in the implementation of the
160 shared memory itself. */
161 perror ("failed to size of shared memory object: ftruncate");
163 shm_unlink (shm_test_name
);
167 if (fstat64 (fd
, &st
) == -1)
169 shm_unlink (shm_test_name
);
170 error (EXIT_FAILURE
, 0, "initial stat failed");
172 if (st
.st_size
!= 4000)
174 shm_unlink (shm_test_name
);
175 error (EXIT_FAILURE
, 0, "initial size not correct");
178 /* Spawn to processes which will do the work. */
184 /* Couldn't create a second process. */
187 shm_unlink (shm_test_name
);
196 /* Couldn't create a second process. */
199 kill (pid1
, SIGTERM
);
200 waitpid (pid1
, &ignore
, 0);
202 shm_unlink (shm_test_name
);
206 /* Wait until the two processes are finished. */
207 waitpid (pid1
, &status1
, 0);
208 waitpid (pid2
, &status2
, 0);
210 /* Now we can unlink the shared object. */
211 shm_unlink (shm_test_name
);
213 return (!WIFEXITED (status1
) || WEXITSTATUS (status1
) != 0
214 || !WIFEXITED (status2
) || WEXITSTATUS (status2
) != 0);
218 cleanup_handler (void)
220 shm_unlink (shm_test_name
);
223 #define CLEANUP_HANDLER cleanup_handler
225 #include <support/test-driver.c>