tree-wide: in all threads we fork off in library code, block all signals
This ensures that in all threads we fork off in the background in our code we mask out all signals, so that our thread won't end up getting signals delivered the main process should be getting. We always set the signal mask before forking off the thread, so that the thread has the right mask set from its earliest existance on.
This commit is contained in:
+31
-11
@@ -32,32 +32,52 @@
|
||||
#include "util.h"
|
||||
|
||||
int asynchronous_job(void* (*func)(void *p), void *arg) {
|
||||
sigset_t ss, saved_ss;
|
||||
pthread_attr_t a;
|
||||
pthread_t t;
|
||||
int r;
|
||||
int r, k;
|
||||
|
||||
/* It kinda sucks that we have to resort to threads to
|
||||
* implement an asynchronous sync(), but well, such is
|
||||
* life.
|
||||
*
|
||||
* Note that issuing this command right before exiting a
|
||||
* process will cause the process to wait for the sync() to
|
||||
* complete. This function hence is nicely asynchronous really
|
||||
* only in long running processes. */
|
||||
/* It kinda sucks that we have to resort to threads to implement an asynchronous close(), but well, such is
|
||||
* life. */
|
||||
|
||||
r = pthread_attr_init(&a);
|
||||
if (r > 0)
|
||||
return -r;
|
||||
|
||||
r = pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
|
||||
if (r > 0)
|
||||
if (r > 0) {
|
||||
r = -r;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
if (sigfillset(&ss) < 0) {
|
||||
r = -errno;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
/* Block all signals before forking off the thread, so that the new thread is started with all signals
|
||||
* blocked. This way the existence of the new thread won't affect signal handling in other threads. */
|
||||
|
||||
r = pthread_sigmask(SIG_BLOCK, &ss, &saved_ss);
|
||||
if (r > 0) {
|
||||
r = -r;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = pthread_create(&t, &a, func, arg);
|
||||
|
||||
k = pthread_sigmask(SIG_SETMASK, &saved_ss, NULL);
|
||||
|
||||
if (r > 0)
|
||||
r = -r;
|
||||
else if (k > 0)
|
||||
r = -k;
|
||||
else
|
||||
r = 0;
|
||||
|
||||
finish:
|
||||
pthread_attr_destroy(&a);
|
||||
return -r;
|
||||
return r;
|
||||
}
|
||||
|
||||
int asynchronous_sync(pid_t *ret_pid) {
|
||||
|
||||
Reference in New Issue
Block a user