Back to index

glibc  2.9
tst-cancel16.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <string.h>
00025 #include <unistd.h>
00026 #include <sys/mman.h>
00027 #include <sys/wait.h>
00028 
00029 
00030 static pthread_barrier_t b2;
00031 static int fd;
00032 static int called;
00033 
00034 
00035 static void
00036 cl (void *arg)
00037 {
00038   called = 1;
00039 }
00040 
00041 
00042 static void *
00043 tf (void *arg)
00044 {
00045   int r = pthread_barrier_wait (&b2);
00046   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
00047     {
00048       puts ("child thread: barrier_wait failed");
00049       exit (1);
00050     }
00051 
00052   pthread_cleanup_push (cl, NULL);
00053 
00054   /* This call should never return.  */
00055   (void) lockf (fd, F_LOCK, 0);
00056 
00057   pthread_cleanup_pop (0);
00058 
00059   return NULL;
00060 }
00061 
00062 
00063 static int
00064 do_test (void)
00065 {
00066   char fname[] = "/tmp/cancel16XXXXXX";
00067   fd = mkstemp (fname);
00068   if (fd == -1)
00069     {
00070       puts ("mkstemp failed");
00071       return 1;
00072     }
00073   unlink (fname);
00074 
00075   char mem[sizeof (pthread_barrier_t)];
00076   memset (mem, '\0', sizeof (mem));
00077   if (TEMP_FAILURE_RETRY (pwrite (fd, mem, sizeof (mem), 0)) != sizeof (mem))
00078     {
00079       puts ("pwrite failed");
00080       return 1;
00081     }
00082 
00083   void *p = mmap (NULL, sizeof (mem), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
00084   if (p == MAP_FAILED)
00085     {
00086       puts ("mmap failed");
00087       return 1;
00088     }
00089   pthread_barrier_t *b = (pthread_barrier_t *) p;
00090 
00091   pthread_barrierattr_t ba;
00092   if (pthread_barrierattr_init (&ba) != 0)
00093     {
00094       puts ("barrierattr_init failed");
00095       return 1;
00096     }
00097   if (pthread_barrierattr_setpshared (&ba, 1) != 0)
00098     {
00099       puts ("barrierattr_setshared failed");
00100       return 1;
00101     }
00102 
00103   if (pthread_barrier_init (b, &ba, 2) != 0)
00104     {
00105       puts ("1st barrier_init failed");
00106       return 1;
00107     }
00108   if (pthread_barrierattr_destroy (&ba) != 0)
00109     {
00110       puts ("barrier_destroy failed");
00111       return 1;
00112     }
00113 
00114   pid_t pid = fork ();
00115   if (pid == 0)
00116     {
00117       /* Child.  Lock the file and wait.  */
00118       if (lockf (fd, F_LOCK, 0) != 0)
00119        {
00120          puts ("child process: lockf failed");
00121          _exit (1);
00122        }
00123 
00124       int r = pthread_barrier_wait (b);
00125       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
00126        {
00127          puts ("child process: 1st barrier_wait failed");
00128          _exit (1);
00129        }
00130 
00131       /* Make sure the process dies.  */
00132       alarm (5);
00133 
00134       r = pthread_barrier_wait (b);
00135       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
00136        {
00137          puts ("child process: 2nd barrier_wait failed");
00138          _exit (1);
00139        }
00140 
00141       _exit (0);
00142     }
00143   if (pid == -1)
00144     {
00145       puts ("fork failed");
00146       return 1;
00147     }
00148 
00149   int r = pthread_barrier_wait (b);
00150   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
00151     {
00152       puts ("main: 1st barrier_wait failed");
00153       _exit (1);
00154     }
00155 
00156   if (pthread_barrier_init (&b2, NULL, 2) != 0)
00157     {
00158       puts ("2nd barrier_init failed");
00159       return 1;
00160     }
00161 
00162   pthread_t th;
00163   if (pthread_create (&th, NULL, tf, NULL) != 0)
00164     {
00165       puts ("create failed");
00166       return 1;
00167     }
00168 
00169   r = pthread_barrier_wait (&b2);
00170   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
00171     {
00172       puts ("main: 2nd barrier_wait failed");
00173       return 1;
00174     }
00175 
00176   /* Delay.  */
00177   sleep (1);
00178 
00179   if (pthread_cancel (th) != 0)
00180     {
00181       puts ("cancel failed");
00182       return 1;
00183     }
00184 
00185   void *result;
00186   if (pthread_join (th, &result) != 0)
00187     {
00188       puts ("join failed");
00189       return 1;
00190     }
00191   if (result != PTHREAD_CANCELED)
00192     {
00193       puts ("thread not canceled");
00194       return 1;
00195     }
00196   if (called == 0)
00197     {
00198       puts ("cleanup handler not called");
00199       return 1;
00200     }
00201 
00202   r = pthread_barrier_wait (b);
00203   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
00204     {
00205       puts ("main: 3rd barrier_wait failed");
00206       return 1;
00207     }
00208 
00209   int status;
00210   if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
00211     {
00212       puts ("waitpid failed");
00213       return 1;
00214     }
00215   if (WEXITSTATUS (status) != 0)
00216     {
00217       printf ("child process exits with %d\n", WEXITSTATUS (status));
00218       return 1;
00219     }
00220 
00221   if (lockf (fd, F_LOCK, 0) != 0)
00222     {
00223       puts ("main: lockf failed");
00224       return 1;
00225     }
00226 
00227   return 0;
00228 }
00229 
00230 #define TEST_FUNCTION do_test ()
00231 #include "../test-skeleton.c"