OSDN Git Service

500eefbe3f330ac7b3281f2e049a494ce99e37dc
[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,2001 by Erik Andersen <andersen@uclibc.org>
7  * Written by Erik Andersen <andersen@uclibc.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Library General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or (at your
12  * option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
17  * for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  */
24
25
26 #ifndef _GNU_SOURCE
27 # define _GNU_SOURCE
28 #endif
29 #include <errno.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <fcntl.h>
35 #include <signal.h>
36
37
38 /* -------------------------------------------------*/
39 /* This stuff is common to all the testing routines */
40 /* -------------------------------------------------*/
41 const char *it = "<UNSET>";             /* Routine name for message routines. */
42 size_t errors = 0;
43
44 void check(int thing, int number)
45 {
46         if (!thing) {
47                 printf("%s: flunked test %d\n", it, number);
48                 ++errors;
49         }
50 }
51
52 void equal(const char *a, const char *b, int number)
53 {
54         check(a != NULL && b != NULL && (strcmp(a, b) == 0), number);
55 }
56
57
58 /* -------------------------------------------------*/
59 /* Let the tests begin....                          */
60 /* -------------------------------------------------*/
61
62 int global_int = 0;
63
64 void set_global_int_to_one(int signum)
65 {
66         printf ("Received signal %d (%s).\n", signum, strsignal(signum));
67         global_int = 1;
68         return;
69 }
70
71 void signal_test_1(void)
72 {
73         global_int = 0;
74
75         it = "global variable set from signal handler";
76         if (signal(SIGUSR1, set_global_int_to_one) == SIG_ERR) {
77                 perror("signal(SIGUSR1) failed");
78                 exit(-1);
79         }
80         raise(SIGUSR1);
81
82         /* This should already have jumped to the signal handler */
83         check((global_int == 1), 1);
84
85         global_int = 0;
86         if (signal(SIGUSR1, SIG_IGN) == SIG_ERR) {
87                 perror("signal(SIGUSR1) failed");
88                 exit(-1);
89         }
90         raise(SIGUSR1);
91         /* This should not go to the signal handler this time since we  */
92         check((global_int == 0), 1);
93 }
94
95
96 int main(void)
97 {
98         int status;
99
100         signal_test_1();
101
102         if (errors == 0) {
103                 status = EXIT_SUCCESS;
104                 printf("No errors.\n");
105         } else {
106                 status = EXIT_FAILURE;
107                 printf("%lu errors.\n", (unsigned long)errors);
108         }
109         exit(status);
110 }