signal.c 2.0 KB

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