]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/run/run.c
Merge pull request #1668 from ssahani/net1
[thirdparty/systemd.git] / src / run / run.c
index 85fe29b6724e9deeb99388074ba5fec961131f9f..6d3e739882dbd277574bcbe00104288ecfb8787e 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <stdio.h>
 #include <getopt.h>
+#include <stdio.h>
 
 #include "sd-bus.h"
 #include "sd-event.h"
+
+#include "bus-error.h"
 #include "bus-util.h"
-#include "event-util.h"
-#include "strv.h"
-#include "build.h"
-#include "unit-name.h"
+#include "calendarspec.h"
 #include "env-util.h"
+#include "event-util.h"
+#include "fd-util.h"
+#include "formats-util.h"
 #include "path-util.h"
-#include "bus-error.h"
-#include "calendarspec.h"
 #include "ptyfwd.h"
-#include "formats-util.h"
+#include "signal-util.h"
+#include "spawn-polkit-agent.h"
+#include "strv.h"
+#include "terminal-util.h"
+#include "unit-name.h"
 
+static bool arg_ask_password = true;
 static bool arg_scope = false;
 static bool arg_remain_after_exit = false;
+static bool arg_no_block = false;
 static const char *arg_unit = NULL;
 static const char *arg_description = NULL;
 static const char *arg_slice = NULL;
@@ -58,17 +64,30 @@ static usec_t arg_on_boot = 0;
 static usec_t arg_on_startup = 0;
 static usec_t arg_on_unit_active = 0;
 static usec_t arg_on_unit_inactive = 0;
-static char *arg_on_calendar = NULL;
+static const char *arg_on_calendar = NULL;
 static char **arg_timer_property = NULL;
 static bool arg_quiet = false;
 
