Home | History | Annotate | Line # | Download | only in c
      1 /* Compiler options:
      2 #progos: linux
      3 #cc: additional_flags=-pthread
      4 #output: abbb ok\n
      5 
      6    Testing a signal handler corner case.  */
      7 
      8 #include <stddef.h>
      9 #include <stdlib.h>
     10 #include <stdio.h>
     11 #include <unistd.h>
     12 #include <signal.h>
     13 #include <pthread.h>
     14 
     15 static void *
     16 process (void *arg)
     17 {
     18   write (2, "a", 1);
     19   write (2, "b", 1);
     20   write (2, "b", 1);
     21   write (2, "b", 1);
     22   return NULL;
     23 }
     24 
     25 int ok = 0;
     26 volatile int done = 0;
     27 
     28 void
     29 sigusr1 (int signum)
     30 {
     31   if (signum != SIGUSR1 || !ok)
     32     abort ();
     33   done = 1;
     34 }
     35 
     36 int
     37 main (void)
     38 {
     39   int retcode;
     40   pthread_t th_a;
     41   void *retval;
     42   sigset_t sigs;
     43 
     44   if (sigemptyset (&sigs) != 0)
     45     abort ();
     46 
     47   retcode = pthread_create (&th_a, NULL, process, NULL);
     48   if (retcode != 0)
     49     abort ();
     50 
     51   if (signal (SIGUSR1, sigusr1) != SIG_DFL)
     52     abort ();
     53   if (pthread_sigmask (SIG_BLOCK, NULL, &sigs) != 0
     54       || sigaddset (&sigs, SIGUSR1) != 0
     55       || pthread_sigmask (SIG_BLOCK, &sigs, NULL) != 0)
     56     abort ();
     57   if (pthread_kill (pthread_self (), SIGUSR1) != 0
     58       || sched_yield () != 0
     59       || sched_yield () != 0
     60       || sched_yield () != 0)
     61     abort ();
     62 
     63   ok = 1;
     64   if (pthread_sigmask (SIG_UNBLOCK, NULL, &sigs) != 0
     65       || sigaddset (&sigs, SIGUSR1) != 0
     66       || pthread_sigmask (SIG_UNBLOCK, &sigs, NULL) != 0)
     67     abort ();
     68 
     69   if (!done)
     70     abort ();
     71 
     72   retcode = pthread_join (th_a, &retval);
     73   if (retcode != 0)
     74     abort ();
     75   fprintf (stderr, " ok\n");
     76   return 0;
     77 }
     78