delayed_exit.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Time-triggered process termination.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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 <support/xthread.h>
  16. #include <support/xsignal.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <support/check.h>
  21. #include <time.h>
  22. static void *
  23. delayed_exit_thread (void *seconds_as_ptr)
  24. {
  25. int seconds = (uintptr_t) seconds_as_ptr;
  26. struct timespec delay = { seconds, 0 };
  27. struct timespec remaining = { 0 };
  28. if (nanosleep (&delay, &remaining) != 0)
  29. FAIL_EXIT1 ("nanosleep: %m");
  30. /* Exit the process sucessfully. */
  31. exit (0);
  32. return NULL;
  33. }
  34. void
  35. delayed_exit (int seconds)
  36. {
  37. /* Create the new thread with all signals blocked. */
  38. sigset_t all_blocked;
  39. sigfillset (&all_blocked);
  40. sigset_t old_set;
  41. xpthread_sigmask (SIG_SETMASK, &all_blocked, &old_set);
  42. /* Create a detached thread. */
  43. pthread_t thr = xpthread_create
  44. (NULL, delayed_exit_thread, (void *) (uintptr_t) seconds);
  45. xpthread_detach (thr);
  46. /* Restore the original signal mask. */
  47. xpthread_sigmask (SIG_SETMASK, &old_set, NULL);
  48. }