From fc53809803cd2bc2434e312b19a18fa36776da12 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 12 Jun 2024 05:50:40 +0200 Subject: Adding upstream version 256. Signed-off-by: Daniel Baumann --- man/notify-selfcontained-example.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'man/notify-selfcontained-example.c') diff --git a/man/notify-selfcontained-example.c b/man/notify-selfcontained-example.c index 9a7553e..6bbe4f2 100644 --- a/man/notify-selfcontained-example.c +++ b/man/notify-selfcontained-example.c @@ -7,6 +7,7 @@ * This protocol is guaranteed to be stable as per: * https://systemd.io/PORTABILITY_AND_STABILITY/ */ +#define _GNU_SOURCE 1 #include #include #include @@ -40,10 +41,7 @@ static int notify(const char *message) { _cleanup_(closep) int fd = -1; const char *socket_path; - socket_path = getenv("NOTIFY_SOCKET"); - if (!socket_path) - return 0; /* Not running under systemd? Nothing to do */ - + /* Verify the argument first */ if (!message) return -EINVAL; @@ -51,6 +49,11 @@ static int notify(const char *message) { if (message_length == 0) return -EINVAL; + /* If the variable is not set, the protocol is a noop */ + socket_path = getenv("NOTIFY_SOCKET"); + if (!socket_path) + return 0; /* Not set? Nothing to do */ + /* Only AF_UNIX is supported, with path or abstract sockets */ if (socket_path[0] != '/' && socket_path[0] != '@') return -EAFNOSUPPORT; @@ -108,6 +111,10 @@ static int notify_reloading(void) { return notify(reload_message); } +static int notify_stopping(void) { + return notify("STOPPING=1"); +} + static volatile sig_atomic_t reloading = 0; static volatile sig_atomic_t terminating = 0; @@ -169,5 +176,13 @@ int main(int argc, char **argv) { sleep(5); } + r = notify_stopping(); + if (r < 0) { + fprintf(stderr, "Failed to report termination to $NOTIFY_SOCKET: %s\n", strerror(-r)); + return EXIT_FAILURE; + } + + /* Do some shutdown work here … */ + return EXIT_SUCCESS; } -- cgit v1.2.3