From 2d21165a4e901a720a45d50344ecc9c3f078141f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 22 Nov 2018 12:07:25 +0100 Subject: [PATCH 1/5] run: add a switch for specifiying the working directory of a service I find myself testing service management quite often with "systemd-run -t /bin/bash". For that it is handy if the invoked shell would use the working directory I am currently in. Hence introduce a shorthand for that: $ systemd-run -dt /bin/bash This will automatically insert a WorkingDirectory= property into the transient service, pointing to the working directory of the caller. --- man/systemd-run.xml | 17 +++++++ src/run/run.c | 106 +++++++++++++++++++++++++++++--------------- 2 files changed, 87 insertions(+), 36 deletions(-) diff --git a/man/systemd-run.xml b/man/systemd-run.xml index 6c15d2d837..a34e20bc2b 100644 --- a/man/systemd-run.xml +++ b/man/systemd-run.xml @@ -207,6 +207,23 @@ + + + + Runs the service process with the specified working directory. Also see + WorkingDirectory= in + systemd.exec5. + + + + + + + + Similar to but uses the current working directory of the + caller for the service to execute. + + diff --git a/src/run/run.c b/src/run/run.c index 63f135db2f..12272eb033 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -58,6 +58,7 @@ static char **arg_timer_property = NULL; static bool with_timer = false; static bool arg_quiet = false; static bool arg_aggressive_gc = false; +static char *arg_working_directory = NULL; static int help(void) { _cleanup_free_ char *link = NULL; @@ -88,6 +89,8 @@ static int help(void) { " --uid=USER Run as system user\n" " --gid=GROUP Run as system group\n" " --nice=NICE Nice level\n" + " --working-directory=PATH Set working directory\n" + " -d --same-dir Inherit working directory from caller\n" " -E --setenv=NAME=VALUE Set environment\n" " -t --pty Run service on pseudo TTY as STDIN/STDOUT/\n" " STDERR\n" @@ -157,44 +160,47 @@ static int parse_argv(int argc, char *argv[]) { ARG_NO_BLOCK, ARG_NO_ASK_PASSWORD, ARG_WAIT, + ARG_WORKING_DIRECTORY, }; static const struct option options[] = { - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, ARG_VERSION }, - { "user", no_argument, NULL, ARG_USER }, - { "system", no_argument, NULL, ARG_SYSTEM }, - { "scope", no_argument, NULL, ARG_SCOPE }, - { "unit", required_argument, NULL, ARG_UNIT }, - { "description", required_argument, NULL, ARG_DESCRIPTION }, - { "slice", required_argument, NULL, ARG_SLICE }, - { "remain-after-exit", no_argument, NULL, 'r' }, - { "send-sighup", no_argument, NULL, ARG_SEND_SIGHUP }, - { "host", required_argument, NULL, 'H' }, - { "machine", required_argument, NULL, 'M' }, - { "service-type", required_argument, NULL, ARG_SERVICE_TYPE }, - { "wait", no_argument, NULL, ARG_WAIT }, - { "uid", required_argument, NULL, ARG_EXEC_USER }, - { "gid", required_argument, NULL, ARG_EXEC_GROUP }, - { "nice", required_argument, NULL, ARG_NICE }, - { "setenv", required_argument, NULL, 'E' }, - { "property", required_argument, NULL, 'p' }, - { "tty", no_argument, NULL, 't' }, /* deprecated alias */ - { "pty", no_argument, NULL, 't' }, - { "pipe", no_argument, NULL, 'P' }, - { "quiet", no_argument, NULL, 'q' }, - { "on-active", required_argument, NULL, ARG_ON_ACTIVE }, - { "on-boot", required_argument, NULL, ARG_ON_BOOT }, - { "on-startup", required_argument, NULL, ARG_ON_STARTUP }, - { "on-unit-active", required_argument, NULL, ARG_ON_UNIT_ACTIVE }, - { "on-unit-inactive", required_argument, NULL, ARG_ON_UNIT_INACTIVE }, - { "on-calendar", required_argument, NULL, ARG_ON_CALENDAR }, - { "timer-property", required_argument, NULL, ARG_TIMER_PROPERTY }, - { "path-property", required_argument, NULL, ARG_PATH_PROPERTY }, - { "socket-property", required_argument, NULL, ARG_SOCKET_PROPERTY }, - { "no-block", no_argument, NULL, ARG_NO_BLOCK }, - { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, - { "collect", no_argument, NULL, 'G' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, ARG_VERSION }, + { "user", no_argument, NULL, ARG_USER }, + { "system", no_argument, NULL, ARG_SYSTEM }, + { "scope", no_argument, NULL, ARG_SCOPE }, + { "unit", required_argument, NULL, ARG_UNIT }, + { "description", required_argument, NULL, ARG_DESCRIPTION }, + { "slice", required_argument, NULL, ARG_SLICE }, + { "remain-after-exit", no_argument, NULL, 'r' }, + { "send-sighup", no_argument, NULL, ARG_SEND_SIGHUP }, + { "host", required_argument, NULL, 'H' }, + { "machine", required_argument, NULL, 'M' }, + { "service-type", required_argument, NULL, ARG_SERVICE_TYPE }, + { "wait", no_argument, NULL, ARG_WAIT }, + { "uid", required_argument, NULL, ARG_EXEC_USER }, + { "gid", required_argument, NULL, ARG_EXEC_GROUP }, + { "nice", required_argument, NULL, ARG_NICE }, + { "setenv", required_argument, NULL, 'E' }, + { "property", required_argument, NULL, 'p' }, + { "tty", no_argument, NULL, 't' }, /* deprecated alias */ + { "pty", no_argument, NULL, 't' }, + { "pipe", no_argument, NULL, 'P' }, + { "quiet", no_argument, NULL, 'q' }, + { "on-active", required_argument, NULL, ARG_ON_ACTIVE }, + { "on-boot", required_argument, NULL, ARG_ON_BOOT }, + { "on-startup", required_argument, NULL, ARG_ON_STARTUP }, + { "on-unit-active", required_argument, NULL, ARG_ON_UNIT_ACTIVE }, + { "on-unit-inactive", required_argument, NULL, ARG_ON_UNIT_INACTIVE }, + { "on-calendar", required_argument, NULL, ARG_ON_CALENDAR }, + { "timer-property", required_argument, NULL, ARG_TIMER_PROPERTY }, + { "path-property", required_argument, NULL, ARG_PATH_PROPERTY }, + { "socket-property", required_argument, NULL, ARG_SOCKET_PROPERTY }, + { "no-block", no_argument, NULL, ARG_NO_BLOCK }, + { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, + { "collect", no_argument, NULL, 'G' }, + { "working-directory", required_argument, NULL, ARG_WORKING_DIRECTORY }, + { "same-dir", no_argument, NULL, 'd' }, {}, }; @@ -204,7 +210,7 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); - while ((c = getopt_long(argc, argv, "+hrH:M:E:p:tPqG", options, NULL)) >= 0) + while ((c = getopt_long(argc, argv, "+hrH:M:E:p:tPqGd", options, NULL)) >= 0) switch (c) { @@ -394,6 +400,27 @@ static int parse_argv(int argc, char *argv[]) { arg_wait = true; break; + case ARG_WORKING_DIRECTORY: + r = parse_path_argument_and_warn(optarg, true, &arg_working_directory); + if (r < 0) + return r; + + break; + + case 'd': { + _cleanup_free_ char *p = NULL; + + r = safe_getcwd(&p); + if (r < 0) + return log_error_errno(r, "Failed to get current working directory: %m"); + + if (empty_or_root(p)) + arg_working_directory = mfree(arg_working_directory); + else + free_and_replace(arg_working_directory, p); + break; + } + case 'G': arg_aggressive_gc = true; break; @@ -581,6 +608,12 @@ static int transient_service_set_properties(sd_bus_message *m, char **argv, cons return bus_log_create_error(r); } + if (arg_working_directory) { + r = sd_bus_message_append(m, "(sv)", "WorkingDirectory", "s", arg_working_directory); + if (r < 0) + return bus_log_create_error(r); + } + if (pty_path) { r = sd_bus_message_append(m, "(sv)(sv)(sv)(sv)", @@ -1520,6 +1553,7 @@ finish: strv_free(arg_path_property); strv_free(arg_socket_property); strv_free(arg_timer_property); + free(arg_working_directory); return r < 0 ? EXIT_FAILURE : retval; } From badd28e1589e221c0b58106141172a86901187ee Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 22 Nov 2018 19:24:48 +0100 Subject: [PATCH 2/5] run: add new --shell switch for spawning a shell as service I keep running "systemd-run -t /bin/bash" to quickly get a shell running in service context. I suspect I am not the only one, hence let's add a shortcut for it. While we are at it, let's make it smarter, and automatically inherit the $SHELL of the invoking user as well as the working directory, and let's imply --pty. --shell (or -S) is hence equivalent to "-t -d $SHELL". --- man/systemd-run.xml | 9 ++++ src/run/run.c | 123 ++++++++++++++++++++++++++++++++------------ 2 files changed, 99 insertions(+), 33 deletions(-) diff --git a/man/systemd-run.xml b/man/systemd-run.xml index a34e20bc2b..3c60340dc1 100644 --- a/man/systemd-run.xml +++ b/man/systemd-run.xml @@ -274,6 +274,15 @@ "hello" >&2 instead, which is mostly equivalent and avoids this pitfall. + + + + + A shortcut for --pty --same-dir --wait --collect --service-type=exec $SHELL, + i.e. requests an interactive shell in the current working directory, running in service context, accessible + with a single switch. + + diff --git a/src/run/run.c b/src/run/run.c index 12272eb033..2459009d64 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -59,6 +59,8 @@ static bool with_timer = false; static bool arg_quiet = false; static bool arg_aggressive_gc = false; static char *arg_working_directory = NULL; +static bool arg_shell = false; +static char **arg_cmdline = NULL; static int help(void) { _cleanup_free_ char *link = NULL; @@ -96,7 +98,8 @@ static int help(void) { " STDERR\n" " -P --pipe Pass STDIN/STDOUT/STDERR directly to service\n" " -q --quiet Suppress information messages during runtime\n" - " -G --collect Unload unit after it ran, even when failed\n\n" + " -G --collect Unload unit after it ran, even when failed\n" + " -S --shell Invoke a $SHELL interactively\n\n" "Path options:\n" " --path-property=NAME=VALUE Set path unit property\n\n" "Socket options:\n" @@ -161,6 +164,7 @@ static int parse_argv(int argc, char *argv[]) { ARG_NO_ASK_PASSWORD, ARG_WAIT, ARG_WORKING_DIRECTORY, + ARG_SHELL, }; static const struct option options[] = { @@ -201,6 +205,7 @@ static int parse_argv(int argc, char *argv[]) { { "collect", no_argument, NULL, 'G' }, { "working-directory", required_argument, NULL, ARG_WORKING_DIRECTORY }, { "same-dir", no_argument, NULL, 'd' }, + { "shell", no_argument, NULL, 'S' }, {}, }; @@ -210,7 +215,7 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); - while ((c = getopt_long(argc, argv, "+hrH:M:E:p:tPqGd", options, NULL)) >= 0) + while ((c = getopt_long(argc, argv, "+hrH:M:E:p:tPqGdS", options, NULL)) >= 0) switch (c) { @@ -425,6 +430,10 @@ static int parse_argv(int argc, char *argv[]) { arg_aggressive_gc = true; break; + case 'S': + arg_shell = true; + break; + case '?': return -EINVAL; @@ -439,6 +448,32 @@ static int parse_argv(int argc, char *argv[]) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Only single trigger (path, socket, timer) unit can be created."); + if (arg_shell) { + /* If --shell is imply --pty --pipe --same-dir --service-type=exec --wait --collect, unless otherwise + * specified. */ + + if (!arg_scope) { + if (arg_stdio == ARG_STDIO_NONE) + arg_stdio = ARG_STDIO_AUTO; + + if (!arg_working_directory) { + r = safe_getcwd(&arg_working_directory); + if (r < 0) + return log_error_errno(r, "Failed to get current working directory: %m"); + } + + if (!arg_service_type) { + arg_service_type = strdup("exec"); + if (!arg_service_type) + return log_oom(); + } + + arg_wait = true; + } + + arg_aggressive_gc = true; + } + if (arg_stdio == ARG_STDIO_AUTO) { /* If we both --pty and --pipe are specified we'll automatically pick --pty if we are connected fully * to a TTY and pick direct fd passing otherwise. This way, we automatically adapt to usage in a shell @@ -448,9 +483,34 @@ static int parse_argv(int argc, char *argv[]) { ARG_STDIO_DIRECT; } - if ((optind >= argc) && (!arg_unit || !with_trigger)) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Command line to execute required."); + if (argc > optind) { + char **l; + + if (arg_shell) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "If --shell is used, no command line is expected."); + + l = strv_copy(argv + optind); + if (!l) + return log_oom(); + + strv_free_and_replace(arg_cmdline, l); + + } else if (arg_shell) { + _cleanup_free_ char *s = NULL; + char **l; + + r = get_shell(&s); + if (r < 0) + return log_error_errno(r, "Failed to determine shell: %m"); + + l = strv_new(s); + if (!l) + return log_oom(); + + strv_free_and_replace(arg_cmdline, l); + + } else if (!arg_unit || !with_trigger) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Command line to execute required."); if (arg_user && arg_transport != BUS_TRANSPORT_LOCAL) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), @@ -554,7 +614,7 @@ static int transient_kill_set_properties(sd_bus_message *m) { return 0; } -static int transient_service_set_properties(sd_bus_message *m, char **argv, const char *pty_path) { +static int transient_service_set_properties(sd_bus_message *m, const char *pty_path) { bool send_term = false; int r; @@ -681,7 +741,7 @@ static int transient_service_set_properties(sd_bus_message *m, char **argv, cons } /* Exec container */ - { + if (!strv_isempty(arg_cmdline)) { r = sd_bus_message_open_container(m, 'r', "sv"); if (r < 0) return bus_log_create_error(r); @@ -702,11 +762,11 @@ static int transient_service_set_properties(sd_bus_message *m, char **argv, cons if (r < 0) return bus_log_create_error(r); - r = sd_bus_message_append(m, "s", argv[0]); + r = sd_bus_message_append(m, "s", arg_cmdline[0]); if (r < 0) return bus_log_create_error(r); - r = sd_bus_message_append_strv(m, argv); + r = sd_bus_message_append_strv(m, arg_cmdline); if (r < 0) return bus_log_create_error(r); @@ -927,7 +987,6 @@ static int pty_forward_handler(PTYForward *f, int rcode, void *userdata) { static int start_transient_service( sd_bus *bus, - char **argv, int *retval) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; @@ -938,7 +997,6 @@ static int start_transient_service( int r; assert(bus); - assert(argv); assert(retval); if (arg_stdio == ARG_STDIO_PTY) { @@ -1030,7 +1088,7 @@ static int start_transient_service( if (r < 0) return bus_log_create_error(r); - r = transient_service_set_properties(m, argv, pty_path); + r = transient_service_set_properties(m, pty_path); if (r < 0) return r; @@ -1180,9 +1238,7 @@ static int start_transient_service( return 0; } -static int start_transient_scope( - sd_bus *bus, - char **argv) { +static int start_transient_scope(sd_bus *bus) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; @@ -1193,7 +1249,7 @@ static int start_transient_scope( int r; assert(bus); - assert(argv); + assert(!strv_isempty(arg_cmdline)); r = bus_wait_for_jobs_new(bus, &w); if (r < 0) @@ -1321,14 +1377,13 @@ static int start_transient_scope( if (!arg_quiet) log_info("Running scope as unit: %s", scope); - execvpe(argv[0], argv, env); + execvpe(arg_cmdline[0], arg_cmdline, env); return log_error_errno(errno, "Failed to execute: %m"); } static int start_transient_trigger( sd_bus *bus, - char **argv, const char *suffix) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; @@ -1339,7 +1394,6 @@ static int start_transient_trigger( int r; assert(bus); - assert(argv); r = bus_wait_for_jobs_new(bus, &w); if (r < 0) @@ -1432,7 +1486,7 @@ static int start_transient_trigger( if (r < 0) return bus_log_create_error(r); - if (!strv_isempty(argv)) { + if (!strv_isempty(arg_cmdline)) { r = sd_bus_message_open_container(m, 'r', "sa(sv)"); if (r < 0) return bus_log_create_error(r); @@ -1445,7 +1499,7 @@ static int start_transient_trigger( if (r < 0) return bus_log_create_error(r); - r = transient_service_set_properties(m, argv, NULL); + r = transient_service_set_properties(m, NULL); if (r < 0) return r; @@ -1478,7 +1532,7 @@ static int start_transient_trigger( if (!arg_quiet) { log_info("Running %s as unit: %s", suffix + 1, trigger); - if (argv[0]) + if (!strv_isempty(arg_cmdline)) log_info("Will run service as unit: %s", service); } @@ -1487,7 +1541,7 @@ static int start_transient_trigger( int main(int argc, char* argv[]) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; - _cleanup_free_ char *description = NULL, *command = NULL; + _cleanup_free_ char *description = NULL; int r, retval = EXIT_SUCCESS; log_parse_environment(); @@ -1497,20 +1551,22 @@ int main(int argc, char* argv[]) { if (r <= 0) goto finish; - if (argc > optind && arg_transport == BUS_TRANSPORT_LOCAL) { + if (!strv_isempty(arg_cmdline) && arg_transport == BUS_TRANSPORT_LOCAL) { + _cleanup_free_ char *command = NULL; + /* Patch in an absolute path */ - r = find_binary(argv[optind], &command); + r = find_binary(arg_cmdline[0], &command); if (r < 0) { - log_error_errno(r, "Failed to find executable %s: %m", argv[optind]); + log_error_errno(r, "Failed to find executable %s: %m", arg_cmdline[0]); goto finish; } - argv[optind] = command; + free_and_replace(arg_cmdline[0], command); } if (!arg_description) { - description = strv_join(argv + optind, " "); + description = strv_join(arg_cmdline, " "); if (!description) { r = log_oom(); goto finish; @@ -1537,15 +1593,15 @@ int main(int argc, char* argv[]) { } if (arg_scope) - r = start_transient_scope(bus, argv + optind); + r = start_transient_scope(bus); else if (arg_path_property) - r = start_transient_trigger(bus, argv + optind, ".path"); + r = start_transient_trigger(bus, ".path"); else if (arg_socket_property) - r = start_transient_trigger(bus, argv + optind, ".socket"); + r = start_transient_trigger(bus, ".socket"); else if (with_timer) - r = start_transient_trigger(bus, argv + optind, ".timer"); + r = start_transient_trigger(bus, ".timer"); else - r = start_transient_service(bus, argv + optind, &retval); + r = start_transient_service(bus, &retval); finish: strv_free(arg_environment); @@ -1553,6 +1609,7 @@ finish: strv_free(arg_path_property); strv_free(arg_socket_property); strv_free(arg_timer_property); + strv_free(arg_cmdline); free(arg_working_directory); return r < 0 ? EXIT_FAILURE : retval; From ef08ad7aa86e9ccc883bcd0e950f7bcbc2169cbb Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 22 Nov 2018 19:30:03 +0100 Subject: [PATCH 3/5] run: port to DEFINE_MAIN_FUNCTION() --- src/run/run.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/run/run.c b/src/run/run.c index 2459009d64..acbc3c7bca 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -14,6 +14,7 @@ #include "env-util.h" #include "fd-util.h" #include "format-util.h" +#include "main-func.h" #include "parse-util.h" #include "path-util.h" #include "pretty-print.h" @@ -1539,7 +1540,7 @@ static int start_transient_trigger( return 0; } -int main(int argc, char* argv[]) { +static int run(int argc, char* argv[]) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; _cleanup_free_ char *description = NULL; int r, retval = EXIT_SUCCESS; @@ -1612,5 +1613,7 @@ finish: strv_free(arg_cmdline); free(arg_working_directory); - return r < 0 ? EXIT_FAILURE : retval; + return r < 0 ? r : retval; } + +DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); From 0565120f004f129bec67b9e92c805c6493b71fa2 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 22 Nov 2018 19:34:15 +0100 Subject: [PATCH 4/5] run: port to static destructors --- src/run/run.c | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/src/run/run.c b/src/run/run.c index acbc3c7bca..daf9451c0b 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -63,6 +63,14 @@ static char *arg_working_directory = NULL; static bool arg_shell = false; static char **arg_cmdline = NULL; +STATIC_DESTRUCTOR_REGISTER(arg_environment, strv_freep); +STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep); +STATIC_DESTRUCTOR_REGISTER(arg_path_property, strv_freep); +STATIC_DESTRUCTOR_REGISTER(arg_socket_property, strv_freep); +STATIC_DESTRUCTOR_REGISTER(arg_timer_property, strv_freep); +STATIC_DESTRUCTOR_REGISTER(arg_working_directory, freep); +STATIC_DESTRUCTOR_REGISTER(arg_cmdline, strv_freep); + static int help(void) { _cleanup_free_ char *link = NULL; int r; @@ -1550,7 +1558,7 @@ static int run(int argc, char* argv[]) { r = parse_argv(argc, argv); if (r <= 0) - goto finish; + return r; if (!strv_isempty(arg_cmdline) && arg_transport == BUS_TRANSPORT_LOCAL) { _cleanup_free_ char *command = NULL; @@ -1558,25 +1566,21 @@ static int run(int argc, char* argv[]) { /* Patch in an absolute path */ r = find_binary(arg_cmdline[0], &command); - if (r < 0) { - log_error_errno(r, "Failed to find executable %s: %m", arg_cmdline[0]); - goto finish; - } + if (r < 0) + return log_error_errno(r, "Failed to find executable %s: %m", arg_cmdline[0]); free_and_replace(arg_cmdline[0], command); } if (!arg_description) { description = strv_join(arg_cmdline, " "); - if (!description) { - r = log_oom(); - goto finish; - } + if (!description) + return log_oom(); if (arg_unit && isempty(description)) { r = free_and_strdup(&description, arg_unit); if (r < 0) - goto finish; + return log_oom(); } arg_description = description; @@ -1588,10 +1592,8 @@ static int run(int argc, char* argv[]) { r = bus_connect_transport(arg_transport, arg_host, arg_user, &bus); else r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus); - if (r < 0) { - log_error_errno(r, "Failed to create bus connection: %m"); - goto finish; - } + if (r < 0) + return log_error_errno(r, "Failed to create bus connection: %m"); if (arg_scope) r = start_transient_scope(bus); @@ -1603,17 +1605,10 @@ static int run(int argc, char* argv[]) { r = start_transient_trigger(bus, ".timer"); else r = start_transient_service(bus, &retval); + if (r < 0) + return r; -finish: - strv_free(arg_environment); - strv_free(arg_property); - strv_free(arg_path_property); - strv_free(arg_socket_property); - strv_free(arg_timer_property); - strv_free(arg_cmdline); - free(arg_working_directory); - - return r < 0 ? r : retval; + return retval; } DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); From 00f8eea8fccbcc6d97171a13c776cd835a0b4580 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 22 Nov 2018 20:40:00 +0100 Subject: [PATCH 5/5] run: set $INVOCATION_ID for scope units Services invoked by PID1 have $INVOCATION_ID initialized, hence let's do that for scope units (where the payload process is invoked by us on the client side) too, to minimize needless differences. Fixes: #8082 --- src/run/run.c | 58 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/src/run/run.c b/src/run/run.c index daf9451c0b..0475156a43 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -1247,14 +1247,46 @@ static int start_transient_service( return 0; } -static int start_transient_scope(sd_bus *bus) { +static int acquire_invocation_id(sd_bus *bus, sd_id128_t *ret) { + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + const void *p; + size_t l; + int r; + assert(bus); + assert(ret); + + r = sd_bus_get_property(bus, + "org.freedesktop.systemd1", + "/org/freedesktop/systemd1/unit/self", + "org.freedesktop.systemd1.Unit", + "InvocationID", + &error, + &reply, + "ay"); + if (r < 0) + return log_error_errno(r, "Failed to request invocation ID for scope: %s", bus_error_message(&error, r)); + + r = sd_bus_message_read_array(reply, 'y', &p, &l); + if (r < 0) + return bus_log_parse_error(r); + + if (l != sizeof(sd_id128_t)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid UUID size, %zu != %zu.", l, sizeof(sd_id128_t)); + + memcpy(ret, p, l); + return 0; +} + +static int start_transient_scope(sd_bus *bus) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; _cleanup_strv_free_ char **env = NULL, **user_env = NULL; _cleanup_free_ char *scope = NULL; const char *object = NULL; + sd_id128_t invocation_id; int r; assert(bus); @@ -1317,6 +1349,22 @@ static int start_transient_scope(sd_bus *bus) { if (r < 0) return log_error_errno(r, "Failed to start transient scope unit: %s", bus_error_message(&error, -r)); + r = sd_bus_message_read(reply, "o", &object); + if (r < 0) + return bus_log_parse_error(r); + + r = bus_wait_for_jobs_one(w, object, arg_quiet); + if (r < 0) + return r; + + r = acquire_invocation_id(bus, &invocation_id); + if (r < 0) + return r; + + r = strv_extendf(&user_env, "INVOCATION_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(invocation_id)); + if (r < 0) + return log_oom(); + if (arg_nice_set) { if (setpriority(PRIO_PROCESS, 0, arg_nice) < 0) return log_error_errno(errno, "Failed to set nice level: %m"); @@ -1375,14 +1423,6 @@ static int start_transient_scope(sd_bus *bus) { if (!env) return log_oom(); - r = sd_bus_message_read(reply, "o", &object); - if (r < 0) - return bus_log_parse_error(r); - - r = bus_wait_for_jobs_one(w, object, arg_quiet); - if (r < 0) - return r; - if (!arg_quiet) log_info("Running scope as unit: %s", scope);