X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fsystemctl%2Fsystemctl.c;h=82c801e4d99ce598fc2fce47afa156956b959aec;hb=f89a3b6f5bf695186e7b9e6cc63993b1272e7569;hp=367dd8060aabef0f8acc8808c689f5fab851bcf4;hpb=d3b52baff90876a648e321f3658a74bc58a8647c;p=elogind.git diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 367dd8060..82c801e4d 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -186,6 +186,14 @@ static void polkit_agent_open_if_enabled(void) { } #endif +static const char *ansi_highlight(bool b) { + + if (!on_tty()) + return ""; + + return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF; +} + static const char *ansi_highlight_red(bool b) { if (!on_tty()) @@ -372,15 +380,6 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) { } else id_len = max_id_len; - if (!arg_no_legend) { - printf("%-*s %-6s %-*s %-*s %-*s ", id_len, "UNIT", "LOAD", - active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB"); - if (!arg_full && arg_no_pager) - printf("%.*s\n", desc_len, "DESCRIPTION"); - else - printf("%s\n", "DESCRIPTION"); - } - for (u = unit_infos; u < unit_infos + c; u++) { char *e; const char *on_loaded, *off_loaded; @@ -389,6 +388,15 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) { if (!output_show_unit(u)) continue; + if (!n_shown && !arg_no_legend) { + printf("%-*s %-6s %-*s %-*s %-*s ", id_len, "UNIT", "LOAD", + active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB"); + if (!arg_full && arg_no_pager) + printf("%.*s\n", desc_len, "DESCRIPTION"); + else + printf("%s\n", "DESCRIPTION"); + } + n_shown++; if (streq(u->load_state, "error")) { @@ -420,17 +428,28 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) { } if (!arg_no_legend) { - 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"); + const char *on, *off; + + if (n_shown) { + printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n" + "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n" + "SUB = The low-level unit activation state, values depend on unit type.\n" + "JOB = Pending job for the unit.\n\n"); + on = ansi_highlight(true); + off = ansi_highlight(false); + } else { + on = ansi_highlight_red(true); + off = ansi_highlight_red(false); + } if (arg_all) - printf("\n%u loaded units listed.\n" - "To show all installed unit files use 'systemctl list-unit-files'.\n", n_shown); + printf("%s%u loaded units listed.%s\n" + "To show all installed unit files use 'systemctl list-unit-files'.\n", + on, n_shown, off); else - printf("\n%u loaded units listed. Pass --all to see loaded but inactive units, too.\n" - "To show all installed unit files use 'systemctl list-unit-files'.\n", n_shown); + printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n" + "To show all installed unit files use 'systemctl list-unit-files'.\n", + on, n_shown, off); } } @@ -1322,7 +1341,10 @@ static int wait_for_jobs(DBusConnection *bus, Set *s) { return -ECONNREFUSED; } - if (!arg_quiet && d.result) { + if (!d.result) + goto free_name; + + if (!arg_quiet) { if (streq(d.result, "timeout")) log_error("Job for %s timed out.", strna(d.name)); else if (streq(d.result, "canceled")) @@ -1343,11 +1365,12 @@ static int wait_for_jobs(DBusConnection *bus, Set *s) { free(d.result); d.result = NULL; + free_name: free(d.name); d.name = NULL; } - /* This is slightly dirty, since we don't undo the filter registration. */ + dbus_connection_remove_filter(bus, wait_filter, &d); return r; } @@ -1441,7 +1464,7 @@ static void check_triggering_units( DBusConnection *bus, const char *unit_name) { - DBusMessage _cleanup_dbus_msg_unref_ *reply = NULL; + _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; DBusMessageIter iter, sub; char *service_trigger = NULL; const char *interface = "org.freedesktop.systemd1.Unit", @@ -1452,10 +1475,14 @@ static void check_triggering_units( int r; n = unit_name_mangle(unit_name); - unit_path = unit_dbus_path_from_name(n ? n : unit_name); + if (!n) { + log_oom(); + return; + } + unit_path = unit_dbus_path_from_name(n); if (!unit_path) { - log_error("Could not allocate dbus path."); + log_oom(); return; } @@ -1515,7 +1542,7 @@ static int start_unit_one( DBusError *error, Set *s) { - DBusMessage *reply = NULL; + _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; const char *path; int r; _cleanup_free_ char *n, *p = NULL; @@ -1548,15 +1575,14 @@ static int start_unit_one( else log_error("Failed to issue method call: %s", bus_error_message(error)); - goto finish; + return r; } if (!dbus_message_get_args(reply, error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) { log_error("Failed to parse reply: %s", bus_error_message(error)); - r = -EIO; - goto finish; + return -EIO; } if (need_daemon_reload(bus, n)) @@ -1565,32 +1591,19 @@ static int start_unit_one( if (s) { p = strdup(path); - if (!p) { - r = log_oom(); - goto finish; - } + if (!p) + return log_oom(); r = set_put(s, p); if (r < 0) { log_error("Failed to add path to set."); - goto finish; + return r; } p = NULL; } - /* When stopping a unit warn if it can still be triggered by - * another active unit (socket, path, timer) */ - if (!arg_quiet && streq(method, "StopUnit")) - check_triggering_units(bus, name); - - r = 0; - -finish: - if (reply) - dbus_message_unref(reply); - - return r; + return 0; } static enum action verb_to_action(const char *verb) { @@ -1724,6 +1737,16 @@ static int start_unit(DBusConnection *bus, char **args) { ret = r; goto finish; } + + /* When stopping units, warn if they can still be triggered by + * another active unit (socket, path, timer) */ + if (!arg_quiet && streq(method, "StopUnit")) { + if (one_name) + check_triggering_units(bus, one_name); + else + STRV_FOREACH(name, args+1) + check_triggering_units(bus, *name); + } } finish: @@ -2270,14 +2293,19 @@ static void print_status_info(UnitStatusInfo *i) { if (i->id && arg_transport != TRANSPORT_SSH) { int flags = arg_all * OUTPUT_SHOW_ALL | + (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | + on_tty() * OUTPUT_COLOR | arg_follow * OUTPUT_FOLLOW | - !arg_quiet * OUTPUT_WARN_CUTOFF | - on_tty() * OUTPUT_COLOR; + !arg_quiet * OUTPUT_WARN_CUTOFF; printf("\n"); - show_journal_by_unit(i->id, arg_output, 0, + show_journal_by_unit(stdout, + i->id, + arg_output, + 0, i->inactive_exit_timestamp_monotonic, - arg_lines, flags); + arg_lines, + flags); } if (i->need_daemon_reload) @@ -3567,7 +3595,15 @@ static int mangle_names(char **original_names, char ***mangled_names) { i = l; STRV_FOREACH(name, original_names) { - *i = unit_name_mangle(*name); + + /* When enabling units qualified path names are OK, + * too, hence allow them explicitly. */ + + if (is_path(*name)) + *i = strdup(*name); + else + *i = unit_name_mangle(*name); + if (!*i) { strv_free(l); return log_oom(); @@ -3912,7 +3948,7 @@ static int systemctl_help(void) { " -n --lines=INTEGER Journal entries to show\n" " --follow Follow journal\n" " -o --output=STRING Change journal output mode (short, short-monotonic,\n" - " verbose, export, json, json-pretty, cat)\n\n" + " verbose, export, json, json-pretty, json-sse, cat)\n\n" "Unit Commands:\n" " list-units List loaded units\n" " start [NAME...] Start (activate) one or more units\n"