]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/run/run.c
everywhere: port everything to sigprocmask_many() and friends
[thirdparty/systemd.git] / src / run / run.c
index 7a8022391865aebf329015e6ecab10a93e16ef8a..99d960a664815ecfc2f5936a2904dd8d68b67336 100644 (file)
@@ -23,7 +23,9 @@
 #include <getopt.h>
 
 #include "sd-bus.h"
+#include "sd-event.h"
 #include "bus-util.h"
+#include "event-util.h"
 #include "strv.h"
 #include "build.h"
 #include "unit-name.h"
 #include "path-util.h"
 #include "bus-error.h"
 #include "calendarspec.h"
+#include "ptyfwd.h"
+#include "formats-util.h"
+#include "signal-util.h"
 
 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;
@@ -48,6 +54,7 @@ static int arg_nice = 0;
 static bool arg_nice_set = false;
 static char **arg_environment = NULL;
 static char **arg_property = NULL;
+static bool arg_pty = false;
 static usec_t arg_on_active = 0;
 static usec_t arg_on_boot = 0;
 static usec_t arg_on_startup = 0;
@@ -55,12 +62,13 @@ static usec_t arg_on_unit_active = 0;
 static usec_t arg_on_unit_inactive = 0;
 static char *arg_on_calendar = NULL;
 static char **arg_timer_property = NULL;
+static bool arg_quiet = false;
 
 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"
                "     --user                       Run as user unit\n"
@@ -71,19 +79,22 @@ 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"
                "     --uid=USER                   Run as system user\n"
                "     --gid=GROUP                  Run as system group\n"
                "     --nice=NICE                  Nice level\n"
-               "     --setenv=NAME=VALUE          Set environment\n\n"
+               "     --setenv=NAME=VALUE          Set environment\n"
+               "  -t --pty                        Run service on pseudo tty\n"
+               "  -q --quiet                      Suppress information messages during runtime\n\n"
                "Timer options:\n\n"
-               "     --on-active=SEC              Run after seconds\n"
-               "     --on-boot=SEC                Run after seconds from machine was booted up\n"
-               "     --on-startup=SEC             Run after seconds from systemd was first started\n"
-               "     --on-unit-active=SEC         Run after seconds from the last activation\n"
-               "     --on-unit-inactive=SEC       Run after seconds from the last deactivation\n"
+               "     --on-active=SECONDS          Run after SECONDS delay\n"
+               "     --on-boot=SECONDS            Run SECONDS after machine was booted up\n"
+               "     --on-startup=SECONDS         Run SECONDS after systemd activation\n"
+               "     --on-unit-active=SECONDS     Run SECONDS after the last activation\n"
+               "     --on-unit-inactive=SECONDS   Run SECONDS after the last deactivation\n"
                "     --on-calendar=SPEC           Realtime timer\n"
                "     --timer-property=NAME=VALUE  Set timer unit property\n",
                program_invocation_short_name);
@@ -109,13 +120,15 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_SERVICE_TYPE,
                 ARG_NICE,
                 ARG_SETENV,
