SD_EVENT_SET_SIGNAL_EXIT(3)
π SD_EVENT_SET_SIGNAL_EXIT(3) π
okay so bestie, gather round β this one's from the sd-event family (that's section 3 of the manual, iykyk) and it's genuinely one of the comfiest little functions ever. it's called sd_event_set_signal_exit and it's literally "Automatically leave event loop on SIGINT and SIGTERM". no cap, that's the whole vibe π
β¨ SYNOPSIS β¨
#include <systemd/sd-event.h>
int sd_event_set_signal_exit(sd_event *event, int b);
πΈ DESCRIPTION πΈ
sd_event_set_signal_exit() is your new bestie for making sure your event loop actually stops when someone hits it with a SIGINT or a SIGTERM. it's a convenience wrapper around calling sd_event_add_signal(3) for both of those signals β like, you could do it manually, but why would you when this exists π
and listen, there's a lil catch: both signals get automatically added to the calling thread's signal mask. so if your program is multi-threaded, be careful β either invoke this function before the first thread is started, or manually block the two signals process-wide first. do not skip this step, it's giving "I didn't read the docs" energy π
here's how the b parameter works, bestie:
- pass
bas true β the event loop will terminate onSIGINTandSIGTERM. iconic behavior, honestly π - pass
bas false β it will no longer do that. and when you turn this functionality off, the calling thread's signal mask gets restored to match the state it was in before you turned it on, for those two signals. very "we're so back / it's so over" of it β¨
by default (like, out of the box), those two signals are not handled by the event loop, and Linux' default signal handling for them is in effect.
and here's a little cultural note: it is customary for UNIX programs to exit on either of these two signals, so it's typically a good idea to enable this functionality for the main event loop of a program. fr fr, do it π«Ά
β RETURN VALUE β
sd_event_set_signal_exit() is out here returning three different vibes depending on how it went:
- a positive non-zero value when the setting was successfully changed β slay π
- zero when the specified setting was already in effect β like, we been knew, no change needed
- on failure, a negative errno-style error code β π
π Errors
returned errors may indicate the following problems:
-ECHILDβ the event loop has been created in a different process, library or module instance. awkward. added in version 252.-EINVALβ the passed event loop object was invalid. added in version 252.
π¦ NOTES π¦
the functions described here are available as a shared library, which can be compiled against and linked to with the libsystemd pkg-config(1) file. standard bestie behavior π
all functions listed here are thread-agnostic, and only a single thread may operate on a given object at any given time. different threads may access the same object at different times, and multiple independent objects may be used from different threads in parallel. so like β share the object, just don't all touch it at literally the same moment, okay? π
β οΈ heads up: the code described here usesgetenv(3), which is declared to be not multi-thread-safe. that means the code calling the functions described here must not callsetenv(3)from a parallel thread. it is recommended to only do calls tosetenv()from an early phase of the program when no other threads have been started. please, for the love of everything, respect this one π
π HISTORY π
sd_event_set_signal_exit() was added in version 252. a relatively recent queen, but a queen nonetheless π
π₯ SEE ALSO π₯
okay that's the whole page!! this beauty is from systemd 261.3, page SD_EVENT_SET_SIGNAL_EXIT(3) β go forth and let your event loop exit gracefully on SIGINT and SIGTERM, bestie πβ¨π
π¦ where this page comes from
- ubuntu stonking: libsystemd-dev 261.3-0ubuntu3 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms