| 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.
|
|---|
| 5 |
|
|---|
| 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.
|
|---|
| 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
|
|---|
| 14 | Lesser General Public License for more details.
|
|---|
| 15 |
|
|---|
| 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. */
|
|---|
| 20 |
|
|---|
| 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>
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | /* We want to see output immediately. */
|
|---|
| 36 | #define STDOUT_UNBUFFERED
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | static int
|
|---|
| 40 | do_open (void)
|
|---|
| 41 | {
|
|---|
| 42 | int fd;
|
|---|
| 43 |
|
|---|
| 44 | /* Create the shared memory object. */
|
|---|
| 45 | fd = shm_open ("/shm-test", O_RDWR, 0600);
|
|---|
| 46 | if (fd == -1)
|
|---|
| 47 | {
|
|---|
| 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;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | return fd;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | static void
|
|---|
| 60 | worker (int write_now)
|
|---|
| 61 | {
|
|---|
| 62 | struct timespec ts;
|
|---|
| 63 | struct stat st;
|
|---|
| 64 | int i;
|
|---|
| 65 | int fd = do_open ();
|
|---|
| 66 | char *mem;
|
|---|
| 67 |
|
|---|
| 68 | if (fd == -1)
|
|---|
| 69 | exit (fd);
|
|---|
| 70 |
|
|---|
| 71 | if (fstat (fd, &st) == -1 || st.st_size != 4000)
|
|---|
| 72 | error (EXIT_FAILURE, 0, "stat failed");
|
|---|
| 73 |
|
|---|
| 74 | mem = mmap (NULL, 4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|---|
| 75 | if (mem == NULL)
|
|---|
| 76 | error (EXIT_FAILURE, 0, "mmap failed");
|
|---|
| 77 |
|
|---|
| 78 | ts.tv_sec = 0;
|
|---|
| 79 | ts.tv_nsec = 500000000;
|
|---|
| 80 |
|
|---|
| 81 | if (write_now)
|
|---|
| 82 | for (i = 0; i <= 4; ++i)
|
|---|
| 83 | mem[i] = i;
|
|---|
| 84 | else
|
|---|
| 85 | /* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4. */
|
|---|
| 86 | while (1)
|
|---|
| 87 | {
|
|---|
| 88 | for (i = 0; i <= 4; ++i)
|
|---|
| 89 | if (mem[i] != i)
|
|---|
| 90 | break;
|
|---|
| 91 |
|
|---|
| 92 | if (i > 4)
|
|---|
| 93 | /* OK, that's done. */
|
|---|
| 94 | break;
|
|---|
| 95 |
|
|---|
| 96 | nanosleep (&ts, NULL);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | if (!write_now)
|
|---|
| 100 | for (i = 0; i <= 4; ++i)
|
|---|
| 101 | mem[i] = 4 + i;
|
|---|
| 102 | else
|
|---|
| 103 | /* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8. */
|
|---|
| 104 | while (1)
|
|---|
| 105 | {
|
|---|
| 106 | for (i = 0; i <= 4; ++i)
|
|---|
| 107 | if (mem[i] != 4 + i)
|
|---|
| 108 | break;
|
|---|
| 109 |
|
|---|
| 110 | if (i > 4)
|
|---|
| 111 | /* OK, that's done. */
|
|---|
| 112 | break;
|
|---|
| 113 |
|
|---|
| 114 | nanosleep (&ts, NULL);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | if (munmap (mem, 4000) == -1)
|
|---|
| 118 | error (EXIT_FAILURE, errno, "munmap");
|
|---|
| 119 |
|
|---|
| 120 | close (fd);
|
|---|
| 121 |
|
|---|
| 122 | exit (0);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | static int
|
|---|
| 127 | do_test (void)
|
|---|
| 128 | {
|
|---|
| 129 | int fd;
|
|---|
| 130 | pid_t pid1;
|
|---|
| 131 | pid_t pid2;
|
|---|
| 132 | int status1;
|
|---|
| 133 | int status2;
|
|---|
| 134 | struct stat st;
|
|---|
| 135 |
|
|---|
| 136 | /* Create the shared memory object. */
|
|---|
| 137 | fd = shm_open ("/shm-test", O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
|
|---|
| 138 | if (fd == -1)
|
|---|
| 139 | {
|
|---|
| 140 | /* We don't regard this as a bug. Simply don't run the test. It could
|
|---|
| 141 | means there is no such implementation or the object is already in
|
|---|
| 142 | use in which case we don't want to disturb. */
|
|---|
| 143 | perror ("failed to create a shared memory object: shm_open");
|
|---|
| 144 | return 0;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /* Size the object. We make it 4000 bytes long. */
|
|---|
| 148 | if (ftruncate (fd, 4000) == -1)
|
|---|
| 149 | {
|
|---|
| 150 | /* This failed. Must be a bug in the implementation of the
|
|---|
| 151 | shared memory itself. */
|
|---|
| 152 | perror ("failed to size of shared memory object: ftruncate");
|
|---|
| 153 | close (fd);
|
|---|
| 154 | shm_unlink ("/shm-test");
|
|---|
| 155 | return 0;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | if (fstat (fd, &st) == -1 || st.st_size != 4000)
|
|---|
| 159 | {
|
|---|
| 160 | shm_unlink ("/shm-test");
|
|---|
| 161 | error (EXIT_FAILURE, 0, "initial stat failed");
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 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);
|
|---|
| 173 | shm_unlink ("/shm-test");
|
|---|
| 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);
|
|---|
| 188 | shm_unlink ("/shm-test");
|
|---|
| 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. */
|
|---|
| 197 | shm_unlink ("/shm-test");
|
|---|
| 198 |
|
|---|
| 199 | return (!WIFEXITED (status1) || WEXITSTATUS (status1) != 0
|
|---|
| 200 | || !WIFEXITED (status2) || WEXITSTATUS (status2) != 0);
|
|---|
| 201 | }
|
|---|
| 202 | #define TEST_FUNCTION do_test ()
|
|---|
| 203 |
|
|---|
| 204 | #define CLEANUP_HANDLER shm_unlink ("/shm-test");
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 | #include "../test-skeleton.c"
|
|---|