#include "shutdownd.h"
#include "exit-status.h"
#include "bus-errors.h"
+#include "build.h"
static const char *arg_type = NULL;
static char **arg_property = NULL;
return strcasecmp(u->id, v->id);
}
+static bool output_show_job(const struct unit_info *u) {
+ const char *dot;
+
+ return (!arg_type || ((dot = strrchr(u->id, '.')) &&
+ streq(dot+1, arg_type))) &&
+ (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
+}
+
+static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
+ unsigned active_len, sub_len, job_len;
+ const struct unit_info *u;
+
+ active_len = sizeof("ACTIVE")-1;
+ sub_len = sizeof("SUB")-1;
+ job_len = sizeof("JOB")-1;
+
+ for (u = unit_infos; u < unit_infos + c; u++) {
+ if (!output_show_job(u))
+ continue;
+
+ active_len = MAX(active_len, strlen(u->active_state));
+ sub_len = MAX(sub_len, strlen(u->sub_state));
+ if (u->job_id != 0)
+ job_len = MAX(job_len, strlen(u->job_type));
+ }
+
+ if (on_tty()) {
+ printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
+ active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
+ if (columns() >= 80+12 || arg_full)
+ printf(" %s\n", "DESCRIPTION");
+ 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;
+
+ if (!streq(u->load_state, "loaded")) {
+ on_loaded = ansi_highlight(true);
+ off_loaded = ansi_highlight(false);
+ } else
+ on_loaded = off_loaded = "";
+
+ if (streq(u->active_state, "failed")) {
+ on_active = ansi_highlight(true);
+ off_active = ansi_highlight(false);
+ } else
+ on_active = off_active = "";
+
+ e = arg_full ? NULL : ellipsize(u->id, 25, 33);
+
+ printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
+ e ? e : u->id,
+ on_loaded, u->load_state, off_loaded,
+ on_active, active_len, u->active_state,
+ sub_len, u->sub_state, off_active,
+ &a);
+
+ free(e);
+
+ a -= strlen(on_loaded) + strlen(off_loaded);
+ a -= strlen(on_active) + strlen(off_active);
+
+ if (u->job_id != 0)
+ printf(" %-*s", job_len, u->job_type);
+ else
+ b = 1 + job_len;
+
+ if (a + b + 1 < columns()) {
+ if (u->job_id == 0)
+ printf(" %-*s", job_len, "");
+
+ if (arg_full)
+ printf(" %s", u->description);
+ else
+ printf(" %.*s", columns() - a - b - 1, u->description);
+ }
+
+ fputs("\n", stdout);
+ }
+
+ 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)
+ printf("\n%u units listed.\n", c);
+ else
+ printf("\n%u units listed. Pass --all to see inactive units, too.\n", c);
+ }
+}
+
static int list_units(DBusConnection *bus, char **args, unsigned n) {
DBusMessage *m = NULL, *reply = NULL;
DBusError error;
int r;
DBusMessageIter iter, sub, sub2;
- unsigned c = 0, k, n_units = 0;
+ unsigned c = 0, n_units = 0;
struct unit_info *unit_infos = NULL;
dbus_error_init(&error);
}
qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
-
- if (isatty(STDOUT_FILENO)) {
- if (columns() >= 80+12 || arg_full)
- printf("%-25s %-6s %-12s %-18s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
- else
- printf("%-25s %-6s %-12s %-18s %-15s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB");
- }
-
- for (k = 0; k < c; k++) {
- const char *dot;
- struct unit_info *u = unit_infos+k;
-
- if ((!arg_type || ((dot = strrchr(u->id, '.')) &&
- streq(dot+1, arg_type))) &&
- (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0)) {
- char *e;
- int a = 0, b = 0;
- const char *on_loaded, *off_loaded;
- const char *on_active, *off_active;
-
- if (!streq(u->load_state, "loaded")) {
- on_loaded = ansi_highlight(true);
- off_loaded = ansi_highlight(false);
- } else
- on_loaded = off_loaded = "";
-
- if (streq(u->active_state, "failed")) {
- on_active = ansi_highlight(true);
- off_active = ansi_highlight(false);
- } else
- on_active = off_active = "";
-
- e = arg_full ? NULL : ellipsize(u->id, 25, 33);
-
- printf("%-25s %s%-6s%s %s%-12s %-18s%s%n",
- e ? e : u->id,
- on_loaded, u->load_state, off_loaded,
- on_active, u->active_state, u->sub_state, off_active,
- &a);
-
- free(e);
-
- a -= strlen(on_loaded) + strlen(off_loaded);
- a -= strlen(on_active) + strlen(off_active);
-
- if (u->job_id != 0)
- printf(" %-15s%n", u->job_type, &b);
- else
- b = 1 + 15;
-
- if (a + b + 1 < columns()) {
- if (u->job_id == 0)
- printf(" ");
-
- if (arg_full)
- printf(" %s", u->description);
- else
- printf(" %.*s", columns() - a - b - 1, u->description);
- }
-
- fputs("\n", stdout);
- }
- }
-
- if (isatty(STDOUT_FILENO)) {
-
- 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)
- printf("\n%u units listed.\n", c);
- else
- printf("\n%u units listed. Pass --all to see inactive units, too.\n", c);
- }
+ output_units_list(unit_infos, c);
r = 0;
if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
/* There's always a fallback possible for
* legacy actions. */
- r = 0;
+ r = -EADDRNOTAVAIL;
goto finish;
}
}
}
- r = 1;
-
- /* Returns 1 if we managed to issue the request, and 0 if we
- * failed due to systemd not being around. This is then used
- * as indication to try a fallback mechanism. */
+ r = 0;
finish:
if (m)
streq(args[0], "stop") ? "StopUnit" :
streq(args[0], "reload") ? "ReloadUnit" :
streq(args[0], "restart") ? "RestartUnit" :
- streq(args[0], "try-restart") ? "TryRestartUnit" :
+ streq(args[0], "try-restart") ||
+ streq(args[0], "condrestart") ? "TryRestartUnit" :
streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
streq(args[0], "reload-or-try-restart") ||
- streq(args[0], "force-reload") ||
- streq(args[0], "condrestart") ? "ReloadOrTryRestartUnit" :
+ streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
"StartUnit";
mode =
method = "StartUnit";
mode = (arg_action == ACTION_EMERGENCY ||
- arg_action == ACTION_RESCUE) ? "isolate" : "replace";
+ arg_action == ACTION_RESCUE ||
+ arg_action == ACTION_RUNLEVEL2 ||
+ arg_action == ACTION_RUNLEVEL3 ||
+ arg_action == ACTION_RUNLEVEL4 ||
+ arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
one_name = table[arg_action];
}
} else {
for (i = 1; i < n; i++)
if ((r = start_unit_one(bus, method, args[i], mode, &error, s)) != 0) {
-
- if (r > 0)
- ret = r;
- else
- ret = translate_bus_error_to_exit_status(r, &error);
-
+ ret = translate_bus_error_to_exit_status(r, &error);
dbus_error_free(&error);
}
}
if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
/* There's always a fallback possible for
* legacy actions. */
- r = 0;
+ r = -EADDRNOTAVAIL;
goto finish;
}
goto finish;
}
- r = 1;
+ r = 0;
finish:
if (m)
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"
enum {
ARG_FAIL = 0x100,
+ ARG_VERSION,
ARG_SESSION,
ARG_SYSTEM,
ARG_GLOBAL,
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, ARG_VERSION },
{ "type", required_argument, NULL, 't' },
{ "property", required_argument, NULL, 'p' },
{ "all", no_argument, NULL, 'a' },
systemctl_help();
return 0;
+ case ARG_VERSION:
+ puts(PACKAGE_STRING);
+ puts(DISTRIBUTION);
+ puts(SYSTEMD_FEATURES);
+ return 0;
+
case 't':
arg_type = optarg;
break;
if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
if (error_is_no_service(&error)) {
- r = 0;
+ r = -EADDRNOTAVAIL;
goto finish;
}
goto finish;
}
- r = 1;
+ r = 0;
finish:
if (m)
dbus_message_unref(reply);
if (bus) {
+ dbus_connection_flush(bus);
dbus_connection_close(bus);
dbus_connection_unref(bus);
}
if (bus) {
/* First, try systemd via D-Bus. */
- if (start_unit(bus, NULL, 0) > 0)
+ if (start_unit(bus, NULL, 0) >= 0)
goto done;
}
finish:
if (bus) {
+ dbus_connection_flush(bus);
dbus_connection_close(bus);
dbus_connection_unref(bus);
}