OSDN Git Service

fix license notice
[uclinux-h8/uclibc-ng.git] / test / signal / signal.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * signal testing function for uClibc
4  *
5  * Copyright (C) 2000 by Lineo, inc. and Erik Andersen
6  * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
7  * Written by Erik Andersen <andersen@uclibc.org>
8  *
9  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
10  */
11
12 #ifndef _GNU_SOURCE
13 # define _GNU_SOURCE
14 #endif
15 #include <errno.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <strings.h>
20 #include <fcntl.h>
21 #include <signal.h>
22
23
24 /* -------------------------------------------------*/
25 /* This stuff is common to all the testing routines */
26 /* -------------------------------------------------*/
27 const char *it = "<UNSET>";             /* Routine name for message routines. */
28 size_t errors = 0;
29
30 void check(int thing, int number)
31 {
32         if (!thing) {
33                 printf("%s: flunked test %d\n", it, number);
34                 ++errors;
35         }
36 }
37
38 void equal(const char *a, const char *b, int number)
39 {
40         check(a != NULL && b != NULL && (strcmp(a, b) == 0), number);
41 }
42
43
44 /* -------------------------------------------------*/
45 /* Let the tests begin....                          */
46 /* -------------------------------------------------*/
47
48 int global_int = 0;
49
50 void set_global_int_to_one(int signum)
51 {
52         printf ("Received signal %d (%s).\n", signum, strsignal(signum));
53         global_int = 1;
54         return;
55 }
56
57 void signal_test_1(void)
58 {
59         global_int = 0;
60
61         it = "global variable set from signal handler";
62         if (signal(SIGUSR1, set_global_int_to_one) == SIG_ERR) {
63                 perror("signal(SIGUSR1) failed");
64                 exit(-1);
65         }
66         raise(SIGUSR1);
67
68         /* This should already have jumped to the signal handler */
69         check((global_int == 1), 1);
70
71         global_int = 0;
72         if (signal(SIGUSR1, SIG_IGN) == SIG_ERR) {
73                 perror("signal(SIGUSR1) failed");
74                 exit(-1);
75         }
76         raise(SIGUSR1);
77         /* This should not go to the signal handler this time since we  */
78         check((global_int == 0), 1);
79 }
80
81
82 int main(void)
83 {
84         int status;
85
86         signal_test_1();
87
88         if (errors == 0) {
89                 status = EXIT_SUCCESS;
90                 printf("No errors.\n");
91         } else {
92                 status = EXIT_FAILURE;
93                 printf("%lu errors.\n", (unsigned long)errors);
94         }
95         exit(status);
96 }