signal.c 2.7 KB

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