File: c-posix-signals.c

package info (click to toggle)
libflorist 2017-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,212 kB
  • sloc: ada: 11,902; ansic: 7,195; makefile: 137; sh: 18
file content (758 lines) | stat: -rw-r--r-- 19,325 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*----------------------------------------------------------------------------
--                                                                          --
--            FLORIST (FSU Implementation of POSIX.5) COMPONENTS            --
--                                                                          --
--                                                                          --
--                     C - P O S I X - S I G N A L S . C                    --
--                                                                          --
--                                                                          --
--                Copyright (C) 1998, Florida State University              --
--                      Copyright (C) 1998-2014, AdaCore                    --
--                                                                          --
--                                                                          --
--  This file is a component of FLORIST, an  implementation of an  Ada API  --
--  for the POSIX OS services, for use with  the  GNAT  Ada  compiler  and  --
--  the FSU Gnu Ada Runtime Library (GNARL).   The  interface  is intended  --
--  to be close to that specified in  IEEE STD  1003.5: 1990  and IEEE STD  --
--  1003.5b: 1996.                                                          --
--                                                                          --
--  FLORIST is free software;  you can  redistribute  it and/or  modify it  --
--  under terms of the  GNU  General  Public  License as  published by the  --
--  Free Software Foundation;  either version  2, or (at  your option) any  --
--  later version.  FLORIST is distributed  in  the hope  that  it will be  --
--  useful, but WITHOUT ANY WARRANTY;  without  even the implied  warranty  --
--  of MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See  the GNU  --
--  General Public License for more details.  You  should have  received a  --
--  copy of the GNU General Public License  distributed  with  GNARL;  see  --
--  file  COPYING.  If not,  write to  the  Free  Software  Foundation, 59  --
--  Temple Place - Suite 330, Boston, MA 02111-1307, USA                   --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
----------------------------------------------------------------------------*/

/* file: c-posix-signals.c
   =======================
   [$Revision: 232963 $]

   This program generates the file
   posix-implementation-ok_signals.ads,
   which is an Ada package specification that defines a
   list of signals that seem to work correctly with sigwait().

   The test is to mask a given signal, then use kill () to send the
   signal to the current process, then call sigwait () to receive the
   signal.

   The observed behavior for some signals is that the process hangs
   on sigwait, notably for signals whose default action is to ignore
   the signal or stop the process.

   In order to allow testing of more than one signal, the program
   tries to recover if sigwait() hangs.

   The first attempt was to use alarm() to arrange for another
   signal to be delivered, if sigwait() did not receive the tested
   signal within one second.  This seemed to have worked for SIGCHLD
   and SIGCONT, i.e. the signals whose default action is to ignore
   them.  It did not seem to work for SIGSTOP, SITTSTP, SIGTTIN,
   SIGTTOU, whose default action is to stop the process.

   In order to recover for these cases, we moved the test of
   sigwait() into a child process, and arranged for the parent
   process to send SIGCONT to the child if it stops.  This seems to
   solve the problem.

   When linking this test don't forget to use "-lpthread",
   or the appropriate thread library.

*/

#define _POSIX_C_SIGNALS_C

#include "confsrc/pconfig.h"

#define _POSIX_C_SOURCE 199506L
#define _REENTRANT

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>

/* Uncomment the following only for debugging. */
/* #define DEBUG */

