Define __ASSUME_AT_RANDOM.
[glibc/history.git] / rt / tst-shm.c
blob5838b0ee1d174acc84d599feb4d237eb587e13b2
1 /* Test program for POSIX shm_* functions.
2 Copyright (C) 2000, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
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.
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
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <error.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <sys/wait.h>
35 /* We want to see output immediately. */
36 #define STDOUT_UNBUFFERED
39 static int
40 do_open (void)
42 int fd;
44 /* Create the shared memory object. */
45 fd = shm_open ("/shm-test", O_RDWR, 0600);
46 if (fd == -1)
48 /* We don't regard this as a bug. Simply don't run the test. It could
49 means there is no such implementation or the object is already in
50 use in which case we don't want to disturb. */
51 perror ("failed to open shared memory object: shm_open");
52 return -1;
55 return fd;
59 static void
60 worker (int write_now)
62 struct timespec ts;
63 struct stat64 st;
64 int i;
65 int fd = do_open ();
66 char *mem;
68 if (fd == -1)
69 exit (fd);
71 if (fstat64 (fd, &st) == -1)
72 error (EXIT_FAILURE, 0, "stat failed");
73 if (st.st_size != 4000)
74 error (EXIT_FAILURE, 0, "size incorrect");
76 mem = mmap (NULL, 4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
77 if (mem == NULL)
78 error (EXIT_FAILURE, 0, "mmap failed");
80 ts.tv_sec = 0;
81 ts.tv_nsec = 500000000;
83 if (write_now)
84 for (i = 0; i <= 4; ++i)
85 mem[i] = i;
86 else
87 /* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4. */
88 while (1)
90 for (i = 0; i <= 4; ++i)
91 if (mem[i] != i)
92 break;
94 if (i > 4)
95 /* OK, that's done. */
96 break;
98 nanosleep (&ts, NULL);
101 if (!write_now)
102 for (i = 0; i <= 4; ++i)
103 mem[i] = 4 + i;
104 else
105 /* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8. */
106 while (1)
108 for (i = 0; i <= 4; ++i)
109 if (mem[i] != 4 + i)
110 break;
112 if (i > 4)
113 /* OK, that's done. */
114 break;
116 nanosleep (&ts, NULL);
119 if (munmap (mem, 4000) == -1)
120 error (EXIT_FAILURE, errno, "munmap");
122 close (fd);
124 exit (0);
128 static int
129 do_test (void)
131 int fd;
132 pid_t pid1;
133 pid_t pid2;
134 int status1;
135 int status2;
136 struct stat64 st;
138 /* Create the shared memory object. */
139 fd = shm_open ("/shm-test", O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
140 if (fd == -1)
142 /* We don't regard this as a bug. Simply don't run the test. It could
143 means there is no such implementation or the object is already in
144 use in which case we don't want to disturb. */
145 perror ("failed to create a shared memory object: shm_open");
146 return 0;
149 /* Size the object. We make it 4000 bytes long. */
150 if (ftruncate (fd, 4000) == -1)
152 /* This failed. Must be a bug in the implementation of the
153 shared memory itself. */
154 perror ("failed to size of shared memory object: ftruncate");
155 close (fd);
156 shm_unlink ("/shm-test");
157 return 0;
160 if (fstat64 (fd, &st) == -1)
162 shm_unlink ("/shm-test");
163 error (EXIT_FAILURE, 0, "initial stat failed");
165 if (st.st_size != 4000)
167 shm_unlink ("/shm-test");
168 error (EXIT_FAILURE, 0, "initial size not correct");
171 /* Spawn to processes which will do the work. */
172 pid1 = fork ();
173 if (pid1 == 0)
174 worker (0);
175 else if (pid1 == -1)
177 /* Couldn't create a second process. */
178 perror ("fork");
179 close (fd);
180 shm_unlink ("/shm-test");
181 return 0;
184 pid2 = fork ();
185 if (pid2 == 0)
186 worker (1);
187 else if (pid2 == -1)
189 /* Couldn't create a second process. */
190 int ignore;
191 perror ("fork");
192 kill (pid1, SIGTERM);
193 waitpid (pid1, &ignore, 0);
194 close (fd);
195 shm_unlink ("/shm-test");
196 return 0;
199 /* Wait until the two processes are finished. */
200 waitpid (pid1, &status1, 0);
201 waitpid (pid2, &status2, 0);
203 /* Now we can unlink the shared object. */
204 shm_unlink ("/shm-test");
206 return (!WIFEXITED (status1) || WEXITSTATUS (status1) != 0
207 || !WIFEXITED (status2) || WEXITSTATUS (status2) != 0);
209 #define TEST_FUNCTION do_test ()
211 #define CLEANUP_HANDLER shm_unlink ("/shm-test");
214 #include "../test-skeleton.c"