| 1 | /* Time-triggered process termination. | 
|---|
| 2 | Copyright (C) 2016-2020 Free Software Foundation, Inc. | 
|---|
| 3 | This file is part of the GNU C Library. | 
|---|
| 4 |  | 
|---|
| 5 | The GNU C Library is free software; you can redistribute it and/or | 
|---|
| 6 | modify it under the terms of the GNU Lesser General Public | 
|---|
| 7 | License as published by the Free Software Foundation; either | 
|---|
| 8 | version 2.1 of the License, or (at your option) any later version. | 
|---|
| 9 |  | 
|---|
| 10 | The GNU C Library is distributed in the hope that it will be useful, | 
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 13 | Lesser General Public License for more details. | 
|---|
| 14 |  | 
|---|
| 15 | You should have received a copy of the GNU Lesser General Public | 
|---|
| 16 | License along with the GNU C Library; if not, see | 
|---|
| 17 | <https://www.gnu.org/licenses/>.  */ | 
|---|
| 18 |  | 
|---|
| 19 | #include <support/xthread.h> | 
|---|
| 20 | #include <support/xsignal.h> | 
|---|
| 21 |  | 
|---|
| 22 | #include <stdint.h> | 
|---|
| 23 | #include <stdio.h> | 
|---|
| 24 | #include <stdlib.h> | 
|---|
| 25 | #include <support/check.h> | 
|---|
| 26 | #include <time.h> | 
|---|
| 27 |  | 
|---|
| 28 | static void * | 
|---|
| 29 | delayed_exit_thread (void *seconds_as_ptr) | 
|---|
| 30 | { | 
|---|
| 31 | int seconds = (uintptr_t) seconds_as_ptr; | 
|---|
| 32 | struct timespec delay = { seconds, 0 }; | 
|---|
| 33 | struct timespec remaining = { 0 }; | 
|---|
| 34 | if (nanosleep (&delay, &remaining) != 0) | 
|---|
| 35 | FAIL_EXIT1 ( "nanosleep: %m"); | 
|---|
| 36 | /* Exit the process sucessfully.  */ | 
|---|
| 37 | exit (0); | 
|---|
| 38 | return NULL; | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | void | 
|---|
| 42 | delayed_exit (int seconds) | 
|---|
| 43 | { | 
|---|
| 44 | /* Create the new thread with all signals blocked.  */ | 
|---|
| 45 | sigset_t all_blocked; | 
|---|
| 46 | sigfillset (&all_blocked); | 
|---|
| 47 | sigset_t old_set; | 
|---|
| 48 | xpthread_sigmask (SIG_SETMASK, &all_blocked, &old_set); | 
|---|
| 49 | /* Create a detached thread. */ | 
|---|
| 50 | pthread_t thr = xpthread_create | 
|---|
| 51 | (NULL, delayed_exit_thread, (void *) (uintptr_t) seconds); | 
|---|
| 52 | xpthread_detach (thr); | 
|---|
| 53 | /* Restore the original signal mask.  */ | 
|---|
| 54 | xpthread_sigmask (SIG_SETMASK, &old_set, NULL); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|