]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
man/notify-selfcontained-example: check argument first
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 4 Apr 2024 08:57:30 +0000 (10:57 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 4 Apr 2024 10:18:30 +0000 (12:18 +0200)
This is just good style. In this particular case, if the argument is incorrect and
the function is not tested with $NOTIFY_SOCKET set, the user could not get the
proper error until running for real.

Also, remove mention of systemd. The protocol is fully generic on purpose.

man/notify-selfcontained-example.c

index 90c386137416a3ee017bae2895a2eeef21104e25..6bbe4f2e3bade2e3bf9da8d87d9adacfa1d5613b 100644 (file)
@@ -41,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;
 
@@ -52,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;