| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fnmatch.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-bus.h" |
| 7 |
#include "sd-login.h" |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "ansi-color.h" |
| 11 |
#include "bus-map-properties.h" |
| 12 |
#include "format-table.h" |
| 13 |
#include "glyph-util.h" |
| 14 |
#include "hostname-setup.h" |
| 15 |
#include "log.h" |
| 16 |
#include "memory-util.h" |
| 17 |
#include "sort-util.h" |
| 18 |
#include "string-util.h" |
| 19 |
#include "strv.h" |
| 20 |
#include "systemctl.h" |
| 21 |
#include "systemctl-list-machines.h" |
| 22 |
#include "systemctl-util.h" |
| 23 |
|
| 24 |
const struct bus_properties_map machine_info_property_map[] = { |
| 25 |
/* Might good to keep same order here as in bus_manager_vtable[], server side */ |
| 26 |
{ "Version", "s", NULL, offsetof(struct machine_info, version) }, |
| 27 |
{ "Tainted", "s", NULL, offsetof(struct machine_info, tainted) }, |
| 28 |
{ "UserspaceTimestamp", "t", NULL, offsetof(struct machine_info, timestamp) }, |
| 29 |
{ "NNames", "u", NULL, offsetof(struct machine_info, n_names) }, |
| 30 |
{ "NFailedUnits", "u", NULL, offsetof(struct machine_info, n_failed_units) }, |
| 31 |
{ "NJobs", "u", NULL, offsetof(struct machine_info, n_jobs) }, |
| 32 |
{ "ControlGroup", "s", NULL, offsetof(struct machine_info, control_group) }, |
| 33 |
{ "SystemState", "s", NULL, offsetof(struct machine_info, state) }, |
| 34 |
{} |
| 35 |
}; |
| 36 |
|
| 37 |
void machine_info_clear(struct machine_info *info) { |
| 38 |
assert(info); |
| 39 |
|
| 40 |
free(info->name); |
| 41 |
free(info->version); |
| 42 |
free(info->tainted); |
| 43 |
free(info->control_group); |
| 44 |
free(info->state); |
| 45 |
zero(*info); |
| 46 |
} |
| 47 |
|
| 48 |
static void free_machines_list(struct machine_info *machine_infos, int n) { |
| 49 |
if (!machine_infos) |
| 50 |
return; |
| 51 |
|
| 52 |
for (int i = 0; i < n; i++) |
| 53 |
machine_info_clear(&machine_infos[i]); |
| 54 |
|
| 55 |
free(machine_infos); |
| 56 |
} |
| 57 |
|
| 58 |
static int compare_machine_info(const struct machine_info *a, const struct machine_info *b) { |
| 59 |
int r; |
| 60 |
|
| 61 |
r = CMP(b->is_host, a->is_host); |
| 62 |
if (r != 0) |
| 63 |
return r; |
| 64 |
|
| 65 |
return strcasecmp(a->name, b->name); |
| 66 |
} |
| 67 |
|
| 68 |
static int get_machine_properties(sd_bus *bus, struct machine_info *mi) { |
| 69 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
| 70 |
int r; |
| 71 |
|
| 72 |
assert(mi); |
| 73 |
|
| 74 |
if (!bus) { |
| 75 |
r = sd_bus_open_system_machine(&container, mi->name); |
| 76 |
if (r < 0) |
| 77 |
return r; |
| 78 |
|
| 79 |
bus = container; |
| 80 |
} |
| 81 |
|
| 82 |
r = bus_map_all_properties( |
| 83 |
bus, |
| 84 |
"org.freedesktop.systemd1", |
| 85 |
"/org/freedesktop/systemd1", |
| 86 |
machine_info_property_map, |
| 87 |
BUS_MAP_STRDUP, |
| 88 |
NULL, |
| 89 |
NULL, |
| 90 |
mi); |
| 91 |
if (r < 0) |
| 92 |
return r; |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
static bool output_show_machine(const char *name, char **patterns) { |
| 98 |
return strv_fnmatch_or_empty(patterns, name, FNM_NOESCAPE); |
| 99 |
} |
| 100 |
|
| 101 |
static int get_machine_list( |
| 102 |
sd_bus *bus, |
| 103 |
struct machine_info **_machine_infos, |
| 104 |
char **patterns) { |
| 105 |
|
| 106 |
struct machine_info *machine_infos = NULL; |
| 107 |
_cleanup_strv_free_ char **m = NULL; |
| 108 |
_cleanup_free_ char *hn = NULL; |
| 109 |
int c = 0, r; |
| 110 |
|
| 111 |
hn = gethostname_malloc(); |
| 112 |
if (!hn) |
| 113 |
return log_oom(); |
| 114 |
|
| 115 |
if (output_show_machine(hn, patterns)) { |
| 116 |
if (!GREEDY_REALLOC0(machine_infos, c+1)) |
| 117 |
return log_oom(); |
| 118 |
|
| 119 |
machine_infos[c].is_host = true; |
| 120 |
machine_infos[c].name = TAKE_PTR(hn); |
| 121 |
|
| 122 |
(void) get_machine_properties(bus, &machine_infos[c]); |
| 123 |
c++; |
| 124 |
} |
| 125 |
|
| 126 |
r = sd_get_machine_names(&m); |
| 127 |
if (r < 0) |
| 128 |
return log_error_errno(r, "Failed to get machine list: %m"); |
| 129 |
|
| 130 |
STRV_FOREACH(i, m) { |
| 131 |
_cleanup_free_ char *class = NULL; |
| 132 |
|
| 133 |
if (!output_show_machine(*i, patterns)) |
| 134 |
continue; |
| 135 |
|
| 136 |
sd_machine_get_class(*i, &class); |
| 137 |
if (!streq_ptr(class, "container")) |
| 138 |
continue; |
| 139 |
|
| 140 |
if (!GREEDY_REALLOC0(machine_infos, c+1)) { |
| 141 |
free_machines_list(machine_infos, c); |
| 142 |
return log_oom(); |
| 143 |
} |
| 144 |
|
| 145 |
machine_infos[c].is_host = false; |
| 146 |
machine_infos[c].name = strdup(*i); |
| 147 |
if (!machine_infos[c].name) { |
| 148 |
free_machines_list(machine_infos, c); |
| 149 |
return log_oom(); |
| 150 |
} |
| 151 |
|
| 152 |
(void) get_machine_properties(NULL, &machine_infos[c]); |
| 153 |
c++; |
| 154 |
} |
| 155 |
|
| 156 |
*_machine_infos = machine_infos; |
| 157 |
return c; |
| 158 |
} |
| 159 |
|
| 160 |
static int output_machines_list(struct machine_info *machine_infos, unsigned n) { |
| 161 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 162 |
bool state_missing = false; |
| 163 |
int r; |
| 164 |
|
| 165 |
assert(machine_infos || n == 0); |
| 166 |
|
| 167 |
table = table_new("", "name", "state", "failed", "jobs"); |
| 168 |
if (!table) |
| 169 |
return log_oom(); |
| 170 |
|
| 171 |
table_set_header(table, arg_legend != 0); |
| 172 |
if (arg_plain) { |
| 173 |
/* Hide the 'glyph' column when --plain is requested */ |
| 174 |
r = table_hide_column_from_display(table, 0); |
| 175 |
if (r < 0) |
| 176 |
return log_error_errno(r, "Failed to hide column: %m"); |
| 177 |
} |
| 178 |
if (arg_full) |
| 179 |
table_set_width(table, 0); |
| 180 |
|
| 181 |
table_set_ersatz_string(table, TABLE_ERSATZ_DASH); |
| 182 |
|
| 183 |
for (struct machine_info *m = machine_infos; m < machine_infos + n; m++) { |
| 184 |
_cleanup_free_ char *mname = NULL; |
| 185 |
const char *on_state = "", *on_failed = ""; |
| 186 |
bool circle = false; |
| 187 |
|
| 188 |
if (streq_ptr(m->state, "degraded")) { |
| 189 |
on_state = ansi_highlight_red(); |
| 190 |
circle = true; |
| 191 |
} else if (!streq_ptr(m->state, "running")) { |
| 192 |
on_state = ansi_highlight_yellow(); |
| 193 |
circle = true; |
| 194 |
} |
| 195 |
|
| 196 |
if (m->n_failed_units > 0) |
| 197 |
on_failed = ansi_highlight_red(); |
| 198 |
else |
| 199 |
on_failed = ""; |
| 200 |
|
| 201 |
if (!m->state) |
| 202 |
state_missing = true; |
| 203 |
|
| 204 |
if (m->is_host) |
| 205 |
mname = strjoin(strna(m->name), " (host)"); |
| 206 |
|
| 207 |
r = table_add_many(table, |
| 208 |
TABLE_STRING, circle ? glyph(GLYPH_BLACK_CIRCLE) : " ", |
| 209 |
TABLE_SET_COLOR, on_state, |
| 210 |
TABLE_STRING, m->is_host ? mname : strna(m->name), |
| 211 |
TABLE_STRING, strna(m->state), |
| 212 |
TABLE_SET_COLOR, on_state, |
| 213 |
TABLE_UINT32, m->n_failed_units, |
| 214 |
TABLE_SET_COLOR, on_failed, |
| 215 |
TABLE_UINT32, m->n_jobs); |
| 216 |
if (r < 0) |
| 217 |
return table_log_add_error(r); |
| 218 |
} |
| 219 |
|
| 220 |
r = output_table(table); |
| 221 |
if (r < 0) |
| 222 |
return r; |
| 223 |
|
| 224 |
if (arg_legend != 0) { |
| 225 |
printf("\n"); |
| 226 |
if (state_missing && geteuid() != 0) |
| 227 |
printf("Notice: some information only available to privileged users was not shown.\n"); |
| 228 |
printf("%u machines listed.\n", n); |
| 229 |
} |
| 230 |
|
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
int verb_list_machines(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 235 |
struct machine_info *machine_infos = NULL; |
| 236 |
sd_bus *bus; |
| 237 |
int r, rc; |
| 238 |
|
| 239 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 240 |
if (r < 0) |
| 241 |
return r; |
| 242 |
|
| 243 |
r = get_machine_list(bus, &machine_infos, strv_skip(argv, 1)); |
| 244 |
if (r < 0) |
| 245 |
return r; |
| 246 |
|
| 247 |
pager_open(arg_pager_flags); |
| 248 |
|
| 249 |
typesafe_qsort(machine_infos, r, compare_machine_info); |
| 250 |
rc = output_machines_list(machine_infos, r); |
| 251 |
free_machines_list(machine_infos, r); |
| 252 |
|
| 253 |
return rc; |
| 254 |
} |