]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
notify-example: also send STOPPING=1 at exit
authorLennart Poettering <lennart@poettering.net>
Wed, 3 Apr 2024 13:54:49 +0000 (15:54 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Wed, 3 Apr 2024 14:52:07 +0000 (15:52 +0100)
I think the example should reflect the full set of lifecycle messages,
including STOPPING=1, which tells the service manager that the service
is already terminating. This is useful for reporting this information
back to the user and to suppress repeated shutdown requests.

It's not as important as the READY=1 and RELOADING=1 messages, since we
actively wait for those from the service message if the right Type= is
set. But it's still very valuable information, easy to do, and completes
the state engine.

man/notify-selfcontained-example.c

index 9a7553e3013ae25007dde84ce60038a2fd1d9ba1..39f0dbe3fda4da7d9bd55b79e5595bc8fff5a06a 100644 (file)
@@ -108,6 +108,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 +173,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;
 }