systemctl.c revision ee87525c5eeacf3ce8fb730bcd3658e8da085046
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
Copyright 2013 Marc-Antoine Perennou
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 <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <locale.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "sd-bus.h"
#include "sd-daemon.h"
#include "sd-login.h"
#include "alloc-util.h"
#include "bus-common-errors.h"
#include "bus-error.h"
#include "bus-message.h"
#include "bus-util.h"
#include "cgroup-show.h"
#include "cgroup-util.h"
#include "copy.h"
#include "dropin.h"
#include "efivars.h"
#include "env-util.h"
#include "exit-status.h"
#include "fd-util.h"
#include "fileio.h"
#include "formats-util.h"
#include "fs-util.h"
#include "glob-util.h"
#include "hostname-util.h"
#include "initreq.h"
#include "install.h"
#include "io-util.h"
#include "list.h"
#include "locale-util.h"
#include "log.h"
#include "logs-show.h"
#include "macro.h"
#include "mkdir.h"
#include "pager.h"
#include "parse-util.h"
#include "path-lookup.h"
#include "path-util.h"
#include "process-util.h"
#include "rlimit-util.h"
#include "set.h"
#include "signal-util.h"
#include "socket-util.h"
#include "spawn-ask-password-agent.h"
#include "spawn-polkit-agent.h"
#include "special.h"
#include "stat-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "unit-name.h"
#include "user-util.h"
#include "util.h"
#include "utmp-wtmp.h"
#include "verbs.h"
#include "virt.h"
static char **arg_states = NULL;
static char **arg_properties = NULL;
static bool arg_all = false;
static enum dependency {
static const char *arg_job_mode = "replace";
static bool arg_no_block = false;
static bool arg_no_legend = false;
static bool arg_no_pager = false;
static bool arg_no_wtmp = false;
static bool arg_no_wall = false;
static bool arg_no_reload = false;
static bool arg_show_types = false;
static bool arg_ignore_inhibitors = false;
static bool arg_dry = false;
static bool arg_quiet = false;
static bool arg_full = false;
static bool arg_recursive = false;
static int arg_force = 0;
static bool arg_ask_password = false;
static bool arg_runtime = false;
static const char *arg_kill_who = NULL;
static int arg_signal = SIGTERM;
static enum action {
static unsigned arg_lines = 10;
static bool arg_plain = false;
static bool arg_firmware_setup = false;
static bool arg_now = false;
static bool original_stdout_is_tty;
typedef enum BusFocus {
BUS_FULL, /* The full bus indicated via --system or --user */
BUS_MANAGER, /* The manager itself, possibly directly, possibly via the bus */
} BusFocus;
int r;
/* We only go directly to the manager, if we are using a local transport */
if (arg_transport != BUS_TRANSPORT_LOCAL)
bool user;
if (focus == BUS_MANAGER)
else
if (r < 0)
return log_error_errno(r, "Failed to connect to bus: %m");
}
return 0;
}
static void release_busses(void) {
BusFocus w;
for (w = 0; w < _BUS_FOCUS_MAX; w++)
}
static void pager_open_if_enabled(void) {
if (arg_no_pager)
return;
pager_open(false);
}
static void ask_password_agent_open_if_enabled(void) {
/* Open the password agent as a child process if necessary */
if (!arg_ask_password)
return;
if (arg_scope != UNIT_FILE_SYSTEM)
return;
if (arg_transport != BUS_TRANSPORT_LOCAL)
return;
}
static void polkit_agent_open_if_enabled(void) {
/* Open the polkit agent as a child process if necessary */
if (!arg_ask_password)
return;
if (arg_scope != UNIT_FILE_SYSTEM)
return;
if (arg_transport != BUS_TRANSPORT_LOCAL)
return;
}
static OutputFlags get_output_flags(void) {
return
on_tty() * OUTPUT_COLOR |
}
if (!sd_bus_error_is_set(error))
return r;
return EXIT_NOPERMISSION;
return EXIT_NOTINSTALLED;
return EXIT_NOTIMPLEMENTED;
return EXIT_NOTCONFIGURED;
if (r != 0)
return r;
return EXIT_FAILURE;
}
static bool install_client_side(void) {
* client-side rather than server-side. */
if (running_in_chroot() > 0)
return true;
if (sd_booted() <= 0)
return true;
return true;
if (arg_scope == UNIT_FILE_GLOBAL)
return true;
/* Unsupported environment variable, mostly for debugging purposes */
if (getenv_bool("SYSTEMCTL_INSTALL_CLIENT_SIDE") > 0)
return true;
return false;
}
static int compare_unit_info(const void *a, const void *b) {
const UnitInfo *u = a, *v = b;
int r;
/* First, order by machine */
return -1;
return 1;
if (r != 0)
return r;
}
/* Second, order by unit type */
if (r != 0)
return r;
}
/* Third, order by name */
}
return false;
if (arg_types) {
const char *dot;
if (!dot)
return false;
return false;
}
if (arg_all)
return true;
if (u->job_id > 0)
return true;
return false;
return true;
}
const UnitInfo *u;
unsigned n_shown = 0;
int job_count = 0;
desc_len = 0;
for (u = unit_infos; u < unit_infos + c; u++) {
if (u->job_id != 0) {
job_count++;
}
if (!arg_no_legend &&
circle_len = 2;
}
if (!arg_full && original_stdout_is_tty) {
unsigned basic_len;
if (job_count)
/* Either UNIT already got 25, or is fully satisfied.
* Grant up to 25 to DESC now. */
/* split the remaining space between UNIT and DESC,
* but do not give UNIT more than it needs. */
if (extra_len > 0) {
}
}
} else
id_len = max_id_len;
for (u = unit_infos; u < unit_infos + c; u++) {
const char *id;
bool circle = false;
if (!n_shown && !arg_no_legend) {
if (circle_len > 0)
printf("%-*s %-*s %-*s %-*s ",
id_len, "UNIT",
load_len, "LOAD",
active_len, "ACTIVE",
sub_len, "SUB");
if (job_count)
if (!arg_full && arg_no_pager)
else
}
n_shown++;
circle = true;
circle = true;
}
if (u->machine) {
if (!j)
return log_oom();
id = j;
} else
if (arg_full) {
if (!e)
return log_oom();
id = e;
}
if (circle_len > 0)
printf("%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s",
if (desc_len > 0)
else
}
if (!arg_no_legend) {
if (n_shown) {
puts("\n"
"LOAD = Reflects whether the unit definition was properly loaded.\n"
"ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
"SUB = The low-level unit activation state, values depend on unit type.");
on = ansi_highlight();
off = ansi_normal();
} else {
on = ansi_highlight_red();
off = ansi_normal();
}
if (arg_all)
printf("%s%u loaded units listed.%s\n"
"To show all installed unit files use 'systemctl list-unit-files'.\n",
else
printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n"
"To show all installed unit files use 'systemctl list-unit-files'.\n",
}
return 0;
}
static int get_unit_list(
const char *machine,
char **patterns,
int c,
sd_bus_message **_reply) {
int r;
UnitInfo u;
bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListUnitsFiltered");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append_strv(m, arg_states);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
if (r < 0)
return bus_log_parse_error(r);
while ((r = bus_parse_unit_info(reply, &u)) > 0) {
if (!output_show_unit(&u, patterns))
continue;
return log_oom();
(*unit_infos)[c++] = u;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
return c;
}
sd_bus_message *m;
while ((m = set_steal_first(*set)))
}
static int get_unit_list_recursive(
char **patterns,
char ***_machines) {
int c, r;
if (!replies)
return log_oom();
if (c < 0)
return c;
if (r < 0) {
return log_oom();
}
if (arg_recursive) {
char **i;
r = sd_get_machine_names(&machines);
if (r < 0)
return log_error_errno(r, "Failed to get machine names: %m");
STRV_FOREACH(i, machines) {
int k;
r = sd_bus_open_system_machine(&container, *i);
if (r < 0) {
log_warning_errno(r, "Failed to connect to container %s, ignoring: %m", *i);
continue;
}
if (k < 0)
return k;
c = k;
if (r < 0) {
return log_oom();
}
}
} else
unit_infos = NULL;
return c;
}
int r;
if (r < 0)
return r;
if (r < 0)
return r;
return output_units_list(unit_infos, r);
}
static int get_triggered_units(
const char* path,
char*** ret) {
int r;
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
"Triggers",
&error,
ret);
if (r < 0)
return 0;
}
static int get_listening(
const char* unit_path,
char*** listening) {
int r, n = 0;
r = sd_bus_get_property(
bus,
"org.freedesktop.systemd1",
"org.freedesktop.systemd1.Socket",
"Listen",
&error,
&reply,
"a(ss)");
if (r < 0)
return log_error_errno(r, "Failed to get list of listening sockets: %s", bus_error_message(&error, r));
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return log_oom();
if (r < 0)
return log_oom();
n++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
return n;
}
struct socket_info {
const char *machine;
const char* id;
char* type;
char* path;
/* Note: triggered is a list here, although it almost certainly
* will always be one unit. Nevertheless, dbus API allows for multiple
* values, so let's follow that. */
char** triggered;
/* The strv above is shared. free is set only in the first one. */
bool own_triggered;
};
int o;
assert(a);
assert(b);
return -1;
return 1;
if (o != 0)
return o;
}
if (o == 0)
return o;
}
struct socket_info *s;
unsigned tmp = 0;
char **a;
if (arg_show_types)
STRV_FOREACH(a, s->triggered)
}
if (cs) {
if (!arg_no_legend)
printf("%-*s %-*.*s%-*s %s\n",
pathlen, "LISTEN",
socklen, "UNIT",
"ACTIVATES");
_cleanup_free_ char *j = NULL;
const char *path;
char **a;
if (s->machine) {
if (!j)
return log_oom();
path = j;
} else
if (arg_show_types)
printf("%-*s %-*s %-*s",
else
printf("%-*s %-*s",
STRV_FOREACH(a, s->triggered)
printf("%s %s",
printf("\n");
}
on = ansi_highlight();
off = ansi_normal();
if (!arg_no_legend)
printf("\n");
} else {
on = ansi_highlight_red();
off = ansi_normal();
}
if (!arg_no_legend) {
if (!arg_all)
printf("Pass --all to see loaded but inactive sockets, too.\n");
}
return 0;
}
const UnitInfo *u;
struct socket_info *s;
unsigned cs = 0;
int r = 0, n;
if (r < 0)
return r;
if (n < 0)
return n;
for (u = unit_infos; u < unit_infos + n; u++) {
int i, c;
continue;
if (r < 0)
goto cleanup;
if (c < 0) {
r = c;
goto cleanup;
}
r = log_oom();
goto cleanup;
}
for (i = 0; i < c; i++)
.own_triggered = i==0,
};
/* from this point on we will cleanup those socket_infos */
cs += c;
}
if (s->own_triggered)
}
return r;
}
static int get_next_elapse(
const char *path,
dual_timestamp *next) {
int r;
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Timer",
"NextElapseUSecMonotonic",
&error,
't',
&t.monotonic);
if (r < 0)
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Timer",
"NextElapseUSecRealtime",
&error,
't',
&t.realtime);
if (r < 0)
*next = t;
return 0;
}
static int get_last_trigger(
const char *path,
int r;
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Timer",
"LastTriggerUSec",
&error,
't',
last);
if (r < 0)
return 0;
}
struct timer_info {
const char* machine;
const char* id;
char** triggered;
};
int o;
assert(a);
assert(b);
return -1;
return 1;
if (o != 0)
return o;
}
if (a->next_elapse < b->next_elapse)
return -1;
if (a->next_elapse > b->next_elapse)
return 1;
}
struct timer_info *t;
unsigned
assert(timer_infos || n == 0);
for (t = timer_infos; t < timer_infos + n; t++) {
unsigned ul = 0;
char **a;
if (t->next_elapse > 0) {
}
if (t->last_trigger > 0) {
}
STRV_FOREACH(a, t->triggered)
}
if (n > 0) {
if (!arg_no_legend)
printf("%-*s %-*s %-*s %-*s %-*s %s\n",
nextlen, "NEXT",
leftlen, "LEFT",
lastlen, "LAST",
passedlen, "PASSED",
unitlen, "UNIT",
"ACTIVATES");
for (t = timer_infos; t < timer_infos + n; t++) {
_cleanup_free_ char *j = NULL;
const char *unit;
char **a;
if (t->machine) {
if (!j)
return log_oom();
unit = j;
} else
printf("%-*s %-*s %-*s %-*s %-*s",
STRV_FOREACH(a, t->triggered)
printf("%s %s",
printf("\n");
}
on = ansi_highlight();
off = ansi_normal();
if (!arg_no_legend)
printf("\n");
} else {
on = ansi_highlight_red();
off = ansi_normal();
}
if (!arg_no_legend) {
if (!arg_all)
printf("Pass --all to see loaded but inactive timers, too.\n");
}
return 0;
}
else
else
} else
return next_elapse;
}
struct timer_info *t;
const UnitInfo *u;
int n, c = 0;
int r = 0;
if (r < 0)
return r;
if (n < 0)
return n;
for (u = unit_infos; u < unit_infos + n; u++) {
continue;
if (r < 0)
goto cleanup;
if (r < 0)
goto cleanup;
r = log_oom();
goto cleanup;
}
timer_infos[c++] = (struct timer_info) {
.next_elapse = m,
.last_trigger = last,
};
}
for (t = timer_infos; t < timer_infos + c; t++)
return r;
}
static int compare_unit_file_list(const void *a, const void *b) {
const UnitFileList *u = a, *v = b;
int r;
if (r != 0)
return r;
}
}
return false;
if (!strv_isempty(arg_types)) {
const char *dot;
if (!dot)
return false;
return false;
}
if (!strv_isempty(arg_states) &&
return false;
return true;
}
const UnitFileList *u;
}
if (!arg_full) {
unsigned basic_cols;
if (basic_cols < (unsigned) columns())
} else
if (!arg_no_legend)
printf("%-*s %-*s\n",
id_cols, "UNIT FILE",
state_cols, "STATE");
_cleanup_free_ char *e = NULL;
const char *id;
UNIT_FILE_BAD)) {
on = ansi_highlight_red();
off = ansi_normal();
} else if (u->state == UNIT_FILE_ENABLED) {
on = ansi_highlight_green();
off = ansi_normal();
} else
printf("%-*s %s%-*s%s\n",
}
if (!arg_no_legend)
printf("\n%u unit files listed.\n", c);
}
unsigned c = 0;
const char *state;
char *path;
int r;
if (install_client_side()) {
Hashmap *h;
UnitFileList *u;
Iterator i;
unsigned n_units;
h = hashmap_new(&string_hash_ops);
if (!h)
return log_oom();
if (r < 0) {
return log_error_errno(r, "Failed to get unit file list: %m");
}
n_units = hashmap_size(h);
return log_oom();
}
HASHMAP_FOREACH(u, h, i) {
continue;
units[c++] = *u;
free(u);
}
hashmap_free(h);
} else {
if (r < 0)
return r;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListUnitFiles",
&error,
&reply,
NULL);
if (r < 0)
if (r < 0)
return bus_log_parse_error(r);
return log_oom();
units[c] = (struct UnitFileList) {
path,
};
c ++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
}
if (install_client_side()) {
}
return 0;
}
_cleanup_free_ char *n = NULL;
int i;
if (!arg_plain) {
for (i = level - 1; i >= 0; i--) {
len += 2;
return 0;
}
}
len += 2;
return 0;
}
}
if (arg_full){
return 0;
}
if (!n)
return log_oom();
printf("%s\n", n);
return 0;
}
static const char *dependencies[_DEPENDENCY_MAX] = {
[DEPENDENCY_FORWARD] = "Requires\0"
"Requisite\0"
"Wants\0"
"ConsistsOf\0"
"BindsTo\0",
[DEPENDENCY_REVERSE] = "RequiredBy\0"
"RequisiteOf\0"
"WantedBy\0"
"PartOf\0"
"BoundBy\0",
[DEPENDENCY_AFTER] = "After\0",
[DEPENDENCY_BEFORE] = "Before\0",
};
int r;
if (!path)
return log_oom();
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"GetAll",
&error,
&reply,
"s", "org.freedesktop.systemd1.Unit");
if (r < 0)
return log_error_errno(r, "Failed to get properties of %s: %s", name, bus_error_message(&error, r));
if (r < 0)
return bus_log_parse_error(r);
const char *prop;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
} else {
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);
if (r < 0)
return bus_log_parse_error(r);
return 0;
}
return 1;
return -1;
return strcasecmp(*a, *b);
}
static int list_dependencies_one(
const char *name,
int level,
char ***units,
unsigned int branches) {
char **c;
int r = 0;
if (r < 0)
return log_oom();
if (r < 0)
return r;
STRV_FOREACH(c, deps) {
if (strv_contains(*units, *c)) {
if (!arg_plain) {
if (r < 0)
return r;
}
continue;
}
if (arg_plain)
printf(" ");
else {
int state;
const char *on;
}
if (r < 0)
return r;
if (r < 0)
return r;
}
}
if (!arg_plain)
return 0;
}
const char *u;
int r;
if (argv[1]) {
if (r < 0)
return log_error_errno(r, "Failed to mangle unit name: %m");
u = unit;
} else
if (r < 0)
return r;
puts(u);
}
struct machine_info {
bool is_host;
char *name;
char *state;
char *control_group;
};
static const struct bus_properties_map machine_info_property_map[] = {
{}
};
if (info) {
}
}
int i;
if (!machine_infos)
return;
for (i = 0; i < n; i++)
}
static int compare_machine_info(const void *a, const void *b) {
const struct machine_info *u = a, *v = b;
}
int r;
if (!bus) {
if (r < 0)
return r;
}
r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map, mi);
if (r < 0)
return r;
return 0;
}
}
static int get_machine_list(
struct machine_info **_machine_infos,
char **patterns) {
_cleanup_strv_free_ char **m = NULL;
char **i;
int c = 0, r;
hn = gethostname_malloc();
if (!hn)
return log_oom();
return log_oom();
machine_infos[c].is_host = true;
c++;
}
r = sd_get_machine_names(&m);
if (r < 0)
return log_error_errno(r, "Failed to get machine list: %m");
STRV_FOREACH(i, m) {
if (!output_show_machine(*i, patterns))
continue;
sd_machine_get_class(*i, &class);
continue;
return log_oom();
}
machine_infos[c].is_host = false;
if (!machine_infos[c].name) {
return log_oom();
}
c++;
}
return c;
}
struct machine_info *m;
unsigned
circle_len = 0,
assert(machine_infos || n == 0);
for (m = machine_infos; m < machine_infos + n; m++) {
circle_len = 2;
}
if (!arg_no_legend) {
if (circle_len > 0)
printf("%-*s %-*s %-*s %-*s\n",
namelen, "NAME",
statelen, "STATE",
failedlen, "FAILED",
jobslen, "JOBS");
}
for (m = machine_infos; m < machine_infos + n; m++) {
bool circle = false;
off_state = ansi_normal();
circle = true;
off_state = ansi_normal();
circle = true;
}
if (m->n_failed_units > 0) {
off_failed = ansi_normal();
} else
if (circle_len > 0)
if (m->is_host)
printf("%-*s (host) %s%-*s%s %s%*u%s %*u\n",
else
printf("%-*s %s%-*s%s %s%*u%s %*u\n",
}
if (!arg_no_legend)
printf("\n%u machines listed.\n", n);
}
int r;
if (geteuid() != 0) {
log_error("Must be root.");
return -EPERM;
}
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
const char *path;
int r;
if (install_client_side()) {
if (r < 0)
return log_error_errno(r, "Failed to get default target: %m");
} else {
if (r < 0)
return r;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetDefaultTarget",
&error,
&reply,
NULL);
if (r < 0)
if (r < 0)
return bus_log_parse_error(r);
}
if (path)
return 0;
}
unsigned i;
for (i = 0; i < n_changes; i++) {
else
}
}
int r;
if (r < 0)
return log_error_errno(r, "Failed to mangle unit name: %m");
if (install_client_side()) {
unsigned n_changes = 0;
if (r < 0)
return log_error_errno(r, "Failed to set default target: %m");
if (!arg_quiet)
r = 0;
} else {
if (r < 0)
return r;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"SetDefaultTarget",
&error,
&reply,
if (r < 0)
if (r < 0)
return r;
/* Try to reload if enabled */
if (!arg_no_reload)
else
r = 0;
}
return r;
}
struct job_info {
};
const struct job_info *j;
bool shorten = false;
if (n == 0) {
if (!arg_no_legend) {
on = ansi_highlight_green();
off = ansi_normal();
}
return;
}
}
shorten = true;
}
if (!arg_no_legend)
printf("%*s %-*s %-*s %-*s\n",
id_len, "JOB",
unit_len, "UNIT",
type_len, "TYPE",
state_len, "STATE");
_cleanup_free_ char *e = NULL;
on = ansi_highlight();
off = ansi_normal();
} else
printf("%*u %s%-*s%s %-*s %s%-*s%s\n",
}
if (!arg_no_legend) {
on = ansi_highlight();
off = ansi_normal();
}
}
}
unsigned c = 0;
int r;
bool skipped = false;
if (r < 0)
return r;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListJobs",
&error,
&reply,
NULL);
if (r < 0)
if (r < 0)
return bus_log_parse_error(r);
while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, &job_path, &unit_path)) > 0) {
skipped = true;
continue;
}
return log_oom();
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
return 0;
}
char **name;
int r = 0;
if (argc <= 1)
if (r < 0)
return r;
int q;
if (q < 0)
q = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"CancelJob",
&error,
NULL,
"u", id);
if (q < 0) {
if (r == 0)
r = q;
}
}
return r;
}
const char *path;
int b, r;
/* We ignore all errors here, since this is used to show a
* warning only */
/* We don't use unit_dbus_path_from_name() directly since we
* don't want to load the unit if it isn't loaded. */
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit",
NULL,
&reply,
"s", unit);
if (r < 0)
return r;
if (r < 0)
return r;
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
"NeedDaemonReload",
NULL,
'b', &b);
if (r < 0)
return r;
return b;
}
static void warn_unit_file_changed(const char *name) {
log_warning("%sWarning:%s %s changed on disk. Run 'systemctl%s daemon-reload' to reload units.",
ansi_normal(),
name,
}
char **p;
_cleanup_free_ char *path;
if (!path)
return log_oom();
return 1;
}
}
return 0;
}
static int unit_find_paths(
const char *unit_name,
char **fragment_path,
char ***dropin_paths) {
int r;
/**
* Finds where the unit is defined on disk. Returns 0 if the unit
* is not found. Returns 1 if it is found, and sets
* - the path to the unit in *path, if it exists on disk,
* - and a strv of existing drop-ins in *dropins,
* if the arg is not NULL and any dropins were found.
*/
if (!unit)
return log_oom();
r = sd_bus_get_property(
bus,
"org.freedesktop.systemd1",
unit,
"org.freedesktop.systemd1.Unit",
"LoadError",
&error,
"(ss)");
if (r < 0)
r = sd_bus_message_read(
"(ss)",
if (r < 0)
return bus_log_parse_error(r);
if (!isempty(unit_load_error_name)) {
return 0;
}
bus,
"org.freedesktop.systemd1",
unit,
"org.freedesktop.systemd1.Unit",
"FragmentPath",
&error,
&path);
if (r < 0)
if (dropin_paths) {
bus,
"org.freedesktop.systemd1",
unit,
"org.freedesktop.systemd1.Unit",
"DropInPaths",
&error,
&dropins);
if (r < 0)
}
} else {
if (!names)
return log_oom();
if (r < 0)
return log_error_errno(r, "Failed to add unit name: %m");
if (r < 0)
return r;
if (r == 0) {
if (r < 0 && r != -EINVAL)
return log_error_errno(r, "Failed to determine template name: %m");
if (r >= 0) {
if (r < 0)
return r;
}
}
if (dropin_paths) {
if (r < 0)
return r;
}
}
r = 0;
*fragment_path = path;
r = 1;
}
*dropin_paths = dropins;
r = 1;
}
if (r == 0)
return r;
}
const char *path;
int r;
if (r < 0)
return log_error_errno(r, "Failed to mangle unit name: %m");
/* We don't use unit_dbus_path_from_name() directly since we
* don't want to load the unit if it isn't loaded. */
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit",
NULL,
&reply,
"s", n);
if (r < 0) {
if (!quiet)
puts("unknown");
return 0;
}
if (r < 0)
return bus_log_parse_error(r);
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
"ActiveState",
NULL,
&state);
if (r < 0) {
if (!quiet)
puts("unknown");
return 0;
}
if (!quiet)
}
static int check_triggering_units(
const char *name) {
bool print_warning_label = true;
char **i;
int r;
if (r < 0)
return log_error_errno(r, "Failed to mangle unit name: %m");
path = unit_dbus_path_from_name(n);
if (!path)
return log_oom();
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
"LoadState",
&error,
&state);
if (r < 0)
return 0;
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
"TriggeredBy",
&error,
&triggered_by);
if (r < 0)
return log_error_errno(r, "Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r));
STRV_FOREACH(i, triggered_by) {
if (r < 0)
return log_error_errno(r, "Failed to check unit: %m");
if (r == 0)
continue;
if (print_warning_label) {
log_warning("Warning: Stopping %s, but it can still be activated by:", n);
print_warning_label = false;
}
log_warning(" %s", *i);
}
return 0;
}
static const struct {
const char *verb;
const char *method;
} unit_actions[] = {
{ "start", "StartUnit" },
{ "stop", "StopUnit" },
{ "condstop", "StopUnit" },
{ "reload", "ReloadUnit" },
{ "restart", "RestartUnit" },
{ "try-restart", "TryRestartUnit" },
{ "condrestart", "TryRestartUnit" },
{ "reload-or-restart", "ReloadOrRestartUnit" },
{ "reload-or-try-restart", "ReloadOrTryRestartUnit" },
{ "condreload", "ReloadOrTryRestartUnit" },
{ "force-reload", "ReloadOrTryRestartUnit" }
};
static const char *verb_to_method(const char *verb) {
uint i;
for (i = 0; i < ELEMENTSOF(unit_actions); i++)
return unit_actions[i].method;
return "StartUnit";
}
static const char *method_to_verb(const char *method) {
uint i;
for (i = 0; i < ELEMENTSOF(unit_actions); i++)
return unit_actions[i].verb;
return "n/a";
}
static int start_unit_one(
const char *method,
const char *name,
const char *mode,
BusWaitForJobs *w) {
const char *path;
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&reply,
if (r < 0) {
const char *verb;
/* There's always a fallback possible for
* legacy actions. */
return -EADDRNOTAVAIL;
return r;
}
if (r < 0)
return bus_log_parse_error(r);
if (w) {
r = bus_wait_for_jobs_add(w, path);
if (r < 0)
return log_oom();
}
return 0;
}
char **name;
int r, i;
char *t;
if (suffix)
else
if (r < 0)
return log_error_errno(r, "Failed to mangle name: %m");
if (string_is_glob(t))
r = strv_consume(&globs, t);
else
r = strv_consume(&mangled, t);
if (r < 0)
return log_oom();
}
/* Query the manager only if any of the names are a glob, since
* this is fairly expensive */
if (!strv_isempty(globs)) {
if (r < 0)
return r;
for (i = 0; i < r; i++)
return log_oom();
}
return 0;
}
static const struct {
const char *target;
const char *verb;
const char *mode;
} action_table[_ACTION_MAX] = {
};
enum action i;
for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
return i;
return _ACTION_INVALID;
}
char **name;
int r = 0;
if (r < 0)
return r;
if (arg_action == ACTION_SYSTEMCTL) {
mode = "isolate";
suffix = ".target";
} else
} else {
method = "StartUnit";
}
if (one_name)
else {
if (r < 0)
return log_error_errno(r, "Failed to expand names: %m");
}
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");
}
int q;
if (r >= 0 && q < 0)
r = translate_bus_error_to_exit_status(q, &error);
}
if (!arg_no_block) {
int q;
q = bus_wait_for_jobs(w, arg_quiet);
if (q < 0)
return q;
/* When stopping units, warn if they can still be triggered by
* another active unit (socket, path, timer) */
}
return r;
}
static int logind_set_wall_message(void) {
#ifdef HAVE_LOGIND
_cleanup_free_ char *m = NULL;
int r;
if (r < 0)
return r;
if (!m)
return log_oom();
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"SetWallMessage",
&error,
NULL,
"sb",
m,
!arg_no_wall);
if (r < 0)
return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r));
#endif
return 0;
}
/* Ask systemd-logind, which might grant access to unprivileged users
* through PolicyKit */
static int logind_reboot(enum action a) {
#ifdef HAVE_LOGIND
const char *method, *description;
int r;
(void) logind_set_wall_message();
if (r < 0)
return r;
switch (a) {
case ACTION_REBOOT:
method = "Reboot";
description = "reboot system";
break;
case ACTION_POWEROFF:
method = "PowerOff";
description = "power off system";
break;
case ACTION_SUSPEND:
method = "Suspend";
description = "suspend system";
break;
case ACTION_HIBERNATE:
method = "Hibernate";
description = "hibernate system";
break;
case ACTION_HYBRID_SLEEP:
method = "HybridSleep";
description = "put system into hybrid sleep";
break;
default:
return -EINVAL;
}
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
&error,
NULL,
"b", arg_ask_password);
if (r < 0)
return log_error_errno(r, "Failed to %s via logind: %s", description, bus_error_message(&error, r));
return 0;
#else
return -ENOSYS;
#endif
}
static int logind_check_inhibitors(enum action a) {
#ifdef HAVE_LOGIND
unsigned c = 0;
char **s;
int r;
if (arg_ignore_inhibitors || arg_force > 0)
return 0;
if (arg_when > 0)
return 0;
if (geteuid() == 0)
return 0;
if (!on_tty())
return 0;
if (r < 0)
return r;
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ListInhibitors",
NULL,
&reply,
NULL);
if (r < 0)
/* If logind is not around, then there are no inhibitors... */
return 0;
if (r < 0)
return bus_log_parse_error(r);
continue;
if (!sv)
return log_oom();
if (!strv_contains(sv,
IN_SET(a,
continue;
c++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
/* Check for current sessions */
STRV_FOREACH(s, sessions) {
_cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
continue;
continue;
continue;
sd_session_get_tty(*s, &tty);
sd_session_get_seat(*s, &seat);
sd_session_get_service(*s, &service);
log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
c++;
}
if (c <= 0)
return 0;
log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.",
action_table[a].verb);
return -EPERM;
#else
return 0;
#endif
}
static int logind_prepare_firmware_setup(void) {
#ifdef HAVE_LOGIND
int r;
if (r < 0)
return r;
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"SetRebootToFirmwareSetup",
&error,
NULL,
"b", true);
if (r < 0)
return log_error_errno(r, "Cannot indicate to EFI to boot into setup mode: %s", bus_error_message(&error, r));
return 0;
#else
log_error("Cannot remotely indicate to EFI to boot into setup mode.");
return -ENOSYS;
#endif
}
static int prepare_firmware_setup(void) {
int r;
if (!arg_firmware_setup)
return 0;
if (arg_transport == BUS_TRANSPORT_LOCAL) {
r = efi_set_reboot_to_firmware(true);
if (r < 0)
log_debug_errno(r, "Cannot indicate to EFI to boot into setup mode, will retry via logind: %m");
else
return r;
}
return logind_prepare_firmware_setup();
}
int r;
if (r < 0)
return r;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"SetExitCode",
&error,
NULL,
"y", code);
if (r < 0)
return 0;
}
enum action a;
int r;
a = verb_to_action(argv[0]);
r = logind_check_inhibitors(a);
if (r < 0)
return r;
log_error("Must be root.");
return -EPERM;
}
r = prepare_firmware_setup();
if (r < 0)
return r;
if (r < 0)
return r;
/* If the exit code is not given on the command line,
* don't reset it to zero: just keep it as it might
* have been set previously. */
if (r < 0)
return log_error_errno(r, "Invalid exit code.");
r = set_exit_code(code);
if (r < 0)
return r;
}
if (arg_force >= 2 &&
IN_SET(a,
return halt_now(a);
if (arg_force >= 1 &&
IN_SET(a,
/* First try logind, to allow authentication with polkit */
if (IN_SET(a,
r = logind_reboot(a);
if (r >= 0)
return r;
/* requested operation is not supported or already in progress */
return r;
/* On all other errors, try low-level operation */
}
}
char **name;
int r;
if (r < 0)
return r;
if (r < 0)
return log_error_errno(r, "Failed to expand names: %m");
int state;
if (state < 0)
return state;
if (state == 0)
r = code;
}
return r;
}
/* According to LSB: 3, "program is not running" */
}
}
int r, q;
if (r < 0)
return r;
if (!arg_kill_who)
arg_kill_who = "all";
/* --fail was specified */
if (r < 0)
return log_error_errno(r, "Failed to expand names: %m");
q = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"KillUnit",
&error,
NULL,
if (q < 0) {
if (r == 0)
r = q;
}
}
return r;
}
typedef struct ExecStatusInfo {
char *name;
char *path;
char **argv;
bool ignore;
int code;
int status;
static void exec_status_info_free(ExecStatusInfo *i) {
assert(i);
free(i);
}
const char *path;
int ignore, r;
assert(m);
assert(i);
if (r < 0)
return bus_log_parse_error(r);
else if (r == 0)
return 0;
if (r < 0)
return bus_log_parse_error(r);
if (!i->path)
return log_oom();
r = sd_bus_message_read_strv(m, &i->argv);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_read(m,
"bttttuii",
&ignore,
&pid,
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
return 1;
}
typedef struct UnitStatusInfo {
const char *id;
const char *load_state;
const char *active_state;
const char *sub_state;
const char *unit_file_state;
const char *unit_file_preset;
const char *description;
const char *following;
char **documentation;
const char *fragment_path;
const char *source_path;
const char *control_group;
char **dropin_paths;
const char *load_error;
const char *result;
bool need_daemon_reload;
bool transient;
/* Service */
const char *status_text;
const char *pid_file;
bool running:1;
int status_errno;
int exit_code, exit_status;
bool condition_result;
bool failed_condition_trigger;
bool failed_condition_negate;
const char *failed_condition;
const char *failed_condition_parameter;
bool assert_result;
bool failed_assert_trigger;
bool failed_assert_negate;
const char *failed_assert;
const char *failed_assert_parameter;
/* Socket */
unsigned n_accepted;
unsigned n_connections;
bool accept;
/* Pairs of type, path */
char **listen;
/* Device */
const char *sysfs_path;
/* Mount, Automount */
const char *where;
/* Swap */
const char *what;
/* CGroup */
static void print_status_info(
UnitStatusInfo *i,
bool *ellipsized) {
ExecStatusInfo *p;
const char *path;
char **t, **t2;
assert(i);
/* This shows pretty information about a unit. See
* print_property() for a low-level property printer */
active_off = ansi_normal();
active_off = ansi_normal();
} else
printf("\n");
if (i->following)
on = ansi_highlight_red();
off = ansi_normal();
} else
if (i->load_error != 0)
printf(" Loaded: %s%s%s (Reason: %s)\n",
printf(" Loaded: %s%s%s (%s; %s; vendor preset: %s)\n",
printf(" Loaded: %s%s%s (%s; %s)\n",
else if (path)
printf(" Loaded: %s%s%s (%s)\n",
else
printf(" Loaded: %s%s%s\n",
if (i->transient)
printf("Transient: yes\n");
if (!strv_isempty(i->dropin_paths)) {
bool last = false;
char ** dropin;
if (!dir) {
log_oom();
return;
}
}
}
}
if (ss)
printf(" Active: %s%s (%s)%s",
else
printf(" Active: %s%s%s",
if (s1)
else if (s2)
else
printf("\n");
if (!i->condition_result && i->condition_timestamp > 0) {
printf("Condition: start %scondition failed%s at %s%s%s\n",
if (i->failed_condition_trigger)
printf(" none of the trigger conditions were met\n");
else if (i->failed_condition)
printf(" %s=%s%s was not met\n",
i->failed_condition,
}
if (!i->assert_result && i->assert_timestamp > 0) {
printf(" Assert: start %sassertion failed%s at %s%s%s\n",
if (i->failed_assert_trigger)
printf(" none of the trigger assertions were met\n");
else if (i->failed_assert)
printf(" %s=%s%s was not met\n",
i->failed_assert,
}
if (i->sysfs_path)
if (i->where)
if (i->what)
STRV_FOREACH(t, i->documentation)
if (i->accept)
bool good;
/* Only show exited processes here */
if (p->code == 0)
continue;
if (!good) {
on = ansi_highlight_red();
off = ansi_normal();
} else
if (p->code == CLD_EXITED) {
const char *c;
if (c)
printf("/%s", c);
} else
i->start_timestamp == p->start_timestamp &&
i->exit_timestamp == p->start_timestamp)
/* Let's not show this twice */
i->main_pid = 0;
if (p->pid == i->control_pid)
i->control_pid = 0;
}
if (i->main_pid > 0 || i->control_pid > 0) {
if (i->main_pid > 0) {
if (i->running) {
if (comm)
} else if (i->exit_code > 0) {
if (i->exit_code == CLD_EXITED) {
const char *c;
if (c)
printf("/%s", c);
} else
printf(")");
}
if (i->control_pid > 0)
printf(";");
}
if (i->control_pid > 0) {
_cleanup_free_ char *c = NULL;
get_process_comm(i->control_pid, &c);
if (c)
printf(" (%s)", c);
}
printf("\n");
}
if (i->status_text)
if (i->status_errno > 0)
else
printf("\n");
}
char buf[FORMAT_BYTES_MAX];
else
printf("\n");
}
char buf[FORMAT_TIMESPAN_MAX];
printf(" CPU: %s\n", format_timespan(buf, sizeof(buf), i->cpu_usage_nsec / NSEC_PER_USEC, USEC_PER_MSEC));
}
if (i->control_group &&
(i->main_pid > 0 || i->control_pid > 0 ||
(!IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE) || cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, i->control_group) == 0))) {
unsigned c;
if (IN_SET(arg_transport,
unsigned k = 0;
static const char prefix[] = " ";
c = columns();
if (c > sizeof(prefix) - 1)
c -= sizeof(prefix) - 1;
else
c = 0;
if (i->main_pid > 0)
if (i->control_pid > 0)
extra[k++] = i->control_pid;
show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, false, extra, k, get_output_flags());
}
}
i->id,
0,
getuid(),
if (i->need_daemon_reload)
warn_unit_file_changed(i->id);
}
static void show_unit_help(UnitStatusInfo *i) {
char **p;
assert(i);
if (!i->documentation) {
return;
}
STRV_FOREACH(p, i->documentation)
if (startswith(*p, "man:"))
show_man_page(*p + 4, false);
else
log_info("Can't show: %s", *p);
}
static int status_property(const char *name, sd_bus_message *m, UnitStatusInfo *i, const char *contents) {
int r;
assert(m);
assert(i);
switch (contents[0]) {
case SD_BUS_TYPE_STRING: {
const char *s;
r = sd_bus_message_read(m, "s", &s);
if (r < 0)
return bus_log_parse_error(r);
if (!isempty(s)) {
i->id = s;
i->load_state = s;
i->active_state = s;
i->sub_state = s;
i->description = s;
i->fragment_path = s;
i->source_path = s;
#ifndef NOLEGACY
const char *e;
if (e)
i->control_group = e;
}
#endif
i->control_group = s;
i->status_text = s;
i->pid_file = s;
i->sysfs_path = s;
i->where = s;
i->what = s;
i->following = s;
i->unit_file_state = s;
i->unit_file_preset = s;
i->result = s;
}
break;
}
case SD_BUS_TYPE_BOOLEAN: {
int b;
r = sd_bus_message_read(m, "b", &b);
if (r < 0)
return bus_log_parse_error(r);
i->accept = b;
i->need_daemon_reload = b;
i->condition_result = b;
i->assert_result = b;
i->transient = b;
break;
}
case SD_BUS_TYPE_UINT32: {
uint32_t u;
r = sd_bus_message_read(m, "u", &u);
if (r < 0)
return bus_log_parse_error(r);
if (u > 0) {
i->running = true;
}
i->control_pid = (pid_t) u;
if (u > 0)
i->n_accepted = u;
i->n_connections = u;
break;
}
case SD_BUS_TYPE_INT32: {
int32_t j;
r = sd_bus_message_read(m, "i", &j);
if (r < 0)
return bus_log_parse_error(r);
i->exit_code = (int) j;
i->exit_status = (int) j;
i->status_errno = (int) j;
break;
}
case SD_BUS_TYPE_UINT64: {
uint64_t u;
r = sd_bus_message_read(m, "t", &u);
if (r < 0)
return bus_log_parse_error(r);
i->start_timestamp = (usec_t) u;
i->exit_timestamp = (usec_t) u;
i->active_enter_timestamp = (usec_t) u;
i->inactive_enter_timestamp = (usec_t) u;
i->inactive_exit_timestamp = (usec_t) u;
i->inactive_exit_timestamp_monotonic = (usec_t) u;
i->active_exit_timestamp = (usec_t) u;
i->condition_timestamp = (usec_t) u;
i->assert_timestamp = (usec_t) u;
i->memory_current = u;
i->memory_limit = u;
i->tasks_current = u;
i->tasks_max = u;
i->cpu_usage_nsec = u;
break;
}
case SD_BUS_TYPE_ARRAY:
if (r < 0)
return bus_log_parse_error(r);
if (!info)
return log_oom();
while ((r = exec_status_info_deserialize(m, info)) > 0) {
return log_oom();
if (!info)
return log_oom();
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
return 0;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return r;
if (r < 0)
return r;
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
return 0;
r = sd_bus_message_read_strv(m, &i->dropin_paths);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_read_strv(m, &i->documentation);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
i->failed_condition = cond;
}
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
i->failed_assert = cond;
i->failed_assert_trigger = trigger;
i->failed_assert_negate = negate;
i->failed_assert_parameter = param;
}
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
} else
goto skip;
break;
case SD_BUS_TYPE_STRUCT_BEGIN:
const char *n, *message;
if (r < 0)
return bus_log_parse_error(r);
i->load_error = message;
} else
goto skip;
break;
default:
goto skip;
}
return 0;
skip:
r = sd_bus_message_skip(m, contents);
if (r < 0)
return bus_log_parse_error(r);
return 0;
}
int r;
assert(m);
/* This is a low-level property printer, see
* print_status_info() for the nicer output */
/* skip what we didn't read */
r = sd_bus_message_skip(m, contents);
return r;
}
switch (contents[0]) {
case SD_BUS_TYPE_STRUCT_BEGIN:
uint32_t u;
if (r < 0)
return bus_log_parse_error(r);
if (u > 0)
else if (arg_all)
return 0;
const char *s;
if (r < 0)
return bus_log_parse_error(r);
return 0;
r = sd_bus_message_read(m, "(ss)", &a, &b);
if (r < 0)
return bus_log_parse_error(r);
return 0;
_cleanup_strv_free_ char **l = NULL;
int whitelist;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_read_strv(m, &l);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
bool first = true;
char **i;
if (!whitelist)
STRV_FOREACH(i, l) {
if (first)
first = false;
else
}
}
return 0;
}
break;
case SD_BUS_TYPE_ARRAY:
const char *path;
int ignore;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
return 0;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
return 0;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
return 0;
const char *base;
if (r < 0)
return bus_log_parse_error(r);
printf("%s={ value=%s ; next_elapse=%s }\n",
base,
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
return 0;
ExecStatusInfo info = {};
if (r < 0)
return bus_log_parse_error(r);
while ((r = exec_status_info_deserialize(m, &info)) > 0) {
_cleanup_free_ char *tt;
printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }\n",
name,
}
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
return 0;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
return 0;
const char *path;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
return 0;
} else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && (streq(name, "BlockIOReadBandwidth") || streq(name, "BlockIOWriteBandwidth"))) {
const char *path;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
return 0;
}
break;
}
if (r < 0)
return bus_log_parse_error(r);
if (r == 0) {
r = sd_bus_message_skip(m, contents);
if (r < 0)
return bus_log_parse_error(r);
if (arg_all)
}
return 0;
}
static int show_one(
const char *verb,
const char *path,
bool show_properties,
bool *new_line,
bool *ellipsized) {
UnitStatusInfo info = {
};
ExecStatusInfo *p;
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"GetAll",
&error,
&reply,
"s", "");
if (r < 0)
if (r < 0)
return bus_log_parse_error(r);
if (*new_line)
printf("\n");
*new_line = true;
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 (show_properties)
else
if (r < 0)
return 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);
r = 0;
if (!show_properties) {
else
}
/* According to LSB: "program not running" */
/* 0: program is running or service is OK
* 1: program is dead and /run PID file exists
* 3: program is not running
* 4: program or service status is unknown
*/
r = 1;
else
r = 3;
}
}
return r;
}
static int get_unit_dbus_path_by_pid(
char **unit) {
char *u;
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnitByPID",
&error,
&reply,
"u", pid);
if (r < 0)
return log_error_errno(r, "Failed to get unit for PID %"PRIu32": %s", pid, bus_error_message(&error, r));
if (r < 0)
return bus_log_parse_error(r);
u = strdup(u);
if (!u)
return log_oom();
*unit = u;
return 0;
}
static int show_all(
const char* verb,
bool show_properties,
bool *new_line,
bool *ellipsized) {
const UnitInfo *u;
unsigned c;
int r, ret = 0;
if (r < 0)
return r;
c = (unsigned) r;
for (u = unit_infos; u < unit_infos + c; u++) {
_cleanup_free_ char *p = NULL;
p = unit_dbus_path_from_name(u->id);
if (!p)
return log_oom();
if (r < 0)
return r;
else if (r > 0 && ret == 0)
ret = r;
}
return ret;
}
int r;
hn = gethostname_malloc();
if (!hn)
return log_oom();
r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map, &mi);
if (r < 0)
return log_error_errno(r, "Failed to read server status: %m");
on = ansi_highlight_red();
off = ansi_normal();
on = ansi_highlight_yellow();
off = ansi_normal();
} else
printf(" State: %s%s%s\n",
printf(" Since: %s; %s\n",
if (IN_SET(arg_transport,
static const char prefix[] = " ";
unsigned c;
c = columns();
if (c > sizeof(prefix) - 1)
c -= sizeof(prefix) - 1;
else
c = 0;
show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, false, get_output_flags());
}
return 0;
}
bool ellipsized = false;
int r, ret = 0;
log_error("This command expects one or more unit names. Did you mean --help?");
return -EINVAL;
}
if (show_status)
/* Increase max number of open files to 16K if we can, we
* might needs this when browsing journal files, which might
* be split up into many files. */
if (r < 0)
return r;
/* If no argument is specified inspect the manager itself */
return show_one(argv[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line, &ellipsized);
new_line = true;
if (arg_all)
} else {
char **name;
return log_oom();
continue;
} else if (show_properties) {
/* Interpret as job id */
return log_oom();
} else {
/* Interpret as PID */
if (r < 0) {
ret = r;
continue;
}
}
if (r < 0)
return r;
else if (r > 0 && ret == 0)
ret = r;
}
if (!strv_isempty(patterns)) {
if (r < 0)
return log_error_errno(r, "Failed to expand names: %m");
_cleanup_free_ char *unit;
if (!unit)
return log_oom();
if (r < 0)
return r;
else if (r > 0 && ret == 0)
ret = r;
}
}
}
if (ellipsized && !arg_quiet)
printf("Hint: Some lines were ellipsized, use -l to show in full.\n");
return ret;
}
int r;
if (arg_scope == UNIT_FILE_USER) {
r = user_config_home(user_home);
if (r < 0)
return log_error_errno(r, "Failed to query XDG_CONFIG_HOME: %m");
else if (r == 0)
r = user_runtime_dir(user_runtime);
if (r < 0)
return log_error_errno(r, "Failed to query XDG_CONFIG_HOME: %m");
else if (r == 0)
}
if (r < 0)
return log_error_errno(r, "Failed to query unit lookup paths: %m");
return 0;
}
_cleanup_close_ int fd;
if (fd < 0)
return -errno;
printf("%s%s# %s%s\n",
ansi_normal());
}
char **name;
bool first = true;
int r;
if (arg_transport != BUS_TRANSPORT_LOCAL) {
log_error("Cannot remotely cat units.");
return -EINVAL;
}
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return log_error_errno(r, "Failed to expand names: %m");
char **path;
if (r < 0)
return r;
else if (r == 0)
return -ENOENT;
if (first)
first = false;
else
puts("");
if (fragment_path) {
r = cat_file(fragment_path, false);
if (r < 0)
}
if (r < 0)
}
}
return 0;
}
_cleanup_free_ char *n = NULL;
char **i;
int r;
if (r < 0)
return r;
bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"SetUnitProperties");
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return log_error_errno(r, "Failed to mangle unit name: %m");
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
r = bus_append_unit_property_assignment(m, *i);
if (r < 0)
return 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);
if (r < 0)
return log_error_errno(r, "Failed to set unit properties on %s: %s", n, bus_error_message(&error, r));
return 0;
}
const char *method;
int r;
if (r < 0)
return r;
if (arg_action == ACTION_RELOAD)
method = "Reload";
else if (arg_action == ACTION_REEXEC)
method = "Reexecute";
else {
method =
/* "daemon-reload" */ "Reload";
}
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
NULL,
NULL);
/* There's always a fallback possible for
* legacy actions. */
r = -EADDRNOTAVAIL;
/* On reexecution, we expect a disconnect, not a
* reply */
r = 0;
else if (r < 0)
return r < 0 ? r : 0;
}
char **name;
int r, q;
if (argc <= 1)
if (r < 0)
return r;
if (r < 0)
return log_error_errno(r, "Failed to expand names: %m");
q = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ResetFailedUnit",
&error,
NULL,
"s", *name);
if (q < 0) {
log_error_errno(q, "Failed to reset failed state of unit %s: %s", *name, bus_error_message(&error, q));
if (r == 0)
r = q;
}
}
return r;
}
const char *text;
int r;
if (r < 0)
return r;
r = sd_bus_get_property(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Environment",
&error,
&reply,
"as");
if (r < 0)
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;
if (arg_transport != BUS_TRANSPORT_LOCAL) {
log_error("Cannot switch root remotely.");
return -EINVAL;
}
log_error("Wrong number of arguments.");
return -EINVAL;
}
if (argc >= 3)
else {
"init", &cmdline_init,
NULL);
if (r < 0)
log_debug_errno(r, "Failed to parse /proc/cmdline: %m");
init = cmdline_init;
}
if (init) {
/* If the passed init is actually the same as the
* systemd binary, then let's suppress it. */
}
if (r < 0)
return r;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"SwitchRoot",
&error,
NULL,
if (r < 0)
return 0;
}
const char *method;
int r;
if (r < 0)
return r;
? "SetEnvironment"
: "UnsetEnvironment";
bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
method);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return 0;
}
int r;
if (r < 0)
return r;
bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"SetEnvironment");
if (r < 0)
return bus_log_create_error(r);
if (argc < 2)
r = sd_bus_message_append_strv(m, environ);
else {
char **a, **b;
if (r < 0)
return bus_log_create_error(r);
if (!env_name_is_valid(*a)) {
log_error("Not a valid environment variable name: %s", *a);
return -EINVAL;
}
STRV_FOREACH(b, environ) {
const char *eq;
eq = startswith(*b, *a);
r = sd_bus_message_append(m, "s", *b);
if (r < 0)
return bus_log_create_error(r);
break;
}
}
}
r = sd_bus_message_close_container(m);
}
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return 0;
}
int r = 0;
#if defined(HAVE_SYSV_COMPAT)
unsigned f = 0;
if (arg_scope != UNIT_FILE_SYSTEM)
return 0;
if (getenv_bool("SYSTEMCTL_SKIP_SYSV") > 0)
return 0;
if (!STR_IN_SET(verb,
"enable",
"disable",
"is-enabled"))
return 0;
/* Processes all SysV units, and reshuffles the array so that
* afterwards only the native units remain */
if (r < 0)
return r;
r = 0;
while (args[f]) {
const char *name;
bool found_native = false, found_sysv;
unsigned c = 1;
char **k;
int j;
continue;
if (path_is_absolute(name))
continue;
if (!path)
return log_oom();
if (found_native)
break;
}
/* If we have both a native unit and a SysV script,
* native unit */
continue;
if (!p)
return log_oom();
if (!found_sysv)
continue;
if (found_native)
else
if (!l)
return log_oom();
log_info("Executing %s", l);
if (pid < 0)
else if (pid == 0) {
/* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
}
if (j < 0) {
log_error_errno(j, "Failed to wait for child: %m");
return j;
}
if (!arg_quiet)
puts("enabled");
r = 1;
} else {
if (!arg_quiet)
puts("disabled");
}
return -EINVAL;
} else
return -EPROTO;
if (found_native)
continue;
/* Remove this entry, so that we don't try enabling it as native unit */
assert(f > 0);
f--;
}
#endif
return r;
}
char **i, **l, **name;
int r;
if (!l)
return log_oom();
/* When enabling units qualified path names are OK,
* too, hence allow them explicitly. */
if (!*i) {
strv_free(l);
return log_oom();
}
} else {
if (r < 0) {
strv_free(l);
return log_error_errno(r, "Failed to mangle unit name: %m");
}
}
i++;
}
*i = NULL;
*mangled_names = l;
return 0;
}
unsigned n_changes = 0;
int carries_install_info = -1;
int r;
if (!argv[1])
return 0;
if (r < 0)
return r;
if (r < 0)
return r;
/* If the operation was fully executed by the SysV compat,
* let's finish early */
if (strv_isempty(names))
return 0;
if (install_client_side()) {
carries_install_info = r;
carries_install_info = r;
r = unit_file_preset(arg_scope, arg_runtime, arg_root, names, arg_preset_mode, arg_force, &changes, &n_changes);
carries_install_info = r;
else
assert_not_reached("Unknown verb");
if (r == -ESHUTDOWN)
return log_error_errno(r, "Unit file is masked.");
if (r < 0)
return log_error_errno(r, "Operation failed: %m");
if (!arg_quiet)
r = 0;
} else {
int expect_carries_install_info = false;
bool send_force = true, send_preset_mode = false;
const char *method;
if (r < 0)
return r;
method = "EnableUnitFiles";
expect_carries_install_info = true;
method = "DisableUnitFiles";
send_force = false;
method = "ReenableUnitFiles";
expect_carries_install_info = true;
method = "LinkUnitFiles";
if (arg_preset_mode != UNIT_FILE_PRESET_FULL) {
method = "PresetUnitFilesWithMode";
send_preset_mode = true;
} else
method = "PresetUnitFiles";
expect_carries_install_info = true;
method = "MaskUnitFiles";
method = "UnmaskUnitFiles";
send_force = false;
} else
assert_not_reached("Unknown verb");
bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
method);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append_strv(m, names);
if (r < 0)
return bus_log_create_error(r);
if (send_preset_mode) {
if (r < 0)
return bus_log_create_error(r);
}
if (r < 0)
return bus_log_create_error(r);
if (send_force) {
if (r < 0)
return bus_log_create_error(r);
}
if (r < 0)
if (expect_carries_install_info) {
if (r < 0)
return bus_log_parse_error(r);
}
if (r < 0)
return r;
/* Try to reload if enabled */
if (!arg_no_reload)
else
r = 0;
}
if (carries_install_info == 0)
log_warning("The unit files have no [Install] section. They are not meant to be enabled\n"
"using systemctl.\n"
"Possible reasons for having this kind of units are:\n"
"1) A unit may be statically enabled by being symlinked from another unit's\n"
" .wants/ or .requires/ directory.\n"
"2) A unit's purpose may be to act as a helper for some other unit which has\n"
" a requirement dependency on it.\n"
"3) A unit may be started when needed via activation (socket, path, timer,\n"
" D-Bus, udev, scripted systemctl call, ...).\n");
unsigned i;
if (r < 0)
goto finish;
for (i = 0; i < n_changes; i++)
}
return r;
}
int r = 0;
if (!argv[1])
return 0;
if (r < 0)
return log_error_errno(r, "Failed to mangle unit name: %m");
if (r < 0)
return r;
dep = UNIT_WANTS;
dep = UNIT_REQUIRES;
else
assert_not_reached("Unknown verb");
if (install_client_side()) {
unsigned n_changes = 0;
r = unit_file_add_dependency(arg_scope, arg_runtime, arg_root, names, target, dep, arg_force, &changes, &n_changes);
if (r == -ESHUTDOWN)
return log_error_errno(r, "Unit file is masked.");
if (r < 0)
return log_error_errno(r, "Can't add dependency: %m");
if (!arg_quiet)
} else {
if (r < 0)
return r;
bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"AddDependencyUnitFiles");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append_strv(m, names);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
if (r < 0)
return r;
if (!arg_no_reload)
else
r = 0;
}
return r;
}
unsigned n_changes = 0;
int r;
if (install_client_side()) {
r = unit_file_preset_all(arg_scope, arg_runtime, arg_root, arg_preset_mode, arg_force, &changes, &n_changes);
if (r < 0) {
log_error_errno(r, "Operation failed: %m");
goto finish;
}
if (!arg_quiet)
r = 0;
} else {
if (r < 0)
return r;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"PresetAllUnitFiles",
&error,
&reply,
"sbb",
if (r < 0)
if (r < 0)
return r;
if (!arg_no_reload)
else
r = 0;
}
return r;
}
bool enabled;
char **name;
int r;
if (r < 0)
return r;
if (r < 0)
return r;
enabled = r > 0;
if (install_client_side()) {
if (r < 0)
enabled = true;
if (!arg_quiet)
}
} else {
if (r < 0)
return r;
const char *s;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnitFileState",
&error,
&reply,
"s", *name);
if (r < 0)
return log_error_errno(r, "Failed to get unit file state for %s: %s", *name, bus_error_message(&error, r));
if (r < 0)
return bus_log_parse_error(r);
enabled = true;
if (!arg_quiet)
puts(s);
}
}
return !enabled;
}
int r;
if (!arg_quiet)
puts("offline");
return EXIT_FAILURE;
}
if (r < 0)
return r;
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"SystemState",
NULL,
&state);
if (r < 0) {
if (!arg_quiet)
puts("unknown");
return 0;
}
if (!arg_quiet)
}
static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) {
_cleanup_free_ char *t = NULL;
int r;
if (r < 0)
if (r < 0)
if (r == -ENOENT) {
r = touch(t);
if (r < 0)
return log_error_errno(r, "Failed to create temporary file \"%s\": %m", t);
} else if (r < 0)
*ret_tmp_fn = t;
t = NULL;
return 0;
}
static int get_file_to_edit(const char *name, const char *user_home, const char *user_runtime, char **ret_path) {
switch (arg_scope) {
case UNIT_FILE_SYSTEM:
if (arg_runtime)
break;
case UNIT_FILE_GLOBAL:
if (arg_runtime)
break;
case UNIT_FILE_USER:
if (arg_runtime) {
if (!path2)
return log_oom();
}
break;
default:
assert_not_reached("Invalid scope");
}
return log_oom();
if (arg_runtime) {
return -EEXIST;
}
log_error("Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.", run, path2);
return -EEXIST;
}
} else {
}
return 0;
}
static int unit_file_create_dropin(const char *unit_name, const char *user_home, const char *user_runtime, char **ret_new_path, char **ret_tmp_path) {
int r;
if (r < 0)
return r;
if (r < 0) {
return r;
}
return 0;
}
static int unit_file_create_copy(
const char *unit_name,
const char *fragment_path,
const char *user_home,
const char *user_runtime,
char **ret_new_path,
char **ret_tmp_path) {
char *tmp_new_path, *tmp_tmp_path;
int r;
if (r < 0)
return r;
char response;
r = ask_char(&response, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", tmp_new_path, fragment_path);
if (r < 0) {
return r;
}
if (response != 'y') {
return -1;
}
}
if (r < 0) {
return r;
}
return 0;
}
static int run_editor(char **paths) {
int r;
if (pid < 0)
if (pid == 0) {
const char **args;
char **tmp_path, **original_path, *p;
unsigned n_editor_args = 0, i = 1;
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
/* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL
* If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present,
* we try to execute well known editors
*/
if (!editor)
if (!editor)
if (!editor_args) {
(void) log_oom();
}
}
if (n_editor_args > 0) {
args[0] = editor_args[0];
for (; i < n_editor_args; i++)
args[i] = editor_args[i];
}
i++;
}
if (n_editor_args > 0)
args[0] = p;
/* We do not fail if the editor doesn't exist
* because we want to try each one of them before
* failing.
*/
}
}
log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL.");
}
if (r < 0)
return log_error_errno(r, "Failed to wait for child: %m");
return 0;
}
char **name;
int r;
if (r < 0)
return r;
if (r < 0)
return r;
else if (r == 0)
return -ENOENT;
else if (!path) {
// FIXME: support units with path==NULL (no FragmentPath)
return -ENOENT;
}
if (arg_full)
else
if (r < 0)
return r;
if (r < 0)
return log_oom();
}
return 0;
}
int r;
if (!on_tty()) {
log_error("Cannot edit units if not on a tty.");
return -EINVAL;
}
if (arg_transport != BUS_TRANSPORT_LOCAL) {
log_error("Cannot edit units remotely.");
return -EINVAL;
}
if (r < 0)
return r;
if (r < 0)
return log_error_errno(r, "Failed to expand names: %m");
if (r < 0)
return r;
if (strv_isempty(paths))
return -ENOENT;
r = run_editor(paths);
if (r < 0)
goto end;
/* If the temporary file is empty we ignore it. It's
* useful if the user wants to cancel its modification
*/
if (null_or_empty_path(*tmp)) {
continue;
}
if (r < 0) {
goto end;
}
}
r = 0;
if (!arg_no_reload && !install_client_side())
end:
return r;
}
static void systemctl_help(void) {
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
"Query or send control commands to the systemd manager.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --system Connect to system manager\n"
" --user Connect to user service manager\n"
" -H --host=[USER@]HOST\n"
" Operate on remote host\n"
" -M --machine=CONTAINER\n"
" Operate on local container\n"
" -t --type=TYPE List units of a particular type\n"
" --state=STATE List units with particular LOAD or SUB or ACTIVE state\n"
" -p --property=NAME Show only properties by this name\n"
" -a --all Show all loaded units/properties, including dead/empty\n"
" ones. To list all units installed on the system, use\n"
" the 'list-unit-files' command instead.\n"
" -l --full Don't ellipsize unit names on output\n"
" -r --recursive Show unit list of host and local containers\n"
" --reverse Show reverse dependencies with 'list-dependencies'\n"
" --job-mode=MODE Specify how to deal with already queued jobs, when\n"
" queueing a new job\n"
" --show-types When showing sockets, explicitly show their type\n"
" -i --ignore-inhibitors\n"
" When shutting down or sleeping, ignore inhibitors\n"
" --kill-who=WHO Who to send signal to\n"
" -s --signal=SIGNAL Which signal to send\n"
" --now Start or stop unit in addition to enabling or disabling it\n"
" -q --quiet Suppress output\n"
" --no-block Do not wait until operation finished\n"
" --no-reload Don't reload daemon after en-/dis-abling unit files\n"
" --no-legend Do not print a legend (column headers and hints)\n"
" --no-pager Do not pipe output into a pager\n"
" --no-ask-password\n"
" Do not ask for system passwords\n"
" --runtime Enable unit files only temporarily until next reboot\n"
" -f --force When enabling unit files, override existing symlinks\n"
" When shutting down, execute action immediately\n"
" --preset-mode= Apply only enable, only disable, or all presets\n"
" --root=PATH Enable unit files in the specified root directory\n"
" -n --lines=INTEGER Number of journal entries to show\n"
" -o --output=STRING Change journal output mode (short, short-iso,\n"
" short-precise, short-monotonic, verbose,\n"
" export, json, json-pretty, json-sse, cat)\n"
" --firmware-setup Tell the firmware to show the setup menu on next boot\n"
" --plain Print unit dependencies as a list instead of a tree\n\n"
"Unit Commands:\n"
" list-units [PATTERN...] List loaded units\n"
" list-sockets [PATTERN...] List loaded sockets ordered by address\n"
" list-timers [PATTERN...] List loaded timers ordered by next elapse\n"
" start NAME... Start (activate) one or more units\n"
" stop NAME... Stop (deactivate) one or more units\n"
" reload NAME... Reload one or more units\n"
" restart NAME... Start or restart one or more units\n"
" try-restart NAME... Restart one or more units if active\n"
" reload-or-restart NAME... Reload one or more units if possible,\n"
" otherwise start or restart\n"
" reload-or-try-restart NAME... Reload one or more units if possible,\n"
" otherwise restart if active\n"
" isolate NAME Start one unit and stop all others\n"
" kill NAME... Send signal to processes of a unit\n"
" is-active PATTERN... Check whether units are active\n"
" is-failed PATTERN... Check whether units are failed\n"
" status [PATTERN...|PID...] Show runtime status of one or more units\n"
" show [PATTERN...|JOB...] Show properties of one or more\n"
" cat PATTERN... Show files and drop-ins of one or more units\n"
" set-property NAME ASSIGNMENT... Sets one or more properties of a unit\n"
" help PATTERN...|PID... Show manual for one or more units\n"
" reset-failed [PATTERN...] Reset failed state for all, one, or more\n"
" units\n"
" list-dependencies [NAME] Recursively show units which are required\n"
" or wanted by this unit or by which this\n"
" unit is required or wanted\n\n"
"Unit File Commands:\n"
" list-unit-files [PATTERN...] List installed unit files\n"
" enable NAME... Enable one or more unit files\n"
" disable NAME... Disable one or more unit files\n"
" reenable NAME... Reenable one or more unit files\n"
" based on preset configuration\n"
" preset configuration\n"
" is-enabled NAME... Check whether unit files are enabled\n"
" mask NAME... Mask one or more units\n"
" unmask NAME... Unmask one or more units\n"
" link PATH... Link one or more units files into\n"
" the search path\n"
" add-wants TARGET NAME... Add 'Wants' dependency for the target\n"
" on specified one or more units\n"
" add-requires TARGET NAME... Add 'Requires' dependency for the target\n"
" on specified one or more units\n"
" edit NAME... Edit one or more unit files\n"
" get-default Get the name of the default target\n"
" set-default NAME Set the default target\n\n"
"Machine Commands:\n"
" list-machines [PATTERN...] List local containers and host\n\n"
"Job Commands:\n"
" list-jobs [PATTERN...] List jobs\n"
" cancel [JOB...] Cancel all, one, or more jobs\n\n"
"Environment Commands:\n"
" show-environment Dump environment\n"
" set-environment NAME=VALUE... Set one or more environment variables\n"
" unset-environment NAME... Unset one or more environment variables\n"
" import-environment [NAME...] Import all or some environment variables\n\n"
"Manager Lifecycle Commands:\n"
" daemon-reload Reload systemd manager configuration\n"
" daemon-reexec Reexecute systemd manager\n\n"
"System Commands:\n"
" is-system-running Check whether system is fully running\n"
" default Enter system default mode\n"
" rescue Enter system rescue mode\n"
" emergency Enter system emergency mode\n"
" halt Shut down and halt the system\n"
" poweroff Shut down and power-off the system\n"
" reboot [ARG] Shut down and reboot the system\n"
" kexec Shut down and reboot the system with kexec\n"
" exit [EXIT_CODE] Request user instance or container exit\n"
" switch-root ROOT [INIT] Change to a different root file system\n"
" suspend Suspend the system\n"
" hibernate Hibernate the system\n"
" hybrid-sleep Hibernate and suspend the system\n",
}
static void halt_help(void) {
printf("%s [OPTIONS...]%s\n\n"
"%s the system.\n\n"
" --help Show this help\n"
" --halt Halt the machine\n"
" -p --poweroff Switch off the machine\n"
" --reboot Reboot the machine\n"
" -d --no-wtmp Don't write wtmp record\n"
"Halt");
}
static void shutdown_help(void) {
printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
"Shut down the system.\n\n"
" --help Show this help\n"
" -H --halt Halt the machine\n"
" -P --poweroff Power-off the machine\n"
" -r --reboot Reboot the machine\n"
" -h Equivalent to --poweroff, overridden by --halt\n"
" -c Cancel a pending shutdown\n",
}
static void telinit_help(void) {
printf("%s [OPTIONS...] {COMMAND}\n\n"
"Send control commands to the init daemon.\n\n"
" --help Show this help\n"
"Commands:\n"
" 0 Power-off the machine\n"
" 6 Reboot the machine\n"
" 2, 3, 4, 5 Start runlevelX.target unit\n"
" 1, s, S Enter rescue mode\n"
" q, Q Reload init daemon configuration\n"
" u, U Reexecute init daemon\n",
}
static void runlevel_help(void) {
printf("%s [OPTIONS...]\n\n"
"Prints the previous and current runlevel of the init system.\n\n"
" --help Show this help\n",
}
static void help_types(void) {
int i;
if (!arg_no_legend)
puts("Available unit types:");
for (i = 0; i < _UNIT_TYPE_MAX; i++)
puts(unit_type_to_string(i));
}
static void help_states(void) {
int i;
if (!arg_no_legend)
puts("Available unit load states:");
for (i = 0; i < _UNIT_LOAD_STATE_MAX; i++)
if (!arg_no_legend)
puts("\nAvailable unit active states:");
for (i = 0; i < _UNIT_ACTIVE_STATE_MAX; i++)
if (!arg_no_legend)
puts("\nAvailable automount unit substates:");
for (i = 0; i < _AUTOMOUNT_STATE_MAX; i++)
if (!arg_no_legend)
puts("\nAvailable busname unit substates:");
for (i = 0; i < _BUSNAME_STATE_MAX; i++)
if (!arg_no_legend)
puts("\nAvailable device unit substates:");
for (i = 0; i < _DEVICE_STATE_MAX; i++)
if (!arg_no_legend)
puts("\nAvailable mount unit substates:");
for (i = 0; i < _MOUNT_STATE_MAX; i++)
if (!arg_no_legend)
puts("\nAvailable path unit substates:");
for (i = 0; i < _PATH_STATE_MAX; i++)
puts(path_state_to_string(i));
if (!arg_no_legend)
puts("\nAvailable scope unit substates:");
for (i = 0; i < _SCOPE_STATE_MAX; i++)
if (!arg_no_legend)
puts("\nAvailable service unit substates:");
for (i = 0; i < _SERVICE_STATE_MAX; i++)
if (!arg_no_legend)
puts("\nAvailable slice unit substates:");
for (i = 0; i < _SLICE_STATE_MAX; i++)
if (!arg_no_legend)
puts("\nAvailable socket unit substates:");
for (i = 0; i < _SOCKET_STATE_MAX; i++)
if (!arg_no_legend)
puts("\nAvailable swap unit substates:");
for (i = 0; i < _SWAP_STATE_MAX; i++)
puts(swap_state_to_string(i));
if (!arg_no_legend)
puts("\nAvailable target unit substates:");
for (i = 0; i < _TARGET_STATE_MAX; i++)
if (!arg_no_legend)
puts("\nAvailable timer unit substates:");
for (i = 0; i < _TIMER_STATE_MAX; i++)
}
enum {
ARG_FAIL = 0x100,
};
{}
};
const char *p;
int c, r;
/* we default to allowing interactive authorization only in systemctl (not in the legacy commands) */
arg_ask_password = true;
switch (c) {
case 'h':
return 0;
case ARG_VERSION:
return version();
case 't': {
log_error("--type requires arguments.");
return -EINVAL;
}
p = optarg;
for(;;) {
if (r < 0)
if (r == 0)
break;
help_types();
return 0;
}
if (unit_type_from_string(type) >= 0) {
return log_oom();
continue;
}
/* It's much nicer to use --state= for
* load states, but let's support this
* in --types= too for compatibility
* with old versions */
if (unit_load_state_from_string(type) >= 0) {
return log_oom();
continue;
}
log_info("Use -t help to see a list of allowed values.");
return -EINVAL;
}
break;
}
case 'p': {
/* Make sure that if the empty property list
was specified, we won't show any properties. */
if (!arg_properties)
return log_oom();
} else {
p = optarg;
for(;;) {
if (r < 0)
if (r == 0)
break;
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 ARG_REVERSE:
break;
case ARG_AFTER:
break;
case ARG_BEFORE:
break;
case ARG_SHOW_TYPES:
arg_show_types = true;
break;
case ARG_JOB_MODE:
break;
case ARG_FAIL:
arg_job_mode = "fail";
break;
case ARG_IRREVERSIBLE:
arg_job_mode = "replace-irreversibly";
break;
case ARG_IGNORE_DEPENDENCIES:
arg_job_mode = "ignore-dependencies";
break;
case ARG_USER:
break;
case ARG_SYSTEM:
break;
case ARG_GLOBAL:
break;
case ARG_NO_BLOCK:
arg_no_block = true;
break;
case ARG_NO_LEGEND:
arg_no_legend = true;
break;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case ARG_NO_WALL:
arg_no_wall = true;
break;
case ARG_ROOT:
if (r < 0)
return r;
break;
case 'l':
arg_full = true;
break;
case ARG_FAILED:
return log_oom();
break;
case 'q':
arg_quiet = true;
break;
case ARG_FORCE:
arg_force ++;
break;
case 'f':
arg_force ++;
break;
case ARG_NO_RELOAD:
arg_no_reload = true;
break;
case ARG_KILL_WHO:
break;
case 's':
if (arg_signal < 0) {
return -EINVAL;
}
break;
case ARG_NO_ASK_PASSWORD:
arg_ask_password = false;
break;
case 'H':
break;
case 'M':
break;
case ARG_RUNTIME:
arg_runtime = true;
break;
case 'n':
return -EINVAL;
}
break;
case 'o':
if (arg_output < 0) {
return -EINVAL;
}
break;
case 'i':
arg_ignore_inhibitors = true;
break;
case ARG_PLAIN:
arg_plain = true;
break;
case ARG_FIRMWARE_SETUP:
arg_firmware_setup = true;
break;
case ARG_STATE: {
log_error("--signal requires arguments.");
return -EINVAL;
}
p = optarg;
for(;;) {
_cleanup_free_ char *s = NULL;
r = extract_first_word(&p, &s, ",", 0);
if (r < 0)
if (r == 0)
break;
if (streq(s, "help")) {
help_states();
return 0;
}
if (strv_push(&arg_states, s) < 0)
return log_oom();
s = NULL;
}
break;
}
case 'r':
if (geteuid() != 0) {
log_error("--recursive requires root privileges.");
return -EPERM;
}
arg_recursive = true;
break;
case ARG_PRESET_MODE:
if (arg_preset_mode < 0) {
return -EINVAL;
}
break;
case ARG_NOW:
arg_now = true;
break;
case ARG_MESSAGE:
return log_oom();
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
log_error("Cannot access user instance remotely.");
return -EINVAL;
}
return 1;
}
enum {
ARG_HELP = 0x100,
};
{}
};
int c, r, runlevel;
arg_force = 2;
switch (c) {
case ARG_HELP:
halt_help();
return 0;
case ARG_HALT:
break;
case 'p':
if (arg_action != ACTION_REBOOT)
break;
case ARG_REBOOT:
break;
case 'f':
arg_force = 2;
break;
case 'w':
arg_dry = true;
break;
case 'd':
arg_no_wtmp = true;
break;
case ARG_NO_WALL:
arg_no_wall = true;
break;
case 'i':
case 'h':
case 'n':
/* Compatibility nops */
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
if (r < 0)
return r;
log_error("Too many arguments.");
return -EINVAL;
}
return 1;
}
assert(t);
if (streq(t, "now"))
*_u = 0;
else if (!strchr(t, ':')) {
uint64_t u;
if (safe_atou64(t, &u) < 0)
return -EINVAL;
} else {
char *e = NULL;
time_t s;
usec_t n;
errno = 0;
return -EINVAL;
return -EINVAL;
n = now(CLOCK_REALTIME);
s = (time_t) (n / USEC_PER_SEC);
while (*_u <= n)
*_u += USEC_PER_DAY;
}
return 0;
}
enum {
ARG_HELP = 0x100,
};
{}
};
int c, r;
switch (c) {
case ARG_HELP:
return 0;
case 'H':
break;
case 'P':
break;
case 'r':
if (kexec_loaded())
else
break;
case 'K':
break;
case 'h':
if (arg_action != ACTION_HALT)
break;
case 'k':
arg_dry = true;
break;
case ARG_NO_WALL:
arg_no_wall = true;
break;
case 't':
case 'a':
case 'f':
case 'F':
/* Compatibility nops */
break;
case 'c':
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
if (r < 0) {
return r;
}
} else
/* No time argument for shutdown cancel */
/* We skip the time argument */
if (wall) {
if (!arg_wall)
return log_oom();
}
return 1;
}
enum {
ARG_HELP = 0x100,
};
{}
};
static const struct {
char from;
} table[] = {
{ '0', ACTION_POWEROFF },
{ '6', ACTION_REBOOT },
{ '1', ACTION_RESCUE },
{ '2', ACTION_RUNLEVEL2 },
{ '3', ACTION_RUNLEVEL3 },
{ '4', ACTION_RUNLEVEL4 },
{ '5', ACTION_RUNLEVEL5 },
{ 's', ACTION_RESCUE },
{ 'S', ACTION_RESCUE },
{ 'q', ACTION_RELOAD },
{ 'Q', ACTION_RELOAD },
{ 'u', ACTION_REEXEC },
{ 'U', ACTION_REEXEC }
};
unsigned i;
int c;
switch (c) {
case ARG_HELP:
telinit_help();
return 0;
case ARG_NO_WALL:
arg_no_wall = true;
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
return -EINVAL;
}
log_error("Too many arguments.");
return -EINVAL;
}
log_error("Expected single character argument.");
return -EINVAL;
}
for (i = 0; i < ELEMENTSOF(table); i++)
break;
if (i >= ELEMENTSOF(table)) {
return -EINVAL;
}
optind ++;
return 1;
}
enum {
ARG_HELP = 0x100,
};
{}
};
int c;
switch (c) {
case ARG_HELP:
return 0;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
log_error("Too many arguments.");
return -EINVAL;
}
return 1;
}
if (kexec_loaded())
else
if (sd_booted() > 0) {
} else {
/* Hmm, so some other init system is
* running, we need to forward this
* request to it. For now we simply
* guess that it is Upstart. */
log_error("Couldn't find an alternative telinit implementation to spawn.");
return -EIO;
}
}
}
}
_pure_ static int action_to_runlevel(void) {
static const char table[_ACTION_MAX] = {
[ACTION_HALT] = '0',
[ACTION_POWEROFF] = '0',
[ACTION_REBOOT] = '6',
[ACTION_RUNLEVEL2] = '2',
[ACTION_RUNLEVEL3] = '3',
[ACTION_RUNLEVEL4] = '4',
[ACTION_RUNLEVEL5] = '5',
[ACTION_RESCUE] = '1'
};
return table[arg_action];
}
static int talk_initctl(void) {
#ifdef HAVE_SYSV_COMPAT
struct init_request request = {
.magic = INIT_MAGIC,
.sleeptime = 0,
};
char rl;
int r;
rl = action_to_runlevel();
if (!rl)
return 0;
if (fd < 0) {
return 0;
}
if (r < 0)
return 1;
#else
return 0;
#endif
}
{}
};
}
static int reload_with_fallback(void) {
/* First, try systemd via D-Bus. */
return 0;
/* Nothing else worked, so let's try signals */
return 0;
}
static int start_with_fallback(void) {
/* First, try systemd via D-Bus. */
return 0;
/* Nothing else worked, so let's try
if (talk_initctl() > 0)
return 0;
log_error("Failed to talk to init daemon.");
return -EIO;
}
/* The kernel will automaticall flush ATA disks and suchlike
* on reboot(), but the file systems need to be synce'd
* explicitly in advance. */
(void) sync();
/* Make sure C-A-D is handled by the kernel from this point
* on... */
(void) reboot(RB_ENABLE_CAD);
switch (a) {
case ACTION_HALT:
log_info("Halting.");
(void) reboot(RB_HALT_SYSTEM);
return -errno;
case ACTION_POWEROFF:
log_info("Powering off.");
(void) reboot(RB_POWER_OFF);
return -errno;
case ACTION_KEXEC:
case ACTION_REBOOT: {
(void) syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, param);
}
log_info("Rebooting.");
(void) reboot(RB_AUTOBOOT);
return -errno;
}
default:
assert_not_reached("Unknown action.");
}
}
static int logind_schedule_shutdown(void) {
#ifdef HAVE_LOGIND
char date[FORMAT_TIMESTAMP_MAX];
const char *action;
int r;
(void) logind_set_wall_message();
if (r < 0)
return r;
switch (arg_action) {
case ACTION_HALT:
action = "halt";
break;
case ACTION_POWEROFF:
action = "poweroff";
break;
case ACTION_KEXEC:
action = "kexec";
break;
case ACTION_EXIT:
action = "exit";
break;
case ACTION_REBOOT:
default:
action = "reboot";
break;
}
if (arg_dry)
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ScheduleShutdown",
&error,
NULL,
"st",
arg_when);
if (r < 0)
return log_warning_errno(r, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s", bus_error_message(&error, r));
log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.", format_timestamp(date, sizeof(date), arg_when));
return 0;
#else
log_error("Cannot schedule shutdown without logind support, proceeding with immediate shutdown.");
return -ENOSYS;
#endif
}
static int halt_main(void) {
int r;
if (r < 0)
return r;
if (arg_when > 0)
return logind_schedule_shutdown();
if (geteuid() != 0) {
log_error("Must be root.");
return -EPERM;
}
/* Try logind if we are a normal user and no special
* mode applies. Maybe PolicyKit allows us to shutdown
* the machine. */
r = logind_reboot(arg_action);
if (r >= 0)
return r;
/* requested operation is not
* supported on the local system or
* already in progress */
return r;
/* on all other errors, try low-level operation */
}
}
return start_with_fallback();
if (!arg_no_wtmp) {
if (sd_booted() > 0)
log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
else {
r = utmp_put_shutdown();
if (r < 0)
log_warning_errno(r, "Failed to write utmp record: %m");
}
}
if (arg_dry)
return 0;
r = halt_now(arg_action);
return log_error_errno(r, "Failed to reboot: %m");
}
static int runlevel_main(void) {
if (r < 0) {
puts("unknown");
return r;
}
printf("%c %c\n",
return 0;
}
static int logind_cancel_shutdown(void) {
#ifdef HAVE_LOGIND
int r;
if (r < 0)
return r;
(void) logind_set_wall_message();
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"CancelScheduledShutdown",
&error,
if (r < 0)
return log_warning_errno(r, "Failed to talk to logind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r));
return 0;
#else
log_error("Not compiled with logind support, cannot cancel scheduled shutdowns.");
return -ENOSYS;
#endif
}
int r;
log_open();
/* Explicitly not on_tty() to avoid setting cached value.
* This becomes relevant for piping output which might be
* ellipsized. */
if (r <= 0)
goto finish;
log_info("Running in chroot, ignoring request.");
r = 0;
goto finish;
}
/* systemctl_main() will print an error message for the bus
* connection, but only if it needs to */
switch (arg_action) {
case ACTION_SYSTEMCTL:
break;
case ACTION_HALT:
case ACTION_POWEROFF:
case ACTION_REBOOT:
case ACTION_KEXEC:
r = halt_main();
break;
case ACTION_RUNLEVEL2:
case ACTION_RUNLEVEL3:
case ACTION_RUNLEVEL4:
case ACTION_RUNLEVEL5:
case ACTION_RESCUE:
case ACTION_EMERGENCY:
case ACTION_DEFAULT:
r = start_with_fallback();
break;
case ACTION_RELOAD:
case ACTION_REEXEC:
r = reload_with_fallback();
break;
case ACTION_CANCEL_SHUTDOWN:
r = logind_cancel_shutdown();
break;
case ACTION_RUNLEVEL:
r = runlevel_main();
break;
case _ACTION_INVALID:
default:
assert_not_reached("Unknown action");
}
pager_close();
/* Note that we return r here, not EXIT_SUCCESS, so that we can implement the LSB-like return codes */
return r < 0 ? EXIT_FAILURE : r;
}