about summary refs log tree commit diff
path: root/support/delayed_exit.c
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2023-06-06 11:37:30 +0200
committerFlorian Weimer <fweimer@redhat.com>2023-06-06 11:37:30 +0200
commit047703fbb88eb38fbe973f3abedb279382f181d0 (patch)
treec0f844283ec811b297af8ec4f2226662dc02df37 /support/delayed_exit.c
parente3622a8f391deea3b75a577dce70d023dfa3f1c7 (diff)
downloadglibc-047703fbb88eb38fbe973f3abedb279382f181d0.tar.gz
glibc-047703fbb88eb38fbe973f3abedb279382f181d0.tar.xz
glibc-047703fbb88eb38fbe973f3abedb279382f181d0.zip
support: Add delayed__exit (with two underscores)
It calls _exit instead of exit once the timeout expires.
Diffstat (limited to 'support/delayed_exit.c')
-rw-r--r--support/delayed_exit.c41
1 files changed, 33 insertions, 8 deletions
diff --git a/support/delayed_exit.c b/support/delayed_exit.c
index 53aa40c275..bc64ff0d3e 100644
--- a/support/delayed_exit.c
+++ b/support/delayed_exit.c
@@ -23,33 +23,58 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <support/check.h>
+#include <support/support.h>
 #include <time.h>
+#include <unistd.h>
+
+struct delayed_exit_request
+{
+  void (*exitfunc) (int);
+  int seconds;
+};
 
 static void *
-delayed_exit_thread (void *seconds_as_ptr)
+delayed_exit_thread (void *closure)
 {
-  int seconds = (uintptr_t) seconds_as_ptr;
-  struct timespec delay = { seconds, 0 };
+  struct delayed_exit_request *request = closure;
+  void (*exitfunc) (int) = request->exitfunc;
+  struct timespec delay = { request->seconds, 0 };
   struct timespec remaining = { 0 };
+  free (request);
+
   if (nanosleep (&delay, &remaining) != 0)
     FAIL_EXIT1 ("nanosleep: %m");
   /* Exit the process successfully.  */
-  exit (0);
+  exitfunc (0);
   return NULL;
 }
 
-void
-delayed_exit (int seconds)
+static void
+delayed_exit_1 (int seconds, void (*exitfunc) (int))
 {
   /* Create the new thread with all signals blocked.  */
   sigset_t all_blocked;
   sigfillset (&all_blocked);
   sigset_t 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. */
-  pthread_t thr = xpthread_create
-    (NULL, delayed_exit_thread, (void *) (uintptr_t) seconds);
+  pthread_t thr = xpthread_create (NULL, delayed_exit_thread, request);
   xpthread_detach (thr);
   /* Restore the original signal mask.  */
   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);
+}