1 /* Time-triggered process termination.
2 Copyright (C) 2016-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <support/xthread.h>
20 #include <support/xsignal.h>
25 #include <support/check.h>
26 #include <support/support.h>
30 struct delayed_exit_request
32 void (*exitfunc
) (int);
37 delayed_exit_thread (void *closure
)
39 struct delayed_exit_request
*request
= closure
;
40 void (*exitfunc
) (int) = request
->exitfunc
;
41 struct timespec delay
= { request
->seconds
, 0 };
42 struct timespec remaining
= { 0 };
45 if (nanosleep (&delay
, &remaining
) != 0)
46 FAIL_EXIT1 ("nanosleep: %m");
47 /* Exit the process successfully. */
53 delayed_exit_1 (int seconds
, void (*exitfunc
) (int))
55 /* Create the new thread with all signals blocked. */
57 sigfillset (&all_blocked
);
59 xpthread_sigmask (SIG_SETMASK
, &all_blocked
, &old_set
);
60 struct delayed_exit_request
*request
= xmalloc (sizeof (*request
));
61 request
->seconds
= seconds
;
62 request
->exitfunc
= exitfunc
;
63 /* Create a detached thread. */
64 pthread_t thr
= xpthread_create (NULL
, delayed_exit_thread
, request
);
65 xpthread_detach (thr
);
66 /* Restore the original signal mask. */
67 xpthread_sigmask (SIG_SETMASK
, &old_set
, NULL
);
71 delayed_exit (int seconds
)
73 delayed_exit_1 (seconds
, exit
);
77 delayed__exit (int seconds
)
79 delayed_exit_1 (seconds
, _exit
);