systemctl.c revision 501fc174c22aebd3181af08a4cfa65cc92bbe233
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <getopt.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>
#include "log.h"
#include "util.h"
#include "macro.h"
#include "set.h"
#include "utmp-wtmp.h"
#include "special.h"
#include "initreq.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 "sd-daemon.h"
#include "shutdownd.h"
#include "exit-status.h"
#include "bus-errors.h"
#include "build.h"
#include "unit-name.h"
static char **arg_property = NULL;
static bool arg_all = false;
static bool arg_fail = false;
static bool arg_session = false;
static bool arg_global = false;
static bool arg_immediate = false;
static bool arg_no_block = false;
static bool arg_no_wtmp = false;
static bool arg_no_sync = 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 bool arg_force = false;
static bool arg_defaults = false;
static bool arg_ask_password = false;
static const char *arg_kill_who = NULL;
static const char *arg_kill_mode = NULL;
static int arg_signal = SIGTERM;
static enum action {
static enum dot {
static bool private_bus = false;
static bool on_tty(void) {
static int t = -1;
if (_unlikely_(t < 0))
t = isatty(STDOUT_FILENO) > 0;
return t;
}
static void spawn_ask_password_agent(void) {
/* We check STDIN here, not STDOUT, since this is about input,
* not output */
if (!isatty(STDIN_FILENO))
return;
if (!arg_ask_password)
return;
/* Spawns a temporary TTY agent, making sure it goes away when
* we go away */
return;
if (child == 0) {
/* In the child */
const char * const args[] = {
"--watch",
};
/* Check whether our parent died before we were able
* to set the death signal */
}
}
static const char *ansi_highlight(bool b) {
if (!on_tty())
return "";
return b ? ANSI_HIGHLIGHT_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 false;
return true;
return true;
}
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;
}
return -EIO;
return -EIO;
return 0;
}
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!"
};
if (arg_no_wall)
return;
if (arg_wall) {
char *p;
log_error("Failed to join strings.");
return;
}
if (*p) {
utmp_wall(p);
free(p);
return;
}
free(p);
}
return;
}
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_job(const struct unit_info *u) {
const char *dot;
}
const struct unit_info *u;
for (u = unit_infos; u < unit_infos + c; u++) {
if (!output_show_job(u))
continue;
if (u->job_id != 0)
}
if (on_tty()) {
else
printf("\n");
}
for (u = unit_infos; u < unit_infos + c; u++) {
char *e;
int a = 0, b = 0;
const char *on_loaded, *off_loaded;
const char *on_active, *off_active;
if (!output_show_job(u))
continue;
n_shown++;
on_loaded = ansi_highlight(true);
off_loaded = ansi_highlight(false);
} else
on_active = ansi_highlight(true);
off_active = ansi_highlight(false);
} else
printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
e ? e : u->id,
&a);
free(e);
if (u->job_id != 0)
else
b = 1 + job_len;
if (a + b + 1 < columns()) {
if (u->job_id == 0)
if (arg_full)
else
}
}
if (on_tty()) {
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"
"JOB = Pending job for the unit.\n");
if (arg_all)
else
}
}
int r;
unsigned c = 0, n_units = 0;
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListUnits"))) {
log_error("Could not allocate message.");
return -ENOMEM;
}
r = -EIO;
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++;
}
r = 0;
if (m)
if (reply)
return r;
}
static const char * const colors[] = {
"Requires", "[color=\"black\"]",
"RequiresOverridable", "[color=\"black\"]",
"Requisite", "[color=\"darkblue\"]",
"RequisiteOverridable", "[color=\"darkblue\"]",
"Wants", "[color=\"darkgrey\"]",
"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;
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"GetAll"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
if (!dbus_message_append_args(m,
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
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;
}
}
r = 0;
if (m)
if (reply)
return r;
}
int r;
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListUnits"))) {
log_error("Could not allocate message.");
return -ENOMEM;
}
r = -EIO;
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");
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 (m)
if (reply)
return r;
}
int r;
unsigned k = 0;
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListJobs"))) {
log_error("Could not allocate message.");
return -ENOMEM;
}
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
if (isatty(STDOUT_FILENO))
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 (isatty(STDOUT_FILENO))
printf("\n%u jobs listed.\n", k);
r = 0;
if (m)
if (reply)
return r;
}
int r;
unsigned i;
for (i = 1; i < n; i++) {
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"LoadUnit"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, &args[i],
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
goto finish;
}
}
r = 0;
if (m)
if (reply)
return r;
}
int r;
unsigned i;
if (n <= 1)
for (i = 1; i < n; i++) {
unsigned id;
const char *path;
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetJob"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
goto finish;
}
if (!dbus_message_append_args(m,
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
goto finish;
}
r = -EIO;
goto finish;
}
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Job",
"Cancel"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
goto finish;
}
}
r = 0;
if (m)
if (reply)
return r;
}
dbus_bool_t b = FALSE;
const char
*interface = "org.freedesktop.systemd1.Unit",
*property = "NeedDaemonReload",
*path;
/* We ignore all errors here, since this is used to show a warning only */
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit")))
goto finish;
if (!dbus_message_append_args(m,
goto finish;
goto finish;
goto finish;
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"Get")))
goto finish;
if (!dbus_message_append_args(m,
goto finish;
}
goto finish;
goto finish;
goto finish;
dbus_message_iter_get_basic(&sub, &b);
if (m)
if (reply)
return b;
}
typedef struct WaitData {
bool failed;
} 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.");
const char *path;
dbus_bool_t success = true;
else {
char *p;
free(p);
if (!success)
d->failed = true;
}
}
}
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;
WaitData d;
assert(s);
zero(d);
d.set = s;
d.failed = false;
log_error("Failed to add filter.");
r = -ENOMEM;
goto finish;
}
while (!set_isempty(s) &&
;
log_error("Job failed, see system logs for details.");
/* This is slightly dirty, since we don't undo the filter registration. */
return r;
}
static int start_unit_one(
const char *method,
const char *name,
const char *mode,
Set *s) {
const char *path;
int r;
assert(arg_no_block || s);
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
method))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
if (!dbus_message_append_args(m,
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
/* There's always a fallback possible for
* legacy actions. */
r = -EADDRNOTAVAIL;
goto finish;
}
r = -EIO;
goto finish;
}
r = -EIO;
goto finish;
}
log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
if (!arg_no_block) {
char *p;
log_error("Failed to duplicate path.");
r = -ENOMEM;
goto finish;
}
if ((r = set_put(s, p)) < 0) {
free(p);
log_error("Failed to add path to set.");
goto finish;
}
}
r = 0;
if (m)
if (reply)
return r;
}
return ACTION_HALT;
return ACTION_POWEROFF;
return ACTION_REBOOT;
return ACTION_KEXEC;
return ACTION_RESCUE;
return ACTION_EMERGENCY;
return ACTION_DEFAULT;
return ACTION_EXIT;
else
return ACTION_INVALID;
}
static const char * const table[_ACTION_MAX] = {
};
int r, ret = 0;
unsigned i;
if (arg_action == ACTION_SYSTEMCTL) {
method =
"StartUnit";
mode =
arg_fail ? "fail" :
"replace";
} else {
method = "StartUnit";
arg_action == ACTION_RESCUE ||
arg_action == ACTION_RUNLEVEL2 ||
arg_action == ACTION_RUNLEVEL3 ||
arg_action == ACTION_RUNLEVEL4 ||
}
if (!arg_no_block) {
goto finish;
}
log_error("Failed to allocate set.");
goto finish;
}
}
if (one_name) {
goto finish;
} else {
for (i = 1; i < n; i++)
}
}
if (!arg_no_block)
if ((r = wait_for_jobs(bus, s)) < 0) {
ret = r;
goto finish;
}
if (s)
set_free_free(s);
return ret;
}
int r;
if (arg_force &&
if (r >= 0)
return r;
}
const char
*interface = "org.freedesktop.systemd1.Unit",
*property = "ActiveState";
int r = 3; /* According to LSB: "program is not running" */
unsigned i;
for (i = 1; i < n; i++) {
const char *state;
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, &args[i],
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
/* Hmm, cannot figure out anything about this unit... */
if (!arg_quiet)
puts("unknown");
continue;
}
r = -EIO;
goto finish;
}
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"Get"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
if (!dbus_message_append_args(m,
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
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)
r = 0;
}
if (m)
if (reply)
return r;
}
int r = 0;
unsigned i;
if (!arg_kill_who)
arg_kill_who = "all";
if (!arg_kill_mode)
for (i = 1; i < n; i++) {
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"KillUnit"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, &args[i],
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
}
if (reply)
}
if (m)
if (reply)
return r;
}
typedef struct ExecStatusInfo {
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 *description;
const char *following;
const char *path;
const char *default_control_group;
bool need_daemon_reload;
/* Service */
const char *status_text;
bool running:1;
#ifdef HAVE_SYSV_COMPAT
bool is_sysv:1;
#endif
int exit_code, exit_status;
/* 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;
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(true);
off = ansi_highlight(false);
} else
if (i->path)
else
on = ansi_highlight(true);
off = ansi_highlight(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->sysfs_path)
else if (i->where)
else if (i->what)
if (i->accept)
char *t;
/* Only show exited processes here */
if (p->code == 0)
continue;
free(t);
if (p->code == CLD_EXITED) {
const char *c;
#ifdef HAVE_SYSV_COMPAT
#else
#endif
printf("/%s", c);
} else
printf(")\n");
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_name(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;
#ifdef HAVE_SYSV_COMPAT
if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
#else
#endif
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_name(i->control_pid, &t);
if (t) {
printf(" (%s)", t);
free(t);
}
}
printf("\n");
}
if (i->status_text)
if (i->default_control_group) {
unsigned c;
if ((c = columns()) > 18)
c -= 18;
else
c = 0;
}
if (i->need_daemon_reload)
printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
ansi_highlight(true),
ansi_highlight(false),
}
switch (dbus_message_iter_get_arg_type(iter)) {
case DBUS_TYPE_STRING: {
const char *s;
if (s[0]) {
i->id = s;
i->load_state = s;
i->active_state = s;
i->sub_state = s;
i->description = s;
i->path = s;
#ifdef HAVE_SYSV_COMPAT
i->is_sysv = true;
i->path = s;
}
#endif
i->default_control_group = s;
i->status_text = s;
i->sysfs_path = s;
i->where = s;
i->what = s;
i->following = s;
}
break;
}
case DBUS_TYPE_BOOLEAN: {
dbus_bool_t b;
i->accept = b;
i->need_daemon_reload = 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->active_exit_timestamp = (usec_t) u;
break;
}
case DBUS_TYPE_ARRAY: {
int r;
return -ENOMEM;
return r;
}
}
}
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_STRING: {
const char *s;
if (arg_all || s[0])
return 0;
}
case DBUS_TYPE_BOOLEAN: {
dbus_bool_t b;
return 0;
}
case DBUS_TYPE_UINT64: {
uint64_t u;
/* Yes, heuristics! But we can change this check
* should it turn out to not be sufficient */
char timestamp[FORMAT_TIMESTAMP_MAX], *t;
char timespan[FORMAT_TIMESPAN_MAX];
} else
return 0;
}
case DBUS_TYPE_UINT32: {
uint32_t u;
else
return 0;
}
case DBUS_TYPE_INT32: {
int32_t i;
return 0;
}
case DBUS_TYPE_DOUBLE: {
double d;
return 0;
}
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;
}
break;
}
case DBUS_TYPE_ARRAY:
bool space = false;
if (arg_all ||
const char *s;
dbus_message_iter_get_basic(&sub, &s);
space = true;
}
puts("");
}
return 0;
if (arg_all ||
uint8_t u;
dbus_message_iter_get_basic(&sub, &u);
printf("%02x", u);
}
puts("");
}
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 && startswith(name, "Exec")) {
char *t;
printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
name,
strna(t),
free(t);
}
}
return 0;
}
break;
}
if (arg_all)
return 0;
}
const char *interface = "";
int r;
ExecStatusInfo *p;
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"GetAll"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
if (!dbus_message_append_args(m,
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
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)
/* According to LSB: "program not running" */
r = 3;
}
if (m)
if (reply)
return r;
}
int r, ret = 0;
unsigned i;
bool show_properties, new_line = false;
if (show_properties && n <= 1) {
/* If not argument is specified inspect the manager
* itself */
goto finish;
}
for (i = 1; i < n; i++) {
/* Interpret as unit name */
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"LoadUnit"))) {
log_error("Could not allocate message.");
goto finish;
}
if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, &args[i],
log_error("Could not append arguments to message.");
goto finish;
}
goto finish;
}
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit"))) {
log_error("Could not allocate message.");
goto finish;
}
if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, &args[i],
log_error("Could not append arguments to message.");
goto finish;
}
else
goto finish;
}
}
} else if (show_properties) {
/* Interpret as job id */
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetJob"))) {
log_error("Could not allocate message.");
goto finish;
}
if (!dbus_message_append_args(m,
log_error("Could not append arguments to message.");
goto finish;
}
goto finish;
}
} else {
/* Interpret as PID */
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnitByPID"))) {
log_error("Could not allocate message.");
goto finish;
}
if (!dbus_message_append_args(m,
log_error("Could not append arguments to message.");
goto finish;
}
goto finish;
}
}
goto finish;
}
ret = r;
}
if (m)
if (reply)
return ret;
}
static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
log_debug("Got D-Bus request: %s.%s() on %s",
log_error("Warning! D-Bus connection terminated.");
else
const char *path;
else
} else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
goto finish;
}
goto finish;
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"Get"))) {
log_error("Could not allocate message.");
goto oom;
}
if (!dbus_message_append_args(m,
log_error("Could not append arguments to message.");
goto finish;
}
goto finish;
}
log_error("Failed to parse reply.");
goto finish;
}
const char *id;
log_error("Failed to parse reply.");
goto finish;
}
} else {
log_error("Failed to parse reply.");
goto finish;
}
}
}
if (m)
if (reply)
oom:
if (m)
if (reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
int r;
if (!private_bus) {
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"path='/org/freedesktop/systemd1'",
&error);
if (dbus_error_is_set(&error)) {
r = -EIO;
goto finish;
}
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.DBus.Properties',"
"member='PropertiesChanged'",
&error);
if (dbus_error_is_set(&error)) {
r = -EIO;
goto finish;
}
}
log_error("Failed to add filter.");
r = -ENOMEM;
goto finish;
}
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Subscribe"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
goto finish;
}
;
r = 0;
/* This is slightly dirty, since we don't undo the filter or the matches. */
if (m)
if (reply)
return r;
}
int r;
const char *text;
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Dump"))) {
log_error("Could not allocate message.");
return -ENOMEM;
}
r = -EIO;
goto finish;
}
r = -EIO;
goto finish;
}
r = 0;
if (m)
if (reply)
return r;
}
int r;
const char
*interface = "org.freedesktop.systemd1.Unit",
*property = "Id";
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"CreateSnapshot"))) {
log_error("Could not allocate message.");
return -ENOMEM;
}
if (n > 1)
if (!dbus_message_append_args(m,
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
goto finish;
}
r = -EIO;
goto finish;
}
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
path,
"org.freedesktop.DBus.Properties",
"Get"))) {
log_error("Could not allocate message.");
return -ENOMEM;
}
if (!dbus_message_append_args(m,
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
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)
r = 0;
if (m)
if (reply)
return r;
}
int r;
unsigned i;
for (i = 1; i < n; i++) {
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, &args[i],
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
goto finish;
}
r = -EIO;
goto finish;
}
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Snapshot",
"Remove"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
goto finish;
}
}
r = 0;
if (m)
if (reply)
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";
}
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
method))) {
log_error("Could not allocate message.");
return -ENOMEM;
}
/* There's always a fallback possible for
* legacy actions. */
r = -EADDRNOTAVAIL;
goto finish;
}
r = -EIO;
goto finish;
}
r = 0;
if (m)
if (reply)
return r;
}
unsigned i;
int r;
if (n <= 1)
for (i = 1; i < n; i++) {
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ResetFailedUnit"))) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, args + i,
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
goto finish;
}
}
r = 0;
if (m)
if (reply)
return r;
}
int r;
const char
*interface = "org.freedesktop.systemd1.Manager",
*property = "Environment";
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.DBus.Properties",
"Get"))) {
log_error("Could not allocate message.");
return -ENOMEM;
}
if (!dbus_message_append_args(m,
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
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 (m)
if (reply)
return r;
}
int r;
const char *method;
unsigned i;
? "SetEnvironment"
: "UnsetEnvironment";
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
method))) {
log_error("Could not allocate message.");
return -ENOMEM;
}
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
for (i = 1; i < n; i++)
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
r = -EIO;
goto finish;
}
r = 0;
if (m)
if (reply)
return r;
}
typedef struct {
char *name;
char *path;
char **aliases;
char **wanted_by;
} InstallInfo;
static unsigned n_symlinks = 0;
static void install_info_free(InstallInfo *i) {
assert(i);
free(i);
}
static void install_info_hashmap_free(Hashmap *m) {
InstallInfo *i;
while ((i = hashmap_steal_first(m)))
hashmap_free(m);
}
static int install_info_add(const char *name) {
InstallInfo *i;
int r;
if (!unit_name_is_valid_no_type(name, true)) {
return -EINVAL;
}
return 0;
r = -ENOMEM;
goto fail;
}
r = -ENOMEM;
goto fail;
}
goto fail;
return 0;
fail:
if (i)
return r;
}
static int config_parse_also(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
const char *rvalue,
void *data,
void *userdata) {
char *w;
size_t l;
char *state;
char *n;
int r;
if (!(n = strndup(w, l)))
return -ENOMEM;
if ((r = install_info_add(n)) < 0) {
free(n);
return r;
}
free(n);
}
return 0;
}
static int mark_symlink_for_removal(const char *p) {
char *n;
int r;
assert(p);
assert(path_is_absolute(p));
if (!remove_symlinks_to)
return 0;
if (!(n = strdup(p)))
return -ENOMEM;
if ((r = set_put(remove_symlinks_to, n)) < 0) {
free(n);
return r == -EEXIST ? 0 : r;
}
return 0;
}
static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
int r = 0;
DIR *d;
return -errno;
}
rewinddir(d);
continue;
is_link = true;
is_dir = true;
if (r == 0)
r = -errno;
continue;
}
} else
continue;
if (is_dir) {
int nfd, q;
char *p;
if (r == 0)
r = -errno;
continue;
}
log_error("Failed to allocate directory string.");
r = -ENOMEM;
break;
}
/* This will close nfd, regardless whether it succeeds or not */
free(p);
if (r == 0)
r = q;
} else if (is_link) {
char *p, *dest, *c;
int q;
log_error("Failed to allocate symlink string.");
r = -ENOMEM;
break;
}
if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
free(p);
if (r == 0)
r = q;
continue;
}
if ((c = canonicalize_file_name(dest))) {
/* This might fail if the destination
* is already removed */
dest = c;
}
if (!arg_quiet)
log_info("rm '%s'", p);
if (unlink(p) < 0) {
log_error("Cannot unlink symlink %s: %m", p);
if (r == 0)
r = -errno;
} else {
rmdir_parents(p, config_path);
if (!set_get(remove_symlinks_to, p)) {
if ((r = mark_symlink_for_removal(p)) < 0) {
if (r == 0)
r = q;
} else
*deleted = true;
}
}
}
free(p);
}
}
closedir(d);
return r;
}
static int remove_marked_symlinks(const char *config_path) {
int fd, r = 0;
bool deleted;
if (set_size(remove_symlinks_to) <= 0)
return 0;
return -errno;
do {
int q, cfd;
deleted = false;
r = -errno;
break;
}
/* This takes possession of cfd and closes it */
if (r == 0)
r = q;
}
} while (deleted);
return r;
}
int r;
char *dest;
if (!arg_quiet)
return 0;
}
return -errno;
}
return -EEXIST;
}
return r;
}
return 0;
}
if (!arg_force) {
log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
return -EEXIST;
}
if (!arg_quiet)
return 0;
return -errno;
char *dest;
if ((r = mark_symlink_for_removal(old_path)) < 0)
return r;
return 0;
return 0;
}
return r;
}
return 0;
}
if ((r = mark_symlink_for_removal(new_path)) < 0)
return r;
if (!arg_quiet)
return 0;
return -errno;
char *dest;
return 0;
return r;
}
return 1;
}
return 0;
}
assert_not_reached("Unknown action.");
}
char **s;
char *alias_path = NULL;
int r;
assert(i);
STRV_FOREACH(s, i->aliases) {
log_error("Out of memory");
r = -ENOMEM;
goto finish;
}
goto finish;
}
r = 0;
return r;
}
char **s;
char *alias_path = NULL;
int r;
assert(i);
STRV_FOREACH(s, i->wanted_by) {
if (!unit_name_is_valid_no_type(*s, true)) {
log_error("Invalid name %s.", *s);
r = -EINVAL;
goto finish;
}
alias_path = NULL;
log_error("Out of memory");
r = -ENOMEM;
goto finish;
}
goto finish;
}
r = 0;
return r;
}
static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
const ConfigItem items[] = {
};
char **p;
int r;
assert(i);
int fd;
log_error("Out of memory");
return -ENOMEM;
}
/* Ensure that we don't follow symlinks */
break;
log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
return -errno;
}
return -errno;
}
}
if (!f) {
return -ENOENT;
}
fclose(f);
return r;
}
fclose(f);
return r;
return r;
if ((r = mark_symlink_for_removal(filename)) < 0)
return r;
if ((r = remove_marked_symlinks(config_path)) < 0)
return r;
return 0;
}
static char *get_config_path(void) {
if (arg_session && arg_global)
return strdup(SESSION_CONFIG_UNIT_PATH);
if (arg_session) {
char *p;
if (session_config_home(&p) < 0)
return NULL;
return p;
}
return strdup(SYSTEM_CONFIG_UNIT_PATH);
}
int r;
char *config_path = NULL;
unsigned j;
InstallInfo *i;
goto finish;
}
if (!(config_path = get_config_path())) {
log_error("Failed to determine config path");
r = -ENOMEM;
goto finish;
}
if (!will_install || !have_installed) {
log_error("Failed to allocate unit sets.");
r = -ENOMEM;
goto finish;
}
log_error("Failed to allocate symlink sets.");
r = -ENOMEM;
goto finish;
}
for (j = 1; j < n; j++)
if ((r = install_info_add(args[j])) < 0) {
goto finish;
}
while ((i = hashmap_first(will_install))) {
int q;
if (q < 0) {
if (r == 0)
r = q;
goto finish;
}
/* In test mode and found something */
r = 1;
break;
}
}
r = r > 0 ? 0 : -ENOENT;
else {
if (n_symlinks <= 0)
log_warning("Unit files contain no applicable installation information. Ignoring.");
if (bus &&
/* Don't try to reload anything if the user asked us to not do this */
!arg_no_reload &&
/* Don't try to reload anything when updating a unit globally */
!arg_global &&
/* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
(arg_session || sd_booted() > 0) &&
/* Don't try to reload anything if we are running in a chroot environment */
(arg_session || running_in_chroot() <= 0) ) {
int q;
r = q;
}
}
return r;
}
static int systemctl_help(void) {
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
"Send control commands to or query 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"
" --full Don't ellipsize unit names on output\n"
" --fail When queueing a new job, fail if conflicting jobs are\n"
" pending\n"
" -q --quiet Suppress output\n"
" --no-block Do not wait until operation finished\n"
" --system Connect to system bus\n"
" --session Connect to session bus\n"
" --order When generating graph for dot, show only order\n"
" --require When generating graph for dot, show only requirement\n"
" configuration\n"
" --no-ask-password\n"
" Do not ask for system passwords\n"
" --kill-mode=MODE How to send signal\n"
" --kill-who=WHO Who to send signal to\n"
" -s --signal=SIGNAL Which signal to send\n"
" -f --force When enabling unit files, override existing symlinks\n"
" When shutting down, execute action immediately\n"
" --defaults When disabling unit files, remove default symlinks only\n\n"
"Commands:\n"
" list-units List 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 is possible,\n"
" otherwise start or restart\n"
" reload-or-try-restart [NAME...] Reload one or more units is 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"
" status [NAME...|PID...] Show runtime status of one or more units\n"
" show [NAME...|JOB...] Show properties of one or more\n"
" reset-failed [NAME...] Reset failed state for all, one, or more\n"
" units\n"
" enable [NAME...] Enable one or more unit files\n"
" disable [NAME...] Disable one or more unit files\n"
" is-enabled [NAME...] Check whether unit files are enabled\n"
" load [NAME...] Load one or more units\n"
" list-jobs List jobs\n"
" cancel [JOB...] Cancel all, one, or more jobs\n"
" dump Dump server status\n"
" dot Dump dependency graph for dot(1)\n"
" snapshot [NAME] Create a snapshot\n"
" delete [NAME...] Remove one or more snapshots\n"
" daemon-reload Reload systemd manager configuration\n"
" daemon-reexec Reexecute systemd manager\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"
" 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 Ask for session termination\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, overriden 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;
}
enum {
ARG_FAIL = 0x100,
};
};
int c;
/* Only when running as systemctl we ask for passwords */
arg_ask_password = true;
switch (c) {
case 'h':
return 0;
case ARG_VERSION:
return 0;
case 't':
break;
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_fail = true;
break;
case ARG_SESSION:
arg_session = true;
break;
case ARG_SYSTEM:
arg_session = false;
break;
case ARG_NO_BLOCK:
arg_no_block = true;
break;
case ARG_NO_WALL:
arg_no_wall = true;
break;
case ARG_ORDER:
break;
case ARG_REQUIRE:
break;
case ARG_FULL:
arg_full = true;
break;
case 'q':
arg_quiet = true;
break;
case 'f':
arg_force = true;
break;
case ARG_NO_RELOAD:
arg_no_reload = true;
break;
case ARG_GLOBAL:
arg_global = true;
arg_session = true;
break;
case ARG_DEFAULTS:
arg_defaults = true;
break;
case ARG_KILL_WHO:
break;
case ARG_KILL_MODE:
break;
case 's':
return -EINVAL;
}
break;
case ARG_NO_ASK_PASSWORD:
arg_ask_password = false;
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
return 1;
}
enum {
ARG_HELP = 0x100,
};
};
int c, runlevel;
arg_immediate = true;
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_immediate = true;
break;
case 'w':
arg_dry = true;
break;
case 'd':
arg_no_wtmp = true;
break;
case 'n':
arg_no_sync = true;
break;
case ARG_NO_WALL:
arg_no_wall = true;
break;
case 'i':
case 'h':
/* 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 (t[0] == '+') {
uint64_t u;
if (safe_atou64(t + 1, &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':
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;
}
}
return r;
}
} else
/* 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 (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 (error_is_no_service(&error)) {
r = -EADDRNOTAVAIL;
goto finish;
}
r = -EIO;
goto finish;
}
r = 0;
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 {
return 0;
}
for (i = 0; i < ELEMENTSOF(verbs); i++)
break;
if (i >= ELEMENTSOF(verbs)) {
return -EINVAL;
}
}
case EQUAL:
log_error("Invalid number of arguments.");
return -EINVAL;
}
break;
case MORE:
log_error("Too few arguments.");
return -EINVAL;
}
break;
case LESS:
log_error("Too many arguments.");
return -EINVAL;
}
break;
default:
assert_not_reached("Unknown comparison operator.");
}
/* Require a bus connection for all operations but
!bus) {
return -EIO;
}
}
int fd = -1;
union sockaddr_union sockaddr;
struct shutdownd_command c;
zero(c);
c.elapse = t;
if (message)
return -errno;
strncpy(sockaddr.un.sun_path+1, "/org/freedesktop/systemd1/shutdownd", sizeof(sockaddr.un.sun_path)-1);
msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + 1 + sizeof("/org/freedesktop/systemd1/shutdownd") - 1;
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;
}
int r;
if (geteuid() != 0) {
log_error("Must be root.");
return -EPERM;
}
if (arg_when > 0) {
char *m;
char date[FORMAT_TIMESTAMP_MAX];
r = send_shutdownd(arg_when,
'r',
m);
free(m);
if (r < 0)
else {
log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
return 0;
}
}
if (!arg_dry && !arg_immediate)
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 if ((r = utmp_put_shutdown(0)) < 0)
}
if (!arg_no_sync)
sync();
if (arg_dry)
return 0;
/* Make sure C-A-D is handled by the kernel from this
* point on... */
switch (arg_action) {
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.");
}
/* We should never reach this. */
return -ENOSYS;
}
static int runlevel_main(void) {
printf("unknown\n");
return r;
}
printf("%c %c\n",
return 0;
}
int r, retval = EXIT_FAILURE;
log_open();
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;
}
switch (arg_action) {
case ACTION_SYSTEMCTL:
break;
case ACTION_HALT:
case ACTION_POWEROFF:
case ACTION_REBOOT:
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:
r = send_shutdownd(0, 0, false, NULL);
break;
case ACTION_INVALID:
case ACTION_RUNLEVEL:
default:
assert_not_reached("Unknown action");
}
retval = r < 0 ? EXIT_FAILURE : r;
if (bus) {
}
return retval;
}