int sigs [] = {
   0,
#ifdef SIG32
   SIG32,
#endif
#ifdef SIGABRT
   SIGABRT,
#endif
#ifdef SIGALRM
   SIGALRM,
#endif
#ifdef SIGBUS
   SIGBUS,
#endif
#ifdef SIGCANCEL
   SIGCANCEL,
#endif
#ifdef SIGCHLD
   SIGCHLD,
#endif
#ifdef SIGCLD
   SIGCLD,
#endif
#ifdef SIGCONT
   SIGCONT,
#endif
#ifdef SIGEMT
   SIGEMT,
#endif
#ifdef SIGFPE
   SIGFPE,
#endif
#ifdef SIGFREEZE
   SIGFREEZE,
#endif
#ifdef SIGHUP
   SIGHUP,
#endif
#ifdef SIGILL
   SIGILL,
#endif
#ifdef SIGINT
   SIGINT,
#endif
#ifdef SIGIO
   SIGIO,
#endif
#ifdef SIGIOT
   SIGIOT,
#endif
#ifdef SIGKILL
   SIGKILL,
#endif
#ifdef SIGLOST
   SIGLOST,
#endif
#ifdef SIGLWP
   SIGLWP,
#endif
#ifdef SIGPIPE
   SIGPIPE,
#endif
#ifdef SIGPOLL
   SIGPOLL,
#endif
#ifdef SIGPROF
   SIGPROF,
#endif
#ifdef SIGQUIT
   SIGQUIT,
#endif
#ifdef SIGSEGV
   SIGSEGV,
#endif
#ifdef SIGSTKFLT
   SIGSTKFLT,
#endif
#ifdef SIGSYS
   SIGSYS,
#endif
#ifdef SIGTERM
   SIGTERM,
#endif
#ifdef SIGUSR1
   SIGUSR1,
#endif
#ifdef SIGUSR2
   SIGUSR2,
#endif
#ifdef SIGTRAP
   SIGTRAP,
#endif
#ifdef SIGSTOP
   SIGSTOP,
#endif
#ifdef SIGTHAW
   SIGTHAW,
#endif
#ifdef SIGTSTP
   SIGTSTP,
#endif
#ifdef SIGTTIN
   SIGTTIN,
#endif
#ifdef SIGTTOU
   SIGTTOU,
#endif
#ifdef SIGPWR
   SIGPWR,
#endif
#ifdef SIGURG
   SIGURG,
#endif
#ifdef SIGUNUSED
   SIGUNUSED,
#endif
#ifdef SIGVTALRM
   SIGVTALRM,
#endif
#ifdef SIGWAITING
   SIGWAITING,
#endif
#ifdef SIGWINCH
   SIGWINCH,
#endif
#ifdef SIGXCPU
   SIGXCPU,
#endif
#ifdef SIGXFSZ
   SIGXFSZ,
#endif
   0};

char *signames [] = {
   "SIGNONE",
#ifdef SIG32
   "SIG32",
#endif
#ifdef SIGABRT
   "SIGABRT",
#endif
#ifdef SIGALRM
   "SIGALRM",
#endif
#ifdef SIGBUS
   "SIGBUS",
#endif
#ifdef SIGCANCEL
   "SIGCANCEL",
#endif
#ifdef SIGCHLD
   "SIGCHLD",
#endif
#ifdef SIGCLD
   "SIGCLD",
#endif
#ifdef SIGCONT
   "SIGCONT",
#endif
#ifdef SIGEMT
   "SIGEMT",
#endif
#ifdef SIGFPE
   "SIGFPE",
#endif
#ifdef SIGFREEZE
   "SIGFREEZE",
#endif
#ifdef SIGHUP
   "SIGHUP",
#endif
#ifdef SIGILL
   "SIGILL",
#endif
#ifdef SIGINT
   "SIGINT",
#endif
#ifdef SIGIO
   "SIGIO",
#endif
#ifdef SIGIOT
   "SIGIOT",
#endif
#ifdef SIGKILL
   "SIGKILL",
#endif
#ifdef SIGLOST
   "SIGLOST",
#endif
#ifdef SIGLWP
   "SIGLWP",
#endif
#ifdef SIGPIPE
   "SIGPIPE",
#endif
#ifdef SIGPOLL
   "SIGPOLL",
#endif
#ifdef SIGPROF
   "SIGPROF",
#endif
#ifdef SIGQUIT
   "SIGQUIT",
#endif
#ifdef SIGSEGV
   "SIGSEGV",
#endif
#ifdef SIGSTKFLT
   "SIGSTKFLT",
#endif
#ifdef SIGSYS
   "SIGSYS",
#endif
#ifdef SIGTERM
   "SIGTERM",
#endif
#ifdef SIGUSR1
   "SIGUSR1",
#endif
#ifdef SIGUSR2
   "SIGUSR2",
#endif
#ifdef SIGTRAP
   "SIGTRAP",
#endif
#ifdef SIGSTOP
   "SIGSTOP",
#endif
#ifdef SIGTHAW
   "SIGTHAW",
#endif
#ifdef SIGTSTP
   "SIGTSTP",
#endif
#ifdef SIGTTIN
   "SIGTTIN",
#endif
#ifdef SIGTTOU
   "SIGTTOU",
#endif
#ifdef SIGPWR
   "SIGPWR",
#endif
#ifdef SIGURG
   "SIGURG",
#endif
#ifdef SIGUNUSED
   "SIGUNUSED",
#endif
#ifdef SIGVTALRM
   "SIGVTALRM",
#endif
#ifdef SIGWAITING
   "SIGWAITING",
#endif
#ifdef SIGWINCH
   "SIGWINCH",
#endif
#ifdef SIGXCPU
   "SIGXCPU",
#endif
#ifdef SIGXFSZ
   "SIGXFSZ",
#endif
   "SIGNONE"};

