From: Mike Yuan Date: Mon, 29 Jan 2024 18:07:35 +0000 (+0800) Subject: notify: don't exit silently when --exec but no msg X-Git-Tag: v256-rc1~1016^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=953134a5850cf8d2aaf7405074f4131330f81cb9;p=thirdparty%2Fsystemd.git notify: don't exit silently when --exec but no msg 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. --- diff --git a/src/notify/notify.c b/src/notify/notify.c index 9251ff280b2..d7421d8cd55 100644 --- a/src/notify/notify.c +++ b/src/notify/notify.c @@ -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)