support: Add delayed__exit (with two underscores)

It calls _exit instead of exit once the timeout expires.
This commit is contained in:
Florian Weimer 2023-06-06 11:37:30 +02:00
parent e3622a8f39
commit 047703fbb8
2 changed files with 39 additions and 11 deletions

View file

@ -23,33 +23,58 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <support/check.h> #include <support/check.h>
#include <support/support.h>
#include <time.h> #include <time.h>
#include <unistd.h>
struct delayed_exit_request
{
void (*exitfunc) (int);
int seconds;
};
static void * static void *
delayed_exit_thread (void *seconds_as_ptr) delayed_exit_thread (void *closure)
{ {
int seconds = (uintptr_t) seconds_as_ptr; struct delayed_exit_request *request = closure;
struct timespec delay = { seconds, 0 }; void (*exitfunc) (int) = request->exitfunc;
struct timespec delay = { request->seconds, 0 };
struct timespec remaining = { 0 }; struct timespec remaining = { 0 };
free (request);
if (nanosleep (&delay, &remaining) != 0) if (nanosleep (&delay, &remaining) != 0)
FAIL_EXIT1 ("nanosleep: %m"); FAIL_EXIT1 ("nanosleep: %m");
/* Exit the process successfully. */ /* Exit the process successfully. */
exit (0); exitfunc (0);
return NULL; return NULL;
} }
void static void
delayed_exit (int seconds) delayed_exit_1 (int seconds, void (*exitfunc) (int))
{ {
/* Create the new thread with all signals blocked. */ /* Create the new thread with all signals blocked. */
sigset_t all_blocked; sigset_t all_blocked;
sigfillset (&all_blocked); sigfillset (&all_blocked);
sigset_t old_set; sigset_t old_set;
xpthread_sigmask (SIG_SETMASK, &all_blocked, &old_set); xpthread_sigmask (SIG_SETMASK, &all_blocked, &old_set);
struct delayed_exit_request *request = xmalloc (sizeof (*request));
request->seconds = seconds;
request->exitfunc = exitfunc;
/* Create a detached thread. */ /* Create a detached thread. */
pthread_t thr = xpthread_create pthread_t thr = xpthread_create (NULL, delayed_exit_thread, request);
(NULL, delayed_exit_thread, (void *) (uintptr_t) seconds);
xpthread_detach (thr); xpthread_detach (thr);
/* Restore the original signal mask. */ /* Restore the original signal mask. */
xpthread_sigmask (SIG_SETMASK, &old_set, NULL); xpthread_sigmask (SIG_SETMASK, &old_set, NULL);
} }
void
delayed_exit (int seconds)
{
delayed_exit_1 (seconds, exit);
}
void
delayed__exit (int seconds)
{
delayed_exit_1 (seconds, _exit);
}

View file

@ -25,11 +25,14 @@
__BEGIN_DECLS __BEGIN_DECLS
/* Terminate the process (with exit status 0) after SECONDS have /* Terminate the process (with exit (0)) after SECONDS have elapsed,
elapsed, from a helper thread. The process is terminated with the from a helper thread. The process is terminated with the exit
exit function, so atexit handlers are executed. */ function, so atexit handlers are executed. */
void delayed_exit (int seconds); void delayed_exit (int seconds);
/* Like delayed_exit, but use _exit (0). */
void delayed__exit (int seconds);
/* Returns true if Priority Inheritance support CLOCK_MONOTONIC. */ /* Returns true if Priority Inheritance support CLOCK_MONOTONIC. */
bool support_mutex_pi_monotonic (void); bool support_mutex_pi_monotonic (void);