static int show_timesync_status_once(sd_bus *bus) { static const struct bus_properties_map map_timesync[] = { { "ServerName", "s", NULL, offsetof(NTPStatusInfo, server_name) }, { "ServerAddress", "(iay)", map_server_address, offsetof(NTPStatusInfo, server_address) }, { "PollIntervalUSec", "t", NULL, offsetof(NTPStatusInfo, poll_interval) }, { "PollIntervalMinUSec", "t", NULL, offsetof(NTPStatusInfo, poll_min) }, { "PollIntervalMaxUSec", "t", NULL, offsetof(NTPStatusInfo, poll_max) }, { "RootDistanceMaxUSec", "t", NULL, offsetof(NTPStatusInfo, root_distance_max) }, { "NTPMessage", "(uuuuittayttttbtt)", map_ntp_message, 0 }, { "Frequency", "x", NULL, offsetof(NTPStatusInfo, freq) }, {} }; _cleanup_(ntp_status_info_clear) NTPStatusInfo info = {}; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; int r; assert(bus); r = bus_map_all_properties(bus, "org.freedesktop.timesync1", "/org/freedesktop/timesync1", map_timesync, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &info); if (r < 0) return log_error_errno(r, "Failed to query server: %s", bus_error_message(&error, r)); if (arg_monitor && !terminal_is_dumb()) fputs(ANSI_HOME_CLEAR, stdout); print_ntp_status_info(&info); return 0; }
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'); } }
int pager_open(bool no_pager, bool jump_to_end) { _cleanup_close_pair_ int fd[2] = { -1, -1 }; const char *pager; pid_t parent_pid; if (no_pager) return 0; if (pager_pid > 0) return 1; if (terminal_is_dumb()) return 0; pager = getenv("SYSTEMD_PAGER"); if (!pager) pager = getenv("PAGER"); /* If the pager is explicitly turned off, honour it */ if (pager && STR_IN_SET(pager, "", "cat")) return 0; /* Determine and cache number of columns before we spawn the * pager so that we get the value from the actual tty */ (void) columns(); if (pipe(fd) < 0) return log_error_errno(errno, "Failed to create pager pipe: %m"); parent_pid = getpid(); pager_pid = fork(); if (pager_pid < 0) return log_error_errno(errno, "Failed to fork pager: %m"); /* In the child start the pager */ if (pager_pid == 0) { const char* less_opts, *less_charset; (void) reset_all_signal_handlers(); (void) reset_signal_mask(); (void) dup2(fd[0], STDIN_FILENO); safe_close_pair(fd); /* Initialize a good set of less options */ less_opts = getenv("SYSTEMD_LESS"); if (!less_opts) less_opts = "FRSXMK"; if (jump_to_end) less_opts = strjoina(less_opts, " +G"); setenv("LESS", less_opts, 1); /* Initialize a good charset for less. This is * particularly important if we output UTF-8 * characters. */ less_charset = getenv("SYSTEMD_LESSCHARSET"); if (!less_charset && is_locale_utf8()) less_charset = "utf-8"; if (less_charset) setenv("LESSCHARSET", less_charset, 1); /* Make sure the pager goes away when the parent dies */ if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0) _exit(EXIT_FAILURE); /* Check whether our parent died before we were able * to set the death signal */ if (getppid() != parent_pid) _exit(EXIT_SUCCESS); if (pager) { execlp(pager, pager, NULL); execl("/bin/sh", "sh", "-c", pager, NULL); } /* Debian's alternatives command for pagers is * called 'pager'. Note that we do not call * sensible-pagers here, since that is just a * shell script that implements a logic that * is similar to this one anyway, but is * Debian-specific. */ execlp("pager", "pager", NULL); execlp("less", "less", NULL); execlp("more", "more", NULL); pager_fallback(); /* not reached */ } /* Return in the parent */ if (dup2(fd[1], STDOUT_FILENO) < 0) return log_error_errno(errno, "Failed to duplicate pager pipe: %m"); if (dup2(fd[1], STDERR_FILENO) < 0) return log_error_errno(errno, "Failed to duplicate pager pipe: %m"); return 1; }