systemctl.c revision 0f0467e63b0e0688ae9edb1512c1a2637d62ddb4
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster This file is part of systemd.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Copyright 2010 Lennart Poettering
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Copyright 2013 Marc-Antoine Perennou
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster systemd is free software; you can redistribute it and/or modify it
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster under the terms of the GNU Lesser General Public License as published by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster the Free Software Foundation; either version 2.1 of the License, or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (at your option) any later version.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster systemd is distributed in the hope that it will be useful, but
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster WITHOUT ANY WARRANTY; without even the implied warranty of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Lesser General Public License for more details.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster You should have received a copy of the GNU Lesser General Public License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster along with systemd; If not, see <http://www.gnu.org/licenses/>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_all = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_no_block = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_no_legend = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_no_pager = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_no_wtmp = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_no_wall = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_no_reload = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_show_types = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_ignore_inhibitors = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_dry = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_quiet = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_full = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_recursive = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic int arg_force = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_ask_password = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic bool arg_runtime = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL;
static bool arg_plain = false;
static bool arg_firmware_setup = false;
static bool arg_now = false;
static bool original_stdout_is_tty;
return NULL;
static void pager_open_if_enabled(void) {
if (arg_no_pager)
pager_open(false);
static void ask_password_agent_open_if_enabled(void) {
if (!arg_ask_password)
static void polkit_agent_open_if_enabled(void) {
if (!arg_ask_password)
return EXIT_NOPERMISSION;
return EXIT_NOTINSTALLED;
return EXIT_NOTIMPLEMENTED;
return EXIT_NOTCONFIGURED;
return EXIT_FAILURE;
if (arg_no_wall)
if (arg_wall) {
_cleanup_free_ char *p;
log_oom();
if (!table[a])
static bool avoid_bus(void) {
if (running_in_chroot() > 0)
if (sd_booted() <= 0)
static int compare_unit_info(const void *a, const void *b) {
const UnitInfo *u = a, *v = b;
if (arg_types) {
const char *dot;
if (!dot)
if (arg_all)
if (u->job_id > 0)
const UnitInfo *u;
unsigned n_shown = 0;
int job_count = 0;
desc_len = 0;
if (u->job_id != 0) {
job_count++;
if (!arg_no_legend &&
unsigned basic_len;
if (job_count)
if (extra_len > 0) {
const char *id;
bool circle = false;
if (circle_len > 0)
if (job_count)
n_shown++;
circle = true;
circle = true;
if (u->machine) {
return log_oom();
id = j;
if (arg_full) {
return log_oom();
id = e;
if (circle_len > 0)
if (desc_len > 0)
if (!arg_no_legend) {
if (n_shown) {
if (arg_all)
static int get_unit_list(
const char *machine,
char **patterns,
UnitInfo u;
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
return bus_log_create_error(r);
return bus_log_create_error(r);
return bus_log_parse_error(r);
return log_oom();
(*unit_infos)[c++] = u;
return bus_log_parse_error(r);
return bus_log_parse_error(r);
sd_bus_message *m;
static int get_unit_list_recursive(
char **patterns,
char ***_machines) {
if (!replies)
return log_oom();
if (arg_recursive) {
static int get_triggered_units(
const char* path,
char*** ret) {
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
&error,
ret);
static int get_listening(
const char* unit_path,
char*** listening) {
r = sd_bus_get_property(
bus,
"org.freedesktop.systemd1",
"org.freedesktop.systemd1.Socket",
&error,
&reply,
return bus_log_parse_error(r);
return log_oom();
return log_oom();
return bus_log_parse_error(r);
return bus_log_parse_error(r);
struct socket_info {
const char *machine;
const char* id;
char* type;
char* path;
char** triggered;
bool own_triggered;
assert(a);
assert(b);
struct socket_info *s;
unsigned tmp = 0;
if (arg_show_types)
if (cs) {
if (!arg_no_legend)
const char *path;
if (s->machine) {
return log_oom();
path = j;
if (arg_show_types)
if (!arg_no_legend)
if (!arg_no_legend) {
if (!arg_all)
const UnitInfo *u;
struct socket_info *s;
unsigned cs = 0;
goto cleanup;
goto cleanup;
r = log_oom();
goto cleanup;
.own_triggered = i==0,
cs += c;
if (s->own_triggered)
static int get_next_elapse(
const char *path,
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Timer",
&error,
&t.monotonic);
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Timer",
&error,
&t.realtime);
*next = t;
static int get_last_trigger(
const char *path,
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Timer",
&error,
last);
struct timer_info {
const char* machine;
const char* id;
char** triggered;
assert(a);
assert(b);
struct timer_info *t;
unsigned ul = 0;
if (t->next_elapse > 0) {
if (t->last_trigger > 0) {
if (!arg_no_legend)
const char *unit;
if (t->machine) {
return log_oom();
unit = j;
if (!arg_no_legend)
if (!arg_no_legend) {
if (!arg_all)
return next_elapse;
struct timer_info *t;
const UnitInfo *u;
goto cleanup;
goto cleanup;
r = log_oom();
goto cleanup;
.next_elapse = m,
static int compare_unit_file_list(const void *a, const void *b) {
const UnitFileList *u = a, *v = b;
const char *dot;
if (!dot)
const UnitFileList *u;
if (!arg_full) {
unsigned basic_cols;
if (!arg_no_legend)
const char *id;
if (!arg_no_legend)
const char *state;
char *path;
if (avoid_bus()) {
Hashmap *h;
UnitFileList *u;
Iterator i;
unsigned n_units;
return log_oom();
return log_oom();
HASHMAP_FOREACH(u, h, i) {
units[c++] = *u;
free(u);
hashmap_free(h);
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
&reply,
NULL);
return bus_log_parse_error(r);
return log_oom();
path,
return bus_log_parse_error(r);
return bus_log_parse_error(r);
if (avoid_bus()) {
if (!arg_plain) {
if (arg_full){
return log_oom();
if (!path)
return log_oom();
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
path,
&error,
&reply,
return bus_log_parse_error(r);
const char *prop;
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return strcasecmp(*a, *b);
static int list_dependencies_one(
const char *name,
int level,
char ***units,
unsigned int branches) {
return log_oom();
if (!arg_plain) {
if (arg_plain)
int state;
const char *on;
if (!arg_plain)
u = unit;
puts(u);
struct machine_info {
bool is_host;
char *name;
char *state;
char *control_group;
if (!machine_infos)
static int compare_machine_info(const void *a, const void *b) {
const struct machine_info *u = a, *v = b;
if (!bus) {
r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map, mi);
static int get_machine_list(
char **patterns) {
if (!hn)
return log_oom();
return log_oom();
sd_get_machine_names(&m);
STRV_FOREACH(i, m) {
return log_oom();
return log_oom();
struct machine_info *m;
circle_len = 0,
if (!arg_no_legend) {
if (circle_len > 0)
bool circle = false;
circle = true;
circle = true;
if (m->n_failed_units > 0) {
if (circle_len > 0)
if (m->is_host)
if (!arg_no_legend)
if (geteuid() != 0) {
return -EPERM;
const char *path;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
&reply,
NULL);
return bus_log_parse_error(r);
if (path)
for (i = 0; i < n_changes; i++) {
unsigned n_changes = 0;
if (!arg_quiet)
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
&reply,
if (!arg_no_reload)
struct job_info {
const struct job_info *j;
bool shorten = false;
if (!arg_no_legend) {
shorten = true;
if (!arg_no_legend)
if (!arg_no_legend) {
bool skipped = false;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
&reply,
NULL);
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;
return log_oom();
return bus_log_parse_error(r);
return bus_log_parse_error(r);
char **name;
q = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
NULL,
const char *path;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
NULL,
&reply,
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
NULL,
name,
if (!path)
return log_oom();
static int unit_find_paths(
const char *unit_name,
bool avoid_bus_cache,
char **fragment_path,
char ***dropin_paths) {
if (!unit)
return log_oom();
r = sd_bus_get_property(
bus,
"org.freedesktop.systemd1",
unit,
"org.freedesktop.systemd1.Unit",
&error,
r = sd_bus_message_read(
return bus_log_parse_error(r);
bus,
"org.freedesktop.systemd1",
unit,
"org.freedesktop.systemd1.Unit",
&error,
&path);
if (dropin_paths) {
bus,
"org.freedesktop.systemd1",
unit,
"org.freedesktop.systemd1.Unit",
&error,
&dropins);
if (!names)
return log_oom();
if (r < 0 && r != -EINVAL)
if (dropin_paths) {
const char *path;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
NULL,
&reply,
if (!quiet)
return bus_log_parse_error(r);
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
NULL,
&state);
if (!quiet)
if (!quiet)
static int check_triggering_units(
const char *name) {
bool print_warning_label = true;
if (!path)
return log_oom();
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
&error,
&state);
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
&error,
&triggered_by);
if (print_warning_label) {
print_warning_label = false;
const char *verb;
const char *method;
} unit_actions[] = {
uint i;
uint i;
static int start_unit_one(
const char *method,
const char *name,
const char *mode,
BusWaitForJobs *w) {
const char *path;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&reply,
const char *verb;
return -EADDRNOTAVAIL;
return bus_log_parse_error(r);
return log_oom();
char **name;
if (suffix)
if (string_is_glob(t))
return log_oom();
if (!bus)
return log_oom();
const char *target;
const char *verb;
const char *mode;
enum action i;
return _ACTION_INVALID;
char **name;
if (one_name)
if (!arg_no_block) {
if (!arg_no_block) {
#ifdef HAVE_LOGIND
const char *method;
if (!bus)
return -EIO;
case ACTION_REBOOT:
case ACTION_POWEROFF:
case ACTION_SUSPEND:
case ACTION_HIBERNATE:
case ACTION_HYBRID_SLEEP:
return -EINVAL;
r = sd_bus_call_method(
bus,
"/org/freedesktop/login1",
&error,
NULL,
return -ENOSYS;
#ifdef HAVE_LOGIND
if (!bus)
if (arg_when > 0)
if (geteuid() == 0)
if (!on_tty())
r = sd_bus_call_method(
bus,
"/org/freedesktop/login1",
NULL,
&reply,
NULL);
return bus_log_parse_error(r);
if (!sv)
return log_oom();
a == ACTION_HALT ||
a == ACTION_POWEROFF ||
a == ACTION_REBOOT ||
return bus_log_parse_error(r);
return bus_log_parse_error(r);
_cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.",
return -EPERM;
#ifdef HAVE_LOGIND
if (!arg_firmware_setup)
r = efi_set_reboot_to_firmware(true);
#ifdef HAVE_LOGIND
r = sd_bus_call_method(
bus,
"/org/freedesktop/login1",
&error,
NULL,
return -EINVAL;
enum action a;
return -EPERM;
(a == ACTION_HALT ||
a == ACTION_POWEROFF ||
a == ACTION_REBOOT))
return halt_now(a);
(a == ACTION_HALT ||
a == ACTION_POWEROFF ||
a == ACTION_REBOOT ||
a == ACTION_KEXEC ||
a == ACTION_EXIT))
if (geteuid() != 0 &&
(a == ACTION_POWEROFF ||
a == ACTION_REBOOT ||
a == ACTION_SUSPEND ||
a == ACTION_HIBERNATE ||
a == ACTION_HYBRID_SLEEP)) {
if (r == EXIT_SUCCESS)
warn_wall(a);
char **name;
int state;
if (state < 0)
return state;
if (state == 0)
r = code;
char **name;
if (!arg_kill_who)
q = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
NULL,
typedef struct ExecStatusInfo {
char *name;
char *path;
char **argv;
bool ignore;
int code;
int status;
assert(i);
free(i);
const char *path;
int ignore, r;
assert(m);
assert(i);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
if (!i->path)
return log_oom();
return bus_log_parse_error(r);
r = sd_bus_message_read(m,
&ignore,
&pid,
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
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;
const char *status_text;
const char *pid_file;
int status_errno;
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;
unsigned n_accepted;
unsigned n_connections;
bool accept;
char **listen;
const char *sysfs_path;
const char *where;
const char *what;
static void print_status_info(
UnitStatusInfo *i,
bool *ellipsized) {
ExecStatusInfo *p;
const char *path;
char **t, **t2;
assert(i);
if (i->following)
if (i->load_error)
else if (path)
bool last = false;
char ** dropin;
log_oom();
if (ss)
if (s1)
else if (s2)
if (i->failed_condition_trigger)
else if (i->failed_condition)
i->failed_condition,
if (i->failed_assert_trigger)
else if (i->failed_assert)
i->failed_assert,
if (i->sysfs_path)
if (i->where)
if (i->what)
if (i->accept)
bool good;
if (p->code == 0)
if (!good) {
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->control_pid > 0)
if (i->control_pid > 0) {
if (i->status_text)
if (i->status_errno > 0)
printf(" CPU: %s\n", format_timespan(buf, sizeof(buf), i->cpu_usage_nsec / NSEC_PER_USEC, USEC_PER_MSEC));
if (i->control_group &&
((arg_transport != BUS_TRANSPORT_LOCAL && arg_transport != BUS_TRANSPORT_MACHINE) || cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, i->control_group, false) == 0))) {
c = columns();
if (i->main_pid > 0)
if (i->control_pid > 0)
show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, false, extra, k, get_output_flags());
i->id,
getuid(),
if (i->need_daemon_reload)
assert(i);
if (!i->documentation) {
static int status_property(const char *name, sd_bus_message *m, UnitStatusInfo *i, const char *contents) {
assert(m);
assert(i);
switch (contents[0]) {
case SD_BUS_TYPE_STRING: {
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
i->control_group = e;
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;
case SD_BUS_TYPE_BOOLEAN: {
return bus_log_parse_error(r);
i->accept = b;
i->need_daemon_reload = b;
i->condition_result = b;
i->assert_result = b;
case SD_BUS_TYPE_UINT32: {
uint32_t u;
return bus_log_parse_error(r);
i->running = true;
i->n_accepted = u;
i->n_connections = u;
case SD_BUS_TYPE_INT32: {
int32_t j;
return bus_log_parse_error(r);
i->exit_code = (int) j;
i->exit_status = (int) j;
i->status_errno = (int) j;
case SD_BUS_TYPE_UINT64: {
uint64_t u;
return bus_log_parse_error(r);
i->memory_current = u;
i->memory_limit = u;
i->cpu_usage_nsec = u;
case SD_BUS_TYPE_ARRAY:
return bus_log_parse_error(r);
if (!info)
return log_oom();
log_oom();
if (!info)
log_oom();
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
goto skip;
case SD_BUS_TYPE_STRUCT_BEGIN:
const char *n, *message;
return bus_log_parse_error(r);
goto skip;
goto skip;
skip:
return bus_log_parse_error(r);
assert(m);
switch (contents[0]) {
case SD_BUS_TYPE_STRUCT_BEGIN:
uint32_t u;
return bus_log_parse_error(r);
else if (arg_all)
return bus_log_parse_error(r);
return bus_log_parse_error(r);
int whitelist;
return bus_log_parse_error(r);
return bus_log_parse_error(r);
r = sd_bus_message_read_strv(m, &l);
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
bool first = true;
if (!whitelist)
STRV_FOREACH(i, l) {
if (first)
first = false;
case SD_BUS_TYPE_ARRAY:
const char *path;
int ignore;
return bus_log_parse_error(r);
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
const char *base;
return bus_log_parse_error(r);
base,
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
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);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
const char *path;
return bus_log_parse_error(r);
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
} else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && (streq(name, "BlockIOReadBandwidth") || streq(name, "BlockIOWriteBandwidth"))) {
const char *path;
return bus_log_parse_error(r);
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
if (arg_all)
static int show_one(
const char *verb,
const char *path,
bool show_properties,
bool *new_line,
bool *ellipsized) {
ExecStatusInfo *p;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
path,
&error,
&reply,
return bus_log_parse_error(r);
if (*new_line)
*new_line = true;
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
if (show_properties)
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
if (!show_properties) {
static int get_unit_dbus_path_by_pid(
char **unit) {
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
&reply,
return bus_log_parse_error(r);
u = strdup(u);
return log_oom();
*unit = u;
static int show_all(
const char* verb,
bool show_properties,
bool *new_line,
bool *ellipsized) {
const UnitInfo *u;
int r, ret = 0;
return log_oom();
else if (r > 0 && ret == 0)
ret = r;
return ret;
if (!hn)
return log_oom();
r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map, &mi);
c = columns();
show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, false, get_output_flags());
bool ellipsized = false;
int r, ret = 0;
if (show_properties)
if (show_status)
return show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line, &ellipsized);
new_line = true;
if (arg_all)
char **name;
return log_oom();
} else if (show_properties) {
return log_oom();
ret = r;
else if (r > 0 && ret == 0)
ret = r;
if (!unit)
return log_oom();
else if (r > 0 && ret == 0)
ret = r;
return ret;
if (fd < 0)
return -errno;
char **name;
return -EINVAL;
char **path;
return -ENOENT;
if (first)
first = false;
if (fragment_path) {
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
return bus_log_create_error(r);
return bus_log_create_error(r);
return bus_log_create_error(r);
return bus_log_create_error(r);
r = bus_append_unit_property_assignment(m, *i);
r = sd_bus_message_close_container(m);
return bus_log_create_error(r);
r = sd_bus_message_close_container(m);
return bus_log_create_error(r);
const char *path;
return log_oom();
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
&reply,
return bus_log_parse_error(r);
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
&error,
&id);
if (!arg_quiet)
char **name;
q = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
NULL,
const char *method;
method =
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
NULL,
NULL);
r = -EADDRNOTAVAIL;
char **name;
q = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
NULL,
const char *text;
r = sd_bus_get_property(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
&reply,
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return bus_log_parse_error(r);
return -EINVAL;
NULL);
if (init) {
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
NULL,
const char *method;
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
method);
return bus_log_create_error(r);
return bus_log_create_error(r);
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
return bus_log_create_error(r);
return bus_log_create_error(r);
if (!env_name_is_valid(*a)) {
return -EINVAL;
const char *eq;
return bus_log_create_error(r);
r = sd_bus_message_close_container(m);
return bus_log_create_error(r);
#if defined(HAVE_SYSV_COMPAT)
while (args[f]) {
const char *name;
if (!path)
return log_oom();
if (found_native)
if (found_native)
return log_oom();
if (!found_sysv)
return log_oom();
if (pid < 0)
else if (pid == 0) {
if (!arg_quiet)
if (!arg_quiet)
return -EINVAL;
return -EPROTO;
assert(f > 0);
char **i, **l, **name;
return log_oom();
strv_free(l);
return log_oom();
strv_free(l);
*i = NULL;
*mangled_names = l;
unsigned n_changes = 0;
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;
goto finish;
if (!arg_quiet)
int expect_carries_install_info = false;
const char *method;
expect_carries_install_info = true;
send_force = false;
expect_carries_install_info = true;
send_preset_mode = true;
expect_carries_install_info = true;
send_force = false;
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
method);
return bus_log_create_error(r);
return bus_log_create_error(r);
if (send_preset_mode) {
return bus_log_create_error(r);
return bus_log_create_error(r);
if (send_force) {
return bus_log_create_error(r);
if (expect_carries_install_info) {
return bus_log_parse_error(r);
if (!arg_no_reload)
if (carries_install_info == 0)
for (i = 0; i < n_changes; i++)
unsigned n_changes = 0;
r = unit_file_add_dependency(arg_scope, arg_runtime, arg_root, names, target, dep, arg_force, &changes, &n_changes);
if (!arg_quiet)
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
return bus_log_create_error(r);
return bus_log_create_error(r);
r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force);
return bus_log_create_error(r);
if (!arg_no_reload)
unsigned n_changes = 0;
r = unit_file_preset_all(arg_scope, arg_runtime, arg_root, arg_preset_mode, arg_force, &changes, &n_changes);
goto finish;
if (!arg_quiet)
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
&reply,
if (!arg_no_reload)
bool enabled;
char **name;
enabled = r > 0;
if (state < 0)
enabled = true;
if (!arg_quiet)
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&error,
&reply,
return bus_log_parse_error(r);
enabled = true;
if (!arg_quiet)
puts(s);
return !enabled;
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
NULL,
&state);
if (!arg_quiet)
if (!arg_quiet)
static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) {
free(t);
if (r == -ENOENT) {
r = touch(t);
free(t);
free(t);
*ret_tmp_fn = t;
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)
case UNIT_FILE_GLOBAL:
if (arg_runtime)
case UNIT_FILE_USER:
if (arg_runtime) {
if (!path2)
return log_oom();
return log_oom();
if (arg_runtime) {
return log_error_errno(EEXIST, "Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.",
return log_error_errno(EEXIST, "Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.",
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) {
char *tmp_tmp_path;
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;
char *tmp_tmp_path;
char response;
r = ask_char(&response, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", tmp_new_path, fragment_path);
if (pid < 0) {
return -errno;
if (pid == 0) {
const char **args;
char *editor;
if (!editor)
if (!editor)
args[0] = p;
log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL.");
bool avoid_bus_cache;
char **name;
return -ENOENT;
else if (!path) {
return -ENOENT;
if (arg_full)
return log_oom();
if (!on_tty()) {
return -EINVAL;
return -EINVAL;
return -ENOENT;
goto end;
goto end;
end:
static void systemctl_help(void) {
" -a --all Show all loaded units/properties, including dead/empty\n"
" --no-reload Don't reload daemon after en-/dis-abling unit files\n"
static void halt_help(void) {
static void shutdown_help(void) {
static void telinit_help(void) {
" 2, 3, 4, 5 Start runlevelX.target unit\n"
static void runlevel_help(void) {
static void help_types(void) {
if (!arg_no_legend)
for (i = 0; i < _UNIT_TYPE_MAX; i++) {
t = unit_type_to_string(i);
puts(t);
case ARG_VERSION:
if (!type)
return -ENOMEM;
help_types();
return log_oom();
return log_oom();
return -EINVAL;
if (!arg_properties)
return log_oom();
char *prop;
if (!prop)
return log_oom();
return log_oom();
arg_all = true;
arg_all = true;
case ARG_REVERSE:
case ARG_AFTER:
case ARG_BEFORE:
case ARG_SHOW_TYPES:
arg_show_types = true;
case ARG_JOB_MODE:
case ARG_FAIL:
case ARG_IRREVERSIBLE:
case ARG_IGNORE_DEPENDENCIES:
case ARG_USER:
case ARG_SYSTEM:
case ARG_GLOBAL:
case ARG_NO_BLOCK:
arg_no_block = true;
case ARG_NO_LEGEND:
arg_no_legend = true;
case ARG_NO_PAGER:
arg_no_pager = true;
case ARG_NO_WALL:
arg_no_wall = true;
case ARG_ROOT:
arg_full = true;
case ARG_FAILED:
return log_oom();
arg_quiet = true;
case ARG_FORCE:
arg_force ++;
arg_force ++;
case ARG_NO_RELOAD:
arg_no_reload = true;
case ARG_KILL_WHO:
return -EINVAL;
case ARG_NO_ASK_PASSWORD:
arg_ask_password = false;
case ARG_RUNTIME:
arg_runtime = true;
return -EINVAL;
if (arg_output < 0) {
return -EINVAL;
arg_ignore_inhibitors = true;
case ARG_PLAIN:
arg_plain = true;
case ARG_FIRMWARE_SETUP:
arg_firmware_setup = true;
case ARG_STATE: {
return log_oom();
return log_oom();
if (geteuid() != 0) {
return -EPERM;
arg_recursive = true;
case ARG_PRESET_MODE:
if (arg_preset_mode < 0) {
return -EINVAL;
case ARG_NOW:
arg_now = true;
return -EINVAL;
return -EINVAL;
int c, r, runlevel;
case ARG_HELP:
halt_help();
case ARG_HALT:
case ARG_REBOOT:
arg_dry = true;
arg_no_wtmp = true;
case ARG_NO_WALL:
arg_no_wall = true;
return -EINVAL;
return -EINVAL;
assert(t);
*_u = 0;
uint64_t u;
if (safe_atou64(t, &u) < 0)
return -EINVAL;
char *e = NULL;
time_t s;
usec_t n;
errno = 0;
return -EINVAL;
return -EINVAL;
while (*_u <= n)
case ARG_HELP:
if (kexec_loaded())
arg_dry = true;
case ARG_NO_WALL:
arg_no_wall = true;
return -EINVAL;
char from;
} table[] = {
case ARG_HELP:
telinit_help();
case ARG_NO_WALL:
arg_no_wall = true;
return -EINVAL;
return -EINVAL;
return -EINVAL;
return -EINVAL;
return -EINVAL;
optind ++;
case ARG_HELP:
return -EINVAL;
return -EINVAL;
if (kexec_loaded())
if (sd_booted() > 0) {
return -EIO;
static int talk_initctl(void) {
.sleeptime = 0,
char rl;
if (!rl)
if (fd < 0) {
return -errno;
const char* verb;
MORE,
LESS,
} argc_cmp;
const int argc;
} bus;
} verbs[] = {
int left;
if (left > 0) {
return -EINVAL;
goto found;
return -EINVAL;
case EQUAL:
return -EINVAL;
case MORE:
return -EINVAL;
case LESS:
return -EINVAL;
return -EIO;
if (running_in_chroot() > 0) {
return -EIO;
if (bus) {
if (bus) {
goto done;
if (talk_initctl() > 0)
goto done;
return -EIO;
done:
sync();
case ACTION_HALT:
return -errno;
case ACTION_POWEROFF:
return -errno;
case ACTION_REBOOT: {
return -errno;
if (geteuid() != 0) {
if (arg_when <= 0 &&
!arg_dry &&
arg_force <= 0 &&
return -EPERM;
if (arg_when > 0) {
if (avoid_bus()) {
return -ENOSYS;
r = sd_bus_open_system(&b);
return log_oom();
r = sd_bus_set_property(
"/org/freedesktop/login1",
&error,
r = sd_bus_set_property(
"/org/freedesktop/login1",
&error,
r = sd_bus_call_method(
"/org/freedesktop/login1",
&error,
NULL,
arg_when);
log_warning_errno(r, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s",
if (!arg_no_wtmp) {
if (sd_booted() > 0)
r = utmp_put_shutdown();
if (arg_dry)
static int runlevel_main(void) {
log_open();
goto finish;
r = runlevel_main();
goto finish;
goto finish;
if (!avoid_bus())
if (bus)
switch (arg_action) {
case ACTION_SYSTEMCTL:
case ACTION_HALT:
case ACTION_POWEROFF:
case ACTION_REBOOT:
case ACTION_KEXEC:
case ACTION_RUNLEVEL2:
case ACTION_RUNLEVEL3:
case ACTION_RUNLEVEL4:
case ACTION_RUNLEVEL5:
case ACTION_RESCUE:
case ACTION_EMERGENCY:
case ACTION_DEFAULT:
case ACTION_RELOAD:
case ACTION_REEXEC:
case ACTION_CANCEL_SHUTDOWN: {
if (avoid_bus()) {
return -ENOSYS;
r = sd_bus_open_system(&b);
if (arg_wall) {
r = log_oom();
goto finish;
r = sd_bus_set_property(
"/org/freedesktop/login1",
&error,
r = sd_bus_set_property(
"/org/freedesktop/login1",
&error,
r = sd_bus_call_method(
"/org/freedesktop/login1",
&error,
case ACTION_RUNLEVEL:
case _ACTION_INVALID:
pager_close();
return r < 0 ? EXIT_FAILURE : r;