Back to index

glibc  2.9
tst-flock2.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2004 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 <pthread.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 #include <sys/file.h>
00026 #include <sys/mman.h>
00027 #include <sys/wait.h>
00028 
00029 
00030 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
00031 static pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;
00032 static int fd;
00033 
00034 
00035 static void *
00036 tf (void *arg)
00037 {
00038   struct flock fl =
00039     {
00040       .l_type = F_WRLCK,
00041       .l_start = 0,
00042       .l_whence = SEEK_SET,
00043       .l_len = 10
00044     };
00045   if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
00046     {
00047       puts ("fourth fcntl failed");
00048       exit (1);
00049     }
00050 
00051   pthread_mutex_unlock (&lock);
00052 
00053   pthread_mutex_lock (&lock2);
00054 
00055   return NULL;
00056 }
00057 
00058 
00059 static int
00060 do_test (void)
00061 {
00062   char tmp[] = "/tmp/tst-flock2-XXXXXX";
00063 
00064   fd = mkstemp (tmp);
00065   if (fd == -1)
00066     {
00067       puts ("mkstemp failed");
00068       return 1;
00069     }
00070 
00071   unlink (tmp);
00072 
00073   int i;
00074   for (i = 0; i < 20; ++i)
00075     write (fd, "foobar xyzzy", 12);
00076 
00077   pthread_barrier_t *b;
00078   b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
00079            MAP_SHARED, fd, 0);
00080   if (b == MAP_FAILED)
00081     {
00082       puts ("mmap failed");
00083       return 1;
00084     }
00085 
00086   pthread_barrierattr_t ba;
00087   if (pthread_barrierattr_init (&ba) != 0)
00088     {
00089       puts ("barrierattr_init failed");
00090       return 1;
00091     }
00092 
00093   if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
00094     {
00095       puts ("barrierattr_setpshared failed");
00096       return 1;
00097     }
00098 
00099   if (pthread_barrier_init (b, &ba, 2) != 0)
00100     {
00101       puts ("barrier_init failed");
00102       return 1;
00103     }
00104 
00105   if (pthread_barrierattr_destroy (&ba) != 0)
00106     {
00107       puts ("barrierattr_destroy failed");
00108       return 1;
00109     }
00110 
00111   struct flock fl =
00112     {
00113       .l_type = F_WRLCK,
00114       .l_start = 0,
00115       .l_whence = SEEK_SET,
00116       .l_len = 10
00117     };
00118   if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
00119     {
00120       puts ("first fcntl failed");
00121       return 1;
00122     }
00123 
00124   pid_t pid = fork ();
00125   if (pid == -1)
00126     {
00127       puts ("fork failed");
00128       return 1;
00129     }
00130 
00131   if (pid == 0)
00132     {
00133       /* Make sure the child does not stay around indefinitely.  */
00134       alarm (10);
00135 
00136       /* Try to get the lock.  */
00137       if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
00138        {
00139          puts ("child:  second flock succeeded");
00140          return 1;
00141        }
00142     }
00143 
00144   pthread_barrier_wait (b);
00145 
00146   if (pid != 0)
00147     {
00148       fl.l_type = F_UNLCK;
00149       if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
00150        {
00151          puts ("third fcntl failed");
00152          return 1;
00153        }
00154     }
00155 
00156   pthread_barrier_wait (b);
00157 
00158   pthread_t th;
00159   if (pid == 0)
00160     {
00161       if (pthread_mutex_lock (&lock) != 0)
00162        {
00163          puts ("1st locking of lock failed");
00164          return 1;
00165        }
00166 
00167       if (pthread_mutex_lock (&lock2) != 0)
00168        {
00169          puts ("1st locking of lock2 failed");
00170          return 1;
00171        }
00172 
00173       if (pthread_create (&th, NULL, tf, NULL) != 0)
00174        {
00175          puts ("pthread_create failed");
00176          return 1;
00177        }
00178 
00179       if (pthread_mutex_lock (&lock) != 0)
00180        {
00181          puts ("2nd locking of lock failed");
00182          return 1;
00183        }
00184 
00185       puts ("child locked file");
00186     }
00187 
00188   pthread_barrier_wait (b);
00189 
00190   if (pid != 0)
00191     {
00192       fl.l_type = F_WRLCK;
00193       if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
00194        {
00195          puts ("fifth fcntl succeeded");
00196          return 1;
00197        }
00198 
00199       puts ("file locked by child");
00200     }
00201 
00202   pthread_barrier_wait (b);
00203 
00204   if (pid == 0)
00205     {
00206       if (pthread_mutex_unlock (&lock2) != 0)
00207        {
00208          puts ("unlock of lock2 failed");
00209          return 1;
00210        }
00211 
00212       if (pthread_join (th, NULL) != 0)
00213        {
00214          puts ("join failed");
00215          return 1;
00216        }
00217 
00218       puts ("child's thread terminated");
00219     }
00220 
00221   pthread_barrier_wait (b);
00222 
00223   if (pid != 0)
00224     {
00225       fl.l_type = F_WRLCK;
00226       if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
00227        {
00228          puts ("fifth fcntl succeeded");
00229          return 1;
00230        }
00231 
00232       puts ("file still locked");
00233     }
00234 
00235   pthread_barrier_wait (b);
00236 
00237   if (pid == 0)
00238     {
00239       _exit (0);
00240     }
00241 
00242   int status;
00243   if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
00244     {
00245       puts ("waitpid failed");
00246       return 1;
00247     }
00248   puts ("child terminated");
00249 
00250   if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
00251     {
00252       puts ("sixth fcntl failed");
00253       return 1;
00254     }
00255 
00256   return status;
00257 }
00258 
00259 #define TEST_FUNCTION do_test ()
00260 #include "../test-skeleton.c"