diff --git a/man/systemd-run.xml b/man/systemd-run.xml index 6c15d2d8375..3c60340dc10 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. + + @@ -257,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 63f135db2fb..0475156a43b 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" @@ -58,6 +59,17 @@ 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 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; @@ -88,12 +100,15 @@ 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" " -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" @@ -157,44 +172,49 @@ static int parse_argv(int argc, char *argv[]) { ARG_NO_BLOCK, ARG_NO_ASK_PASSWORD, ARG_WAIT, + ARG_WORKING_DIRECTORY, + ARG_SHELL, }; 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' }, + { "shell", no_argument, NULL, 'S' }, {}, }; @@ -204,7 +224,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:tPqGdS", options, NULL)) >= 0) switch (c) { @@ -394,10 +414,35 @@ 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; + case 'S': + arg_shell = true; + break; + case '?': return -EINVAL; @@ -412,6 +457,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 @@ -421,9 +492,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), @@ -527,7 +623,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; @@ -581,6 +677,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)", @@ -648,7 +750,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); @@ -669,11 +771,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); @@ -894,7 +996,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; @@ -905,7 +1006,6 @@ static int start_transient_service( int r; assert(bus); - assert(argv); assert(retval); if (arg_stdio == ARG_STDIO_PTY) { @@ -997,7 +1097,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; @@ -1147,20 +1247,50 @@ static int start_transient_service( return 0; } -static int start_transient_scope( - sd_bus *bus, - char **argv) { +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); - assert(argv); + assert(!strv_isempty(arg_cmdline)); r = bus_wait_for_jobs_new(bus, &w); if (r < 0) @@ -1219,6 +1349,22 @@ static int start_transient_scope( 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"); @@ -1277,25 +1423,16 @@ static int start_transient_scope( 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); - 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; @@ -1306,7 +1443,6 @@ static int start_transient_trigger( int r; assert(bus); - assert(argv); r = bus_wait_for_jobs_new(bus, &w); if (r < 0) @@ -1399,7 +1535,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); @@ -1412,7 +1548,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; @@ -1445,16 +1581,16 @@ 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); } 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, *command = NULL; + _cleanup_free_ char *description = NULL; int r, retval = EXIT_SUCCESS; log_parse_environment(); @@ -1462,31 +1598,29 @@ int main(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; - if (argc > optind && arg_transport == BUS_TRANSPORT_LOCAL) { /* Patch in an absolute path */ - r = find_binary(argv[optind], &command); - if (r < 0) { - log_error_errno(r, "Failed to find executable %s: %m", argv[optind]); - goto finish; - } + r = find_binary(arg_cmdline[0], &command); + if (r < 0) + return log_error_errno(r, "Failed to find executable %s: %m", arg_cmdline[0]); - argv[optind] = command; + free_and_replace(arg_cmdline[0], command); } if (!arg_description) { - description = strv_join(argv + optind, " "); - if (!description) { - r = log_oom(); - goto finish; - } + description = strv_join(arg_cmdline, " "); + 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; @@ -1498,28 +1632,23 @@ int main(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, 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); + 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); - - return r < 0 ? EXIT_FAILURE : retval; + return retval; } + +DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);