Back to index

glibc  2.9
tst-signal6.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 <pthread.h>
00021 #include <signal.h>
00022 #include <stdint.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <unistd.h>
00026 
00027 
00028 #define N 2
00029 static pthread_barrier_t bar;
00030 static struct
00031 {
00032   void *p;
00033   pthread_t s;
00034 } ti[N];
00035 static int sig1;
00036 
00037 
00038 static void
00039 handler (int sig)
00040 {
00041   pthread_t self = pthread_self ();
00042   size_t i;
00043 
00044   for (i = 0; i < N; ++i)
00045     if (ti[i].s == self)
00046       {
00047        if ((uintptr_t) ti[i].p <= (uintptr_t) &self
00048            && (uintptr_t) ti[i].p + 2 * MINSIGSTKSZ > (uintptr_t) &self)
00049          {
00050            puts ("alt stack not used");
00051            exit (1);
00052          }
00053 
00054        printf ("thread %zu used alt stack for signal %d\n", i, sig);
00055 
00056        return;
00057       }
00058 
00059   puts ("handler: thread not found");
00060   exit (1);
00061 }
00062 
00063 
00064 static void *
00065 tf (void *arg)
00066 {
00067   size_t nr = (uintptr_t) arg;
00068   if (nr >= N)
00069     {
00070       puts ("wrong nr parameter");
00071       exit (1);
00072     }
00073 
00074   sigset_t ss;
00075   sigemptyset (&ss);
00076   size_t i;
00077   for (i = 0; i < N; ++i)
00078     if (i != nr)
00079       if (sigaddset (&ss, sig1 + i) != 0)
00080        {
00081          puts ("tf: sigaddset failed");
00082          exit (1);
00083        }
00084   if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
00085     {
00086       puts ("tf: sigmask failed");
00087       exit (1);
00088     }
00089 
00090   void *p = malloc (2 * MINSIGSTKSZ);
00091   if (p == NULL)
00092     {
00093       puts ("tf: malloc failed");
00094       exit (1);
00095     }
00096 
00097   stack_t s;
00098   s.ss_sp = p;
00099   s.ss_size = 2 * MINSIGSTKSZ;
00100   s.ss_flags = 0;
00101   if (sigaltstack (&s, NULL) != 0)
00102     {
00103       puts ("tf: sigaltstack failed");
00104       exit (1);
00105     }
00106 
00107   ti[nr].p = p;
00108   ti[nr].s = pthread_self ();
00109 
00110   pthread_barrier_wait (&bar);
00111 
00112   pthread_barrier_wait (&bar);
00113 
00114   return NULL;
00115 }
00116 
00117 
00118 static int
00119 do_test (void)
00120 {
00121   sig1 = SIGRTMIN;
00122   if (sig1 + N > SIGRTMAX)
00123     {
00124       puts ("too few RT signals");
00125       return 0;
00126     }
00127 
00128   struct sigaction sa;
00129   sa.sa_handler = handler;
00130   sa.sa_flags = 0;
00131   sigemptyset (&sa.sa_mask);
00132 
00133   if (sigaction (sig1, &sa, NULL) != 0
00134       || sigaction (sig1 + 1, &sa, NULL) != 0
00135       || sigaction (sig1 + 2, &sa, NULL) != 0)
00136     {
00137       puts ("sigaction failed");
00138       return 1;
00139     }
00140 
00141   if (pthread_barrier_init (&bar, NULL, 1 + N) != 0)
00142     {
00143       puts ("barrier_init failed");
00144       return 1;
00145     }
00146 
00147   pthread_t th[N];
00148   size_t i;
00149   for (i = 0; i < N; ++i)
00150     if (pthread_create (&th[i], NULL, tf, (void *) (long int) i) != 0)
00151       {
00152        puts ("create failed");
00153        return 1;
00154       }
00155 
00156   /* Block the three signals.  */
00157   sigset_t ss;
00158   sigemptyset (&ss);
00159   for (i = 0; i <= N; ++i)
00160     sigaddset (&ss, sig1 + i);
00161   if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
00162     {
00163       puts ("main: sigmask failed");
00164       return 1;
00165     }
00166 
00167   pthread_barrier_wait (&bar);
00168 
00169   /* Send some signals.  */
00170   pid_t me = getpid ();
00171   kill (me, sig1 + N);
00172   for (i = 0; i < N; ++i)
00173     kill (me, sig1 + i);
00174   kill (me, sig1 + N);
00175 
00176   /* Give the signals a chance to be worked on.  */
00177   sleep (1);
00178 
00179   pthread_barrier_wait (&bar);
00180 
00181   for (i = 0; i < N; ++i)
00182     if (pthread_join (th[i], NULL) != 0)
00183       {
00184        puts ("join failed");
00185        return 1;
00186       }
00187 
00188   return 0;
00189 }
00190 
00191 #define TEST_FUNCTION do_test ()
00192 #include "../test-skeleton.c"