eintr.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <pthread.h>
  17. #include <signal.h>
  18. #include <unistd.h>
  19. static int the_sig;
  20. static void
  21. eintr_handler (int sig)
  22. {
  23. if (sig != the_sig)
  24. {
  25. write (STDOUT_FILENO, "eintr_handler: signal number wrong\n", 35);
  26. _exit (1);
  27. }
  28. write (STDOUT_FILENO, ".", 1);
  29. }
  30. static void *
  31. eintr_source (void *arg)
  32. {
  33. struct timespec ts = { .tv_sec = 0, .tv_nsec = 500000 };
  34. if (arg == NULL)
  35. {
  36. sigset_t ss;
  37. sigemptyset (&ss);
  38. sigaddset (&ss, the_sig);
  39. pthread_sigmask (SIG_BLOCK, &ss, NULL);
  40. }
  41. while (1)
  42. {
  43. if (arg != NULL)
  44. pthread_kill (*(pthread_t *) arg, the_sig);
  45. else
  46. kill (getpid (), the_sig);
  47. nanosleep (&ts, NULL);
  48. }
  49. /* NOTREACHED */
  50. return NULL;
  51. }
  52. static void
  53. setup_eintr (int sig, pthread_t *thp)
  54. {
  55. struct sigaction sa;
  56. sigemptyset (&sa.sa_mask);
  57. sa.sa_flags = 0;
  58. sa.sa_handler = eintr_handler;
  59. if (sigaction (sig, &sa, NULL) != 0)
  60. {
  61. puts ("setup_eintr: sigaction failed");
  62. exit (1);
  63. }
  64. the_sig = sig;
  65. /* Create the thread which will fire off the signals. */
  66. pthread_t th;
  67. if (pthread_create (&th, NULL, eintr_source, thp) != 0)
  68. {
  69. puts ("setup_eintr: pthread_create failed");
  70. exit (1);
  71. }
  72. }