Back to index

glibc  2.9
tst-signal1.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 <signal.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <unistd.h>
00027 #include <sys/mman.h>
00028 #include <sys/wait.h>
00029 
00030 
00031 static sigset_t ss;
00032 static pthread_barrier_t *b;
00033 
00034 
00035 static void *
00036 tf (void *arg)
00037 {
00038   sigdelset (&ss, SIGINT);
00039 
00040   if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
00041     {
00042       puts ("2nd pthread_sigmask failed");
00043       exit (1);
00044     }
00045 
00046   pthread_barrier_wait (b);
00047 
00048   int sig;
00049   int res = sigwait (&ss, &sig);
00050   if (res == 0)
00051     {
00052       printf ("sigwait returned successfully with signal %d\n", sig);
00053       exit (1);
00054     }
00055 
00056   printf ("sigwait returned with %s (%d)\n", strerror (res), res);
00057 
00058   return NULL;
00059 }
00060 
00061 
00062 static void
00063 receiver (void)
00064 {
00065   pthread_t th;
00066 
00067   /* Make sure the process doesn't run forever.  */
00068   alarm (10);
00069 
00070   sigfillset (&ss);
00071 
00072   if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
00073     {
00074       puts ("1st pthread_sigmask failed");
00075       exit (1);
00076     }
00077 
00078   if (pthread_create (&th, NULL, tf, NULL) != 0)
00079     {
00080       puts ("pthread_create failed");
00081       exit (1);
00082     }
00083 
00084   if (pthread_join (th, NULL) == 0)
00085     {
00086       puts ("thread joined?!");
00087       exit (1);
00088     }
00089 
00090   _exit (0);
00091 }
00092 
00093 
00094 static int
00095 do_test (void)
00096 {
00097   char tmp[] = "/tmp/tst-signal1-XXXXXX";
00098 
00099   int fd = mkstemp (tmp);
00100   if (fd == -1)
00101     {
00102       puts ("mkstemp failed");
00103       exit (1);
00104     }
00105 
00106   unlink (tmp);
00107 
00108   int i;
00109   for (i = 0; i < 20; ++i)
00110     write (fd, "foobar xyzzy", 12);
00111 
00112   b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
00113            MAP_SHARED, fd, 0);
00114   if (b == MAP_FAILED)
00115     {
00116       puts ("mmap failed");
00117       exit (1);
00118     }
00119 
00120   pthread_barrierattr_t ba;
00121   if (pthread_barrierattr_init (&ba) != 0)
00122     {
00123       puts ("barrierattr_init failed");
00124       exit (1);
00125     }
00126 
00127   if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
00128     {
00129       puts ("barrierattr_setpshared failed");
00130       exit (1);
00131     }
00132 
00133   if (pthread_barrier_init (b, &ba, 2) != 0)
00134     {
00135       puts ("barrier_init failed");
00136       exit (1);
00137     }
00138 
00139   if (pthread_barrierattr_destroy (&ba) != 0)
00140     {
00141       puts ("barrierattr_destroy failed");
00142       exit (1);
00143     }
00144 
00145   pid_t pid = fork ();
00146   if (pid == -1)
00147     {
00148       puts ("fork failed");
00149       exit (1);
00150     }
00151 
00152   if (pid == 0)
00153     receiver ();
00154 
00155   pthread_barrier_wait (b);
00156 
00157   /* Wait a bit more.  */
00158   struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
00159   nanosleep (&ts, NULL);
00160 
00161   /* Send the signal.  */
00162   puts ("sending the signal now");
00163   kill (pid, SIGINT);
00164 
00165   /* Wait for the process to terminate.  */
00166   int status;
00167   if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
00168     {
00169       puts ("wrong child reported terminated");
00170       exit (1);
00171     }
00172 
00173   if (!WIFSIGNALED (status))
00174     {
00175       puts ("child wasn't signalled");
00176       exit (1);
00177     }
00178 
00179   if (WTERMSIG (status) != SIGINT)
00180     {
00181       puts ("child not terminated with SIGINT");
00182       exit (1);
00183     }
00184 
00185   return 0;
00186 }
00187 
00188 #define TEST_FUNCTION do_test ()
00189 #include "../test-skeleton.c"