1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/socket.h>
37 #include "bus-error.h"
40 #include "unit-name.h"
41 #include "cgroup-show.h"
42 #include "cgroup-util.h"
45 static char **arg_property = NULL;
46 static bool arg_all = false;
47 static bool arg_full = false;
48 static bool arg_no_pager = false;
49 static const char *arg_kill_who = NULL;
50 static int arg_signal = SIGTERM;
51 static bool arg_ask_password = true;
52 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
53 static char *arg_host = NULL;
55 static void pager_open_if_enabled(void) {
57 /* Cache result before we open the pager */
64 static int list_machines(sd_bus *bus, char **args, unsigned n) {
65 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
66 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
67 const char *name, *class, *service, *object;
71 pager_open_if_enabled();
73 r = sd_bus_call_method(
75 "org.freedesktop.machine1",
76 "/org/freedesktop/machine1",
77 "org.freedesktop.machine1.Manager",
83 log_error("Could not get machines: %s", bus_error_message(&error, -r));
87 printf("%-32s %-9s %-16s\n", "MACHINE", "CONTAINER", "SERVICE");
89 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssso)");
91 return bus_log_parse_error(r);
93 while ((r = sd_bus_message_read(reply, "(ssso)", &name, &class, &service, &object)) > 0) {
94 printf("%-32s %-9s %-16s\n", name, class, service);
99 return bus_log_parse_error(r);
101 r = sd_bus_message_exit_container(reply);
103 return bus_log_parse_error(r);
105 printf("\n%u machines listed.\n", k);
110 static int show_scope_cgroup(sd_bus *bus, const char *unit, pid_t leader) {
111 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
112 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
113 _cleanup_free_ char *path = NULL;
121 if (arg_transport == BUS_TRANSPORT_REMOTE)
124 path = unit_dbus_path_from_name(unit);
128 r = sd_bus_get_property(
130 "org.freedesktop.systemd1",
132 "org.freedesktop.systemd1.Scope",
138 log_error("Failed to query ControlGroup: %s", bus_error_message(&error, -r));
142 r = sd_bus_message_read(reply, "s", &cgroup);
144 return bus_log_parse_error(r);
149 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup, false) != 0 && leader <= 0)
153 arg_all * OUTPUT_SHOW_ALL |
154 arg_full * OUTPUT_FULL_WIDTH;
162 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, false, &leader, leader > 0, output_flags);
166 typedef struct MachineStatusInfo {
172 char *root_directory;
177 static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
178 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
179 char since2[FORMAT_TIMESTAMP_MAX], *s2;
182 fputs(strna(i->name), stdout);
184 if (!sd_id128_equal(i->id, SD_ID128_NULL))
185 printf("(" SD_ID128_FORMAT_STR ")\n", SD_ID128_FORMAT_VAL(i->id));
189 s1 = format_timestamp_relative(since1, sizeof(since1), i->timestamp);
190 s2 = format_timestamp(since2, sizeof(since2), i->timestamp);
193 printf("\t Since: %s; %s\n", s2, s1);
195 printf("\t Since: %s\n", s2);
198 _cleanup_free_ char *t = NULL;
200 printf("\t Leader: %u", (unsigned) i->leader);
202 get_process_comm(i->leader, &t);
210 printf("\t Service: %s", i->service);
213 printf("; class %s", i->class);
217 printf("\t Class: %s\n", i->class);
219 if (i->root_directory)
220 printf("\t Root: %s\n", i->root_directory);
223 printf("\t Unit: %s\n", i->scope);
224 show_scope_cgroup(bus, i->scope, i->leader);
228 static int show_info(const char *verb, sd_bus *bus, const char *path, bool *new_line) {
230 static const struct bus_properties_map map[] = {
231 { "Name", "s", NULL, offsetof(MachineStatusInfo, name) },
232 { "Class", "s", NULL, offsetof(MachineStatusInfo, class) },
233 { "Service", "s", NULL, offsetof(MachineStatusInfo, service) },
234 { "Scope", "s", NULL, offsetof(MachineStatusInfo, scope) },
235 { "RootDirectory", "s", NULL, offsetof(MachineStatusInfo, root_directory) },
236 { "Leader", "u", NULL, offsetof(MachineStatusInfo, leader) },
237 { "Timestamp", "t", NULL, offsetof(MachineStatusInfo, timestamp) },
238 { "Id", "ay", bus_map_id128, offsetof(MachineStatusInfo, id) },
242 MachineStatusInfo info = {};
248 r = bus_map_all_properties(bus,
249 "org.freedesktop.machine1",
254 log_error("Could not get properties: %s", strerror(-r));
262 print_machine_status_info(bus, &info);
268 free(info.root_directory);
273 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
281 r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, arg_property, arg_all);
283 log_error("Could not get properties: %s", strerror(-r));
288 static int show(sd_bus *bus, char **args, unsigned n) {
289 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
290 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
293 bool properties, new_line = false;
298 properties = !strstr(args[0], "status");
300 pager_open_if_enabled();
302 if (properties && n <= 1) {
304 /* If no argument is specified, inspect the manager
306 r = show_properties(bus, "/org/freedesktop/machine1", &new_line);
308 log_error("Failed to query properties: %s", strerror(-r));
313 for (i = 1; i < n; i++) {
314 const char *path = NULL;
316 r = sd_bus_call_method(
318 "org.freedesktop.machine1",
319 "/org/freedesktop/machine1",
320 "org.freedesktop.machine1.Manager",
326 log_error("Could not get path to machine: %s", bus_error_message(&error, -r));
330 r = sd_bus_message_read(reply, "o", &path);
332 return bus_log_parse_error(r);
335 r = show_properties(bus, path, &new_line);
337 r = show_info(args[0], bus, path, &new_line);
343 static int kill_machine(sd_bus *bus, char **args, unsigned n) {
344 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
350 arg_kill_who = "all";
352 for (i = 1; i < n; i++) {
355 r = sd_bus_call_method(
357 "org.freedesktop.machine1",
358 "/org/freedesktop/machine1",
359 "org.freedesktop.machine1.Manager",
363 "ssi", args[i], arg_kill_who, arg_signal);
365 log_error("Could not kill machine: %s", bus_error_message(&error, -r));
373 static int terminate_machine(sd_bus *bus, char **args, unsigned n) {
374 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
379 for (i = 1; i < n; i++) {
382 r = sd_bus_call_method(
384 "org.freedesktop.machine1",
385 "/org/freedesktop/machine1",
386 "org.freedesktop.machine1.Manager",
392 log_error("Could not terminate machine: %s", bus_error_message(&error, -r));
400 static int openpt_in_namespace(pid_t pid, int flags) {
401 _cleanup_close_ int nsfd = -1, rootfd = -1;
402 _cleanup_free_ char *ns = NULL, *root = NULL;
403 _cleanup_close_pipe_ int sock[2] = { -1, -1 };
405 struct cmsghdr cmsghdr;
406 uint8_t buf[CMSG_SPACE(sizeof(int))];
409 .msg_control = &control,
410 .msg_controllen = sizeof(control),
412 struct cmsghdr *cmsg;
417 r = asprintf(&ns, "/proc/%lu/ns/mnt", (unsigned long) pid);
421 nsfd = open(ns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
425 r = asprintf(&root, "/proc/%lu/root", (unsigned long) pid);
429 rootfd = open(root, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
433 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sock) < 0)
441 close_nointr_nofail(sock[0]);
444 r = setns(nsfd, CLONE_NEWNS);
448 if (fchdir(rootfd) < 0)
454 master = posix_openpt(flags);
458 cmsg = CMSG_FIRSTHDR(&mh);
459 cmsg->cmsg_level = SOL_SOCKET;
460 cmsg->cmsg_type = SCM_RIGHTS;
461 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
462 memcpy(CMSG_DATA(cmsg), &master, sizeof(int));
464 mh.msg_controllen = cmsg->cmsg_len;
466 r = sendmsg(sock[1], &mh, MSG_NOSIGNAL);
467 close_nointr_nofail(master);
474 close_nointr_nofail(sock[1]);
477 if (recvmsg(sock[0], &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) < 0)
480 for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg))
481 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
485 fds = (int*) CMSG_DATA(cmsg);
486 n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
489 close_many(fds, n_fds);
496 r = wait_for_terminate(child, &si);
497 if (r < 0 || si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS || master < 0) {
500 close_nointr_nofail(master);
502 return r < 0 ? r : -EIO;
508 static int login_machine(sd_bus *bus, char **args, unsigned n) {
509 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *reply2 = NULL, *reply3 = NULL;
510 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
511 _cleanup_bus_unref_ sd_bus *container_bus = NULL;
512 _cleanup_close_ int master = -1;
513 _cleanup_free_ char *getty = NULL;
514 const char *path, *pty, *p;
522 if (arg_transport != BUS_TRANSPORT_LOCAL) {
523 log_error("Login only support on local machines.");
527 r = sd_bus_call_method(
529 "org.freedesktop.machine1",
530 "/org/freedesktop/machine1",
531 "org.freedesktop.machine1.Manager",
537 log_error("Could not get path to machine: %s", bus_error_message(&error, -r));
541 r = sd_bus_message_read(reply, "o", &path);
543 return bus_log_parse_error(r);
545 r = sd_bus_get_property(
547 "org.freedesktop.machine1",
549 "org.freedesktop.machine1.Machine",
555 log_error("Failed to retrieve PID of leader: %s", strerror(-r));
559 r = sd_bus_message_read(reply2, "u", &leader);
561 return bus_log_parse_error(r);
563 master = openpt_in_namespace(leader, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY);
565 log_error("Failed to acquire pseudo tty: %s", strerror(-master));
569 pty = ptsname(master);
571 log_error("Failed to get pty name: %m");
575 p = startswith(pty, "/dev/pts/");
577 log_error("Invalid pty name %s.", pty);
581 r = sd_bus_open_system_container(args[1], &container_bus);
583 log_error("Failed to get container bus: %s", strerror(-r));
587 getty = strjoin("container-getty@", p, ".service", NULL);
591 if (unlockpt(master) < 0) {
592 log_error("Failed to unlock tty: %m");
596 r = sd_bus_call_method(container_bus,
597 "org.freedesktop.systemd1",
598 "/org/freedesktop/systemd1",
599 "org.freedesktop.systemd1.Manager",
602 "ss", getty, "replace");
604 log_error("Failed to start getty service: %s", bus_error_message(&error, r));
608 container_bus = sd_bus_unref(container_bus);
610 assert_se(sigemptyset(&mask) == 0);
611 sigset_add_many(&mask, SIGWINCH, SIGTERM, SIGINT, -1);
612 assert_se(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
614 log_info("Connected to container %s. Press ^] three times within 1s to exit session.", args[1]);
616 r = process_pty(master, &mask, 0, 0);
618 log_error("Failed to process pseudo tty: %s", strerror(-r));
624 log_info("Connection to container %s terminated.", args[1]);
629 static int help(void) {
631 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
632 "Send control commands to or query the virtual machine and container registration manager.\n\n"
633 " -h --help Show this help\n"
634 " --version Show package version\n"
635 " --no-pager Do not pipe output into a pager\n"
636 " --no-ask-password Don't prompt for password\n"
637 " -H --host=[USER@]HOST Operate on remote host\n"
638 " -M --machine=CONTAINER Operate on local container\n"
639 " -p --property=NAME Show only properties by this name\n"
640 " -a --all Show all properties, including empty ones\n"
641 " -l --full Do not ellipsize output\n"
642 " --kill-who=WHO Who to send signal to\n"
643 " -s --signal=SIGNAL Which signal to send\n\n"
645 " list List running VMs and containers\n"
646 " status [NAME...] Show VM/container status\n"
647 " show [NAME...] Show properties of one or more VMs/containers\n"
648 " terminate [NAME...] Terminate one or more VMs/containers\n"
649 " kill [NAME...] Send signal to processes of a VM/container\n"
650 " login [NAME] Get a login prompt on a container\n",
651 program_invocation_short_name);
656 static int parse_argv(int argc, char *argv[]) {
665 static const struct option options[] = {
666 { "help", no_argument, NULL, 'h' },
667 { "version", no_argument, NULL, ARG_VERSION },
668 { "property", required_argument, NULL, 'p' },
669 { "all", no_argument, NULL, 'a' },
670 { "full", no_argument, NULL, 'l' },
671 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
672 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
673 { "signal", required_argument, NULL, 's' },
674 { "host", required_argument, NULL, 'H' },
675 { "machine", required_argument, NULL, 'M' },
676 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
685 while ((c = getopt_long(argc, argv, "hp:als:H:M:", options, NULL)) >= 0) {
693 puts(PACKAGE_STRING);
694 puts(SYSTEMD_FEATURES);
698 r = strv_extend(&arg_property, optarg);
702 /* If the user asked for a particular
703 * property, show it to him, even if it is
720 case ARG_NO_ASK_PASSWORD:
721 arg_ask_password = false;
725 arg_kill_who = optarg;
729 arg_signal = signal_from_string_try_harder(optarg);
730 if (arg_signal < 0) {
731 log_error("Failed to parse signal string %s.", optarg);
737 arg_transport = BUS_TRANSPORT_REMOTE;
742 arg_transport = BUS_TRANSPORT_CONTAINER;
750 assert_not_reached("Unhandled option");
757 static int machinectl_main(sd_bus *bus, int argc, char *argv[]) {
759 static const struct {
767 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
769 { "list", LESS, 1, list_machines },
770 { "status", MORE, 2, show },
771 { "show", MORE, 1, show },
772 { "terminate", MORE, 2, terminate_machine },
773 { "kill", MORE, 2, kill_machine },
774 { "login", MORE, 2, login_machine },
783 left = argc - optind;
786 /* Special rule: no arguments means "list" */
789 if (streq(argv[optind], "help")) {
794 for (i = 0; i < ELEMENTSOF(verbs); i++)
795 if (streq(argv[optind], verbs[i].verb))
798 if (i >= ELEMENTSOF(verbs)) {
799 log_error("Unknown operation %s", argv[optind]);
804 switch (verbs[i].argc_cmp) {
807 if (left != verbs[i].argc) {
808 log_error("Invalid number of arguments.");
815 if (left < verbs[i].argc) {
816 log_error("Too few arguments.");
823 if (left > verbs[i].argc) {
824 log_error("Too many arguments.");
831 assert_not_reached("Unknown comparison operator.");
834 return verbs[i].dispatch(bus, argv + optind, left);
837 int main(int argc, char*argv[]) {
838 _cleanup_bus_unref_ sd_bus *bus = NULL;
841 setlocale(LC_ALL, "");
842 log_parse_environment();
845 r = parse_argv(argc, argv);
849 r = bus_open_transport(arg_transport, arg_host, false, &bus);
851 log_error("Failed to create bus connection: %s", strerror(-r));
855 r = machinectl_main(bus, argc, argv);
860 strv_free(arg_property);
862 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;