systemctl.c revision bc2708414babc5c99bb8000e63c84e87606cc15d
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <getopt.h>
#include <locale.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>
#include <systemd/sd-daemon.h>
#include <systemd/sd-shutdown.h>
#include "log.h"
#include "util.h"
#include "macro.h"
#include "set.h"
#include "utmp-wtmp.h"
#include "special.h"
#include "initreq.h"
#include "path-util.h"
#include "strv.h"
#include "dbus-common.h"
#include "cgroup-show.h"
#include "cgroup-util.h"
#include "list.h"
#include "path-lookup.h"
#include "conf-parser.h"
#include "exit-status.h"
#include "bus-errors.h"
#include "build.h"
#include "unit-name.h"
#include "pager.h"
#include "spawn-ask-password-agent.h"
#include "spawn-polkit-agent.h"
#include "install.h"
#include "logs-show.h"
#include "path-util.h"
#include "socket-util.h"
static const char *arg_load_state = NULL;
static char **arg_property = NULL;
static bool arg_all = false;
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_dry = false;
static bool arg_quiet = false;
static bool arg_full = false;
static int arg_force = 0;
static bool arg_ask_password = true;
static bool arg_failed = false;
static bool arg_runtime = false;
static const char *arg_kill_who = NULL;
static int arg_signal = SIGTERM;
static enum action {
static enum dot {
static enum transport {
static unsigned arg_lines = 10;
static bool private_bus = false;
static void pager_open_if_enabled(void) {
if (arg_no_pager)
return;
pager_open();
}
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;
}
#ifdef HAVE_LOGIND
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;
}
#endif
static const char *ansi_highlight(bool b) {
if (!on_tty())
return "";
return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
}
static const char *ansi_highlight_red(bool b) {
if (!on_tty())
return "";
return b ? ANSI_HIGHLIGHT_RED_ON : ANSI_HIGHLIGHT_OFF;
}
static const char *ansi_highlight_green(bool b) {
if (!on_tty())
return "";
return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
}
if (!dbus_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 const char *table[_ACTION_MAX] = {
[ACTION_HALT] = "The system is going down for system halt NOW!",
[ACTION_REBOOT] = "The system is going down for reboot NOW!",
[ACTION_POWEROFF] = "The system is going down for power-off NOW!",
[ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
[ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
[ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!",
[ACTION_CANCEL_SHUTDOWN] = "The system shutdown has been cancelled NOW!"
};
if (arg_no_wall)
return;
if (arg_wall) {
char *p;
if (!p) {
log_error("Failed to join strings.");
return;
}
if (*p) {
free(p);
return;
}
free(p);
}
if (!table[a])
return;
}
static bool avoid_bus(void) {
if (running_in_chroot() > 0)
return true;
if (sd_booted() <= 0)
return true;
return true;
if (arg_scope == UNIT_FILE_GLOBAL)
return true;
return false;
}
struct unit_info {
const char *id;
const char *description;
const char *load_state;
const char *active_state;
const char *sub_state;
const char *following;
const char *unit_path;
const char *job_type;
const char *job_path;
};
static int compare_unit_info(const void *a, const void *b) {
const struct unit_info *u = a, *v = b;
int r;
return r;
}
}
static bool output_show_unit(const struct unit_info *u) {
const char *dot;
if (arg_failed)
}
const struct unit_info *u;
int job_count = 0;
desc_len = 0;
for (u = unit_infos; u < unit_infos + c; u++) {
if (!output_show_unit(u))
continue;
if (u->job_id != 0) {
job_count++;
}
}
if (!arg_full) {
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++) {
char *e;
const char *on_loaded, *off_loaded;
const char *on_active, *off_active;
if (!output_show_unit(u))
continue;
if (!n_shown && !arg_no_legend) {
if (job_count)
if (!arg_full && arg_no_pager)
else
}
n_shown++;
on_loaded = ansi_highlight_red(true);
off_loaded = ansi_highlight_red(false);
} else
on_active = ansi_highlight_red(true);
off_active = ansi_highlight_red(false);
} else
printf("%-*s %s%-6s%s %s%-*s %-*s%s %-*s",
if (!arg_full && arg_no_pager)
else
free(e);
}
if (!arg_no_legend) {
if (n_shown) {
printf("\nLOAD = 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.\n");
if (job_count)
printf("JOB = Pending job for the unit.\n");
puts("");
on = ansi_highlight(true);
off = ansi_highlight(false);
} else {
on = ansi_highlight_red(true);
off = ansi_highlight_red(false);
}
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",
}
}
int r;
unsigned c = 0, n_units = 0;
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListUnits",
&reply,
NULL,
if (r)
goto finish;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
struct unit_info *u;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
if (c >= n_units) {
struct unit_info *w;
if (!w) {
log_error("Failed to allocate unit array.");
r = -ENOMEM;
goto finish;
}
unit_infos = w;
}
u = unit_infos+c;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
c++;
}
if (c > 0) {
}
if (reply)
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;
}
}
static bool output_show_unit_file(const UnitFileList *u) {
const char *dot;
}
const UnitFileList *u;
if (!output_show_unit_file(u))
continue;
}
if (!arg_full) {
unsigned basic_cols;
if (basic_cols < (unsigned) columns())
} else
if (!arg_no_legend)
char *e;
const char *id;
if (!output_show_unit_file(u))
continue;
n_shown++;
if (u->state == UNIT_FILE_MASKED ||
u->state == UNIT_FILE_MASKED_RUNTIME ||
u->state == UNIT_FILE_DISABLED ||
u->state == UNIT_FILE_INVALID) {
on = ansi_highlight_red(true);
off = ansi_highlight_red(false);
} else if (u->state == UNIT_FILE_ENABLED) {
on = ansi_highlight_green(true);
off = ansi_highlight_green(false);
} else
printf("%-*s %s%-*s%s\n",
free(e);
}
if (!arg_no_legend)
}
int r;
unsigned c = 0, n_units = 0;
if (avoid_bus()) {
Hashmap *h;
UnitFileList *u;
Iterator i;
if (!h)
return log_oom();
if (r < 0) {
return r;
}
n_units = hashmap_size(h);
if (!units) {
return log_oom();
}
HASHMAP_FOREACH(u, h, i) {
free(u);
}
hashmap_free(h);
} else {
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListUnitFiles",
&reply,
NULL,
if (r)
goto finish;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
UnitFileList *u;
const char *state;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
if (c >= n_units) {
UnitFileList *w;
if (!w) {
log_error("Failed to allocate unit array.");
r = -ENOMEM;
goto finish;
}
units = w;
}
u = units+c;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
c++;
}
}
if (c > 0) {
}
r = 0;
if (reply)
return r;
}
static const char * const colors[] = {
"Requires", "[color=\"black\"]",
"RequiresOverridable", "[color=\"black\"]",
"Requisite", "[color=\"darkblue\"]",
"RequisiteOverridable", "[color=\"darkblue\"]",
"Wants", "[color=\"grey66\"]",
"Conflicts", "[color=\"red\"]",
"ConflictedBy", "[color=\"red\"]",
"After", "[color=\"green\"]"
};
const char *c = NULL;
unsigned i;
c = colors[i+1];
break;
}
if (!c)
return 0;
return 0;
switch (dbus_message_iter_get_arg_type(iter)) {
case DBUS_TYPE_ARRAY:
const char *s;
dbus_message_iter_get_basic(&sub, &s);
}
return 0;
}
}
return 0;
}
const char *interface = "org.freedesktop.systemd1.Unit";
int r;
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"GetAll",
&reply,
NULL,
if (r)
goto finish;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
const char *prop;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
}
if (reply)
return r;
}
int r;
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListUnits",
&reply,
NULL,
if (r)
goto finish;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
printf("digraph systemd {\n");
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
goto finish;
/* printf("\t\"%s\";\n", id); */
}
printf("}\n");
log_info(" Color legend: black = Requires\n"
" dark blue = Requisite\n"
" dark grey = Wants\n"
" red = Conflicts\n"
" green = After\n");
if (on_tty())
log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
"-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
r = 0;
if (reply)
return r;
}
int r;
unsigned k = 0;
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListJobs",
&reply,
NULL,
if (r)
goto finish;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
if (on_tty())
char *e;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
free(e);
k++;
}
if (on_tty())
printf("\n%u jobs listed.\n", k);
r = 0;
if (reply)
return r;
}
int r = 0;
char **name, *n;
n = unit_name_mangle(*name);
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"LoadUnit",
NULL,
NULL,
DBUS_TYPE_STRING, n ? &n : name,
free(n);
if (r)
goto finish;
}
return r;
}
int r = 0;
char **name;
unsigned id;
const char *path;
if (r < 0) {
goto finish;
}
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetJob",
&reply,
NULL,
if (r)
goto finish;
log_error("Failed to parse reply");
r = -EIO;
goto finish;
}
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Job",
"Cancel",
NULL,
NULL,
if (r)
goto finish;
}
return r;
}
dbus_bool_t b = FALSE;
const char
*interface = "org.freedesktop.systemd1.Unit",
*property = "NeedDaemonReload",
*path;
char *n;
int r;
/* We ignore all errors here, since this is used to show a warning only */
n = unit_name_mangle(unit);
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit",
&reply,
NULL,
DBUS_TYPE_STRING, n ? (const char**) &n : &unit,
free(n);
if (r)
goto finish;
goto finish;
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"Get",
&reply,
NULL,
if (r)
goto finish;
goto finish;
goto finish;
dbus_message_iter_get_basic(&sub, &b);
if (reply)
return b;
}
typedef struct WaitData {
char *name;
char *result;
} WaitData;
static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
assert(d);
log_debug("Got D-Bus request: %s.%s() on %s",
log_error("Warning! D-Bus connection terminated.");
dbus_bool_t success = true;
char *p;
free(p);
goto finish;
}
#ifndef LEGACY
char *p;
/* Compatibility with older systemd versions <
* 183 during upgrades. This should be dropped
* one day. */
free(p);
if (*result)
goto finish;
}
char *p;
/* Compatibility with older systemd versions <
* 19 during upgrades. This should be dropped
* one day */
free(p);
if (!success)
goto finish;
}
#endif
}
}
if (private_bus)
return 0;
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='JobRemoved',"
"path='/org/freedesktop/systemd1'",
&error);
if (dbus_error_is_set(&error)) {
return -EIO;
}
/* This is slightly dirty, since we don't undo the match registrations. */
return 0;
}
int r = 0;
WaitData d;
assert(s);
zero(d);
d.set = s;
return log_oom();
while (!set_isempty(s)) {
log_error("Disconnected from bus.");
return -ECONNREFUSED;
}
if (!d.result)
goto free_name;
if (!arg_quiet) {
log_error("Job for %s failed. See 'systemctl status %s' and 'journalctl -xn' for details.", strna(d.name), strna(d.name));
}
r = -ETIME;
r = -ECANCELED;
r = -EIO;
}
return r;
}
const char
*interface = "org.freedesktop.systemd1.Unit",
*property = "ActiveState";
const char *state;
int r;
char *n;
n = unit_name_mangle(name);
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit",
&reply,
NULL,
DBUS_TYPE_STRING, n ? &n : &name,
free(n);
if (r) {
puts("unknown");
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"Get",
&reply,
NULL,
if (r)
goto finish;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
if (!quiet)
r = 0;
else
r = 3; /* According to LSB: "program is not running" */
if (reply)
return r;
}
static void check_triggering_units(
const char *unit_name) {
char *service_trigger = NULL;
const char *interface = "org.freedesktop.systemd1.Unit",
*triggered_by_property = "TriggeredBy";
bool print_warning_label = true;
int r;
n = unit_name_mangle(unit_name);
if (!n) {
log_oom();
return;
}
if (!unit_path) {
log_oom();
return;
}
bus,
"org.freedesktop.systemd1",
"org.freedesktop.DBus.Properties",
"Get",
&reply,
NULL,
if (r)
return;
log_error("Failed to parse reply.");
return;
}
char **check_states = NULL;
log_error("Failed to parse reply.");
return;
}
if (r < 0)
return;
if (r == 0) {
if (print_warning_label) {
print_warning_label = false;
}
}
}
}
static int start_unit_one(
const char *method,
const char *name,
const char *mode,
Set *s) {
const char *path;
int r;
_cleanup_free_ char *n, *p = NULL;
n = unit_name_mangle(name);
if (!n)
return log_oom();
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&reply,
DBUS_TYPE_STRING, &n,
if (r) {
/* There's always a fallback possible for
* legacy actions. */
r = -EADDRNOTAVAIL;
else
return r;
}
return -EIO;
}
if (need_daemon_reload(bus, n))
log_warning("Warning: Unit file of %s changed on disk, 'systemctl %s daemon-reload' recommended.",
if (s) {
if (!p)
return log_oom();
r = set_put(s, p);
if (r < 0) {
log_error("Failed to add path to set.");
return r;
}
p = NULL;
}
return 0;
}
return ACTION_HALT;
return ACTION_POWEROFF;
return ACTION_REBOOT;
return ACTION_KEXEC;
return ACTION_RESCUE;
return ACTION_EMERGENCY;
return ACTION_DEFAULT;
return ACTION_EXIT;
return ACTION_SUSPEND;
return ACTION_HIBERNATE;
return ACTION_HYBRID_SLEEP;
else
return ACTION_INVALID;
}
static const char * const table[_ACTION_MAX] = {
};
int r, ret = 0;
char **name;
if (arg_action == ACTION_SYSTEMCTL) {
method =
"StartUnit";
mode =
} else {
method = "StartUnit";
arg_action == ACTION_RESCUE ||
arg_action == ACTION_RUNLEVEL2 ||
arg_action == ACTION_RUNLEVEL3 ||
arg_action == ACTION_RUNLEVEL4 ||
}
if (!arg_no_block) {
if (ret < 0) {
goto finish;
}
if (!s) {
goto finish;
}
}
if (one_name) {
if (ret < 0)
} else {
if (r < 0) {
}
}
}
if (!arg_no_block) {
r = wait_for_jobs(bus, s);
if (r < 0) {
ret = r;
goto finish;
}
/* When stopping units, warn if they can still be triggered by
* another active unit (socket, path, timer) */
if (one_name)
else
}
}
set_free_free(s);
return ret;
}
/* Ask systemd-logind, which might grant access to unprivileged users
* through PolicyKit */
#ifdef HAVE_LOGIND
const char *method;
dbus_bool_t interactive = true;
switch (a) {
case ACTION_REBOOT:
method = "Reboot";
break;
case ACTION_POWEROFF:
method = "PowerOff";
break;
case ACTION_SUSPEND:
method = "Suspend";
break;
case ACTION_HIBERNATE:
method = "Hibernate";
break;
case ACTION_HYBRID_SLEEP:
method = "HybridSleep";
break;
default:
return -EINVAL;
}
return bus_method_call_with_reply (
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
NULL,
NULL,
#else
return -ENOSYS;
#endif
}
enum action a;
int r;
a = verb_to_action(args[0]);
log_error("Must be root.");
return -EPERM;
}
if (arg_force >= 2 &&
(a == ACTION_HALT ||
a == ACTION_POWEROFF ||
a == ACTION_REBOOT))
halt_now(a);
if (arg_force >= 1 &&
(a == ACTION_HALT ||
a == ACTION_POWEROFF ||
a == ACTION_REBOOT ||
a == ACTION_KEXEC ||
a == ACTION_EXIT))
/* first try logind, to allow authentication with polkit */
if (geteuid() != 0 &&
(a == ACTION_POWEROFF ||
a == ACTION_REBOOT ||
a == ACTION_SUSPEND ||
a == ACTION_HIBERNATE ||
a == ACTION_HYBRID_SLEEP)) {
r = reboot_with_logind(bus, a);
if (r >= 0)
return r;
}
if (r >= 0)
warn_wall(a);
return r;
}
char **name;
int r = 3; /* According to LSB: "program is not running" */
if (state < 0)
return state;
if (state == 0)
r = 0;
}
return r;
}
char **name;
int r = 1;
if (state < 0)
return state;
if (state == 0)
r = 0;
}
return r;
}
int r = 0;
char **name, *n;
if (!arg_kill_who)
arg_kill_who = "all";
n = unit_name_mangle(*name);
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"KillUnit",
NULL,
NULL,
DBUS_TYPE_STRING, n ? &n : name,
free(n);
if (r)
return r;
}
return 0;
}
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;
unsigned n;
assert(i);
assert(i);
return -EIO;
return -EIO;
return -ENOMEM;
return -EIO;
n = 0;
n++;
}
return -ENOMEM;
n = 0;
const char *s;
dbus_message_iter_get_basic(&sub3, &s);
return -ENOMEM;
}
if (!dbus_message_iter_next(&sub2) ||
return -EIO;
return 0;
}
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 *description;
const char *following;
char **documentation;
const char *fragment_path;
const char *source_path;
const char *default_control_group;
const char *load_error;
const char *result;
bool need_daemon_reload;
/* Service */
const char *status_text;
bool running:1;
int exit_code, exit_status;
bool condition_result;
/* Socket */
unsigned n_accepted;
unsigned n_connections;
bool accept;
/* Device */
const char *sysfs_path;
/* Mount, Automount */
const char *where;
/* Swap */
const char *what;
static void print_status_info(UnitStatusInfo *i) {
ExecStatusInfo *p;
const char *path;
assert(i);
/* This shows pretty information about a unit. See
* print_property() for a low-level property printer */
printf("\n");
if (i->following)
on = ansi_highlight_red(true);
off = ansi_highlight_red(false);
} else
if (i->load_error)
else if (path && i->unit_file_state)
else if (path)
else
on = ansi_highlight_red(true);
off = ansi_highlight_red(false);
on = ansi_highlight_green(true);
off = ansi_highlight_green(false);
} else
if (ss)
printf("\t Active: %s%s (%s)%s",
on,
strna(i->active_state),
ss,
off);
else
printf("\t Active: %s%s%s",
on,
strna(i->active_state),
off);
if (s1)
else if (s2)
else
printf("\n");
if (!i->condition_result && i->condition_timestamp > 0) {
if (s1)
else if (s2)
}
if (i->sysfs_path)
if (i->where)
if (i->what)
if (!strv_isempty(i->documentation)) {
char **t;
bool first = true;
STRV_FOREACH(t, i->documentation) {
if (first) {
printf("\t Docs: %s\n", *t);
first = false;
} else
printf("\t %s\n", *t);
}
}
if (i->accept)
char *t;
bool good;
/* Only show exited processes here */
if (p->code == 0)
continue;
free(t);
if (!good) {
on = ansi_highlight_red(true);
off = ansi_highlight_red(false);
} 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) {
printf("\t");
if (i->main_pid > 0) {
if (i->running) {
char *t = NULL;
get_process_comm(i->main_pid, &t);
if (t) {
printf(" (%s)", t);
free(t);
}
} else if (i->exit_code > 0) {
if (i->exit_code == CLD_EXITED) {
const char *c;
if (c)
printf("/%s", c);
} else
printf(")");
}
}
if (i->main_pid > 0 && i->control_pid > 0)
printf(";");
if (i->control_pid > 0) {
char *t = NULL;
get_process_comm(i->control_pid, &t);
if (t) {
printf(" (%s)", t);
free(t);
}
}
printf("\n");
}
if (i->status_text)
if (i->default_control_group &&
(i->main_pid > 0 || i->control_pid > 0 || cg_is_empty_by_spec(i->default_control_group, false) == 0)) {
unsigned c;
if (arg_transport != TRANSPORT_SSH) {
unsigned k = 0;
c = columns();
if (c > 18)
c -= 18;
else
c = 0;
if (i->main_pid > 0)
if (i->control_pid > 0)
extra[k++] = i->control_pid;
}
}
int flags =
on_tty() * OUTPUT_COLOR |
printf("\n");
i->id,
0,
flags);
}
if (i->need_daemon_reload)
printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
ansi_highlight_red(true),
ansi_highlight_red(false),
}
static void show_unit_help(UnitStatusInfo *i) {
char **p;
assert(i);
if (!i->documentation) {
return;
}
STRV_FOREACH(p, i->documentation) {
if (startswith(*p, "man:")) {
size_t k;
char *e = NULL;
k = strlen(*p);
if ((*p)[k-1] == ')')
e = strrchr(*p, '(');
if (e) {
if (!page) {
log_oom();
return;
}
if (!section) {
log_oom();
return;
}
} else
if (pid < 0) {
log_error("Failed to fork: %m");
continue;
}
if (pid == 0) {
/* Child */
log_error("Failed to execute man: %m");
}
} else
log_info("Can't show: %s", *p);
}
}
assert(i);
switch (dbus_message_iter_get_arg_type(iter)) {
case DBUS_TYPE_STRING: {
const char *s;
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;
i->default_control_group = s;
i->status_text = s;
i->sysfs_path = s;
i->where = s;
i->what = s;
i->following = s;
i->unit_file_state = s;
i->result = s;
}
break;
}
case DBUS_TYPE_BOOLEAN: {
dbus_bool_t b;
i->accept = b;
i->need_daemon_reload = b;
i->condition_result = b;
break;
}
case DBUS_TYPE_UINT32: {
uint32_t u;
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 DBUS_TYPE_INT32: {
int32_t j;
i->exit_code = (int) j;
i->exit_status = (int) j;
break;
}
case DBUS_TYPE_UINT64: {
uint64_t u;
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;
break;
}
case DBUS_TYPE_ARRAY: {
int r;
return -ENOMEM;
return -ENOMEM;
}
return r;
}
}
const char *s;
char **l;
dbus_message_iter_get_basic(&sub, &s);
l = strv_append(i->documentation, s);
if (!l)
return -ENOMEM;
strv_free(i->documentation);
i->documentation = l;
}
}
break;
}
case DBUS_TYPE_STRUCT: {
const char *n, *message;
int r;
if (r < 0)
return r;
if (r < 0)
return r;
i->load_error = message;
}
break;
}
}
return 0;
}
/* This is a low-level property printer, see
* print_status_info() for the nicer output */
return 0;
switch (dbus_message_iter_get_arg_type(iter)) {
case DBUS_TYPE_STRUCT: {
uint32_t u;
dbus_message_iter_get_basic(&sub, &u);
if (u)
else if (arg_all)
return 0;
const char *s;
dbus_message_iter_get_basic(&sub, &s);
if (arg_all || s[0])
return 0;
return 0;
}
break;
}
case DBUS_TYPE_ARRAY:
if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
const char *path;
}
return 0;
}
return 0;
const char *base;
printf("%s={ value=%s ; next_elapse=%s }\n",
base,
}
}
return 0;
} else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
attr,
value);
}
}
return 0;
} else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
char *t;
printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
name,
strna(t),
free(t);
}
}
return 0;
}
break;
}
return 0;
if (arg_all)
return 0;
}
static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
const char *interface = "";
int r;
ExecStatusInfo *p;
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"GetAll",
&reply,
NULL,
if (r)
goto finish;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
if (*new_line)
printf("\n");
*new_line = true;
const char *name;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
if (show_properties)
else
if (r < 0) {
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
}
r = 0;
if (!show_properties) {
else
}
/* According to LSB: "program not running" */
r = 3;
}
if (reply)
return r;
}
int r;
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnitByPID",
&reply,
NULL,
if (r)
goto finish;
r = -EIO;
goto finish;
}
if (reply)
return r;
}
int r, ret = 0;
bool show_properties, new_line = false;
char **name;
if (show_properties)
/* If not argument is specified inspect the manager
* itself */
}
char *p, *n;
/* Interpret as unit name */
n = unit_name_mangle(*name);
p = unit_dbus_path_from_name(n ? n : *name);
free(n);
if (!p)
return log_oom();
free(p);
if (r != 0)
ret = r;
} else if (show_properties) {
/* Interpret as job id */
char *p;
return log_oom();
free(p);
if (r != 0)
ret = r;
} else {
/* Interpret as PID */
if (r != 0)
ret = r;
}
}
return ret;
}
int r;
const char *text;
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Dump",
&reply,
NULL,
if (r)
goto finish;
r = -EIO;
goto finish;
}
if (reply)
return r;
}
int r;
const char
*interface = "org.freedesktop.systemd1.Unit",
*property = "Id";
char *n;
n = unit_name_mangle(name);
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"CreateSnapshot",
&reply,
NULL,
DBUS_TYPE_STRING, n ? (const char**) &n : &name,
free(n);
if (r)
goto finish;
r = -EIO;
goto finish;
}
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"Get",
&reply,
NULL,
if (r)
goto finish;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
if (!arg_quiet)
if (reply)
return r;
}
int r = 0;
char **name;
char *n;
n = unit_name_mangle(*name);
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit",
&reply,
NULL,
DBUS_TYPE_STRING, n ? &n : name,
free(n);
if (r)
goto finish;
r = -EIO;
goto finish;
}
bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Snapshot",
"Remove",
NULL,
NULL,
if (r)
goto finish;
}
return r;
}
int r;
const char *method;
if (arg_action == ACTION_RELOAD)
method = "Reload";
else if (arg_action == ACTION_REEXEC)
method = "Reexecute";
else {
method =
/* "daemon-reload" */ "Reload";
}
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
NULL,
&error,
/* 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)
return r;
}
int r = 0;
char **name, *n;
n = unit_name_mangle(*name);
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ResetFailedUnit",
NULL,
NULL,
DBUS_TYPE_STRING, n ? &n : name,
free(n);
if (r)
goto finish;
}
return r;
}
int r;
const char
*interface = "org.freedesktop.systemd1.Manager",
*property = "Environment";
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.DBus.Properties",
"Get",
&reply,
NULL,
if (r)
goto finish;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
const char *text;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
}
r = 0;
if (reply)
return r;
}
unsigned l;
const char *root;
l = strv_length(args);
if (l < 2 || l > 3) {
log_error("Wrong number of arguments.");
return -EINVAL;
}
if (l >= 3)
else {
"init", &init,
NULL);
if (!init)
if (!init)
return log_oom();
}
return bus_method_call_with_reply (
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"SwitchRoot",
NULL,
NULL,
}
const char *method;
int r;
? "SetEnvironment"
: "UnsetEnvironment";
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
method);
if (!m)
return log_oom();
if (r < 0)
return log_oom();
if (!reply) {
r = -EIO;
goto finish;
}
r = 0;
return r;
}
static int enable_sysv_units(char **args) {
int r = 0;
#if defined(HAVE_SYSV_COMPAT) && defined(HAVE_CHKCONFIG)
unsigned f = 1, t = 1;
if (arg_scope != UNIT_FILE_SYSTEM)
return 0;
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;
for (f = 1; args[f]; f++) {
const char *name;
char *p;
bool found_native = false, found_sysv;
unsigned c = 1;
char **k, *l, *q = NULL;
int j;
continue;
if (path_is_absolute(name))
continue;
p = NULL;
else
if (!p) {
r = log_oom();
goto finish;
}
free(p);
if (found_native)
break;
}
if (found_native)
continue;
p = NULL;
else
if (!p) {
r = log_oom();
goto finish;
}
if (!found_sysv) {
free(p);
continue;
}
/* Mark this entry, so that we don't try enabling it as native unit */
args[f] = (char*) "";
argv[c++] = path_get_file_name(p);
argv[c++] =
if (!l) {
free(q);
free(p);
r = log_oom();
goto finish;
}
log_info("Executing %s", l);
free(l);
if (pid < 0) {
log_error("Failed to fork: %m");
free(p);
free(q);
r = -errno;
goto finish;
} else if (pid == 0) {
/* Child */
}
free(p);
free(q);
if (j < 0) {
r = j;
goto finish;
}
if (!arg_quiet)
puts("enabled");
r = 1;
} else {
if (!arg_quiet)
puts("disabled");
}
r = -EINVAL;
goto finish;
}
} else {
r = -EPROTO;
goto finish;
}
}
/* Drop all SysV units */
continue;
}
#endif
return r;
}
char **i, **l, **name;
if (!l)
return log_oom();
i = l;
/* When enabling units qualified path names are OK,
* too, hence allow them explicitly. */
else
*i = unit_name_mangle(*name);
if (!*i) {
strv_free(l);
return log_oom();
}
i++;
}
*i = NULL;
*mangled_names = l;
return 0;
}
unsigned n_changes = 0, i;
int carries_install_info = -1;
int r;
char **mangled_names = NULL;
r = enable_sysv_units(args);
if (r < 0)
return r;
if (!args[1])
return 0;
carries_install_info = r;
carries_install_info = r;
carries_install_info = r;
else
assert_not_reached("Unknown verb");
if (r < 0) {
goto finish;
}
if (!arg_quiet) {
for (i = 0; i < n_changes; i++) {
else
}
}
r = 0;
} else {
const char *method;
bool send_force = true, expect_carries_install_info = false;
dbus_bool_t a, b;
method = "EnableUnitFiles";
expect_carries_install_info = true;
method = "DisableUnitFiles";
send_force = false;
method = "ReenableUnitFiles";
expect_carries_install_info = true;
method = "LinkUnitFiles";
method = "PresetUnitFiles";
expect_carries_install_info = true;
method = "MaskUnitFiles";
method = "UnmaskUnitFiles";
send_force = false;
} else
assert_not_reached("Unknown verb");
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
method);
if (!m) {
r = log_oom();
goto finish;
}
if(r < 0)
goto finish;
if (r < 0) {
log_error("Failed to append unit files.");
goto finish;
}
a = arg_runtime;
log_error("Failed to append runtime boolean.");
r = -ENOMEM;
goto finish;
}
if (send_force) {
b = arg_force;
log_error("Failed to append force boolean.");
r = -ENOMEM;
goto finish;
}
}
if (!reply) {
r = -EIO;
goto finish;
}
log_error("Failed to initialize iterator.");
goto finish;
}
if (expect_carries_install_info) {
if (r < 0) {
log_error("Failed to parse reply.");
goto finish;
}
carries_install_info = b;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
if (!arg_quiet) {
else
}
}
/* Try to reload if enabeld */
if (!arg_no_reload)
}
if (carries_install_info == 0)
"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");
if (m)
if (reply)
return r;
}
int r;
bool enabled;
char **name;
r = enable_sysv_units(args);
if (r < 0)
return r;
enabled = r > 0;
if (state < 0) {
r = state;
goto finish;
}
if (state == UNIT_FILE_ENABLED ||
enabled = true;
if (!arg_quiet)
}
} else {
const char *s;
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnitFileState",
&reply,
NULL,
if (r)
goto finish;
DBUS_TYPE_STRING, &s,
r = -EIO;
goto finish;
}
if (streq(s, "enabled") ||
streq(s, "enabled-runtime") ||
streq(s, "static"))
enabled = true;
if (!arg_quiet)
puts(s);
}
}
r = enabled ? 0 : 1;
if (reply)
return r;
}
static int 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"
" -t --type=TYPE List only units of a particular type\n"
" -p --property=NAME Show only properties by this name\n"
" -a --all Show all units/properties, including dead/empty ones\n"
" --failed Show only failed units\n"
" --full Don't ellipsize unit names on output\n"
" --fail When queueing a new job, fail if conflicting jobs are\n"
" pending\n"
" --ignore-dependencies\n"
" When queueing a new job, ignore all its dependencies\n"
" --kill-who=WHO Who to send signal to\n"
" -s --signal=SIGNAL Which signal to send\n"
" -H --host=[USER@]HOST\n"
" Show information for remote host\n"
" -P --privileged Acquire privileges before execution\n"
" -q --quiet Suppress output\n"
" --no-block Do not wait until operation finished\n"
" configuration\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"
" --order When generating graph for dot, show only order\n"
" --require When generating graph for dot, show only requirement\n"
" --system Connect to system manager\n"
" --user Connect to user service manager\n"
" -f --force When enabling unit files, override existing symlinks\n"
" When shutting down, execute action immediately\n"
" --root=PATH Enable unit files in the specified root directory\n"
" --runtime Enable unit files only temporarily until next reboot\n"
" -n --lines=INTEGER Journal entries to show\n"
" -o --output=STRING Change journal output mode (short, short-monotonic,\n"
" verbose, export, json, json-pretty, json-sse, cat)\n\n"
"Unit Commands:\n"
" list-units List loaded units\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 [NAME...] Check whether units are active\n"
" is-failed [NAME...] Check whether units are failed\n"
" status [NAME...|PID...] Show runtime status of one or more units\n"
" show [NAME...|JOB...] Show properties of one or more\n"
" help [NAME...|PID...] Show manual for one or more units\n"
" reset-failed [NAME...] Reset failed state for all, one, or more\n"
" units\n"
" load [NAME...] Load one or more units\n\n"
"Unit File Commands:\n"
" list-unit-files 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"
" 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"
" is-enabled [NAME...] Check whether unit files are enabled\n\n"
"Job Commands:\n"
" list-jobs List jobs\n"
" cancel [JOB...] Cancel all, one, or more jobs\n\n"
"Status Commands:\n"
" dump Dump server status\n"
" dot Dump dependency graph for dot(1)\n\n"
"Snapshot Commands:\n"
" snapshot [NAME] Create a snapshot\n"
" delete [NAME...] Remove one or more snapshots\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\n"
"Manager Lifecycle Commands:\n"
" daemon-reload Reload systemd manager configuration\n"
" daemon-reexec Reexecute systemd manager\n\n"
"System Commands:\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 Shut down and reboot the system\n"
" kexec Shut down and reboot the system with kexec\n"
" exit Request user instance 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",
return 0;
}
static int halt_help(void) {
printf("%s [OPTIONS...]\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");
return 0;
}
static int 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",
return 0;
}
static int 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",
return 0;
}
static int 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",
return 0;
}
static int help_types(void) {
int i;
puts("Available unit types:");
for(i = UNIT_SERVICE; i < _UNIT_TYPE_MAX; i++)
if (unit_type_table[i])
puts(unit_type_table[i]);
puts("\nAvailable unit load states: ");
for(i = UNIT_STUB; i < _UNIT_LOAD_STATE_MAX; i++)
if (unit_type_table[i])
return 0;
}
enum {
ARG_FAIL = 0x100,
};
};
int c;
switch (c) {
case 'h':
return 0;
case ARG_VERSION:
return 0;
case 't':
help_types();
return 0;
}
if (unit_type_from_string(optarg) >= 0) {
break;
}
if (unit_load_state_from_string(optarg) >= 0) {
break;
}
log_error("Unkown unit type or load state '%s'.",
optarg);
log_info("Use -t help to see a list of allowed values.");
return -EINVAL;
case 'p': {
char **l;
return -ENOMEM;
arg_property = l;
/* 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_FAIL:
arg_job_mode = "fail";
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_ORDER:
break;
case ARG_REQUIRE:
break;
case ARG_ROOT:
break;
case ARG_FULL:
arg_full = true;
break;
case ARG_FAILED:
arg_failed = true;
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':
return -EINVAL;
}
break;
case ARG_NO_ASK_PASSWORD:
arg_ask_password = false;
break;
case 'P':
break;
case 'H':
break;
case ARG_RUNTIME:
arg_runtime = true;
break;
case 'n':
return -EINVAL;
}
break;
case 'o':
if (arg_output < 0) {
return -EINVAL;
}
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code '%c'.", c);
return -EINVAL;
}
}
log_error("Cannot access user instance remotely.");
return -EINVAL;
}
return 1;
}
enum {
ARG_HELP = 0x100,
};
};
int c, 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:
log_error("Unknown option code '%c'.", c);
return -EINVAL;
}
}
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':
/* Compatibility nops */
break;
case 'c':
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code '%c'.", c);
return -EINVAL;
}
}
if (r < 0) {
return r;
}
} else
/* No time argument for shutdown cancel */
/* We skip the time argument */
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:
log_error("Unknown option code '%c'.", c);
return -EINVAL;
}
}
telinit_help();
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:
log_error("Unknown option code '%c'.", c);
return -EINVAL;
}
}
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;
}
}
}
}
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_upstart(void) {
char
env1_buf[] = "RUNLEVEL=X",
env2_buf[] = "PREVLEVEL=X";
const char *emit = "runlevel";
if (!(rl = action_to_runlevel()))
return 0;
previous = 'N';
r = 0;
goto finish;
}
r = -EIO;
goto finish;
}
if ((r = bus_check_peercred(bus)) < 0) {
log_error("Failed to verify owner of bus.");
goto finish;
}
if (!(m = dbus_message_new_method_call(
"com.ubuntu.Upstart",
"com.ubuntu.Upstart0_6",
"EmitEvent"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
if (bus_error_is_no_service(&error)) {
r = -EADDRNOTAVAIL;
goto finish;
}
r = -EIO;
goto finish;
}
r = 1;
if (m)
if (reply)
if (bus) {
}
return r;
}
static int talk_initctl(void) {
struct init_request request;
int r, fd;
char rl;
if (!(rl = action_to_runlevel()))
return 0;
return 0;
return -errno;
}
errno = 0;
if (r < 0) {
}
return 1;
}
static const struct {
const char* verb;
const enum {
MORE,
LESS,
} argc_cmp;
const int argc;
} verbs[] = {
};
int left;
unsigned i;
if (left <= 0)
/* Special rule: no arguments means "list-units" */
i = 0;
else {
log_error("This command expects one or more "
"unit names. Did you mean --help?");
return -EINVAL;
}
for (i = 0; i < ELEMENTSOF(verbs); i++)
break;
if (i >= ELEMENTSOF(verbs)) {
return -EINVAL;
}
}
case EQUAL:
log_error("Invalid number of arguments.");
return -EINVAL;
}
break;
case MORE:
log_error("Too few arguments.");
return -EINVAL;
}
break;
case LESS:
log_error("Too many arguments.");
return -EINVAL;
}
break;
default:
assert_not_reached("Unknown comparison operator.");
}
/* Require a bus connection for all operations but
if (running_in_chroot() > 0) {
log_info("Running in chroot, ignoring request.");
return 0;
}
log_error("Failed to get D-Bus connection: %s",
return -EIO;
}
} else {
log_error("Failed to get D-Bus connection: %s",
return -EIO;
}
}
}
int fd;
union sockaddr_union sockaddr;
struct sd_shutdown_command c;
if (fd < 0)
return -errno;
zero(c);
c.usec = t;
else {
}
return -errno;
}
return 0;
}
if (bus) {
/* First, try systemd via D-Bus. */
return 0;
}
/* Nothing else worked, so let's try signals */
log_error("kill() failed: %m");
return -errno;
}
return 0;
}
if (bus) {
/* First, try systemd via D-Bus. */
goto done;
}
/* Hmm, talking to systemd via D-Bus didn't work. Then
* let's try to talk to Upstart via D-Bus. */
if (talk_upstart() > 0)
goto done;
/* Nothing else worked, so let's try
if (talk_initctl() > 0)
goto done;
log_error("Failed to talk to init daemon.");
return -EIO;
done:
return 0;
}
/* Make sure C-A-D is handled by the kernel from this
* point on... */
switch (a) {
case ACTION_HALT:
log_info("Halting.");
break;
case ACTION_POWEROFF:
log_info("Powering off.");
break;
case ACTION_REBOOT:
log_info("Rebooting.");
break;
default:
assert_not_reached("Unknown halt action.");
}
assert_not_reached("Uh? This shouldn't happen.");
}
int r;
if (geteuid() != 0) {
/* Try logind if we are a normal user and no special
* mode applies. Maybe PolicyKit allows us to shutdown
* the machine. */
if (arg_when <= 0 &&
!arg_dry &&
!arg_force &&
(arg_action == ACTION_POWEROFF ||
arg_action == ACTION_REBOOT)) {
if (r >= 0)
return r;
}
log_error("Must be root.");
return -EPERM;
}
if (arg_when > 0) {
char *m;
r = send_shutdownd(arg_when,
'r',
m);
free(m);
if (r < 0)
else {
char date[FORMAT_TIMESTAMP_MAX];
log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
return 0;
}
}
return start_with_fallback(bus);
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)
}
}
if (arg_dry)
return 0;
/* We should never reach this. */
return -ENOSYS;
}
static int runlevel_main(void) {
if (r < 0) {
puts("unknown");
return r;
}
printf("%c %c\n",
return 0;
}
int r, retval = EXIT_FAILURE;
log_open();
if (r < 0)
goto finish;
else if (r == 0) {
goto finish;
}
* let's shortcut this */
if (arg_action == ACTION_RUNLEVEL) {
r = runlevel_main();
retval = r < 0 ? EXIT_FAILURE : r;
goto finish;
}
log_info("Running in chroot, ignoring request.");
retval = 0;
goto finish;
}
if (!avoid_bus()) {
if (arg_transport == TRANSPORT_NORMAL)
bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
else if (arg_transport == TRANSPORT_POLKIT) {
private_bus = false;
} else if (arg_transport == TRANSPORT_SSH) {
private_bus = false;
} else
assert_not_reached("Uh, invalid transport...");
}
switch (arg_action) {
case ACTION_SYSTEMCTL:
break;
case ACTION_HALT:
case ACTION_POWEROFF:
case ACTION_REBOOT:
case ACTION_KEXEC:
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(bus);
break;
case ACTION_RELOAD:
case ACTION_REEXEC:
r = reload_with_fallback(bus);
break;
case ACTION_CANCEL_SHUTDOWN: {
char *m = NULL;
if (arg_wall) {
if (!m) {
goto finish;
}
}
if (r < 0)
free(m);
break;
}
case ACTION_INVALID:
case ACTION_RUNLEVEL:
default:
assert_not_reached("Unknown action");
}
retval = r < 0 ? EXIT_FAILURE : r;
if (bus) {
}
pager_close();
return retval;
}