signal.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #ifndef _GNU_SOURCE
  9. # define _GNU_SOURCE
  10. #endif
  11. #include <errno.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <strings.h>
  16. #include <fcntl.h>
  17. #include <signal.h>
  18. /* -------------------------------------------------*/
  19. /* This stuff is common to all the testing routines */
  20. /* -------------------------------------------------*/
  21. const char *it = "<UNSET>"; /* Routine name for message routines. */
  22. size_t errors = 0;
  23. void check(int thing, int number)
  24. {
  25. if (!thing) {
  26. printf("%s: flunked test %d\n", it, number);
  27. ++errors;
  28. }
  29. }
  30. void equal(const char *a, const char *b, int number)
  31. {
  32. check(a != NULL && b != NULL && (strcmp(a, b) == 0), number);
  33. }
  34. /* -------------------------------------------------*/
  35. /* Let the tests begin.... */
  36. /* -------------------------------------------------*/
  37. int global_int = 0;
  38. void set_global_int_to_one(int signum)
  39. {
  40. printf ("Received signal %d (%s).\n", signum, strsignal(signum));
  41. global_int = 1;
  42. return;
  43. }
  44. void signal_test_1(void)
  45. {
  46. global_int = 0;
  47. it = "global variable set from signal handler";
  48. if (signal(SIGUSR1, set_global_int_to_one) == SIG_ERR) {
  49. perror("signal(SIGUSR1) failed");
  50. exit(-1);
  51. }
  52. raise(SIGUSR1);
  53. /* This should already have jumped to the signal handler */
  54. check((global_int == 1), 1);
  55. global_int = 0;
  56. if (signal(SIGUSR1, SIG_IGN) == SIG_ERR) {
  57. perror("signal(SIGUSR1) failed");
  58. exit(-1);
  59. }
  60. raise(SIGUSR1);
  61. /* This should not go to the signal handler this time since we */
  62. check((global_int == 0), 1);
  63. }
  64. int main(void)
  65. {
  66. int status;
  67. signal_test_1();
  68. if (errors == 0) {
  69. status = EXIT_SUCCESS;
  70. printf("No errors.\n");
  71. } else {
  72. status = EXIT_FAILURE;
  73. printf("%lu errors.\n", (unsigned long)errors);
  74. }
  75. exit(status);
  76. }