static void operational_state_to_color(const char *state, const char **on, const char **off) { assert(on); assert(off); if (streq_ptr(state, "routable")) { *on = ansi_highlight_green(); *off = ansi_normal(); } else if (streq_ptr(state, "degraded")) { *on = ansi_highlight_yellow(); *off = ansi_normal(); } else *on = *off = ""; }
static void setup_state_to_color(const char *state, const char **on, const char **off) { assert(on); assert(off); if (streq_ptr(state, "configured")) { *on = ansi_highlight_green(); *off = ansi_normal(); } else if (streq_ptr(state, "configuring")) { *on = ansi_highlight_yellow(); *off = ansi_normal(); } else if (streq_ptr(state, "failed") || streq_ptr(state, "linger")) { *on = ansi_highlight_red(); *off = ansi_normal(); } else *on = *off = ""; }
static int notify_override_extended(const char *top, const char *bottom) { if (!(arg_flags & SHOW_EXTENDED)) return 0; printf("%s%s%s %s %s %s\n", ansi_highlight(), "[EXTENDED]", ansi_normal(), top, special_glyph(ARROW), bottom); return 1; }
static int notify_override_overridden(const char *top, const char *bottom) { if (!(arg_flags & SHOW_OVERRIDDEN)) return 0; printf("%s%s%s %s %s %s\n", ansi_highlight(), "[OVERRIDDEN]", ansi_normal(), top, special_glyph(ARROW), bottom); return 1; }
static int notify_override_redirected(const char *top, const char *bottom) { if (!(arg_flags & SHOW_REDIRECTED)) return 0; printf("%s%s%s %s %s %s\n", ansi_highlight(), "[REDIRECTED]", ansi_normal(), top, special_glyph(ARROW), bottom); return 1; }
static int notify_override_equivalent(const char *top, const char *bottom) { if (!(arg_flags & SHOW_EQUIVALENT)) return 0; printf("%s%s%s %s %s %s\n", ansi_highlight_green(), "[EQUIVALENT]", ansi_normal(), top, special_glyph(ARROW), bottom); return 1; }
static int notify_override_masked(const char *top, const char *bottom) { if (!(arg_flags & SHOW_MASKED)) return 0; printf("%s%s%s %s %s %s\n", ansi_highlight_red(), "[MASKED]", ansi_normal(), top, special_glyph(ARROW), bottom); return 1; }
static void display(Hashmap *a) { Iterator i; Group *g; Group **array; signed path_columns; unsigned rows, n = 0, j, maxtcpu = 0, maxtpath = 3; /* 3 for ellipsize() to work properly */ char buffer[MAX3(21, FORMAT_BYTES_MAX, FORMAT_TIMESPAN_MAX)]; assert(a); if (!terminal_is_dumb()) fputs(ANSI_HOME_CLEAR, stdout); array = alloca(sizeof(Group*) * hashmap_size(a)); HASHMAP_FOREACH(g, a, i) if (g->n_tasks_valid || g->cpu_valid || g->memory_valid || g->io_valid) array[n++] = g; qsort_safe(array, n, sizeof(Group*), group_compare); /* Find the longest names in one run */ for (j = 0; j < n; j++) { unsigned cputlen, pathtlen; format_timespan(buffer, sizeof(buffer), (usec_t) (array[j]->cpu_usage / NSEC_PER_USEC), 0); cputlen = strlen(buffer); maxtcpu = MAX(maxtcpu, cputlen); pathtlen = strlen(array[j]->path); maxtpath = MAX(maxtpath, pathtlen); } if (arg_cpu_type == CPU_PERCENT) xsprintf(buffer, "%6s", "%CPU"); else xsprintf(buffer, "%*s", maxtcpu, "CPU Time"); rows = lines(); if (rows <= 10) rows = 10; if (on_tty()) { const char *on, *off; path_columns = columns() - 36 - strlen(buffer); if (path_columns < 10) path_columns = 10; on = ansi_highlight_underline(); off = ansi_underline(); printf("%s%s%-*s%s %s%7s%s %s%s%s %s%8s%s %s%8s%s %s%8s%s%s\n", ansi_underline(), arg_order == ORDER_PATH ? on : "", path_columns, "Control Group", arg_order == ORDER_PATH ? off : "", arg_order == ORDER_TASKS ? on : "", arg_count == COUNT_PIDS ? "Tasks" : arg_count == COUNT_USERSPACE_PROCESSES ? "Procs" : "Proc+", arg_order == ORDER_TASKS ? off : "", arg_order == ORDER_CPU ? on : "", buffer, arg_order == ORDER_CPU ? off : "", arg_order == ORDER_MEMORY ? on : "", "Memory", arg_order == ORDER_MEMORY ? off : "", arg_order == ORDER_IO ? on : "", "Input/s", arg_order == ORDER_IO ? off : "", arg_order == ORDER_IO ? on : "", "Output/s", arg_order == ORDER_IO ? off : "", ansi_normal()); } else path_columns = maxtpath; for (j = 0; j < n; j++) { _cleanup_free_ char *ellipsized = NULL; const char *path; if (on_tty() && j + 6 > rows) break; g = array[j]; path = isempty(g->path) ? "/" : g->path; ellipsized = ellipsize(path, path_columns, 33); printf("%-*s", path_columns, ellipsized ?: path); if (g->n_tasks_valid) printf(" %7" PRIu64, g->n_tasks); else fputs(" -", stdout); if (arg_cpu_type == CPU_PERCENT) { if (g->cpu_valid) printf(" %6.1f", g->cpu_fraction*100); else fputs(" -", stdout); } else printf(" %*s", maxtcpu, format_timespan(buffer, sizeof(buffer), (usec_t) (g->cpu_usage / NSEC_PER_USEC), 0)); printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->memory_valid, g->memory)); printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->io_valid, g->io_input_bps)); printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->io_valid, g->io_output_bps)); putchar('\n'); } }
static void print_status_info(const StatusInfo *i) { char a[FORMAT_TIMESTAMP_MAX]; struct tm tm; time_t sec; bool have_time = false; const char *old_tz = NULL, *tz; int r; assert(i); /* Save the old $TZ */ tz = getenv("TZ"); if (tz) old_tz = strdupa(tz); /* Set the new $TZ */ if (setenv("TZ", isempty(i->timezone) ? "UTC" : i->timezone, true) < 0) log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m"); else tzset(); if (i->time != 0) { sec = (time_t) (i->time / USEC_PER_SEC); have_time = true; } else if (IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) { sec = time(NULL); have_time = true; } else log_warning("Could not get time from timedated and not operating locally, ignoring."); if (have_time) { xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)); printf(" Local time: %.*s\n", (int) sizeof(a), a); xstrftime(a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)); printf(" Universal time: %.*s\n", (int) sizeof(a), a); } else { printf(" Local time: %s\n", "n/a"); printf(" Universal time: %s\n", "n/a"); } if (i->rtc_time > 0) { time_t rtc_sec; rtc_sec = (time_t) (i->rtc_time / USEC_PER_SEC); xstrftime(a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm)); printf(" RTC time: %.*s\n", (int) sizeof(a), a); } else printf(" RTC time: %s\n", "n/a"); if (have_time) xstrftime(a, "%Z, %z", localtime_r(&sec, &tm)); /* Restore the $TZ */ if (old_tz) r = setenv("TZ", old_tz, true); else r = unsetenv("TZ"); if (r < 0) log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m"); else tzset(); printf(" Time zone: %s (%.*s)\n" " Network time on: %s\n" "NTP synchronized: %s\n" " RTC in local TZ: %s\n", strna(i->timezone), (int) sizeof(a), have_time ? a : "n/a", i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a", yes_no(i->ntp_synced), yes_no(i->rtc_local)); if (i->rtc_local) printf("\n%s" "Warning: The system is configured to read the RTC time in the local time zone.\n" " This mode can not be fully supported. It will create various problems\n" " with time zone changes and daylight saving time adjustments. The RTC\n" " time is never updated, it relies on external facilities to maintain it.\n" " If at all possible, use RTC in UTC by calling\n" " 'timedatectl set-local-rtc 0'.%s\n", ansi_highlight(), ansi_normal()); }
static int inspect_image(int argc, char *argv[], void *userdata) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; _cleanup_strv_free_ char **matches = NULL; _cleanup_free_ char *image = NULL; bool nl = false, header = false; const void *data; const char *path; size_t sz; int r; r = determine_image(argv[1], false, &image); if (r < 0) return r; r = determine_matches(argv[1], argv + 2, true, &matches); if (r < 0) return r; r = acquire_bus(&bus); if (r < 0) return r; r = sd_bus_message_new_method_call( bus, &m, "org.freedesktop.portable1", "/org/freedesktop/portable1", "org.freedesktop.portable1.Manager", "GetImageMetadata"); if (r < 0) return bus_log_create_error(r); r = sd_bus_message_append(m, "s", image); if (r < 0) return bus_log_create_error(r); r = sd_bus_message_append_strv(m, matches); if (r < 0) return bus_log_create_error(r); r = sd_bus_call(bus, m, 0, &error, &reply); if (r < 0) return log_error_errno(r, "Failed to inspect image metadata: %s", bus_error_message(&error, r)); r = sd_bus_message_read(reply, "s", &path); if (r < 0) return bus_log_parse_error(r); r = sd_bus_message_read_array(reply, 'y', &data, &sz); if (r < 0) return bus_log_parse_error(r); (void) pager_open(arg_pager_flags); if (arg_cat) { printf("%s-- OS Release: --%s\n", ansi_highlight(), ansi_normal()); fwrite(data, sz, 1, stdout); fflush(stdout); nl = true; } else { _cleanup_free_ char *pretty_portable = NULL, *pretty_os = NULL; _cleanup_fclose_ FILE *f; f = fmemopen_unlocked((void*) data, sz, "re"); if (!f) return log_error_errno(errno, "Failed to open /etc/os-release buffer: %m"); r = parse_env_file(f, "/etc/os-release", "PORTABLE_PRETTY_NAME", &pretty_portable, "PRETTY_NAME", &pretty_os); if (r < 0) return log_error_errno(r, "Failed to parse /etc/os-release: %m"); printf("Image:\n\t%s\n" "Portable Service:\n\t%s\n" "Operating System:\n\t%s\n", path, strna(pretty_portable), strna(pretty_os)); } r = sd_bus_message_enter_container(reply, 'a', "{say}"); if (r < 0) return bus_log_parse_error(r); for (;;) { const char *name; r = sd_bus_message_enter_container(reply, 'e', "say"); if (r < 0) return bus_log_parse_error(r); if (r == 0) break; r = sd_bus_message_read(reply, "s", &name); if (r < 0) return bus_log_parse_error(r); r = sd_bus_message_read_array(reply, 'y', &data, &sz); if (r < 0) return bus_log_parse_error(r); if (arg_cat) { if (nl) fputc('\n', stdout); printf("%s-- Unit file: %s --%s\n", ansi_highlight(), name, ansi_normal()); fwrite(data, sz, 1, stdout); fflush(stdout); nl = true; } else { if (!header) { fputs("Unit files:\n", stdout); header = true; } fputc('\t', stdout); fputs(name, stdout); fputc('\n', stdout); } r = sd_bus_message_exit_container(reply); if (r < 0) return bus_log_parse_error(r); } r = sd_bus_message_exit_container(reply); if (r < 0) return bus_log_parse_error(r); return 0; }