1/* Time-triggered process termination.
2 Copyright (C) 2016-2024 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 <support/support.h>
27#include <time.h>
28#include <unistd.h>
29
30struct delayed_exit_request
31{
32 void (*exitfunc) (int);
33 int seconds;
34};
35
36static void *
37delayed_exit_thread (void *closure)
38{
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 };
43 free (ptr: request);
44
45 if (nanosleep (requested_time: &delay, remaining: &remaining) != 0)
46 FAIL_EXIT1 ("nanosleep: %m");
47 /* Exit the process successfully. */
48 exitfunc (0);
49 return NULL;
50}
51
52static void
53delayed_exit_1 (int seconds, void (*exitfunc) (int))
54{
55 /* Create the new thread with all signals blocked. */
56 sigset_t all_blocked;
57 sigfillset (set: &all_blocked);
58 sigset_t old_set;
59 xpthread_sigmask (SIG_SETMASK, set: &all_blocked, oldset: &old_set);
60 struct delayed_exit_request *request = xmalloc (n: sizeof (*request));
61 request->seconds = seconds;
62 request->exitfunc = exitfunc;
63 /* Create a detached thread. */
64 pthread_t thr = xpthread_create (NULL, thread_func: delayed_exit_thread, closure: request);
65 xpthread_detach (thr);
66 /* Restore the original signal mask. */
67 xpthread_sigmask (SIG_SETMASK, set: &old_set, NULL);
68}
69
70void
71delayed_exit (int seconds)
72{
73 delayed_exit_1 (seconds, exitfunc: exit);
74}
75
76void
77delayed__exit (int seconds)
78{
79 delayed_exit_1 (seconds, exitfunc: _exit);
80}
81

source code of glibc/support/delayed_exit.c