Back to index

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