systemctl.c revision 5b6319dceedd81f3f1ce7eb70ea5defaef43bcec
/*-*- 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 "log.h"
#include "util.h"
#include "macro.h"
#include "set.h"
static bool arg_all = false;
static bool arg_replace = false;
static bool arg_session = false;
static bool arg_block = false;
return -EIO;
return -EIO;
return 0;
}
static int columns(void) {
static int parsed_columns = 0;
const char *e;
if (parsed_columns > 0)
return parsed_columns;
if ((e = getenv("COLUMNS")))
parsed_columns = atoi(e);
if (parsed_columns <= 0) {
}
if (parsed_columns <= 0)
parsed_columns = 80;
return parsed_columns;
}
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;
}
static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
assert(s);
/* log_debug("Got D-Bus request: %s.%s() on %s", */
/* dbus_message_get_interface(message), */
/* dbus_message_get_member(message), */
/* dbus_message_get_path(message)); */
log_error("Warning! D-Bus connection terminated.");
const char *path;
else {
char *p;
if ((p = set_remove(s, (char*) path)))
free(p);
}
}
}
int r;
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='JobRemoved',"
"path='/org/freedesktop/systemd1'",
&error);
if (dbus_error_is_set(&error)) {
r = -EIO;
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 match registrations. */
if (m)
if (reply)
return r;
}
int r;
assert(s);
log_error("Failed to add filter.");
r = -ENOMEM;
goto finish;
}
while (!set_isempty(s) &&
;
r = 0;
/* This is slightly dirty, since we don't undo the filter registration. */
return r;
}
int r;
unsigned i;
char *p = NULL;
method =
"RestartUnit";
if (arg_block) {
if ((r = enable_wait_for_jobs(bus)) < 0) {
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",
method))) {
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 (arg_block) {
const char *path;
r = -EIO;
goto finish;
}
if (!s)
log_error("Failed to allocate set.");
r = -ENOMEM;
goto finish;
}
log_error("Failed to duplicate path.");
r = -ENOMEM;
goto finish;
}
if ((r = set_put(s, p)) < 0) {
log_error("Failed to add path to set.");
goto finish;
}
p = NULL;
}
}
if (arg_block)
r = wait_for_jobs(bus, s);
else
r = 0;
free(p);
if (s)
set_free_free(s);
if (m)
if (reply)
return r;
}
int r;
const char *mode = "isolate";
char *p = NULL;
if (arg_block) {
if ((r = enable_wait_for_jobs(bus)) < 0) {
goto finish;
}
}
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartUnit"))) {
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;
}
if (arg_block) {
const char *path;
r = -EIO;
goto finish;
}
log_error("Failed to allocate set.");
r = -ENOMEM;
goto finish;
}
log_error("Failed to duplicate path.");
r = -ENOMEM;
goto finish;
}
if ((r = set_put(s, p)) < 0) {
log_error("Failed to add path to set.");
goto finish;
}
p = NULL;
r = wait_for_jobs(bus, s);
} else
r = 0;
free(p);
if (s)
set_free_free(s);
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", */
/* dbus_message_get_interface(message), */
/* dbus_message_get_member(message), */
/* dbus_message_get_path(message)); */
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;
"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;
}
r = 0;
if (m)
if (reply)
return r;
}
int r;
const char *method;
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;
}
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;
}
static int help(void) {
printf("%s [options]\n\n"
" -h --help Show this help\n"
" -t --type=TYPE List only units of a particular type\n"
" -a --all Show all units, including dead 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"
" --block Wait until operation finished\n\n"
"Commands:\n"
" list-units List units\n"
" list-jobs List jobs\n"
" clear-jobs Cancel all jobs\n"
" load [NAME...] Load one or more units\n"
" cancel [JOB...] Cancel one or more jobs\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"
" dump Dump server status\n"
" snapshot [NAME] Create a snapshot\n"
" daemon-reload Reload daemon configuration\n"
" daemon-reexecute Reexecute daemon\n"
" daemon-exit Ask the daemon 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",
return 0;
}
enum {
ARG_REPLACE = 0x100,
};
};
int c;
switch (c) {
case 'h':
help();
return 0;
case 't':
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_BLOCK:
arg_block = true;
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
return 1;
}
static const struct {
const char* verb;
const enum {
MORE,
LESS,
} argc_cmp;
const int argc;
} verbs[] = {
};
unsigned i;
goto finish;
else if (r == 0) {
retval = 0;
goto finish;
}
if (left <= 0)
/* Special rule: no arguments means "list-units" */
i = 0;
else {
for (i = 0; i < ELEMENTSOF(verbs); i++)
break;
if (i >= ELEMENTSOF(verbs)) {
goto finish;
}
}
case EQUAL:
log_error("Invalid number of arguments.");
goto finish;
}
break;
case MORE:
log_error("Too few arguments.");
goto finish;
}
break;
case LESS:
log_error("Too many arguments.");
goto finish;
}
break;
default:
assert_not_reached("Unknown comparison operator.");
}
goto finish;
}
if (bus)
return retval;
}