summaryrefslogtreecommitdiffstats
path: root/src/sighook.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sighook.c')
-rw-r--r--src/sighook.c198
1 files changed, 136 insertions, 62 deletions
diff --git a/src/sighook.c b/src/sighook.c
index 6269614..a87bed5 100644
--- a/src/sighook.c
+++ b/src/sighook.c
@@ -249,64 +249,90 @@ static void *rcu_update(struct rcu *rcu, void *ptr) {
return rcu_decode(arc_wait(prev));
}
-struct sighook {
- /** The signal being hooked, or 0 for atsigexit(). */
- int sig;
- /** Signal hook flags. */
- enum sigflags flags;
- /** The function to call. */
- sighook_fn *fn;
- /** An argument to pass to the function. */
- void *arg;
- /** Flag for SH_ONESHOT. */
- atomic bool armed;
-
- /** The RCU pointer to this hook. */
- struct rcu *self;
- /** The next hook in the list. */
- struct rcu next;
-};
-
/**
- * An RCU-protected linked list of signal hooks.
+ * An RCU-protected linked list.
*/
-struct siglist {
- /** The first hook in the list. */
+struct rcu_list {
+ /** The first node in the list. */
struct rcu head;
/** &last->next */
struct rcu *tail;
};
-/** Initialize a siglist. */
-static void siglist_init(struct siglist *list) {
+/**
+ * An rcu_list node.
+ */
+struct rcu_node {
+ /** The RCU pointer to this node. */
+ struct rcu *self;
+ /** The next node in the list. */
+ struct rcu next;
+};
+
+/** Initialize an rcu_list. */
+static void rcu_list_init(struct rcu_list *list) {
rcu_init(&list->head, NULL);
list->tail = &list->head;
}
-/** Append a hook to a linked list. */
-static void sigpush(struct siglist *list, struct sighook *hook) {
- hook->self = list->tail;
- list->tail = &hook->next;
- rcu_init(&hook->next, NULL);
- rcu_update(hook->self, hook);
+/** Append to an rcu_list. */
+static void rcu_list_append(struct rcu_list *list, struct rcu_node *node) {
+ node->self = list->tail;
+ list->tail = &node->next;
+ rcu_init(&node->next, NULL);
+ rcu_update(node->self, node);
}
-/** Remove a hook from the linked list. */
-static void sigpop(struct siglist *list, struct sighook *hook) {
- struct sighook *next = rcu_peek(&hook->next);
- rcu_update(hook->self, next);
+/** Remove from an rcu_list. */
+static void rcu_list_remove(struct rcu_list *list, struct rcu_node *node) {
+ struct rcu_node *next = rcu_peek(&node->next);
+ rcu_update(node->self, next);
if (next) {
- next->self = hook->self;
+ next->self = node->self;
} else {
list->tail = &list->head;
}
+ rcu_destroy(&node->next);
}
+/**
+ * Iterate over an rcu_list.
+ *
+ * It is save to `break` out of this loop, but `return` or `goto` will lead to
+ * a missed arc_put().
+ */
+#define for_rcu(type, node, list) \
+ for_rcu_(type, node, (list), node##_slot_, node##_prev_, node##_done_)
+
+#define for_rcu_(type, node, list, slot, prev, done) \
+ for (struct arc *slot, *prev, **done = NULL; !done; arc_put(slot), done = &slot) \
+ for (type *node = rcu_read(&list->head, &slot); \
+ node; \
+ prev = slot, \
+ node = rcu_read(&((struct rcu_node *)node)->next, &slot), \
+ arc_put(prev))
+
+struct sighook {
+ /** The RCU list node (must be the first field). */
+ struct rcu_node node;
+
+ /** The signal being hooked, or 0 for atsigexit(). */
+ int sig;
+ /** Signal hook flags. */
+ enum sigflags flags;
+ /** The function to call. */
+ sighook_fn *fn;
+ /** An argument to pass to the function. */
+ void *arg;
+ /** Flag for SH_ONESHOT. */
+ atomic bool armed;
+};
+
/** The lists of signal hooks. */
-static struct siglist sighooks[64];
+static struct rcu_list sighooks[64];
/** Get the hook list for a particular signal. */
-static struct siglist *siglist(int sig) {
+static struct rcu_list *siglist(int sig) {
return &sighooks[sig % countof(sighooks)];
}
@@ -342,22 +368,31 @@ static const int FATAL_SIGNALS[] = {
SIGHUP,
SIGILL,
SIGINT,
+#ifdef SIGIO
+ SIGIO,
+#endif
SIGPIPE,
- SIGQUIT,
- SIGSEGV,
- SIGTERM,
- SIGUSR1,
- SIGUSR2,
#ifdef SIGPOLL
SIGPOLL,
#endif
#ifdef SIGPROF
SIGPROF,
#endif
+#ifdef SIGPWR
+ SIGPWR,
+#endif
+ SIGQUIT,
+ SIGSEGV,
+#ifdef SIGSTKFLT
+ SIGSTKFLT,
+#endif
#ifdef SIGSYS
SIGSYS,
#endif
+ SIGTERM,
SIGTRAP,
+ SIGUSR1,
+ SIGUSR2,
#ifdef SIGVTALRM
SIGVTALRM,
#endif
@@ -433,23 +468,16 @@ static bool should_run(int sig, struct sighook *hook) {
}
/** Find any matching hooks and run them. */
-static enum sigflags run_hooks(struct siglist *list, int sig, siginfo_t *info) {
+static enum sigflags run_hooks(struct rcu_list *list, int sig, siginfo_t *info) {
enum sigflags ret = 0;
- struct arc *slot = NULL;
- struct sighook *hook = rcu_read(&list->head, &slot);
- while (hook) {
+ for_rcu (struct sighook, hook, list) {
if (should_run(sig, hook)) {
hook->fn(sig, info, hook->arg);
ret |= hook->flags;
}
-
- struct arc *prev = slot;
- hook = rcu_read(&hook->next, &slot);
- arc_put(prev);
}
- arc_put(slot);
return ret;
}
@@ -488,7 +516,7 @@ static void sigdispatch(int sig, siginfo_t *info, void *context) {
int error = errno;
// Run the normal hooks
- struct siglist *list = siglist(sig);
+ struct rcu_list *list = siglist(sig);
enum sigflags flags = run_hooks(list, sig, info);
// Run the atsigexit() hooks, if we're exiting
@@ -501,6 +529,18 @@ static void sigdispatch(int sig, siginfo_t *info, void *context) {
errno = error;
}
+/** A saved signal handler, for sigreset() to restore. */
+struct sigsave {
+ struct rcu_node node;
+ int sig;
+ struct sigaction action;
+};
+
+/** The list of saved signal handlers. */
+static struct rcu_list saved;
+/** `saved` initialization status (since rcu_list_init() isn't atomic). */
+static atomic bool initialized = false;
+
/** Make sure our signal handler is installed for a given signal. */
static int siginit(int sig) {
#ifdef SA_RESTART
@@ -515,19 +555,19 @@ static int siginit(int sig) {
};
static sigset_t signals;
- static bool initialized = false;
- if (!initialized) {
+ if (!load(&initialized, relaxed)) {
if (sigemptyset(&signals) != 0
|| sigemptyset(&action.sa_mask) != 0) {
return -1;
}
for (size_t i = 0; i < countof(sighooks); ++i) {
- siglist_init(&sighooks[i]);
+ rcu_list_init(&sighooks[i]);
}
- initialized = true;
+ rcu_list_init(&saved);
+ store(&initialized, true, release);
}
int installed = sigismember(&signals, sig);
@@ -537,14 +577,32 @@ static int siginit(int sig) {
return 0;
}
- if (sigaction(sig, &action, NULL) != 0) {
+ sigset_t updated = signals;
+ if (sigaddset(&updated, sig) != 0) {
+ return -1;
+ }
+
+ struct sigaction original;
+ if (sigaction(sig, NULL, &original) != 0) {
+ return -1;
+ }
+
+ struct sigsave *save = ALLOC(struct sigsave);
+ if (!save) {
return -1;
}
- if (sigaddset(&signals, sig) != 0) {
+ save->sig = sig;
+ save->action = original;
+ rcu_list_append(&saved, &save->node);
+
+ if (sigaction(sig, &action, NULL) != 0) {
+ rcu_list_remove(&saved, &save->node);
+ free(save);
return -1;
}
+ signals = updated;
return 0;
}
@@ -561,8 +619,8 @@ static struct sighook *sighook_impl(int sig, sighook_fn *fn, void *arg, enum sig
hook->arg = arg;
atomic_init(&hook->armed, true);
- struct siglist *list = siglist(sig);
- sigpush(list, hook);
+ struct rcu_list *list = siglist(sig);
+ rcu_list_append(list, &hook->node);
return hook;
}
@@ -608,11 +666,27 @@ void sigunhook(struct sighook *hook) {
mutex_lock(&sigmutex);
- struct siglist *list = siglist(hook->sig);
- sigpop(list, hook);
+ struct rcu_list *list = siglist(hook->sig);
+ rcu_list_remove(list, &hook->node);
mutex_unlock(&sigmutex);
- rcu_destroy(&hook->next);
free(hook);
}
+
+int sigreset(void) {
+ if (!load(&initialized, acquire)) {
+ return 0;
+ }
+
+ int ret = 0;
+
+ for_rcu (struct sigsave, save, &saved) {
+ if (sigaction(save->sig, &save->action, NULL) != 0) {
+ ret = -1;
+ break;
+ }
+ }
+
+ return ret;
+}