int namedsigs = sizeof (sigs) / sizeof (int);
int nsigs;
int *oksigs;
int *oksigs_nodefault;
int *oksigs_stop;

void comment (char const *msg) {
#ifdef DEBUG
   fprintf (stderr, "%s\n", msg);
   fflush (stderr);
#endif
}

void handler (int sig) {
   if (sig == SIGALRM) {
      comment ("==>   in handler for SIGALRM");
   } else {
      comment ("==>   in handler: unexpected signal");
      exit (-1);
   }
}

void handler_parent (int sig) {
   if (sig == SIGALRM) {
      comment ("==>   PARENT: in handler for SIGALRM");
   } else {
      comment ("==>   PARENT: in handler: unexpected signal");
      kill (0, SIGKILL);
      exit (-1);
   }
}

void print_package
  (int *oksigs, int *oksigs_nodefault, int *oksigs_stop) {
   int sig;
   FILE *fp;

   fprintf (stderr,"creating package POSIX.Implementation.OK_Signals\n");
   if (! (fp = fopen (GENDIR "/posix-implementation-ok_signals.ads", "w"))) {
      perror ("posix-implemenation-ok_signals.ads");
      exit (-1);
   }
   fprintf (fp, "package POSIX.Implementation.OK_Signals is\n");
   fprintf (fp, "\n");
   fprintf (fp, "   --  OK (Sig) = True iff we can use Sig"
     " with sigwait ().\n\n");
   fprintf (fp, "   OK : constant array (0 .. %d) of Boolean :=\n",
     nsigs - 1);
   fprintf (fp, "     (");
   for (sig = 0; sig < nsigs; sig++) {
      if (oksigs_nodefault[sig] == 1) fprintf (fp, " True");
      else  fprintf (fp, "False");
      if (sig == nsigs - 1) fprintf (fp, ");\n");
      else if (sig % 10 == 9) fprintf (fp, ",\n      ");
      else fprintf (fp, ", ");
   }
   fprintf (fp, "\n");
   fprintf (fp, "   --  Default_Is_Ignore (Sig) = True iff we need to"
     " override the default\n");
   fprintf (fp, "   --  treatment of Sig with a do-nothing handler"
     " before we try to\n");
   fprintf (fp, "   --  use sigwait() with it.\n\n");
   fprintf (fp, "   Default_Is_Ignore : constant array"
     " (0 .. %d) of Boolean :=\n",
     nsigs - 1);
   fprintf (fp, "     (");
   for (sig = 0; sig < nsigs; sig++) {
      if ((oksigs[sig] != 1) && (oksigs_nodefault[sig] == 1))
         fprintf (fp, " True");
      else  fprintf (fp, "False");
      if (sig == nsigs - 1) fprintf (fp, ");\n");
      else if (sig % 10 == 9) fprintf (fp, ",\n      ");
      else fprintf (fp, ", ");
   }
   fprintf (fp, "\n   --  Default_Is_Stop (Sig) = True iff the default"
     " action of Sig\n   --  is to stop the process.\n\n");
   fprintf (fp, "   Default_Is_Stop : constant array (0 .. %d)"
     " of Boolean :=\n",
     nsigs - 1);
   fprintf (fp, "     (");
   for (sig = 0; sig < nsigs; sig++) {
      if (oksigs_stop[sig] == 1)
         fprintf (fp, " True");
      else  fprintf (fp, "False");
      if (sig == nsigs - 1) fprintf (fp, ");\n");
      else if (sig % 10 == 9) fprintf (fp, ",\n      ");
      else fprintf (fp, ", ");
   }
   fprintf (fp, "\nend POSIX.Implementation.OK_Signals;\n");
   fclose (fp);
}

