summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-05-05 23:47:56 +0200
committerLennart Poettering <lennart@poettering.net>2014-05-05 23:47:59 +0200
commit59bc1fd776b5270bcabe3c536852afd0c892fbe1 (patch)
tree56d60e201a20529b565fef327fd5a504d47e2f1b
parent7ed0dd4be6ef4e73d7fb2fc1e80e6f9405c2f257 (diff)
sd-event: make it easy to bind signal handling to event loop exits
By passing a NULL callback provide a simple way to make event loops exit on SIGTERM and other signals.
-rw-r--r--src/libsystemd/sd-event/sd-event.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
index 0ad6454fd7..70770fc7ad 100644
--- a/src/libsystemd/sd-event/sd-event.c
+++ b/src/libsystemd/sd-event/sd-event.c
@@ -886,6 +886,12 @@ static int event_update_signal_fd(sd_event *e) {
return 0;
}
+static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
+ assert(s);
+
+ return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
+}
+
_public_ int sd_event_add_signal(
sd_event *e,
sd_event_source **ret,
@@ -900,11 +906,13 @@ _public_ int sd_event_add_signal(
assert_return(e, -EINVAL);
assert_return(sig > 0, -EINVAL);
assert_return(sig < _NSIG, -EINVAL);
- assert_return(callback, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD);
+ if (!callback)
+ callback = signal_exit_callback;
+
r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
if (r < 0)
return -errno;