+                ARG_TTY,
                 ARG_ON_ACTIVE,
                 ARG_ON_BOOT,
                 ARG_ON_STARTUP,
                 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[] = {
@@ -137,6 +150,8 @@ static int parse_argv(int argc, char *argv[]) {
                 { "nice",              required_argument, NULL, ARG_NICE             },
                 { "setenv",            required_argument, NULL, ARG_SETENV           },
                 { "property",          required_argument, NULL, 'p'                  },
+                { "tty",               no_argument,       NULL, 't'                  },
+                { "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       },
@@ -144,6 +159,7 @@ 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         },
                 {},
         };
 
@@ -153,7 +169,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "+hrH:M:p:", options, NULL)) >= 0)
+        while ((c = getopt_long(argc, argv, "+hrH:M:p:tq", options, NULL)) >= 0)
 
                 switch (c) {
 
@@ -204,7 +220,7 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case 'M':
-                        arg_transport = BUS_TRANSPORT_CONTAINER;
+                        arg_transport = BUS_TRANSPORT_MACHINE;
                         arg_host = optarg;
                         break;
 
@@ -231,19 +247,25 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_SETENV:
-
                         if (strv_extend(&arg_environment, optarg) < 0)
                                 return log_oom();
 
                         break;
 
                 case 'p':
-
                         if (strv_extend(&arg_property, optarg) < 0)
                                 return log_oom();
 
                         break;
 
+                case 't':
+                        arg_pty = true;
+                        break;
+
+                case 'q':
+                        arg_quiet = true;
+                        break;
+
                 case ARG_ON_ACTIVE:
 
                         r = parse_sec(optarg, &arg_on_active);
@@ -312,6 +334,10 @@ static int parse_argv(int argc, char *argv[]) {
 
                         break;
 
+                case ARG_NO_BLOCK:
+                        arg_no_block = true;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -339,6 +365,11 @@ static int parse_argv(int argc, char *argv[]) {
                 return -EINVAL;
         }
 
+        if (arg_pty && (with_timer() || arg_scope)) {
+                log_error("--pty is not compatible in timer or --scope mode.");
+                return -EINVAL;
+        }
+
         if (arg_scope && with_timer()) {
                 log_error("Timer options are not supported in --scope mode.");
                 return -EINVAL;
@@ -352,45 +383,43 @@ static int parse_argv(int argc, char *argv[]) {
         return 1;
 }
 
-static int transient_unit_set_properties(sd_bus_message *m, UnitType t) {
+static int transient_unit_set_properties(sd_bus_message *m, char **properties) {
         char **i;
         int r;
 
-        STRV_FOREACH(i, t == UNIT_TIMER ? arg_timer_property : arg_property) {
+        r = sd_bus_message_append(m, "(sv)", "Description", "s", arg_description);
+        if (r < 0)
+                return r;
+
+        STRV_FOREACH(i, properties) {
                 r = sd_bus_message_open_container(m, 'r', "sv");
                 if (r < 0)
                         return r;
 
                 r = bus_append_unit_property_assignment(m, *i);
-                if (r < 0) {
-                        r = sd_bus_message_append(m, "sv", 0);
-                        if (r < 0)
-                                return r;
-                }
+                if (r < 0)
+                        return r;
 
                 r = sd_bus_message_close_container(m);
                 if (r < 0)
                         return r;
         }
 
-        r = sd_bus_message_append(m, "(sv)", "Description", "s", arg_description);
-        if (r < 0)
-                return r;
+        return 0;
+}
+
+static int transient_cgroup_set_properties(sd_bus_message *m) {
+        int r;
+        assert(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 = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
+                r = unit_name_mangle_with_suffix(arg_slice, UNIT_NAME_NOGLOB, ".slice", &slice);
                 if (r < 0)
                         return r;
-        }
 
-        if (arg_send_sighup && t != UNIT_TIMER) {
-                r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", arg_send_sighup);
+                r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
                 if (r < 0)
                         return r;
         }
@@ -398,12 +427,29 @@ static int transient_unit_set_properties(sd_bus_message *m, UnitType t) {
         return 0;
 }
 
-static int transient_service_set_properties(sd_bus_message *m, char **argv) {
+static int transient_kill_set_properties(sd_bus_message *m) {
+        assert(m);
+
+        if (arg_send_sighup)
+                return sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", arg_send_sighup);
+        else
+                return 0;
+}
+
+static int transient_service_set_properties(sd_bus_message *m, char **argv, const char *pty_path) {
         int r;
 
         assert(m);
 
-        r = transient_unit_set_properties(m, UNIT_SERVICE);
+        r = transient_unit_set_properties(m, arg_property);
+        if (r < 0)
+                return r;
+
+        r = transient_kill_set_properties(m);
+        if (r < 0)
+                return r;
+
+        r = transient_cgroup_set_properties(m);
         if (r < 0)
                 return r;
 
@@ -437,6 +483,31 @@ static int transient_service_set_properties(sd_bus_message *m, char **argv) {
                         return r;
         }
 
+        if (pty_path) {
+                const char *e;
+
+                r = sd_bus_message_append(m,
+                                          "(sv)(sv)(sv)(sv)",
+                                          "StandardInput", "s", "tty",
+                                          "StandardOutput", "s", "tty",
+                                          "StandardError", "s", "tty",
+                                          "TTYPath", "s", pty_path);
+                if (r < 0)
+                        return r;
+
+                e = getenv("TERM");
+                if (e) {
+                        char *n;
+
+                        n = strjoina("TERM=", e);
+                        r = sd_bus_message_append(m,
+                                                  "(sv)",
+                                                  "Environment", "as", 1, n);
+                        if (r < 0)
+                                return r;
+                }
+        }
+
         if (!strv_isempty(arg_environment)) {
                 r = sd_bus_message_open_container(m, 'r', "sv");
                 if (r < 0)
@@ -517,12 +588,32 @@ static int transient_service_set_properties(sd_bus_message *m, char **argv) {
         return 0;
 }
 
+static int transient_scope_set_properties(sd_bus_message *m) {
+        int r;
+
+        assert(m);
+
+        r = transient_unit_set_properties(m, arg_property);
+        if (r < 0)
+                return r;
+
+        r = transient_kill_set_properties(m);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid());
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
 static int transient_timer_set_properties(sd_bus_message *m) {
         int r;
 
         assert(m);
 
-        r = transient_unit_set_properties(m, UNIT_TIMER);
+        r = transient_unit_set_properties(m, arg_timer_property);
         if (r < 0)
                 return r;
 
@@ -565,125 +656,83 @@ static int transient_timer_set_properties(sd_bus_message *m) {
         return 0;
 }
 
-static int transient_scope_set_properties(sd_bus_message *m) {
-        int r;
-
-        assert(m);
-
-        r = transient_unit_set_properties(m, UNIT_SCOPE);
-        if (r < 0)
-                return r;
-
-        r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid());
-        if (r < 0)
-                return r;
-
-        return 0;
-}
-
 static int start_transient_service(
                 sd_bus *bus,
-                char **argv,
-                sd_bus_error *error) {
+                char **argv) {
 
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
-        _cleanup_free_ char *service = NULL;
+        _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_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
+        _cleanup_free_ char *service = NULL, *pty_path = NULL;
+        _cleanup_close_ int master = -1;
         int r;
 
         assert(bus);
         assert(argv);
 
-        if (arg_unit) {
-                service = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
-                if (!service)
-                        return log_oom();
-        } else if (asprintf(&service, "run-"PID_FMT".service", getpid()) < 0)
-                return log_oom();
-
-        r = sd_bus_message_new_method_call(
-                        bus,
-                        &m,
-                        "org.freedesktop.systemd1",
-                        "/org/freedesktop/systemd1",
-                        "org.freedesktop.systemd1.Manager",
-                        "StartTransientUnit");
-        if (r < 0)
-                return bus_log_create_error(r);
+        if (arg_pty) {
 
-        /* name and mode */
-        r = sd_bus_message_append(m, "ss", service, "fail");
-        if (r < 0)
-                return bus_log_create_error(r);
+                if (arg_transport == BUS_TRANSPORT_LOCAL) {
+                        master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY);
+                        if (master < 0)
+                                return log_error_errno(errno, "Failed to acquire pseudo tty: %m");
 
-        /* properties */
-        r = sd_bus_message_open_container(m, 'a', "(sv)");
-        if (r < 0)
-                return bus_log_create_error(r);
-
-        r = transient_service_set_properties(m, argv);
-        if (r < 0)
-                return bus_log_create_error(r);
-
-        r = sd_bus_message_close_container(m);
-        if (r < 0)
-                return bus_log_create_error(r);
-
-        /* aux */
-        r = sd_bus_message_append(m, "a(sa(sv))", 0);
-        if (r < 0)
-                return bus_log_create_error(r);
-
-        /* send dbus */
-        r = sd_bus_call(bus, m, 0, error, NULL);
-        if (r < 0)
-                return bus_log_create_error(r);
-
-        log_info("Running as unit %s.", service);
+                        r = ptsname_malloc(master, &pty_path);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to determine tty name: %m");
 
-        return 0;
-}
+                } else if (arg_transport == BUS_TRANSPORT_MACHINE) {
+                        _cleanup_bus_unref_ sd_bus *system_bus = NULL;
+                        const char *s;
 
-static int start_transient_timer(
-                sd_bus *bus,
-                char **argv,
-                sd_bus_error *error) {
+                        r = sd_bus_open_system(&system_bus);
+                        if (r < 0)
+                                log_error_errno(r, "Failed to connect to system bus: %m");
+
+                        r = sd_bus_call_method(system_bus,
+                                               "org.freedesktop.machine1",
+                                               "/org/freedesktop/machine1",
+                                               "org.freedesktop.machine1.Manager",
+                                               "OpenMachinePTY",
+                                               &error,
+                                               &reply,
+                                               "s", arg_host);
+                        if (r < 0) {
+                                log_error("Failed to get machine PTY: %s", bus_error_message(&error, -r));
+                                return r;
+                        }
 
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
-        _cleanup_free_ char *timer = NULL, *service = NULL;
-        int r;
+                        r = sd_bus_message_read(reply, "hs", &master, &s);
+                        if (r < 0)
+                                return bus_log_parse_error(r);
 
-        assert(bus);
-        assert(argv);
+                        reply = sd_bus_message_unref(reply);
 
-        if (arg_unit) {
-                switch(unit_name_to_type(arg_unit)) {
-                case UNIT_SERVICE:
-                        service = strdup(arg_unit);
-                        timer = unit_name_change_suffix(service, ".timer");
-                        if (!timer)
-                                return log_oom();
-                        break;
+                        master = fcntl(master, F_DUPFD_CLOEXEC, 3);
+                        if (master < 0)
+                                return log_error_errno(errno, "Failed to duplicate master fd: %m");
 
-                case UNIT_TIMER:
-                        timer = strdup(arg_unit);
-                        service = unit_name_change_suffix(timer, ".service");
-                        if (!service)
+                        pty_path = strdup(s);
+                        if (!pty_path)
                                 return log_oom();
-                        break;
+                } else
+                        assert_not_reached("Can't allocate tty via ssh");
 
-                default:
-                        service = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
-                        if (!service)
-                                return log_oom();
+                if (unlockpt(master) < 0)
+                        return log_error_errno(errno, "Failed to unlock tty: %m");
+        }
 
-                        timer = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".timer");
-                        if (!timer)
-                                return log_oom();
+        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");
+        }
 
-                        break;
-                }
-        } else if ((asprintf(&service, "run-"PID_FMT".service", getpid()) < 0) ||
-                   (asprintf(&timer, "run-"PID_FMT".timer", getpid()) < 0))
+        if (arg_unit) {
+                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();
 
         r = sd_bus_message_new_method_call(
@@ -696,17 +745,17 @@ static int start_transient_timer(
         if (r < 0)
                 return bus_log_create_error(r);
 
-        /* name and mode */
-        r = sd_bus_message_append(m, "ss", timer, "fail");
+        /* Name and mode */
+        r = sd_bus_message_append(m, "ss", service, "fail");
         if (r < 0)
                 return bus_log_create_error(r);
 
-        /* properties */
+        /* Properties */
         r = sd_bus_message_open_container(m, 'a', "(sv)");
         if (r < 0)
                 return bus_log_create_error(r);
 
-        r = transient_timer_set_properties(m);
+        r = transient_service_set_properties(m, argv, pty_path);
         if (r < 0)
                 return bus_log_create_error(r);
 
@@ -714,92 +763,109 @@ static int start_transient_timer(
         if (r < 0)
                 return bus_log_create_error(r);
 
-        if (argv[0]) {
-                r = sd_bus_message_open_container(m, 'a', "(sa(sv))");
-                if (r < 0)
-                        return bus_log_create_error(r);
+        /* Auxiliary units */
+        r = sd_bus_message_append(m, "a(sa(sv))", 0);
+        if (r < 0)
+                return bus_log_create_error(r);
 
-                r = sd_bus_message_open_container(m, 'r', "sa(sv)");
-                if (r < 0)
-                        return bus_log_create_error(r);
+        r = sd_bus_call(bus, m, 0, &error, &reply);
+        if (r < 0) {
+                log_error("Failed to start transient service unit: %s", bus_error_message(&error, -r));
+                return r;
+        }
 
-                r = sd_bus_message_append(m, "s", service);
-                if (r < 0)
-                        return bus_log_create_error(r);
+        if (w) {
+                const char *object;
 
-                r = sd_bus_message_open_container(m, 'a', "(sv)");
+                r = sd_bus_message_read(reply, "o", &object);
                 if (r < 0)
-                        return bus_log_create_error(r);
+                        return bus_log_parse_error(r);
 
-                r = transient_service_set_properties(m, argv);
+                r = bus_wait_for_jobs_one(w, object, arg_quiet);
                 if (r < 0)
-                        return bus_log_create_error(r);
+                        return r;
+        }
 
-                r = sd_bus_message_close_container(m);
-                if (r < 0)
-                        return bus_log_create_error(r);
+        if (master >= 0) {
+                _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
+                _cleanup_event_unref_ sd_event *event = NULL;
+                char last_char = 0;
 
-                r = sd_bus_message_close_container(m);
+                r = sd_event_default(&event);
                 if (r < 0)
-                        return bus_log_create_error(r);
+                        return log_error_errno(r, "Failed to get event loop: %m");
 
-                r = sd_bus_message_close_container(m);
+                assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
+
+                (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);
                 if (r < 0)
-                        return bus_log_create_error(r);
-        } else {
-                r = sd_bus_message_append(m, "a(sa(sv))", 0);
+                        return log_error_errno(r, "Failed to create PTY forwarder: %m");
+
+                r = sd_event_loop(event);
                 if (r < 0)
-                        return bus_log_create_error(r);
-        }
+                        return log_error_errno(r, "Failed to run event loop: %m");
 
-        /* send dbus */
-        r = sd_bus_call(bus, m, 0, error, NULL);
-        if (r < 0)
-                return bus_log_create_error(r);
+                pty_forward_get_last_char(forward, &last_char);
 
-        log_info("Running as unit %s.", timer);
-        if (argv[0])
-                log_info("Will run as unit %s.", service);
+                forward = pty_forward_free(forward);
+
+                if (!arg_quiet && last_char != '\n')
+                        fputc('\n', stdout);
+
+        } else if (!arg_quiet)
+                log_info("Running as unit %s.", service);
 
         return 0;
 }
 
 static int start_transient_scope(
                 sd_bus *bus,
-                char **argv,
-                sd_bus_error *error) {
+                char **argv) {
 
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
-        _cleanup_free_ char *scope = NULL;
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        _cleanup_bus_message_unref_ 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;
         int r;
 
         assert(bus);
         assert(argv);
 
+        r = bus_wait_for_jobs_new(bus, &w);
+        if (r < 0)
+                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();
 
         r = sd_bus_message_new_method_call(
-                bus,
-                &m,
-                "org.freedesktop.systemd1",
-                "/org/freedesktop/systemd1",
-                "org.freedesktop.systemd1.Manager",
-                "StartTransientUnit");
+                        bus,
+                        &m,
+                        "org.freedesktop.systemd1",
+                        "/org/freedesktop/systemd1",
+                        "org.freedesktop.systemd1.Manager",
+                        "StartTransientUnit");
         if (r < 0)
                 return bus_log_create_error(r);
 
-        /* name and mode */
+        /* Name and Mode */
         r = sd_bus_message_append(m, "ss", scope, "fail");
         if (r < 0)
                 return bus_log_create_error(r);
 
-        /* properties */
+        /* Properties */
         r = sd_bus_message_open_container(m, 'a', "(sv)");
         if (r < 0)
                 return bus_log_create_error(r);
@@ -812,15 +878,16 @@ static int start_transient_scope(
         if (r < 0)
                 return bus_log_create_error(r);
 
-        /* aux */
+        /* Auxiliary units */
         r = sd_bus_message_append(m, "a(sa(sv))", 0);
         if (r < 0)
                 return bus_log_create_error(r);
 
-        /* send dbus */
-        r = sd_bus_call(bus, m, 0, error, NULL);
-        if (r < 0)
-                return bus_log_create_error(r);
+        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));
+                return r;
+        }
 
         if (arg_nice_set) {
                 if (setpriority(PRIO_PROCESS, 0, arg_nice) < 0)
@@ -876,15 +943,162 @@ static int start_transient_scope(
         if (!env)
                 return log_oom();
 
-        log_info("Running as unit %s.", scope);
+        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);
-        log_error_errno(errno, "Failed to execute: %m");
-        return -errno;
+
+        return log_error_errno(errno, "Failed to execute: %m");
 }
 
-int main(int argc, char* argv[]) {
+static int start_transient_timer(
+                sd_bus *bus,
+                char **argv) {
+
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
+        _cleanup_free_ char *timer = NULL, *service = NULL;
+        const char *object = NULL;
+        int r;
+
+        assert(bus);
+        assert(argv);
+
+        r = bus_wait_for_jobs_new(bus, &w);
+        if (r < 0)
+                return log_oom();
+
+        if (arg_unit) {
+                switch (unit_name_to_type(arg_unit)) {
+
+                case UNIT_SERVICE:
+                        service = strdup(arg_unit);
+                        if (!service)
+                                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:
+                        timer = strdup(arg_unit);
+                        if (!timer)
+                                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:
+                        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");
+
+                        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;
+                }
+        } else if ((asprintf(&service, "run-"PID_FMT".service", getpid()) < 0) ||
+                   (asprintf(&timer, "run-"PID_FMT".timer", getpid()) < 0))
+                return log_oom();
+
+        r = sd_bus_message_new_method_call(
+                        bus,
+                        &m,
+                        "org.freedesktop.systemd1",
+                        "/org/freedesktop/systemd1",
+                        "org.freedesktop.systemd1.Manager",
+                        "StartTransientUnit");
+        if (r < 0)
+                return bus_log_create_error(r);
+
+        /* Name and Mode */
+        r = sd_bus_message_append(m, "ss", timer, "fail");
+        if (r < 0)
+                return bus_log_create_error(r);
+
+        /* Properties */
+        r = sd_bus_message_open_container(m, 'a', "(sv)");
+        if (r < 0)
+                return bus_log_create_error(r);
+
+        r = transient_timer_set_properties(m);
+        if (r < 0)
+                return bus_log_create_error(r);
+
+        r = sd_bus_message_close_container(m);
+        if (r < 0)
+                return bus_log_create_error(r);
+
+        r = sd_bus_message_open_container(m, 'a', "(sa(sv))");
+        if (r < 0)
+                return bus_log_create_error(r);
+
+        if (argv[0]) {
+                r = sd_bus_message_open_container(m, 'r', "sa(sv)");
+                if (r < 0)
+                        return bus_log_create_error(r);
+
+                r = sd_bus_message_append(m, "s", service);
+                if (r < 0)
+                        return bus_log_create_error(r);
+
+                r = sd_bus_message_open_container(m, 'a', "(sv)");
+                if (r < 0)
+                        return bus_log_create_error(r);
+
+                r = transient_service_set_properties(m, argv, NULL);
+                if (r < 0)
+                        return bus_log_create_error(r);
+
+                r = sd_bus_message_close_container(m);
+                if (r < 0)
+                        return bus_log_create_error(r);
+
+                r = sd_bus_message_close_container(m);
+                if (r < 0)
+                        return bus_log_create_error(r);
+        }
+
+        r = sd_bus_message_close_container(m);
+        if (r < 0)
+                return bus_log_create_error(r);
+
+        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));
+                return 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;
+
+        log_info("Running timer as unit %s.", timer);
+        if (argv[0])
+                log_info("Will run service as unit %s.", service);
+
+        return 0;
+}
+
+int main(int argc, char* argv[]) {
         _cleanup_bus_close_unref_ sd_bus *bus = NULL;
         _cleanup_free_ char *description = NULL, *command = NULL;
         int r;
@@ -934,11 +1148,11 @@ int main(int argc, char* argv[]) {
         }
 
         if (arg_scope)
-                r = start_transient_scope(bus, argv + optind, &error);
+                r = start_transient_scope(bus, argv + optind);
         else if (with_timer())
-                r = start_transient_timer(bus, argv + optind, &error);
+                r = start_transient_timer(bus, argv + optind);
         else
-                r = start_transient_service(bus, argv + optind, &error);
+                r = start_transient_service(bus, argv + optind);
 
 finish:
         strv_free(arg_environment);