signal.c 2.0 KB

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