Back to index

glibc  2.9
tst-shm.c
Go to the documentation of this file.
00001 /* Test program for POSIX shm_* functions.
00002    Copyright (C) 2000, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <error.h>
00023 #include <fcntl.h>
00024 #include <signal.h>
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <time.h>
00029 #include <unistd.h>
00030 #include <sys/mman.h>
00031 #include <sys/stat.h>
00032 #include <sys/wait.h>
00033 
00034 
00035 /* We want to see output immediately.  */
00036 #define STDOUT_UNBUFFERED
00037 
00038 
00039 static int
00040 do_open (void)
00041 {
00042   int fd;
00043 
00044   /* Create the shared memory object.  */
00045   fd = shm_open ("/shm-test", O_RDWR, 0600);
00046   if (fd == -1)
00047     {
00048       /* We don't regard this as a bug.  Simply don't run the test.  It could
00049         means there is no such implementation or the object is already in
00050         use in which case we don't want to disturb.  */
00051       perror ("failed to open shared memory object: shm_open");
00052       return -1;
00053     }
00054 
00055   return fd;
00056 }
00057 
00058 
00059 static void
00060 worker (int write_now)
00061 {
00062   struct timespec ts;
00063   struct stat64 st;
00064   int i;
00065   int fd = do_open ();
00066   char *mem;
00067 
00068   if (fd == -1)
00069     exit (fd);
00070 
00071   if (fstat64 (fd, &st) == -1)
00072     error (EXIT_FAILURE, 0, "stat failed");
00073   if (st.st_size != 4000)
00074     error (EXIT_FAILURE, 0, "size incorrect");
00075 
00076   mem = mmap (NULL, 4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
00077   if (mem == NULL)
00078     error (EXIT_FAILURE, 0, "mmap failed");
00079 
00080   ts.tv_sec = 0;
00081   ts.tv_nsec = 500000000;
00082 
00083   if (write_now)
00084     for (i = 0; i <= 4; ++i)
00085       mem[i] = i;
00086   else
00087     /* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4.  */
00088     while (1)
00089       {
00090        for (i = 0; i <= 4; ++i)
00091          if (mem[i] != i)
00092            break;
00093 
00094        if (i > 4)
00095          /* OK, that's done.  */
00096          break;
00097 
00098        nanosleep (&ts, NULL);
00099       }
00100 
00101   if (!write_now)
00102     for (i = 0; i <= 4; ++i)
00103       mem[i] = 4 + i;
00104   else
00105     /* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8.  */
00106     while (1)
00107       {
00108        for (i = 0; i <= 4; ++i)
00109          if (mem[i] != 4 + i)
00110            break;
00111 
00112        if (i > 4)
00113          /* OK, that's done.  */
00114          break;
00115 
00116        nanosleep (&ts, NULL);
00117       }
00118 
00119   if (munmap (mem, 4000) == -1)
00120     error (EXIT_FAILURE, errno, "munmap");
00121 
00122   close (fd);
00123 
00124   exit (0);
00125 }
00126 
00127 
00128 static int
00129 do_test (void)
00130 {
00131   int fd;
00132   pid_t pid1;
00133   pid_t pid2;
00134   int status1;
00135   int status2;
00136   struct stat64 st;
00137 
00138   /* Create the shared memory object.  */
00139   fd = shm_open ("/shm-test", O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
00140   if (fd == -1)
00141     {
00142       /* We don't regard this as a bug.  Simply don't run the test.  It could
00143         means there is no such implementation or the object is already in
00144         use in which case we don't want to disturb.  */
00145       perror ("failed to create a shared memory object: shm_open");
00146       return 0;
00147     }
00148 
00149   /* Size the object.  We make it 4000 bytes long.  */
00150   if (ftruncate (fd, 4000) == -1)
00151     {
00152       /* This failed.  Must be a bug in the implementation of the
00153          shared memory itself.  */
00154       perror ("failed to size of shared memory object: ftruncate");
00155       close (fd);
00156       shm_unlink ("/shm-test");
00157       return 0;
00158     }
00159 
00160   if (fstat64 (fd, &st) == -1)
00161     {
00162       shm_unlink ("/shm-test");
00163       error (EXIT_FAILURE, 0, "initial stat failed");
00164     }
00165   if (st.st_size != 4000)
00166     {
00167       shm_unlink ("/shm-test");
00168       error (EXIT_FAILURE, 0, "initial size not correct");
00169     }
00170 
00171   /* Spawn to processes which will do the work.  */
00172   pid1 = fork ();
00173   if (pid1 == 0)
00174     worker (0);
00175   else if (pid1 == -1)
00176     {
00177       /* Couldn't create a second process.  */
00178       perror ("fork");
00179       close (fd);
00180       shm_unlink ("/shm-test");
00181       return 0;
00182     }
00183 
00184   pid2 = fork ();
00185   if (pid2 == 0)
00186     worker (1);
00187   else if (pid2 == -1)
00188     {
00189       /* Couldn't create a second process.  */
00190       int ignore;
00191       perror ("fork");
00192       kill (pid1, SIGTERM);
00193       waitpid (pid1, &ignore, 0);
00194       close (fd);
00195       shm_unlink ("/shm-test");
00196       return 0;
00197     }
00198 
00199   /* Wait until the two processes are finished.  */
00200   waitpid (pid1, &status1, 0);
00201   waitpid (pid2, &status2, 0);
00202 
00203   /* Now we can unlink the shared object.  */
00204   shm_unlink ("/shm-test");
00205 
00206   return (!WIFEXITED (status1) || WEXITSTATUS (status1) != 0
00207          || !WIFEXITED (status2) || WEXITSTATUS (status2) != 0);
00208 }
00209 #define TEST_FUNCTION do_test ()
00210 
00211 #define CLEANUP_HANDLER shm_unlink ("/shm-test");
00212 
00213 
00214 #include "../test-skeleton.c"