]>
Commit | Line | Data |
---|---|---|
ca99b8a0 | 1 | /* Test program for POSIX shm_* functions. |
dff8da6b | 2 | Copyright (C) 2000-2024 Free Software Foundation, Inc. |
41bdb6e2 | 3 | This file is part of the GNU C Library. |
ca99b8a0 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
ca99b8a0 UD |
9 | |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
ca99b8a0 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
ca99b8a0 UD |
18 | |
19 | #include <errno.h> | |
20 | #include <error.h> | |
21 | #include <fcntl.h> | |
22 | #include <signal.h> | |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <time.h> | |
27 | #include <unistd.h> | |
28 | #include <sys/mman.h> | |
29 | #include <sys/stat.h> | |
30 | #include <sys/wait.h> | |
31 | ||
32 | ||
33 | /* We want to see output immediately. */ | |
34 | #define STDOUT_UNBUFFERED | |
35 | ||
90303774 L |
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]; | |
38 | ||
39 | static void | |
40 | init_shm_test_names (void) | |
41 | { | |
42 | snprintf (shm_test_name, sizeof (shm_test_name), "/glibc-shm-test-%u", | |
43 | getpid ()); | |
44 | snprintf (shm_escape_name, sizeof (shm_escape_name), "/../escaped-%u", | |
45 | getpid ()); | |
46 | } | |
47 | ||
ca99b8a0 UD |
48 | static void |
49 | worker (int write_now) | |
50 | { | |
51 | struct timespec ts; | |
eb4b098a | 52 | struct stat64 st; |
ca99b8a0 | 53 | int i; |
90303774 L |
54 | |
55 | int fd = shm_open (shm_test_name, O_RDWR, 0600); | |
b20de2c3 OB |
56 | |
57 | if (fd == -1) | |
58 | error (EXIT_FAILURE, 0, "failed to open shared memory object: shm_open"); | |
59 | ||
ca99b8a0 UD |
60 | char *mem; |
61 | ||
62 | if (fd == -1) | |
63 | exit (fd); | |
64 | ||
eb4b098a | 65 | if (fstat64 (fd, &st) == -1) |
ca99b8a0 | 66 | error (EXIT_FAILURE, 0, "stat failed"); |
eb4b098a UD |
67 | if (st.st_size != 4000) |
68 | error (EXIT_FAILURE, 0, "size incorrect"); | |
ca99b8a0 UD |
69 | |
70 | mem = mmap (NULL, 4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
cbc818d0 | 71 | if (mem == MAP_FAILED) |
ca99b8a0 UD |
72 | error (EXIT_FAILURE, 0, "mmap failed"); |
73 | ||
74 | ts.tv_sec = 0; | |
75 | ts.tv_nsec = 500000000; | |
76 | ||
77 | if (write_now) | |
78 | for (i = 0; i <= 4; ++i) | |
79 | mem[i] = i; | |
80 | else | |
81 | /* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4. */ | |
82 | while (1) | |
83 | { | |
84 | for (i = 0; i <= 4; ++i) | |
85 | if (mem[i] != i) | |
86 | break; | |
87 | ||
88 | if (i > 4) | |
89 | /* OK, that's done. */ | |
90 | break; | |
91 | ||
92 | nanosleep (&ts, NULL); | |
93 | } | |
94 | ||
95 | if (!write_now) | |
96 | for (i = 0; i <= 4; ++i) | |
97 | mem[i] = 4 + i; | |
98 | else | |
99 | /* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8. */ | |
100 | while (1) | |
101 | { | |
102 | for (i = 0; i <= 4; ++i) | |
103 | if (mem[i] != 4 + i) | |
104 | break; | |
105 | ||
106 | if (i > 4) | |
107 | /* OK, that's done. */ | |
108 | break; | |
109 | ||
110 | nanosleep (&ts, NULL); | |
111 | } | |
112 | ||
113 | if (munmap (mem, 4000) == -1) | |
114 | error (EXIT_FAILURE, errno, "munmap"); | |
115 | ||
116 | close (fd); | |
117 | ||
118 | exit (0); | |
119 | } | |
120 | ||
121 | ||
de149cdb | 122 | static int |
ca99b8a0 UD |
123 | do_test (void) |
124 | { | |
125 | int fd; | |
126 | pid_t pid1; | |
127 | pid_t pid2; | |
128 | int status1; | |
129 | int status2; | |
eb4b098a | 130 | struct stat64 st; |
ca99b8a0 | 131 | |
90303774 L |
132 | init_shm_test_names (); |
133 | ||
134 | fd = shm_open (shm_escape_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600); | |
5d30d853 OB |
135 | if (fd != -1) |
136 | { | |
137 | perror ("read file outside of SHMDIR directory"); | |
138 | return 1; | |
139 | } | |
140 | ||
141 | ||
ca99b8a0 | 142 | /* Create the shared memory object. */ |
90303774 | 143 | fd = shm_open (shm_test_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600); |
ca99b8a0 UD |
144 | if (fd == -1) |
145 | { | |
b20de2c3 OB |
146 | /* If shm_open is unimplemented we skip the test. */ |
147 | if (errno == ENOSYS) | |
148 | { | |
149 | perror ("shm_open unimplemented. Test skipped."); | |
150 | return 0; | |
151 | } | |
152 | else | |
153 | error (EXIT_FAILURE, 0, "failed to create shared memory object: shm_open"); | |
ca99b8a0 UD |
154 | } |
155 | ||
156 | /* Size the object. We make it 4000 bytes long. */ | |
157 | if (ftruncate (fd, 4000) == -1) | |
158 | { | |
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"); | |
162 | close (fd); | |
90303774 | 163 | shm_unlink (shm_test_name); |
ca99b8a0 UD |
164 | return 0; |
165 | } | |
166 | ||
eb4b098a | 167 | if (fstat64 (fd, &st) == -1) |
f7ddf3d3 | 168 | { |
90303774 | 169 | shm_unlink (shm_test_name); |
f7ddf3d3 UD |
170 | error (EXIT_FAILURE, 0, "initial stat failed"); |
171 | } | |
eb4b098a UD |
172 | if (st.st_size != 4000) |
173 | { | |
90303774 | 174 | shm_unlink (shm_test_name); |
eb4b098a UD |
175 | error (EXIT_FAILURE, 0, "initial size not correct"); |
176 | } | |
f7ddf3d3 | 177 | |
ca99b8a0 UD |
178 | /* Spawn to processes which will do the work. */ |
179 | pid1 = fork (); | |
180 | if (pid1 == 0) | |
181 | worker (0); | |
182 | else if (pid1 == -1) | |
183 | { | |
184 | /* Couldn't create a second process. */ | |
185 | perror ("fork"); | |
186 | close (fd); | |
90303774 | 187 | shm_unlink (shm_test_name); |
ca99b8a0 UD |
188 | return 0; |
189 | } | |
190 | ||
191 | pid2 = fork (); | |
192 | if (pid2 == 0) | |
193 | worker (1); | |
194 | else if (pid2 == -1) | |
195 | { | |
196 | /* Couldn't create a second process. */ | |
197 | int ignore; | |
198 | perror ("fork"); | |
199 | kill (pid1, SIGTERM); | |
200 | waitpid (pid1, &ignore, 0); | |
201 | close (fd); | |
90303774 | 202 | shm_unlink (shm_test_name); |
ca99b8a0 UD |
203 | return 0; |
204 | } | |
205 | ||
206 | /* Wait until the two processes are finished. */ | |
207 | waitpid (pid1, &status1, 0); | |
208 | waitpid (pid2, &status2, 0); | |
209 | ||
210 | /* Now we can unlink the shared object. */ | |
90303774 | 211 | shm_unlink (shm_test_name); |
ca99b8a0 UD |
212 | |
213 | return (!WIFEXITED (status1) || WEXITSTATUS (status1) != 0 | |
214 | || !WIFEXITED (status2) || WEXITSTATUS (status2) != 0); | |
215 | } | |
ca99b8a0 | 216 | |
c23de0aa FW |
217 | static void |
218 | cleanup_handler (void) | |
219 | { | |
90303774 | 220 | shm_unlink (shm_test_name); |
c23de0aa | 221 | } |
ca99b8a0 | 222 | |
c23de0aa | 223 | #define CLEANUP_HANDLER cleanup_handler |
ca99b8a0 | 224 | |
c23de0aa | 225 | #include <support/test-driver.c> |