]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
notify: don't exit silently when --exec but no msg
authorMike Yuan <me@yhndnzj.com>
Mon, 29 Jan 2024 18:07:35 +0000 (02:07 +0800)
committerMike Yuan <me@yhndnzj.com>
Mon, 29 Jan 2024 19:28:16 +0000 (03:28 +0800)
Before this commit, if --exec is used but no message shall
be sent, we silently ignore --exec and exit, which is pretty
surprising. Therefore, let's emit clear error instead.

src/notify/notify.c

index 9251ff280b20c14a2f2281e35c847ea602a4cdd8..d7421d8cd552074d9f125fc06bcf739b5176293c 100644 (file)
@@ -273,22 +273,11 @@ static int parse_argv(int argc, char *argv[]) {
                 }
         }
 
-        if (optind >= argc &&
-            !arg_ready &&
-            !arg_stopping &&
-            !arg_reloading &&
-            !arg_status &&
-            !arg_pid &&
-            !arg_booted &&
-            fdset_isempty(arg_fds)) {
-                help();
-                return -EINVAL;
-        }
-
         if (arg_fdname && fdset_isempty(arg_fds))
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No file descriptors passed, but --fdname= set, refusing.");
 
-        size_t n_env;
+        bool have_env = arg_ready || arg_stopping || arg_reloading || arg_status || arg_pid > 0 || !fdset_isempty(arg_fds);
+        size_t n_arg_env;
 
         if (do_exec) {
                 int i;
@@ -306,12 +295,23 @@ static int parse_argv(int argc, char *argv[]) {
                 if (!arg_exec)
                         return log_oom();
 
-                n_env = i - optind;
+                n_arg_env = i - optind;
         } else
-                n_env = argc - optind;
+                n_arg_env = argc - optind;
+
+        have_env = have_env || n_arg_env > 0;
+
+        if (!have_env && !arg_booted) {
+                if (do_exec)
+                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No notify message specified while --exec, refusing.");
 
-        if (n_env > 0) {
-                arg_env = strv_copy_n(argv + optind, n_env);
+                /* No argument at all? */
+                help();
+                return -EINVAL;
+        }
+
+        if (n_arg_env > 0) {
+                arg_env = strv_copy_n(argv + optind, n_arg_env);
                 if (!arg_env)
                         return log_oom();
         }
@@ -392,9 +392,7 @@ static int run(int argc, char* argv[]) {
         final_env = strv_env_merge((char**) our_env, arg_env);
         if (!final_env)
                 return log_oom();
-
-        if (strv_isempty(final_env))
-                return 0;
+        assert(!strv_isempty(final_env));
 
         msg = strv_join(final_env, "\n");
         if (!msg)