]> git.ipfire.org Git - thirdparty/glibc.git/blame - rt/tst-shm.c
rt: Replace generic stub of shm_unlink with the posix version
[thirdparty/glibc.git] / rt / tst-shm.c
CommitLineData
ca99b8a0 1/* Test program for POSIX shm_* functions.
2b778ceb 2 Copyright (C) 2000-2021 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
90303774
L
37static char shm_test_name[sizeof "/glibc-shm-test-" + sizeof (pid_t) * 3];
38static char shm_escape_name[sizeof "/../escaped-" + sizeof (pid_t) * 3];
39
40static void
41init_shm_test_names (void)
42{
43 snprintf (shm_test_name, sizeof (shm_test_name), "/glibc-shm-test-%u",
44 getpid ());
45 snprintf (shm_escape_name, sizeof (shm_escape_name), "/../escaped-%u",
46 getpid ());
47}
48
ca99b8a0
UD
49static void
50worker (int write_now)
51{
52 struct timespec ts;
eb4b098a 53 struct stat64 st;
ca99b8a0 54 int i;
90303774
L
55
56 int fd = shm_open (shm_test_name, O_RDWR, 0600);
b20de2c3
OB
57
58 if (fd == -1)
59 error (EXIT_FAILURE, 0, "failed to open shared memory object: shm_open");
60
ca99b8a0
UD
61 char *mem;
62
63 if (fd == -1)
64 exit (fd);
65
eb4b098a 66 if (fstat64 (fd, &st) == -1)
ca99b8a0 67 error (EXIT_FAILURE, 0, "stat failed");
eb4b098a
UD
68 if (st.st_size != 4000)
69 error (EXIT_FAILURE, 0, "size incorrect");
ca99b8a0
UD
70
71 mem = mmap (NULL, 4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
cbc818d0 72 if (mem == MAP_FAILED)
ca99b8a0
UD
73 error (EXIT_FAILURE, 0, "mmap failed");
74
75 ts.tv_sec = 0;
76 ts.tv_nsec = 500000000;
77
78 if (write_now)
79 for (i = 0; i <= 4; ++i)
80 mem[i] = i;
81 else
82 /* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4. */
83 while (1)
84 {
85 for (i = 0; i <= 4; ++i)
86 if (mem[i] != i)
87 break;
88
89 if (i > 4)
90 /* OK, that's done. */
91 break;
92
93 nanosleep (&ts, NULL);
94 }
95
96 if (!write_now)
97 for (i = 0; i <= 4; ++i)
98 mem[i] = 4 + i;
99 else
100 /* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8. */
101 while (1)
102 {
103 for (i = 0; i <= 4; ++i)
104 if (mem[i] != 4 + i)
105 break;
106
107 if (i > 4)
108 /* OK, that's done. */
109 break;
110
111 nanosleep (&ts, NULL);
112 }
113
114 if (munmap (mem, 4000) == -1)
115 error (EXIT_FAILURE, errno, "munmap");
116
117 close (fd);
118
119 exit (0);
120}
121
122
de149cdb 123static int
ca99b8a0
UD
124do_test (void)
125{
126 int fd;
127 pid_t pid1;
128 pid_t pid2;
129 int status1;
130 int status2;
eb4b098a 131 struct stat64 st;
ca99b8a0 132
90303774
L
133 init_shm_test_names ();
134
135 fd = shm_open (shm_escape_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
5d30d853
OB
136 if (fd != -1)
137 {
138 perror ("read file outside of SHMDIR directory");
139 return 1;
140 }
141
142
ca99b8a0 143 /* Create the shared memory object. */
90303774 144 fd = shm_open (shm_test_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
ca99b8a0
UD
145 if (fd == -1)
146 {
b20de2c3
OB
147 /* If shm_open is unimplemented we skip the test. */
148 if (errno == ENOSYS)
149 {
150 perror ("shm_open unimplemented. Test skipped.");
151 return 0;
152 }
153 else
154 error (EXIT_FAILURE, 0, "failed to create shared memory object: shm_open");
ca99b8a0
UD
155 }
156
157 /* Size the object. We make it 4000 bytes long. */
158 if (ftruncate (fd, 4000) == -1)
159 {
160 /* This failed. Must be a bug in the implementation of the
161 shared memory itself. */
162 perror ("failed to size of shared memory object: ftruncate");
163 close (fd);
90303774 164 shm_unlink (shm_test_name);
ca99b8a0
UD
165 return 0;
166 }
167
eb4b098a 168 if (fstat64 (fd, &st) == -1)
f7ddf3d3 169 {
90303774 170 shm_unlink (shm_test_name);
f7ddf3d3
UD
171 error (EXIT_FAILURE, 0, "initial stat failed");
172 }
eb4b098a
UD
173 if (st.st_size != 4000)
174 {
90303774 175 shm_unlink (shm_test_name);
eb4b098a
UD
176 error (EXIT_FAILURE, 0, "initial size not correct");
177 }
f7ddf3d3 178
ca99b8a0
UD
179 /* Spawn to processes which will do the work. */
180 pid1 = fork ();
181 if (pid1 == 0)
182 worker (0);
183 else if (pid1 == -1)
184 {
185 /* Couldn't create a second process. */
186 perror ("fork");
187 close (fd);
90303774 188 shm_unlink (shm_test_name);
ca99b8a0
UD
189 return 0;
190 }
191
192 pid2 = fork ();
193 if (pid2 == 0)
194 worker (1);
195 else if (pid2 == -1)
196 {
197 /* Couldn't create a second process. */
198 int ignore;
199 perror ("fork");
200 kill (pid1, SIGTERM);
201 waitpid (pid1, &ignore, 0);
202 close (fd);
90303774 203 shm_unlink (shm_test_name);
ca99b8a0
UD
204 return 0;
205 }
206
207 /* Wait until the two processes are finished. */
208 waitpid (pid1, &status1, 0);
209 waitpid (pid2, &status2, 0);
210
211 /* Now we can unlink the shared object. */
90303774 212 shm_unlink (shm_test_name);
ca99b8a0
UD
213
214 return (!WIFEXITED (status1) || WEXITSTATUS (status1) != 0
215 || !WIFEXITED (status2) || WEXITSTATUS (status2) != 0);
216}
ca99b8a0 217
c23de0aa
FW
218static void
219cleanup_handler (void)
220{
90303774 221 shm_unlink (shm_test_name);
c23de0aa 222}
ca99b8a0 223
c23de0aa 224#define CLEANUP_HANDLER cleanup_handler
ca99b8a0 225
c23de0aa 226#include <support/test-driver.c>