machinectl.c revision 0a1beeb64207eaa88ab9236787b1cbc2f704ae14
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <pwd.h>
#include <locale.h>
#include <fcntl.h>
#include "sd-bus.h"
#include "log.h"
#include "util.h"
#include "macro.h"
#include "pager.h"
#include "bus-util.h"
#include "bus-error.h"
#include "build.h"
#include "strv.h"
#include "unit-name.h"
#include "cgroup-show.h"
#include "cgroup-util.h"
#include "ptyfwd.h"
#include "event-util.h"
static char **arg_property = NULL;
static bool arg_all = false;
static bool arg_full = false;
static bool arg_no_pager = false;
static bool arg_legend = true;
static const char *arg_kill_who = NULL;
static int arg_signal = SIGTERM;
static void pager_open_if_enabled(void) {
/* Cache result before we open the pager */
if (arg_no_pager)
return;
pager_open(false);
}
unsigned k = 0;
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"ListMachines",
&error,
&reply,
"");
if (r < 0) {
return r;
}
if (arg_legend)
if (r < 0)
return bus_log_parse_error(r);
k++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (arg_legend)
printf("\n%u machines listed.\n", k);
return 0;
}
const char *cgroup;
int r, output_flags;
unsigned c;
if (arg_transport == BUS_TRANSPORT_REMOTE)
return 0;
if (!path)
return log_oom();
r = sd_bus_get_property(
bus,
"org.freedesktop.systemd1",
path,
"ControlGroup",
&error,
&reply,
"s");
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
return 0;
return 0;
c = columns();
if (c > 18)
c -= 18;
else
c = 0;
show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, false, &leader, leader > 0, output_flags);
return 0;
}
static int print_addresses(sd_bus *bus, const char *name, int ifi, const char *prefix, const char *prefix2) {
int r;
r = sd_bus_call_method(bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"GetMachineAddresses",
NULL,
&reply,
"s", name);
if (r < 0)
return r;
if (r < 0)
return bus_log_parse_error(r);
int family;
const void *a;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
return 0;
}
int r;
r = sd_bus_call_method(bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"GetMachineOSRelease",
NULL,
&reply,
"s", name);
if (r < 0)
return r;
if (r < 0)
return bus_log_parse_error(r);
if (streq(k, "PRETTY_NAME"))
pretty = v;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (pretty)
return 0;
}
typedef struct MachineStatusInfo {
char *name;
char *class;
char *service;
char *unit;
char *root_directory;
int *netif;
unsigned n_netif;
int ifi = -1;
assert(i);
else
putchar('\n');
if (s1)
else if (s2)
if (i->leader > 0) {
_cleanup_free_ char *t = NULL;
get_process_comm(i->leader, &t);
if (t)
printf(" (%s)", t);
putchar('\n');
}
if (i->service) {
if (i->class)
putchar('\n');
} else if (i->class)
if (i->root_directory)
if (i->n_netif > 0) {
unsigned c;
for (c = 0; c < i->n_netif; c++) {
if (ifi < 0)
else
ifi = 0;
} else
}
}
"\t Address: ",
"\t ");
if (i->unit) {
}
}
static int map_netif(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
MachineStatusInfo *i = userdata;
size_t l;
const void *v;
int r;
r = sd_bus_message_read_array(m, SD_BUS_TYPE_INT32, &v, &l);
if (r < 0)
return r;
if (r == 0)
return -EBADMSG;
if (!i->netif)
return -ENOMEM;
return 0;
}
static const struct bus_properties_map map[] = {
{}
};
MachineStatusInfo info = {};
int r;
"org.freedesktop.machine1",
path,
map,
&info);
if (r < 0) {
log_error_errno(-r, "Could not get properties: %m");
return r;
}
if (*new_line)
printf("\n");
*new_line = true;
return r;
}
int r;
if (*new_line)
printf("\n");
*new_line = true;
if (r < 0)
log_error_errno(-r, "Could not get properties: %m");
return r;
}
int r = 0;
unsigned i;
bool properties, new_line = false;
if (properties && n <= 1) {
/* If no argument is specified, inspect the manager
* itself */
if (r < 0)
return r;
}
for (i = 1; i < n; i++) {
r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"GetMachine",
&error,
&reply,
"s", args[i]);
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
if (properties)
else
}
return r;
}
unsigned i;
if (!arg_kill_who)
arg_kill_who = "all";
for (i = 1; i < n; i++) {
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"KillMachine",
&error,
NULL,
if (r < 0) {
return r;
}
}
return 0;
}
arg_kill_who = "leader";
}
arg_kill_who = "leader";
}
unsigned i;
for (i = 1; i < n; i++) {
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"TerminateMachine",
&error,
NULL,
"s", args[i]);
if (r < 0) {
return r;
}
}
return 0;
}
union {
} control = {};
.msg_control = &control,
.msg_controllen = sizeof(control),
};
int master = -1, r;
if (r < 0)
return r;
return -errno;
if (child < 0)
return -errno;
if (child == 0) {
if (r < 0)
if (master < 0)
}
if (r < 0)
return r;
return -EIO;
return -errno;
int *fds;
unsigned n_fds;
if (n_fds != 1) {
return -EIO;
}
}
if (master < 0)
return -EIO;
return master;
}
int r, ret = 0;
if (arg_transport != BUS_TRANSPORT_LOCAL) {
log_error("Login only supported on local machines.");
return -ENOTSUP;
}
r = sd_event_default(&event);
if (r < 0) {
log_error_errno(-r, "Failed to get event loop: %m");
return r;
}
if (r < 0) {
log_error_errno(-r, "Failed to attach bus to event loop: %m");
return r;
}
r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"GetMachine",
&error,
&reply,
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_get_property(
bus,
"org.freedesktop.machine1",
path,
"org.freedesktop.machine1.Machine",
"Leader",
&error,
&reply2,
"u");
if (r < 0) {
log_error_errno(-r, "Failed to retrieve PID of leader: %m");
return r;
}
if (r < 0)
return bus_log_parse_error(r);
if (master < 0) {
return master;
}
if (!pty) {
log_error("Failed to get pty name: %m");
return -errno;
}
if (!p) {
return -EIO;
}
if (r < 0) {
log_error_errno(-r, "Failed to get container bus: %m");
return r;
}
if (!getty)
return log_oom();
log_error("Failed to unlock tty: %m");
return -errno;
}
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartUnit",
if (r < 0) {
return r;
}
if (r < 0) {
log_error_errno(-r, "Failed to create PTY forwarder: %m");
return r;
}
r = sd_event_loop(event);
if (r < 0) {
log_error_errno(-r, "Failed to run event loop: %m");
return r;
}
return ret;
}
static void help(void) {
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
"Send control commands to or query the virtual machine and container registration manager.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --no-pager Do not pipe output into a pager\n"
" --no-legend Do not show the headers and footers\n"
" -H --host=[USER@]HOST Operate on remote host\n"
" -M --machine=CONTAINER Operate on local container\n"
" -p --property=NAME Show only properties by this name\n"
" -a --all Show all properties, including empty ones\n"
" -l --full Do not ellipsize output\n"
" --kill-who=WHO Who to send signal to\n"
" -s --signal=SIGNAL Which signal to send\n\n"
"Commands:\n"
" list List running VMs and containers\n"
" show NAME... Show properties of one or more VMs/containers\n"
" login NAME Get a login prompt on a container\n"
" poweroff NAME... Power off one or more containers\n"
" reboot NAME... Reboot one or more containers\n"
" terminate NAME... Terminate one or more VMs/containers\n",
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c, r;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case 'p':
if (r < 0)
return log_oom();
/* If the user asked for a particular
* property, show it to him, even if it is
* empty. */
arg_all = true;
break;
case 'a':
arg_all = true;
break;
case 'l':
arg_full = true;
break;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case ARG_NO_LEGEND:
arg_legend = false;
break;
case ARG_KILL_WHO:
break;
case 's':
if (arg_signal < 0) {
return -EINVAL;
}
break;
case 'H':
break;
case 'M':
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
return 1;
}
static const struct {
const char* verb;
const enum {
MORE,
LESS,
} argc_cmp;
const int argc;
} verbs[] = {
};
int left;
unsigned i;
if (left <= 0)
/* Special rule: no arguments means "list" */
i = 0;
else {
help();
return 0;
}
for (i = 0; i < ELEMENTSOF(verbs); i++)
break;
if (i >= ELEMENTSOF(verbs)) {
return -EINVAL;
}
}
case EQUAL:
log_error("Invalid number of arguments.");
return -EINVAL;
}
break;
case MORE:
log_error("Too few arguments.");
return -EINVAL;
}
break;
case LESS:
log_error("Too many arguments.");
return -EINVAL;
}
break;
default:
assert_not_reached("Unknown comparison operator.");
}
}
int r;
log_open();
if (r <= 0)
goto finish;
if (r < 0) {
log_error_errno(-r, "Failed to create bus connection: %m");
goto finish;
}
pager_close();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}