systemctl.c revision fa776d8e962da9d90459e2f3e86a2a0c6366ee12
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
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 "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"
static const char *arg_property = NULL;
static bool arg_all = false;
static bool arg_replace = false;
static bool arg_session = false;
static bool arg_no_block = false;
static bool arg_immediate = false;
static bool arg_no_wtmp = false;
static bool arg_no_sync = false;
static bool arg_no_wall = false;
static bool arg_dry = false;
static bool arg_quiet = false;
enum action {
static bool private_bus = false;
if (!dbus_error_is_set(error))
return false;
return true;
return true;
}
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_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;
}
int r;
unsigned k = 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;
}
const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
int a = 0, b = 0;
if (job_id != 0)
else
b = 1 + 15;
if (a + b + 2 < columns()) {
if (job_id == 0)
printf(" ");
}
k++;
}
}
if (arg_all)
printf("\n%u units listed.\n", k);
else
printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
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;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
k++;
}
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;
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;
}
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 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) {
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 = 0;
goto finish;
}
r = -EIO;
goto finish;
}
if (!arg_no_block) {
const char *path;
char *p;
r = -EIO;
goto finish;
}
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 = 1;
if (m)
if (reply)
return r;
}
return ACTION_HALT;
return ACTION_POWEROFF;
return ACTION_REBOOT;
return ACTION_RESCUE;
return ACTION_EMERGENCY;
return ACTION_DEFAULT;
else
return ACTION_INVALID;
}
static const char * const table[_ACTION_MAX] = {
};
int r;
unsigned i;
if (arg_action == ACTION_SYSTEMCTL) {
method =
"StartUnit";
mode =
arg_replace ? "replace" :
"fail";
} else {
method = "StartUnit";
}
if (!arg_no_block) {
if ((r = enable_wait_for_jobs(bus)) < 0) {
goto finish;
}
log_error("Failed to allocate set.");
r = -ENOMEM;
goto finish;
}
}
r = 0;
if (one_name) {
goto finish;
} else {
for (i = 1; i < n; i++)
goto finish;
}
if (!arg_no_block)
r = wait_for_jobs(bus, s);
if (s)
set_free_free(s);
return r;
}
}
const char
*interface = "org.freedesktop.systemd1.Unit",
*property = "ActiveState";
int r = -EADDRNOTAVAIL;
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;
}
typedef struct UnitStatusInfo {
const char *id;
const char *load_state;
const char *active_state;
const char *sub_state;
const char *description;
const char *fragment_path;
const char *default_control_group;
/* Service */
const char *status_text;
bool running;
int exit_code, exit_status;
/* Socket */
unsigned n_accepted;
unsigned n_connections;
/* Device */
const char *sysfs_path;
/* Mount, Automount */
const char *where;
/* Swap */
const char *what;
static void print_status_info(UnitStatusInfo *i) {
assert(i);
/* This shows pretty information about a unit. See
* print_property() for a low-level property printer */
printf("\n");
if (i->fragment_path)
else
strna(i->active_state),
else
printf("\t Active: %s (%s)\n",
strna(i->active_state),
if (i->sysfs_path)
else if (i->where)
else if (i->what)
if (i->status_text)
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 == CLD_EXITED)
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->default_control_group) {
unsigned c;
if ((c = columns()) > 18)
c -= 18;
else
c = 0;
}
}
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->fragment_path = s;
i->default_control_group = s;
i->status_text = s;
i->sysfs_path = s;
i->where = s;
i->what = s;
}
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;
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_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")) {
const char *path;
continue;
continue;
const char *s;
dbus_message_iter_get_basic(&sub3, &s);
printf("%s ", s);
}
if (dbus_message_iter_next(&sub2) &&
printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
(unsigned) pid,
}
printf(" }\n");
}
return 0;
}
break;
}
if (arg_all)
return 0;
}
const char *interface = "";
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;
}
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;
}
}
if (!show_properties)
r = 0;
if (m)
if (reply)
return r;
}
int r;
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++) {
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;
}
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;
}
}
} else {
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;
}
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;
}
goto finish;
}
r = 0;
if (m)
if (reply)
return r;
}
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
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.systemd1.Unit',"
"member='Changed'",
&error);
if (dbus_error_is_set(&error)) {
r = -EIO;
goto finish;
}
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Job',"
"member='Changed'",
&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 =
"Exit";
}
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 = 0;
goto finish;
}
r = -EIO;
goto finish;
}
r = 1;
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;
}
static int systemctl_help(void) {
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
"Send control commands to the systemd manager.\n\n"
" -h --help Show this help\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"
" --replace When installing a new job, replace existing conflicting ones\n"
" --system Connect to system bus\n"
" --session Connect to session bus\n"
" -q --quiet Suppress output\n"
" --no-block Do not wait until operation finished\n"
"Commands:\n"
" list-units List units\n"
" start [NAME...] Start one or more units\n"
" stop [NAME...] Stop one or more units\n"
" restart [NAME...] Restart one or more units\n"
" reload [NAME...] Reload one or more units\n"
" isolate [NAME] Start one unit and stop all others\n"
" check [NAME...] Check whether any of the passed units are active\n"
" status [NAME...] Show status of one or more units\n"
" load [NAME...] Load one or more units\n"
" list-jobs List jobs\n"
" cancel [JOB...] Cancel one or more jobs\n"
" clear-jobs Cancel all jobs\n"
" dump Dump server status\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"
" daemon-exit Ask the systemd manager to quit\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"
" 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"
" default Enter default mode\n"
" rescue Enter rescue mode\n"
" emergency Enter emergency mode\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...] [now] [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"
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_REPLACE = 0x100,
};
};
int c;
switch (c) {
case 'h':
return 0;
case 't':
break;
case 'p':
/* 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_REPLACE:
arg_replace = 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 'q':
arg_quiet = true;
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':
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;
}
enum {
ARG_HELP = 0x100,
};
};
int c;
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 '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
/* We ignore 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;
}
}
}
}
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 = 0;
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 {
return 0;
}
for (i = 0; i < ELEMENTSOF(verbs); i++)
break;
if (i >= ELEMENTSOF(verbs)) {
return -EINVAL;
}
}
case EQUAL:
log_error("Invalid number of arguments.");
return -EINVAL;
}
break;
case MORE:
log_error("Too few arguments.");
return -EINVAL;
}
break;
case LESS:
log_error("Too many arguments.");
return -EINVAL;
}
break;
default:
assert_not_reached("Unknown comparison operator.");
}
}
int r;
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;
}
int r;
if (bus) {
/* First, try systemd via D-Bus. */
return 0;
/* Hmm, talking to systemd via D-Bus didn't work. Then
* let's try to talk to Upstart via D-Bus. */
if ((r = talk_upstart()) > 0)
return 0;
}
/* Nothing else worked, so let's try
if ((r = talk_initctl()) != 0)
return 0;
log_error("Failed to talk to init daemon.");
return -EIO;
}
int r;
if (!arg_immediate)
return start_with_fallback(bus);
if (!arg_no_wtmp)
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");
return r;
}
printf("%c %c\n",
return 0;
}
int r, retval = 1;
goto finish;
else if (r == 0) {
retval = 0;
goto finish;
}
* let's shortcut this */
if (arg_action == ACTION_RUNLEVEL) {
retval = runlevel_main() < 0;
goto finish;
}
switch (arg_action) {
case ACTION_SYSTEMCTL: {
if (!bus) {
goto finish;
}
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:
break;
case ACTION_RELOAD:
case ACTION_REEXEC:
break;
case ACTION_INVALID:
case ACTION_RUNLEVEL:
default:
assert_not_reached("Unknown action");
}
if (bus)
return retval;
}