X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fsystemctl%2Fsystemctl.c;h=3cb10964fd22f504513c453d46e274d655efa5a7;hp=f3952653e153698352bae410bc6cb9f6a565eef4;hb=15ef11449b31e2fc91f2172c8afedb20ecad5ae9;hpb=df31a6c0fe07805cb50045fbe91c2a6e7e430562 diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index f3952653e..3cb10964f 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -1031,7 +1031,7 @@ static bool output_show_unit_file(const UnitFileList *u, char **patterns) { } static void output_unit_file_list(const UnitFileList *units, unsigned c) { - unsigned max_id_len, id_cols, state_cols, n_shown = 0; + unsigned max_id_len, id_cols, state_cols; const UnitFileList *u; max_id_len = sizeof("UNIT FILE")-1; @@ -1062,8 +1062,6 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) { const char *on, *off; const char *id; - n_shown++; - if (u->state == UNIT_FILE_MASKED || u->state == UNIT_FILE_MASKED_RUNTIME || u->state == UNIT_FILE_DISABLED || @@ -1086,7 +1084,7 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) { } if (!arg_no_legend) - printf("\n%u unit files listed.\n", n_shown); + printf("\n%u unit files listed.\n", c); } static int list_unit_files(sd_bus *bus, char **args) { @@ -1414,6 +1412,228 @@ static int list_dependencies(sd_bus *bus, char **args) { return list_dependencies_one(bus, u, 0, &units, 0); } +struct machine_info { + bool is_host; + char *name; + char *state; + char *control_group; + uint32_t n_failed_units; + uint32_t n_jobs; + usec_t timestamp; +}; + +static const struct bus_properties_map machine_info_property_map[] = { + { "SystemState", "s", NULL, offsetof(struct machine_info, state) }, + { "NJobs", "u", NULL, offsetof(struct machine_info, n_jobs) }, + { "NFailedUnits", "u", NULL, offsetof(struct machine_info, n_failed_units) }, + { "ControlGroup", "s", NULL, offsetof(struct machine_info, control_group) }, + { "UserspaceTimestamp", "t", NULL, offsetof(struct machine_info, timestamp) }, + {} +}; + +static void free_machines_list(struct machine_info *machine_infos, int n) { + int i; + + if (!machine_infos) + return; + + for (i = 0; i < n; i++) { + free(machine_infos[i].name); + free(machine_infos[i].state); + free(machine_infos[i].control_group); + } + + free(machine_infos); +} + +static int compare_machine_info(const void *a, const void *b) { + const struct machine_info *u = a, *v = b; + + if (u->is_host != v->is_host) + return u->is_host > v->is_host ? 1 : -1; + + return strcasecmp(u->name, v->name); +} + +static int get_machine_properties(sd_bus *bus, struct machine_info *mi) { + _cleanup_bus_unref_ sd_bus *container = NULL; + int r; + + assert(mi); + + if (!bus) { + r = sd_bus_open_system_container(&container, mi->name); + if (r < 0) + return r; + + bus = container; + } + + r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map, mi); + if (r < 0) + return r; + + return 0; +} + +static bool output_show_machine(const char *name, char **patterns) { + char **i; + + assert(name); + + if (strv_isempty(patterns)) + return true; + + STRV_FOREACH(i, patterns) + if (fnmatch(*i, name, FNM_NOESCAPE) == 0) + return true; + + return false; +} + +static int get_machine_list( + sd_bus *bus, + struct machine_info **_machine_infos, + char **patterns) { + + struct machine_info *machine_infos = NULL; + _cleanup_strv_free_ char **m = NULL; + _cleanup_free_ char *hn = NULL; + size_t sz = 0; + char **i; + int c = 0; + + hn = gethostname_malloc(); + if (!hn) + return log_oom(); + + if (output_show_machine(hn, patterns)) { + if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) + return log_oom(); + + machine_infos[c].is_host = true; + machine_infos[c].name = hn; + hn = NULL; + + get_machine_properties(bus, &machine_infos[c]); + c++; + } + + sd_get_machine_names(&m); + STRV_FOREACH(i, m) { + _cleanup_free_ char *class = NULL; + + if (!output_show_machine(*i, patterns)) + continue; + + sd_machine_get_class(*i, &class); + if (!streq_ptr(class, "container")) + continue; + + if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) { + free_machines_list(machine_infos, c); + return log_oom(); + } + + machine_infos[c].is_host = false; + machine_infos[c].name = strdup(*i); + if (!machine_infos[c].name) { + free_machines_list(machine_infos, c); + return log_oom(); + } + + get_machine_properties(NULL, &machine_infos[c]); + c++; + } + + *_machine_infos = machine_infos; + return c; +} + +static void output_machines_list(struct machine_info *machine_infos, unsigned n) { + struct machine_info *m; + unsigned + namelen = sizeof("NAME") - 1, + statelen = sizeof("STATE") - 1, + failedlen = sizeof("FAILED") - 1, + jobslen = sizeof("JOBS") - 1; + + assert(machine_infos || n == 0); + + for (m = machine_infos; m < machine_infos + n; m++) { + namelen = MAX(namelen, strlen(m->name) + (m->is_host ? sizeof(" (host)") - 1 : 0)); + statelen = MAX(statelen, m->state ? strlen(m->state) : 0); + failedlen = MAX(failedlen, DECIMAL_STR_WIDTH(m->n_failed_units)); + jobslen = MAX(jobslen, DECIMAL_STR_WIDTH(m->n_jobs)); + } + + if (!arg_no_legend) + printf("%-*s %-*s %-*s %-*s\n", + namelen, "NAME", + statelen, "STATE", + failedlen, "FAILED", + jobslen, "JOBS"); + + for (m = machine_infos; m < machine_infos + n; m++) { + const char *on_state, *off_state, *on_failed, *off_failed; + + if (streq_ptr(m->state, "degraded")) { + on_state = ansi_highlight_red(); + off_state = ansi_highlight_off(); + } else if (!streq_ptr(m->state, "running")) { + on_state = ansi_highlight_yellow(); + off_state = ansi_highlight_off(); + } else + on_state = off_state = ""; + + if (m->n_failed_units > 0) { + on_failed = ansi_highlight_red(); + off_failed = ansi_highlight_off(); + } else + on_failed = off_failed = ""; + + if (m->is_host) + printf("%-*s (host) %s%-*s%s %s%*u%s %*u\n", + (int) (namelen - (sizeof(" (host)")-1)), strna(m->name), + on_state, statelen, strna(m->state), off_state, + on_failed, failedlen, m->n_failed_units, off_failed, + jobslen, m->n_jobs); + else + printf("%-*s %s%-*s%s %s%*u%s %*u\n", + namelen, strna(m->name), + on_state, statelen, strna(m->state), off_state, + on_failed, failedlen, m->n_failed_units, off_failed, + jobslen, m->n_jobs); + } + + if (!arg_no_legend) + printf("\n%u machines listed.\n", n); +} + +static int list_machines(sd_bus *bus, char **args) { + struct machine_info *machine_infos = NULL; + int r; + + assert(bus); + + if (geteuid() != 0) { + log_error("Must be root."); + return -EPERM; + } + + pager_open_if_enabled(); + + r = get_machine_list(bus, &machine_infos, strv_skip_first(args)); + if (r < 0) + return r; + + qsort_safe(machine_infos, r, sizeof(struct machine_info), compare_machine_info); + output_machines_list(machine_infos, r); + free_machines_list(machine_infos, r); + + return 0; +} + static int get_default(sd_bus *bus, char **args) { _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; @@ -1625,16 +1845,17 @@ static void output_jobs_list(const struct job_info* jobs, unsigned n, bool skipp } static bool output_show_job(struct job_info *job, char **patterns) { - if (!strv_isempty(patterns)) { - char **pattern; + char **pattern; - STRV_FOREACH(pattern, patterns) - if (fnmatch(*pattern, job->name, FNM_NOESCAPE) == 0) - return true; - return false; - } + assert(job); - return true; + if (strv_isempty(patterns)) + return true; + + STRV_FOREACH(pattern, patterns) + if (fnmatch(*pattern, job->name, FNM_NOESCAPE) == 0) + return true; + return false; } static int list_jobs(sd_bus *bus, char **args) { @@ -2742,7 +2963,7 @@ static void print_status_info( bool *ellipsized) { ExecStatusInfo *p; - const char *on, *off, *ss; + const char *active_on, *active_off, *on, *off, *ss; usec_t timestamp; char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1; char since2[FORMAT_TIMESTAMP_MAX], *s2; @@ -2760,7 +2981,16 @@ static void print_status_info( /* This shows pretty information about a unit. See * print_property() for a low-level property printer */ - printf("%s", strna(i->id)); + if (streq_ptr(i->active_state, "failed")) { + active_on = ansi_highlight_red(); + active_off = ansi_highlight_off(); + } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) { + active_on = ansi_highlight_green(); + active_off = ansi_highlight_off(); + } else + active_on = active_off = ""; + + printf("%s%s%s%s", active_on, draw_special_char(DRAW_BLACK_CIRCLE), active_off, strna(i->id)); if (i->description && !streq_ptr(i->id, i->description)) printf(" - %s", i->description); @@ -2819,22 +3049,12 @@ static void print_status_info( } ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state; - - if (streq_ptr(i->active_state, "failed")) { - on = ansi_highlight_red(); - off = ansi_highlight_off(); - } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) { - on = ansi_highlight_green(); - off = ansi_highlight_off(); - } else - on = off = ""; - if (ss) printf(" Active: %s%s (%s)%s", - on, strna(i->active_state), ss, off); + active_on, strna(i->active_state), ss, active_off); else printf(" Active: %s%s%s", - on, strna(i->active_state), off); + active_on, strna(i->active_state), active_off); if (!isempty(i->result) && !streq(i->result, "success")) printf(" (Result: %s)", i->result); @@ -2978,7 +3198,8 @@ static void print_status_info( printf(" Status: \"%s\"\n", i->status_text); if (i->control_group && - (i->main_pid > 0 || i->control_pid > 0 || cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, i->control_group, false) == 0)) { + (i->main_pid > 0 || i->control_pid > 0 || + ((arg_transport != BUS_TRANSPORT_LOCAL && arg_transport != BUS_TRANSPORT_CONTAINER) || cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, i->control_group, false) == 0))) { unsigned c; printf(" CGroup: %s\n", i->control_group); @@ -2986,7 +3207,7 @@ static void print_status_info( if (arg_transport == BUS_TRANSPORT_LOCAL || arg_transport == BUS_TRANSPORT_CONTAINER) { unsigned k = 0; pid_t extra[2]; - char prefix[] = " "; + static const char prefix[] = " "; c = columns(); if (c > sizeof(prefix) - 1) @@ -3000,8 +3221,7 @@ static void print_status_info( if (i->control_pid > 0) extra[k++] = i->control_pid; - show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, - c, false, extra, k, flags); + show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, false, extra, k, flags); } } @@ -3842,13 +4062,162 @@ static int show_all( return log_oom(); r = show_one(verb, bus, p, show_properties, new_line, ellipsized); - if (r != 0) + if (r < 0) return r; } return 0; } +static int show_system_status(sd_bus *bus) { + char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; + _cleanup_free_ char *hn = NULL; + struct machine_info mi = {}; + const char *on, *off; + int r; + + hn = gethostname_malloc(); + if (!hn) + return log_oom(); + + r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map, &mi); + if (r < 0) { + log_error("Failed to read server status: %s", strerror(-r)); + return r; + } + + if (streq_ptr(mi.state, "degraded")) { + on = ansi_highlight_red(); + off = ansi_highlight_off(); + } else if (!streq_ptr(mi.state, "running")) { + on = ansi_highlight_yellow(); + off = ansi_highlight_off(); + } else + on = off = ""; + + printf("%s%s%s%s\n", on, draw_special_char(DRAW_BLACK_CIRCLE), off, arg_host ? arg_host : hn); + + printf(" State: %s%s%s\n", + on, strna(mi.state), off); + + printf(" Jobs: %u queued\n", mi.n_jobs); + printf(" Failed: %u units\n", mi.n_failed_units); + + printf(" Since: %s; %s\n", + format_timestamp(since2, sizeof(since2), mi.timestamp), + format_timestamp_relative(since1, sizeof(since1), mi.timestamp)); + + printf(" CGroup: %s\n", mi.control_group ?: "/"); + if (arg_transport == BUS_TRANSPORT_LOCAL || arg_transport == BUS_TRANSPORT_CONTAINER) { + int flags = + arg_all * OUTPUT_SHOW_ALL | + (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | + on_tty() * OUTPUT_COLOR | + !arg_quiet * OUTPUT_WARN_CUTOFF | + arg_full * OUTPUT_FULL_WIDTH; + + static const char prefix[] = " "; + unsigned c; + + c = columns(); + if (c > sizeof(prefix) - 1) + c -= sizeof(prefix) - 1; + else + c = 0; + + show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, false, flags); + } + + free(mi.state); + free(mi.control_group); + + return 0; +} + +static int show(sd_bus *bus, char **args) { + bool show_properties, show_status, new_line = false; + bool ellipsized = false; + int r, ret = 0; + + assert(bus); + assert(args); + + show_properties = streq(args[0], "show"); + show_status = streq(args[0], "status"); + + if (show_properties) + pager_open_if_enabled(); + + /* If no argument is specified inspect the manager itself */ + + if (show_properties && strv_length(args) <= 1) + return show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line, &ellipsized); + + if (show_status && strv_length(args) <= 1) { + + if (arg_all) + pager_open_if_enabled(); + + show_system_status(bus); + new_line = true; + + if (arg_all) + ret = show_all(args[0], bus, false, &new_line, &ellipsized); + } else { + _cleanup_free_ char **patterns = NULL; + char **name; + + STRV_FOREACH(name, args + 1) { + _cleanup_free_ char *unit = NULL; + uint32_t id; + + if (safe_atou32(*name, &id) < 0) { + if (strv_push(&patterns, *name) < 0) + return log_oom(); + + continue; + } else if (show_properties) { + /* Interpret as job id */ + if (asprintf(&unit, "/org/freedesktop/systemd1/job/%u", id) < 0) + return log_oom(); + + } else { + /* Interpret as PID */ + r = get_unit_dbus_path_by_pid(bus, id, &unit); + if (r < 0) { + ret = r; + continue; + } + } + + show_one(args[0], bus, unit, show_properties, &new_line, &ellipsized); + } + + if (!strv_isempty(patterns)) { + _cleanup_strv_free_ char **names = NULL; + + r = expand_names(bus, patterns, NULL, &names); + if (r < 0) + log_error("Failed to expand names: %s", strerror(-r)); + + STRV_FOREACH(name, names) { + _cleanup_free_ char *unit; + + unit = unit_dbus_path_from_name(*name); + if (!unit) + return log_oom(); + + show_one(args[0], bus, unit, show_properties, &new_line, &ellipsized); + } + } + } + + if (ellipsized && !arg_quiet) + printf("Hint: Some lines were ellipsized, use -l to show in full.\n"); + + return ret; +} + static int cat(sd_bus *bus, char **args) { _cleanup_free_ char *unit = NULL; _cleanup_strv_free_ char **names = NULL; @@ -3943,82 +4312,6 @@ static int cat(sd_bus *bus, char **args) { return r < 0 ? r : 0; } -static int show(sd_bus *bus, char **args) { - bool show_properties, show_status, new_line = false; - bool ellipsized = false; - int r, ret = 0; - - assert(bus); - assert(args); - - show_properties = streq(args[0], "show"); - show_status = streq(args[0], "status"); - - if (show_properties) - pager_open_if_enabled(); - - /* If no argument is specified inspect the manager itself */ - - if (show_properties && strv_length(args) <= 1) - return show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line, &ellipsized); - - if (show_status && strv_length(args) <= 1) - ret = show_all(args[0], bus, false, &new_line, &ellipsized); - else { - _cleanup_free_ char **patterns = NULL; - char **name; - - STRV_FOREACH(name, args + 1) { - _cleanup_free_ char *unit = NULL; - uint32_t id; - - if (safe_atou32(*name, &id) < 0) { - if (strv_push(&patterns, *name) < 0) - return log_oom(); - - continue; - } else if (show_properties) { - /* Interpret as job id */ - if (asprintf(&unit, "/org/freedesktop/systemd1/job/%u", id) < 0) - return log_oom(); - - } else { - /* Interpret as PID */ - r = get_unit_dbus_path_by_pid(bus, id, &unit); - if (r < 0) { - ret = r; - continue; - } - } - - show_one(args[0], bus, unit, show_properties, &new_line, &ellipsized); - } - - if (!strv_isempty(patterns)) { - _cleanup_strv_free_ char **names = NULL; - - r = expand_names(bus, patterns, NULL, &names); - if (r < 0) - log_error("Failed to expand names: %s", strerror(-r)); - - STRV_FOREACH(name, names) { - _cleanup_free_ char *unit; - - unit = unit_dbus_path_from_name(*name); - if (!unit) - return log_oom(); - - show_one(args[0], bus, unit, show_properties, &new_line, &ellipsized); - } - } - } - - if (ellipsized && !arg_quiet) - printf("Hint: Some lines were ellipsized, use -l to show in full.\n"); - - return ret; -} - static int set_property(sd_bus *bus, char **args) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; @@ -4282,8 +4575,8 @@ static int show_environment(sd_bus *bus, char **args) { static int switch_root(sd_bus *bus, char **args) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_free_ char *init = NULL; - const char *root; + _cleanup_free_ char *cmdline_init = NULL; + const char *root, *init; unsigned l; int r; @@ -4296,20 +4589,33 @@ static int switch_root(sd_bus *bus, char **args) { root = args[1]; if (l >= 3) - init = strdup(args[2]); + init = args[2]; else { - parse_env_file("/proc/cmdline", WHITESPACE, - "init", &init, - NULL); + r = parse_env_file("/proc/cmdline", WHITESPACE, + "init", &cmdline_init, + NULL); + if (r < 0) + log_debug("Failed to parse /proc/cmdline: %s", strerror(-r)); - if (!init) - init = strdup(""); + init = cmdline_init; } - if (!init) - return log_oom(); + if (isempty(init)) + init = NULL; + + if (init) { + const char *root_systemd_path = NULL, *root_init_path = NULL; + + root_systemd_path = strappenda(root, "/" SYSTEMD_BINARY_PATH); + root_init_path = strappenda3(root, "/", init); + + /* If the passed init is actually the same as the + * systemd binary, then let's suppress it. */ + if (files_same(root_init_path, root_systemd_path) > 0) + init = NULL; + } - log_debug("switching root - root: %s; init: %s", root, init); + log_debug("Switching root - root: %s; init: %s", root, strna(init)); r = sd_bus_call_method( bus, @@ -4927,6 +5233,8 @@ static int systemctl_help(void) { " the search path\n" " get-default Get the name of the default target\n" " set-default NAME Set the default target\n\n" + "Machine Commands:\n" + " list-machines [PATTERN...] List local containers and host\n\n" "Job Commands:\n" " list-jobs [PATTERN...] List jobs\n" " cancel [JOB...] Cancel all, one, or more jobs\n\n" @@ -5869,6 +6177,7 @@ static int systemctl_main(sd_bus *bus, int argc, char *argv[], int bus_error) { { "list-sockets", MORE, 1, list_sockets }, { "list-timers", MORE, 1, list_timers }, { "list-jobs", MORE, 1, list_jobs }, + { "list-machines", MORE, 1, list_machines }, { "clear-jobs", EQUAL, 1, daemon_reload }, { "cancel", MORE, 2, cancel_job }, { "start", MORE, 2, start_unit },