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