Back to index

glibc  2.9
tst-sem4.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <fcntl.h>
00022 #include <semaphore.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <unistd.h>
00026 
00027 
00028 static void
00029 remove_sem (int status, void *arg)
00030 {
00031   sem_unlink (arg);
00032 }
00033 
00034 
00035 int
00036 main (void)
00037 {
00038   sem_t *s;
00039   sem_t *s2;
00040   pid_t pid;
00041   int val;
00042 
00043   s = sem_open ("/glibc-tst-sem4", O_CREAT, 0600, 1);
00044   if (s == SEM_FAILED)
00045     {
00046       if (errno == ENOSYS)
00047        {
00048          puts ("sem_open not supported.  Oh well.");
00049          return 0;
00050        }
00051 
00052       /* Maybe the shm filesystem has strict permissions.  */
00053       if (errno == EACCES)
00054        {
00055          puts ("sem_open not allowed.  Oh well.");
00056          return 0;
00057        }
00058 
00059       printf ("sem_open: %m\n");
00060       return 1;
00061     }
00062 
00063   on_exit (remove_sem, (void *) "/glibc-tst-sem4");
00064 
00065   /* We have the semaphore object.  Now try again with O_EXCL, this
00066      should fail.  */
00067   s2 = sem_open ("/glibc-tst-sem4", O_CREAT | O_EXCL, 0600, 1);
00068   if (s2 != SEM_FAILED)
00069     {
00070       puts ("2nd sem_open didn't fail");
00071       return 1;
00072     }
00073   if (errno != EEXIST)
00074     {
00075       puts ("2nd sem_open returned wrong error");
00076       return 1;
00077     }
00078 
00079   /* Check the value.  */
00080   if (sem_getvalue (s, &val) == -1)
00081     {
00082       puts ("getvalue failed");
00083       return 1;
00084     }
00085   if (val != 1)
00086     {
00087       printf ("initial value wrong: got %d, expected 1\n", val);
00088       return 1;
00089     }
00090 
00091   if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
00092     {
00093       puts ("1st sem_wait failed");
00094       return 1;
00095     }
00096 
00097   pid = fork ();
00098   if (pid == -1)
00099     {
00100       printf ("fork failed: %m\n");
00101       return 1;
00102     }
00103 
00104   if (pid == 0)
00105     {
00106       /* Child.  */
00107 
00108       /* Check the value.  */
00109       if (sem_getvalue (s, &val) == -1)
00110        {
00111          puts ("child: getvalue failed");
00112          return 1;
00113        }
00114       if (val != 0)
00115        {
00116          printf ("child: value wrong: got %d, expect 0\n", val);
00117          return 1;
00118        }
00119 
00120       if (sem_post (s) == -1)
00121        {
00122          puts ("child: post failed");
00123          return 1;
00124        }
00125     }
00126   else
00127     {
00128       if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
00129        {
00130          puts ("2nd sem_wait failed");
00131          return 1;
00132        }
00133 
00134       if (sem_getvalue (s, &val) == -1)
00135        {
00136          puts ("parent: 2nd getvalue failed");
00137          return 1;
00138        }
00139       if (val != 0)
00140        {
00141          printf ("parent: value wrong: got %d, expected 0\n", val);
00142          return 1;
00143        }
00144     }
00145 
00146   return 0;
00147 }