]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/machine/machinectl.c
tree-wide: drop {} from one-line if blocks
[thirdparty/systemd.git] / src / machine / machinectl.c
index 72b9a619d2d7860782f2269c3f7b2936de5e92d3..7b8f6d1fabe3deaad7a9fe08c96124967744180d 100644 (file)
@@ -158,6 +158,9 @@ static int list_machines(int argc, char *argv[], void *userdata) {
         while ((r = sd_bus_message_read(reply, "(ssso)", &name, &class, &service, &object)) > 0) {
                 size_t l;
 
+                if (name[0] == '.' && !arg_all)
+                        continue;
+
                 if (!GREEDY_REALLOC(machines, n_allocated, n_machines + 1))
                         return log_oom();
 
@@ -358,7 +361,7 @@ static int show_unit_cgroup(sd_bus *bus, const char *unit, pid_t leader) {
                         bus,
                         "org.freedesktop.systemd1",
                         path,
-                        endswith(unit, ".scope") ? "org.freedesktop.systemd1.Scope" : "org.freedesktop.systemd1.Service",
+                        unit_dbus_interface_from_name(unit),
                         "ControlGroup",
                         &error,
                         &reply,
@@ -372,10 +375,7 @@ static int show_unit_cgroup(sd_bus *bus, const char *unit, pid_t leader) {
         if (r < 0)
                 return bus_log_parse_error(r);
 
-        if (isempty(cgroup))
-                return 0;
-
-        if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup, false) != 0 && leader <= 0)
+        if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
                 return 0;
 
         c = columns();
@@ -597,7 +597,7 @@ static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
                 printf("\t    Unit: %s\n", i->unit);
                 show_unit_cgroup(bus, i->unit, i->leader);
 
-                if (arg_transport == BUS_TRANSPORT_LOCAL) {
+                if (arg_transport == BUS_TRANSPORT_LOCAL)
 
                         show_journal_by_unit(
                                         stdout,
@@ -611,7 +611,6 @@ static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
                                         SD_JOURNAL_LOCAL_ONLY,
                                         true,
                                         NULL);
-                }
         }
 }
 
@@ -1185,7 +1184,10 @@ static int process_forward(sd_event *event, PTYForward **forward, int master, bo
 
         assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
 
-        log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name);
+        if (streq(name, ".host"))
+                log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
+        else
+                log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name);
 
         sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
         sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
