chiark / gitweb /
nspawn: when connected to pipes for stdin/stdout, pass them as-is to PID 1
[elogind.git] / src / run / run.c
index 242bed0a43ae317b66167fc9df5e88a6a1b34f90..3ded2c7e66ebd9a4cc427dcf9e53fa4e71303289 100644 (file)
@@ -86,11 +86,11 @@ static void help(void) {
                "  -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);
@@ -214,7 +214,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;
 
@@ -241,14 +241,12 @@ 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();
 
@@ -389,11 +387,8 @@ static int transient_unit_set_properties(sd_bus_message *m, char **properties) {
                         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)
@@ -423,16 +418,12 @@ static int transient_cgroup_set_properties(sd_bus_message *m) {
 }
 
 static int transient_kill_set_properties(sd_bus_message *m) {
-        int r;
         assert(m);
 
-        if (arg_send_sighup) {
-                r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", arg_send_sighup);
-                if (r < 0)
-                        return r;
-        }
-
-        return r;
+        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) {
@@ -498,7 +489,7 @@ static int transient_service_set_properties(sd_bus_message *m, char **argv, cons
                 if (e) {
                         char *n;
 
-                        n = strappenda("TERM=", e);
+                        n = strjoina("TERM=", e);
                         r = sd_bus_message_append(m,
                                                   "(sv)",
                                                   "Environment", "as", 1, n);
@@ -657,26 +648,63 @@ static int transient_timer_set_properties(sd_bus_message *m) {
 
 static int start_transient_service(
                 sd_bus *bus,
-                char **argv,
-                sd_bus_error *error) {
+                char **argv) {
 
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
-        _cleanup_free_ char *service = NULL;
+        _cleanup_free_ char *service = NULL, *pty_path = NULL;
         _cleanup_close_ int master = -1;
-        const char *pty_path = NULL;
         int r;
 
         assert(bus);
         assert(argv);
 
         if (arg_pty) {
-                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");
 
-                pty_path = ptsname(master);
-                if (!pty_path)
-                        return log_error_errno(errno, "Failed to determine tty name: %m");
+                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");
+
+                        r = ptsname_malloc(master, &pty_path);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to determine tty name: %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);
+                        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;
+                        }
+
+                        r = sd_bus_message_read(reply, "hs", &master, &s);
+                        if (r < 0)
+                                return bus_log_parse_error(r);
+
+                        master = fcntl(master, F_DUPFD_CLOEXEC, 3);
+                        if (master < 0)
+                                return log_error_errno(errno, "Failed to duplicate master fd: %m");
+
+                        pty_path = strdup(s);
+                        if (!pty_path)
+                                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");
@@ -722,9 +750,11 @@ 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)
-                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;
+        }
 
         if (master >= 0) {
                 _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
@@ -746,7 +776,7 @@ static int start_transient_service(
                 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, &forward);
+                r = pty_forward_new(event, master, false, false, &forward);
                 if (r < 0)
                         return log_error_errno(r, "Failed to create PTY forwarder: %m");
 
@@ -754,7 +784,7 @@ static int start_transient_service(
                 if (r < 0)
                         return log_error_errno(r, "Failed to run event loop: %m");
 
-                pty_forward_last_char(forward, &last_char);
+                pty_forward_get_last_char(forward, &last_char);
 
                 forward = pty_forward_free(forward);
 
@@ -769,9 +799,9 @@ static int start_transient_service(
 
 static int start_transient_scope(
                 sd_bus *bus,
-                char **argv,
-                sd_bus_error *error) {
+                char **argv) {
 
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_strv_free_ char **env = NULL, **user_env = NULL;
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
         _cleanup_free_ char *scope = NULL;
@@ -815,15 +845,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, NULL);
+        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)
@@ -889,9 +920,9 @@ static int start_transient_scope(
 
 static int start_transient_timer(
                 sd_bus *bus,
-                char **argv,
-                sd_bus_error *error) {
+                char **argv) {
 
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
         _cleanup_free_ char *timer = NULL, *service = NULL;
         int r;
@@ -999,10 +1030,11 @@ static int start_transient_timer(
         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, NULL);
+        if (r < 0) {
+                log_error("Failed to start transient timer unit: %s", bus_error_message(&error, -r));
+                return r;
+        }
 
         log_info("Running as unit %s.", timer);
         if (argv[0])
@@ -1012,7 +1044,6 @@ static int start_transient_timer(
 }
 
 int main(int argc, char* argv[]) {
-        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_bus_close_unref_ sd_bus *bus = NULL;
         _cleanup_free_ char *description = NULL, *command = NULL;
         int r;
@@ -1062,11 +1093,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);