int guess_nsigs () {

/* Try to find out the range of valid signals.
   We have not yet discovered a portable C way of doing this.
   We assume the range starts at 0 and is continuous up to some
   limit.
   We need this becuase we want to represent sets of signals as
   Boolean arrays.
   We considered using sigset_t directly, and would have liked
   to do so, but had two problems:
   (1) sigset_t apparently allows the use of dynamically allocated memory
   (2) we could not figure out how to check for signal validity;
       in particular, we needed a way to check for whether a given signal
       is reserved by the Ada runtime system.
 */

#if defined(__APPLE__)
# define BADSIG (0)
#else
# define BADSIG (-1)
#endif

   sigset_t set;
   int sig;
   int result;
   int last_good = -1;
   int first_bad = -1;

   sigfillset (&set);
   for (sig = 0; sig < 1024; sig++) {
      result = sigismember (&set, sig);
      if (result == 1) {
         last_good = sig;
      } else if ((result == BADSIG) && (first_bad == -1)) {
         if (sig == 0) {
            fprintf (stderr, "WARNING: C library problem? "
             "sigfillset does not include zero\n");
         } else  first_bad = sig;
      }
   }
   if (last_good == 1023)
      fprintf (stderr, "WARNING: signal range estimate probably too small\n");
   if (first_bad < last_good) {
      fprintf (stderr, "WARNING: signal range estimate may be invalid\n");
      last_good = first_bad - 1;
   }

#if defined(__APPLE__)

  /* On Darwin, the above mechanism fails to make a reasonable guess
     as to the number of available signals. In the test loop
     sigismember returns true for every value of sig, including zero,
     and no first_bad is ever set. For now, hard code a reasonable
     value. */

   return 32;
#else
   return last_good + 1;
#endif
}

void parent_process(pid_t child, int *oksigs, int sig) {

/* Monitor the child process until it terminates.
   If the child process stops, wake it up.
   If the child process dies via signal, restart it and tell it to
   skip the last signal it was trying.
 */

   pid_t ret;
   int status;
   struct sigaction act;

   act.sa_flags = 0;
   act.sa_handler = handler_parent;
   if (sigaction (SIGALRM, &act, NULL)) perror ("sigaction (handler_parent)");

   while (1) {
      alarm (30);
      comment ("      PARENT: awaiting child status");
      fflush (stderr);
      ret = waitpid ( (pid_t) -1, &status, WUNTRACED);
      fflush (stderr);
      if (ret == -1) {
	 if (errno == EINTR) {
	    comment ("      PARENT: timed out, sending SIGCONT");
	    fflush (stderr);
	    kill (child, SIGCONT);
	    alarm (0);
	    sleep (1);
	    continue;
	 }
	 comment ("      PARENT: waitpid failed");
	 kill (0, SIGKILL);
	 exit (-1);
      }
      if (ret != child) {
	 comment ("      PARENT: unknown child");
	 kill (0, SIGKILL);
	 exit (-1);
      }
      if (WIFSTOPPED (status)) {
         oksigs_stop [sig] = 1;
	 comment ("      PARENT: sending SIGCONT to stopped child");
	 kill (child, SIGCONT);
	 alarm (0);
	 sleep (1);
	 continue;
      }
      if (WIFEXITED (status)) {
         if (WEXITSTATUS (status) != 1)
   	    comment ("      PARENT: child exited abnormally");
         else {
            oksigs [sig] = 1;
   	    comment ("      PARENT: child exited normally");
         }
         return;
      }
      if (WIFSIGNALED (status)) {
	 comment("      PARENT: child killed by signal");
	 fprintf (stderr, "child process killed by signal %d\n", WTERMSIG (status));
         return;
      }
      comment ("      PARENT: invalid child status %x");
      kill (0, SIGKILL);
      exit (-1);
   }
}