@@ -1211,6 +1213,8 @@ static int process_forward(sd_event *event, PTYForward **forward, int master, bo
 
         if (machine_died)
                 log_info("Machine %s terminated.", name);
+        else if (streq(name, ".host"))
+                log_info("Connection to the local host terminated.");
         else
                 log_info("Connection to machine %s terminated.", name);
 
@@ -1219,14 +1223,14 @@ static int process_forward(sd_event *event, PTYForward **forward, int master, bo
 }
 
 static int login_machine(int argc, char *argv[], void *userdata) {
-        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *m = NULL;
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
         _cleanup_bus_slot_unref_ sd_bus_slot *slot = NULL;
         _cleanup_event_unref_ sd_event *event = NULL;
         int master = -1, r;
         sd_bus *bus = userdata;
-        const char *pty, *match;
+        const char *pty, *match, *machine;
 
         assert(bus);
 
@@ -1251,14 +1255,14 @@ static int login_machine(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return log_error_errno(r, "Failed to attach bus to event loop: %m");
 
+        machine = argc < 2 || isempty(argv[1]) ? ".host" : argv[1];
+
         match = strjoina("type='signal',"
                          "sender='org.freedesktop.machine1',"
                          "path='/org/freedesktop/machine1',",
                          "interface='org.freedesktop.machine1.Manager',"
                          "member='MachineRemoved',"
-                         "arg0='",
-                         argv[1],
-                         "'");
+                         "arg0='", machine, "'");
 
         r = sd_bus_add_match(bus, &slot, match, on_machine_removed, &forward);
         if (r < 0)
@@ -1272,7 +1276,7 @@ static int login_machine(int argc, char *argv[], void *userdata) {
                         "OpenMachineLogin",
                         &error,
                         &reply,
-                        "s", argv[1]);
+                        "s", machine);
         if (r < 0) {
                 log_error("Failed to get login PTY: %s", bus_error_message(&error, -r));
                 return r;
@@ -1282,7 +1286,7 @@ static int login_machine(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return bus_log_parse_error(r);
 
-        return process_forward(event, &forward, master, true, argv[1]);
+        return process_forward(event, &forward, master, true, machine);
 }
 
 static int shell_machine(int argc, char *argv[], void *userdata) {
@@ -1293,7 +1297,7 @@ static int shell_machine(int argc, char *argv[], void *userdata) {
         _cleanup_event_unref_ sd_event *event = NULL;
         int master = -1, r;
         sd_bus *bus = userdata;
-        const char *pty, *match;
+        const char *pty, *match, *machine, *path, *uid = NULL;
 
         assert(bus);
 
@@ -1303,6 +1307,17 @@ static int shell_machine(int argc, char *argv[], void *userdata) {
                 return -EOPNOTSUPP;
         }
 
+        /* Pass $TERM to shell session, if not explicitly specified. */
+        if (!strv_find_prefix(arg_setenv, "TERM=")) {
+                const char *t;
+
+                t = strv_find_prefix(environ, "TERM=");
+                if (t) {
+                        if (strv_extend(&arg_setenv, t) < 0)
+                                return log_oom();
+                }
+        }
+
         polkit_agent_open_if_enabled();
 
         r = sd_event_default(&event);
@@ -1313,14 +1328,29 @@ static int shell_machine(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return log_error_errno(r, "Failed to attach bus to event loop: %m");
 
+        machine = argc < 2 || isempty(argv[1]) ? NULL : argv[1];
+
+        if (arg_uid)
+                uid = arg_uid;
+        else if (machine) {
+                const char *at;
+
+                at = strchr(machine, '@');
+                if (at) {
+                        uid = strndupa(machine, at - machine);
+                        machine = at + 1;
+                }
+        }
+
+        if (isempty(machine))
+                machine = ".host";
+
         match = strjoina("type='signal',"
                          "sender='org.freedesktop.machine1',"
                          "path='/org/freedesktop/machine1',",
                          "interface='org.freedesktop.machine1.Manager',"
                          "member='MachineRemoved',"
-                         "arg0='",
-                         argv[1],
-                         "'");
+                         "arg0='", machine, "'");
 
         r = sd_bus_add_match(bus, &slot, match, on_machine_removed, &forward);
         if (r < 0)
@@ -1336,11 +1366,13 @@ static int shell_machine(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return bus_log_create_error(r);
 
-        r = sd_bus_message_append(m, "sss", argv[1], arg_uid, argv[2]);
+        path = argc < 3 || isempty(argv[2]) ? NULL : argv[2];
+
+        r = sd_bus_message_append(m, "sss", machine, uid, path);
         if (r < 0)
                 return bus_log_create_error(r);
 
-        r = sd_bus_message_append_strv(m, strv_length(argv + 2) <= 1 ? NULL : argv + 2);
+        r = sd_bus_message_append_strv(m, strv_length(argv) <= 3 ? NULL : argv + 2);
         if (r < 0)
                 return bus_log_create_error(r);
 
@@ -1358,7 +1390,7 @@ static int shell_machine(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return bus_log_parse_error(r);
 
-        return process_forward(event, &forward, master, false, argv[1]);
+        return process_forward(event, &forward, master, false, machine);
 }
 
 static int remove_image(int argc, char *argv[], void *userdata) {
@@ -2428,10 +2460,13 @@ static int help(int argc, char *argv[], void *userdata) {
                "Machine Commands:\n"
                "  list                        List running VMs and containers\n"
                "  status NAME...              Show VM/container details\n"
-               "  show NAME...                Show properties of one or more VMs/containers\n"
+               "  show [NAME...]              Show properties of one or more VMs/containers\n"
                "  start NAME...               Start container as a service\n"
-               "  login NAME                  Get a login prompt on a container\n"
-               "  shell NAME [COMMAND...]     Invoke a shell (or other command) in a container\n"
+               "  login [NAME]                Get a login prompt in a container or on the\n"
+               "                              local host\n"
+               "  shell [[USER@]NAME [COMMAND...]]\n"
+               "                              Invoke a shell (or other command) in a container\n"
+               "                              or on the local host\n"
                "  enable NAME...              Enable automatic container start at boot\n"
                "  disable NAME...             Disable automatic container start at boot\n"
                "  poweroff NAME...            Power off one or more containers\n"
@@ -2443,8 +2478,8 @@ static int help(int argc, char *argv[], void *userdata) {
                "  bind NAME PATH [PATH]       Bind mount a path from the host into a container\n\n"
                "Image Commands:\n"
                "  list-images                 Show available container and VM images\n"
-               "  image-status NAME...        Show image details\n"
-               "  show-image NAME...          Show properties of image\n"
+               "  image-status [NAME...]      Show image details\n"
+               "  show-image [NAME...]        Show properties of image\n"
                "  clone NAME NAME             Clone an image\n"
                "  rename NAME NAME            Rename an image\n"
                "  read-only NAME [BOOL]       Mark or unmark image read-only\n"
@@ -2676,8 +2711,8 @@ static int machinectl_main(int argc, char *argv[], sd_bus *bus) {
                 { "reboot",          2,        VERB_ANY, 0,            reboot_machine    },
                 { "poweroff",        2,        VERB_ANY, 0,            poweroff_machine  },
                 { "kill",            2,        VERB_ANY, 0,            kill_machine      },
-                { "login",           2,        2,        0,            login_machine     },
-                { "shell",           2,        VERB_ANY, 0,            shell_machine     },
+                { "login",           VERB_ANY, 2,        0,            login_machine     },
+                { "shell",           VERB_ANY, VERB_ANY, 0,            shell_machine     },
                 { "bind",            3,        4,        0,            bind_mount        },
                 { "copy-to",         3,        4,        0,            copy_files        },
                 { "copy-from",       3,        4,        0,            copy_files        },