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