void test_signal (int nodefaults, int signal) {
   struct sigaction act;
   int sig;
   sigset_t  set, oset;
   int ret;

   act.sa_flags = 0;
   act.sa_handler = handler;

#ifdef LYNX_SIGTHREADKILL_HACK

   /*
      Attempting to test SIGTHREADKILL on LynxOS will kill both
      the child and the parent process: The call to sigaction fails
      and falls through to kill (0, SIGKILL).

      This code works around a problem specific to LynxOS, where
      signal 24 is reserved by the user space portion of the
      pthreads implementation.  It should be replaced by a general
      mechanism for skipping problematic signals.
   */

   if (signal == SIGTHREADKILL) {
     fprintf (stderr, "Reserved by C library.\n");
     fflush (stderr);
     exit (-1);
   }

#endif /* LYNX_SIGTHREADKILL_HACK */

   if (sigaction (signal, &act, NULL)) {
      if (errno == EINVAL) {
        fprintf (stderr, "cannot be caught\n");
        fflush (stderr);
        exit (-1);
      } else {
        fprintf (stderr, "  *** sigaction: %s\n", strerror (errno));
        fflush (stderr);
        kill (0, SIGKILL);
        exit (-1);
      }
   }
   if (! nodefaults) {
      act.sa_handler = SIG_IGN;
      if (sigaction (signal, &act, NULL)) {
         if (errno == EINVAL) {
            fprintf (stderr, "cannot be ignored\n");
            fflush (stderr);
	    exit (-1);
	 } else {
            fprintf (stderr, "  *** sigaction: %s\n", strerror (errno));
            fflush (stderr);
            kill (0, SIGKILL);
            exit (-1);
         }
      }
   }
   if (! nodefaults) {
     act.sa_handler = SIG_DFL;
     if (sigaction (signal, &act, NULL)) {
        fprintf (stderr, "  *** sigaction: %s\n", strerror (errno));
        fflush (stderr);
        kill (0, SIGKILL);
        exit (-1);
     }
   }
   if (sigemptyset (&set)) perror ("sigemptyset");
   if (sigaddset (&set, signal)) perror ("sigaddset");
   if (sigaddset (&set, SIGALRM)) perror ("sigaddset");
     comment ("  masking signal");
   if ((ret = pthread_sigmask (SIG_BLOCK, &set, NULL)))
     fprintf (stderr, "  *** pthread_sigmask (SIG_BLOCK): %s\n",
       strerror (ret));
   if ((ret = pthread_sigmask (SIG_BLOCK, &set, &oset)))
     fprintf (stderr, "  *** pthread_sigmask (SIG_BLOCK 2): %s\n",
       strerror (ret));
   if (sigismember (&oset, signal) == 0) {
     /* we are unable to mask this signal */
     fprintf (stderr, "cannot be masked.\n");
     exit (-1);
   }
   comment ("  sending signal to self");
   if (kill (getpid (), signal)) perror ("kill");
   comment ("  setting one second timeout");
   alarm (1);
   comment ("  doing sigwait");

#ifndef _CMA_OS_
   if (ret = sigwait (&set, &sig)) {
#else
   ret = sigwait (&set);
   if (ret == -1)
      ret = errno;
   else {
      sig = ret;
      ret = 0;
   }
   if (ret) {
#endif
     fprintf (stderr, "*** sigwait: %s\n", strerror (ret));
   } else if (sig == signal) {
     fprintf (stderr, "works OK.\n");
     exit (1);  /* signal is OK */
   } else if (sig == SIGALRM) {
     fprintf (stderr, "*** timed out\n");
   } else {
     fprintf (stderr, "*** wrong signal: %d\n", sig);
   }
   exit (-1);
}

void test_signals (int nodefaults, int *oksigs) {

/* Test each signal, to see if it works with sigwait().
   If nodefault != 0 then override the default treatment
   by installing a dummy handler.
 */

   int i;
   int signal;
   pid_t child;

   if (nodefaults)
     fprintf (stderr, "testing signals without default treatments\n");
   else
     fprintf (stderr, "testing signals with default treatments\n");

   for (signal = 1; signal < nsigs; signal++) {
      for (i = 0; i < namedsigs; i++)
        if (sigs[i] == signal) break;
      if (i < namedsigs)
        fprintf (stderr, "testing signal: %2d %10s ...", signal, signames[i]);
      else
        fprintf (stderr, "testing signal: %2d            ...", signal);
      fflush (stderr);
      if ((child = fork ()))
         parent_process (child, oksigs, signal);
      else {
         test_signal (nodefaults, signal);
         fprintf (stderr, "*** should never return ***");
         exit (-1);
      }
   }
}

int main (int argc, char *argv[]){

   int i;
   struct sigaction act;

   nsigs = guess_nsigs();
   fprintf (stderr, "nsigs: %d\n", nsigs);
   oksigs = (int *) malloc (nsigs * sizeof (int));
   oksigs_nodefault = (int *) malloc (nsigs * sizeof (int));
   oksigs_stop = (int *) malloc (nsigs * sizeof (int));
   for (i = 0; i < nsigs; i++)
     oksigs[i] = oksigs_nodefault[i] = oksigs_stop[i]= 0;

   act.sa_flags = 0;
   act.sa_handler = handler;
   if (sigaction (SIGALRM, &act, NULL)) perror ("sigaction (handler)");

#ifdef DEBUG
   comment ("sending 3 SIGALRM's, to test handler");
   fflush (stderr);
   kill (getpid (), SIGALRM);
   kill (getpid (), SIGALRM);
   kill (getpid (), SIGALRM);
#endif

   test_signals (0, oksigs);
   test_signals (1, oksigs_nodefault);
   print_package (oksigs, oksigs_nodefault, oksigs_stop);
   fprintf (stderr, "done.\n");
   return 0;
}