Back to index

glibc  2.9
tst-sighandler1.c
Go to the documentation of this file.
00001 /* Copyright (C) 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <pthread.h>
00020 #include <errno.h>
00021 #include <stdio.h>
00022 #include <signal.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 #include <sys/wait.h>
00026 #include <sys/time.h>
00027 #include <limits.h>
00028 
00029 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
00030 pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
00031 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00032 
00033 static void *
00034 tf (void *arg)
00035 {
00036   int fd = (long) arg;
00037   int pid = getpid ();
00038 #ifdef TIMED
00039   struct timespec ts;
00040 #endif
00041 
00042   if (pthread_mutex_lock (&mutex) != 0)
00043     {
00044       puts ("Could not lock mutex in thread");
00045       return (void *) 1L;
00046     }
00047 
00048   write (fd, &pid, sizeof (int));
00049 
00050   if (pthread_mutex_unlock (&mutex2) != 0)
00051     {
00052       puts ("Could not unlock mutex2 in thread");
00053       return (void *) 1L;
00054     }
00055 
00056 #ifdef TIMED
00057   ts.tv_sec = INT_MAX;
00058   ts.tv_nsec = 0;
00059   if (pthread_cond_timedwait (&cond, &mutex, &ts) != 0)
00060     {
00061       puts ("pthread_cond_wait failed in thread");
00062       return (void *) 1L;
00063     }
00064 #else
00065   if (pthread_cond_wait (&cond, &mutex) != 0)
00066     {
00067       puts ("pthread_cond_wait failed in thread");
00068       return (void *) 1L;
00069     }
00070 #endif
00071 
00072   if (pthread_mutex_unlock (&mutex) != 0)
00073     {
00074       puts ("Could not unlock mutex in thread");
00075       return (void *) 1L;
00076     }
00077 
00078   return NULL;
00079 }
00080 
00081 static void
00082 sig_segv (int unused)
00083 {
00084   exit (2);
00085 }
00086 
00087 int
00088 do_test (void)
00089 {
00090   int fds[2];
00091   pthread_t thr;
00092 
00093   if (pipe (fds) < 0)
00094     {
00095       printf ("couldn't create pipe: %m\n");
00096       return 1;
00097     }
00098 
00099   pid_t pid = fork ();
00100   if (pid < 0)
00101     {
00102       printf ("fork failed: %m\n");
00103       return 1;
00104     }
00105 
00106   if (!pid)
00107     {
00108       void *ret;
00109 
00110       close (fds[0]);
00111       signal (SIGSEGV, sig_segv);
00112       pthread_mutex_lock (&mutex2);
00113       pthread_create (&thr, NULL, tf, (void *) (long) (fds[1]));
00114       /* Wait for the thread to go into cond_wait.  */
00115       pthread_mutex_lock (&mutex2);
00116       pthread_mutex_lock (&mutex);
00117       write (fds[1], "", 1);
00118       pthread_join (thr, &ret);
00119       exit (0);
00120     }
00121 
00122   close (fds[1]);
00123 
00124   int child_pid;
00125   if (TEMP_FAILURE_RETRY (read (fds[0], &child_pid, sizeof (int)))
00126       != sizeof (int))
00127     {
00128       puts ("could not read pid from child");
00129       return 1;
00130     }
00131 
00132   char dummy;
00133   if (TEMP_FAILURE_RETRY (read (fds[0], &dummy, 1)) != 1)
00134     {
00135       puts ("could not read sync byte from child");
00136       return 1;
00137     }
00138 
00139   close (fds[0]);
00140 
00141   /* The sync byte may come immediately after the mutex is released;
00142      wait a little bit more to make sure the thread has suspended, to
00143      test the right bug.  */
00144   struct timespec ts;
00145   ts.tv_sec = 0;
00146   ts.tv_nsec = 200000000;
00147   nanosleep (&ts, NULL);
00148 
00149   kill (child_pid, SIGSEGV);
00150 
00151   pid_t termpid;
00152   int status;
00153   termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
00154   if (termpid == -1)
00155     {
00156       printf ("waitpid failed: %m\n");
00157       return 1;
00158     }
00159   else if (termpid != pid)
00160     {
00161       printf ("waitpid returned %ld != %ld\n",
00162              (long int) termpid, (long int) pid);
00163       return 1;
00164     }
00165   else if (!WIFEXITED (status) || WEXITSTATUS (status) != 2)
00166     {
00167       puts ("child hasn't exited with exit status 2");
00168       return 1;
00169     }
00170 
00171   return 0;
00172 }
00173 
00174 #define TEST_FUNCTION do_test ()
00175 #include "../test-skeleton.c"