eintr.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <pthread.h>
  16. #include <signal.h>
  17. #include <unistd.h>
  18. static int the_sig;
  19. static void
  20. eintr_handler (int sig)
  21. {
  22. if (sig != the_sig)
  23. {
  24. write (STDOUT_FILENO, "eintr_handler: signal number wrong\n", 35);
  25. _exit (1);
  26. }
  27. write (STDOUT_FILENO, ".", 1);
  28. }
  29. static void *
  30. eintr_source (void *arg)
  31. {
  32. struct timespec ts = { .tv_sec = 0, .tv_nsec = 500000 };
  33. if (arg == NULL)
  34. {
  35. sigset_t ss;
  36. sigemptyset (&ss);
  37. sigaddset (&ss, the_sig);
  38. pthread_sigmask (SIG_BLOCK, &ss, NULL);
  39. }
  40. while (1)
  41. {
  42. if (arg != NULL)
  43. pthread_kill (*(pthread_t *) arg, the_sig);
  44. else
  45. kill (getpid (), the_sig);
  46. nanosleep (&ts, NULL);
  47. }
  48. /* NOTREACHED */
  49. return NULL;
  50. }
  51. static void
  52. setup_eintr (int sig, pthread_t *thp)
  53. {
  54. struct sigaction sa;
  55. sigemptyset (&sa.sa_mask);
  56. sa.sa_flags = 0;
  57. sa.sa_handler = eintr_handler;
  58. if (sigaction (sig, &sa, NULL) != 0)
  59. {
  60. puts ("setup_eintr: sigaction failed");
  61. exit (1);
  62. }
  63. the_sig = sig;
  64. /* Create the thread which will fire off the signals. */
  65. pthread_t th;
  66. if (pthread_create (&th, NULL, eintr_source, thp) != 0)
  67. {
  68. puts ("setup_eintr: pthread_create failed");
  69. exit (1);
  70. }
  71. }