+static void polkit_agent_open_if_enabled(void) {
+
+        /* Open the polkit agent as a child process if necessary */
+        if (!arg_ask_password)
+                return;
+
+        if (arg_transport != BUS_TRANSPORT_LOCAL)
+                return;
+
+        polkit_agent_open();
+}
+
 static void help(void) {
         printf("%s [OPTIONS...] {COMMAND} [ARGS...]\n\n"
                "Run the specified command in a transient scope or service or timer\n"
                "unit. If timer option is specified and unit is exist which is\n"
-               "specified with --unit option then command can be ommited.\n\n"
+               "specified with --unit option then command can be omitted.\n\n"
                "  -h --help                       Show this help\n"
                "     --version                    Show package version\n"
+               "     --no-ask-password            Do not prompt for password\n"
                "     --user                       Run as user unit\n"
                "  -H --host=[USER@]HOST           Operate on remote host\n"
                "  -M --machine=CONTAINER          Operate on local container\n"
@@ -77,6 +96,7 @@ static void help(void) {
                "  -p --property=NAME=VALUE        Set unit property\n"
                "     --description=TEXT           Description for unit\n"
                "     --slice=SLICE                Run in the specified slice\n"
+               "     --no-block                   Do not wait until operation finished\n"
                "  -r --remain-after-exit          Leave service around until explicitly stopped\n"
                "     --send-sighup                Send SIGHUP when terminating\n"
                "     --service-type=TYPE          Service type\n"
@@ -105,6 +125,7 @@ static int parse_argv(int argc, char *argv[]) {
 
         enum {
                 ARG_VERSION = 0x100,
+                ARG_NO_ASK_PASSWORD,
                 ARG_USER,
                 ARG_SYSTEM,
                 ARG_SCOPE,
@@ -124,7 +145,8 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_ON_UNIT_ACTIVE,
                 ARG_ON_UNIT_INACTIVE,
                 ARG_ON_CALENDAR,
-                ARG_TIMER_PROPERTY
+                ARG_TIMER_PROPERTY,
+                ARG_NO_BLOCK,
         };
 
         static const struct option options[] = {
@@ -155,11 +177,12 @@ static int parse_argv(int argc, char *argv[]) {
                 { "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   },
+                { "no-block",          no_argument,       NULL, ARG_NO_BLOCK         },
+                { "no-ask-password",   no_argument,       NULL, ARG_NO_ASK_PASSWORD },
                 {},
         };
 
         int r, c;
-        CalendarSpec *spec = NULL;
 
         assert(argc >= 0);
         assert(argv);
@@ -172,10 +195,12 @@ static int parse_argv(int argc, char *argv[]) {
                         help();
                         return 0;
 
+                case ARG_NO_ASK_PASSWORD:
+                        arg_ask_password = false;
+                        break;
+
                 case ARG_VERSION:
-                        puts(PACKAGE_STRING);
-                        puts(SYSTEMD_FEATURES);
-                        return 0;
+                        return version();
 
                 case ARG_USER:
                         arg_user = true;
@@ -311,16 +336,19 @@ static int parse_argv(int argc, char *argv[]) {
 
                         break;
 
-                case ARG_ON_CALENDAR:
+                case ARG_ON_CALENDAR: {
+                        CalendarSpec *spec = NULL;
 
                         r = calendar_spec_from_string(optarg, &spec);
                         if (r < 0) {
                                 log_error("Invalid calendar spec: %s", optarg);
                                 return r;
                         }
-                        free(spec);
+
+                        calendar_spec_free(spec);
                         arg_on_calendar = optarg;
                         break;
+                }
 
                 case ARG_TIMER_PROPERTY:
 
@@ -329,6 +357,10 @@ static int parse_argv(int argc, char *argv[]) {
 
                         break;
 
+                case ARG_NO_BLOCK:
+                        arg_no_block = true;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -361,6 +393,11 @@ static int parse_argv(int argc, char *argv[]) {
                 return -EINVAL;
         }
 
+        if (arg_pty && arg_transport == BUS_TRANSPORT_REMOTE) {
+                log_error("--pty is only supported when connecting to the local system or containers.");
+                return -EINVAL;
+        }
+
         if (arg_scope && with_timer()) {
                 log_error("Timer options are not supported in --scope mode.");
                 return -EINVAL;
@@ -406,9 +443,9 @@ static int transient_cgroup_set_properties(sd_bus_message *m) {
         if (!isempty(arg_slice)) {
                 _cleanup_free_ char *slice;
 
-                slice = unit_name_mangle_with_suffix(arg_slice, MANGLE_NOGLOB, ".slice");
-                if (!slice)
-                        return -ENOMEM;
+                r = unit_name_mangle_with_suffix(arg_slice, UNIT_NAME_NOGLOB, ".slice", &slice);
+                if (r < 0)
+                        return r;
 
                 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
                 if (r < 0)
@@ -651,8 +688,9 @@ static int start_transient_service(
                 sd_bus *bus,
                 char **argv) {
 
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
         _cleanup_free_ char *service = NULL, *pty_path = NULL;
         _cleanup_close_ int master = -1;
         int r;
@@ -671,14 +709,16 @@ static int start_transient_service(
                         if (r < 0)
                                 return log_error_errno(r, "Failed to determine tty name: %m");
 
+                        if (unlockpt(master) < 0)
+                                return log_error_errno(errno, "Failed to unlock tty: %m");
+
                 } else if (arg_transport == BUS_TRANSPORT_MACHINE) {
                         _cleanup_bus_unref_ sd_bus *system_bus = NULL;
-                        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
                         const char *s;
 
-                        r = sd_bus_open_system(&system_bus);
+                        r = sd_bus_default_system(&system_bus);
                         if (r < 0)
-                                log_error_errno(r, "Failed to connect to system bus: %m");
+                                return log_error_errno(r, "Failed to connect to system bus: %m");
 
                         r = sd_bus_call_method(system_bus,
                                                "org.freedesktop.machine1",
@@ -697,6 +737,8 @@ static int start_transient_service(
                         if (r < 0)
                                 return bus_log_parse_error(r);
 
+                        reply = sd_bus_message_unref(reply);
+
                         master = fcntl(master, F_DUPFD_CLOEXEC, 3);
                         if (master < 0)
                                 return log_error_errno(errno, "Failed to duplicate master fd: %m");
@@ -706,15 +748,18 @@ static int start_transient_service(
                                 return log_oom();
                 } else
                         assert_not_reached("Can't allocate tty via ssh");
+        }
 
-                if (unlockpt(master) < 0)
-                        return log_error_errno(errno, "Failed to unlock tty: %m");
+        if (!arg_no_block) {
+                r = bus_wait_for_jobs_new(bus, &w);
+                if (r < 0)
+                        return log_error_errno(r, "Could not watch jobs: %m");
         }
 
         if (arg_unit) {
-                service = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
-                if (!service)
-                        return log_oom();
+                r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".service", &service);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to mangle unit name: %m");
         } else if (asprintf(&service, "run-"PID_FMT".service", getpid()) < 0)
                 return log_oom();
 
@@ -728,6 +773,10 @@ static int start_transient_service(
         if (r < 0)
                 return bus_log_create_error(r);
 
+        r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
+        if (r < 0)
+                return bus_log_create_error(r);
+
         /* Name and mode */
         r = sd_bus_message_append(m, "ss", service, "fail");
         if (r < 0)
@@ -751,33 +800,42 @@ static int start_transient_service(
         if (r < 0)
                 return bus_log_create_error(r);
 
-        r = sd_bus_call(bus, m, 0, &error, NULL);
-        if (r < 0) {
-                log_error("Failed to start transient service unit: %s", bus_error_message(&error, -r));
-                return r;
+        polkit_agent_open_if_enabled();
+
+        r = sd_bus_call(bus, m, 0, &error, &reply);
+        if (r < 0)
+                return log_error_errno(r, "Failed to start transient service unit: %s", bus_error_message(&error, r));
+
+        if (w) {
+                const char *object;
+
+                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 (master >= 0) {
                 _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
                 _cleanup_event_unref_ sd_event *event = NULL;
-                sigset_t mask;
                 char last_char = 0;
 
                 r = sd_event_default(&event);
                 if (r < 0)
                         return log_error_errno(r, "Failed to get event loop: %m");
 
-                assert_se(sigemptyset(&mask) == 0);
-                sigset_add_many(&mask, SIGWINCH, SIGTERM, SIGINT, -1);
-                assert_se(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
+                assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
 
-                sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
-                sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
+                (void) sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
+                (void) sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
 
                 if (!arg_quiet)
                         log_info("Running as unit %s.\nPress ^] three times within 1s to disconnect TTY.", service);
 
-                r = pty_forward_new(event, master, false, false, &forward);
+                r = pty_forward_new(event, master, PTY_FORWARD_IGNORE_INITIAL_VHANGUP, &forward);
                 if (r < 0)
                         return log_error_errno(r, "Failed to create PTY forwarder: %m");
 
@@ -818,9 +876,9 @@ static int start_transient_scope(
                 return log_oom();
 
         if (arg_unit) {
-                scope = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".scope");
-                if (!scope)
-                        return log_oom();
+                r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".scope", &scope);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to mangle scope name: %m");
         } else if (asprintf(&scope, "run-"PID_FMT".scope", getpid()) < 0)
                 return log_oom();
 
@@ -834,6 +892,10 @@ static int start_transient_scope(
         if (r < 0)
                 return bus_log_create_error(r);
 
+        r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
+        if (r < 0)
+                return bus_log_create_error(r);
+
         /* Name and Mode */
         r = sd_bus_message_append(m, "ss", scope, "fail");
         if (r < 0)
@@ -857,6 +919,8 @@ static int start_transient_scope(
         if (r < 0)
                 return bus_log_create_error(r);
 
+        polkit_agent_open_if_enabled();
+
         r = sd_bus_call(bus, m, 0, &error, &reply);
         if (r < 0) {
                 log_error("Failed to start transient scope unit: %s", bus_error_message(&error, -r));
@@ -952,16 +1016,16 @@ static int start_transient_timer(
                 return log_oom();
 
         if (arg_unit) {
-                switch(unit_name_to_type(arg_unit)) {
+                switch (unit_name_to_type(arg_unit)) {
 
                 case UNIT_SERVICE:
                         service = strdup(arg_unit);
                         if (!service)
                                 return log_oom();
 
-                        timer = unit_name_change_suffix(service, ".timer");
-                        if (!timer)
-                                return log_oom();
+                        r = unit_name_change_suffix(service, ".timer", &timer);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to change unit suffix: %m");
                         break;
 
                 case UNIT_TIMER:
@@ -969,19 +1033,19 @@ static int start_transient_timer(
                         if (!timer)
                                 return log_oom();
 
-                        service = unit_name_change_suffix(timer, ".service");
-                        if (!service)
-                                return log_oom();
+                        r = unit_name_change_suffix(timer, ".service", &service);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to change unit suffix: %m");
                         break;
 
                 default:
-                        service = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
-                        if (!service)
-                                return log_oom();
+                        r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".service", &service);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to mangle unit name: %m");
 
-                        timer = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".timer");
-                        if (!timer)
-                                return log_oom();
+                        r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".timer", &timer);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to mangle unit name: %m");
 
                         break;
                 }
@@ -999,6 +1063,10 @@ static int start_transient_timer(
         if (r < 0)
                 return bus_log_create_error(r);
 
+        r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
+        if (r < 0)
+                return bus_log_create_error(r);
+
         /* Name and Mode */
         r = sd_bus_message_append(m, "ss", timer, "fail");
         if (r < 0)
@@ -1051,6 +1119,8 @@ static int start_transient_timer(
         if (r < 0)
                 return bus_log_create_error(r);
 
+        polkit_agent_open_if_enabled();
+
         r = sd_bus_call(bus, m, 0, &error, &reply);
         if (r < 0) {
                 log_error("Failed to start transient timer unit: %s", bus_error_message(&error, -r));
@@ -1073,7 +1143,7 @@ static int start_transient_timer(
 }
 
 int main(int argc, char* argv[]) {
-        _cleanup_bus_close_unref_ sd_bus *bus = NULL;
+        _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
         _cleanup_free_ char *description = NULL, *command = NULL;
         int r;
 
@@ -1084,14 +1154,20 @@ int main(int argc, char* argv[]) {
         if (r <= 0)
                 goto finish;
 
-        if (argc > optind) {
-                r = find_binary(argv[optind], arg_transport == BUS_TRANSPORT_LOCAL, &command);
+        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%s: %m",
-                                        argv[optind],
-                                        arg_transport == BUS_TRANSPORT_LOCAL ? "" : " on local system");
+                        log_error_errno(r, "Failed to find executable %s: %m", argv[optind]);
+                        goto finish;
+                }
+                if (r == 0) {
+                        log_error("Couldn't find executable %s.", argv[optind]);
+                        r = -ENOENT;
                         goto finish;
                 }
+
                 argv[optind] = command;
         }
 
@@ -1103,19 +1179,15 @@ int main(int argc, char* argv[]) {
                 }
 
                 if (arg_unit && isempty(description)) {
-                        free(description);
-                        description = strdup(arg_unit);
-
-                        if (!description) {
-                                r = log_oom();
+                        r = free_and_strdup(&description, arg_unit);
+                        if (r < 0)
                                 goto finish;
-                        }
                 }
 
                 arg_description = description;
         }
 
-        r = bus_open_transport_systemd(arg_transport, arg_host, arg_user, &bus);
+        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;