]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/analyze/analyze.c
docs: Clarify systemctl show manual
[thirdparty/systemd.git] / src / analyze / analyze.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2265fbf7 2/***
96b2fb93 3 Copyright © 2013 Simon Peeters
2265fbf7
SP
4***/
5
2265fbf7 6#include <getopt.h>
3f1c1287 7#include <inttypes.h>
3f6fd1ba
LP
8#include <stdio.h>
9#include <stdlib.h>
ca78ad1d 10#include <unistd.h>
2265fbf7 11
048ecf5b 12#include "sd-bus.h"
3f6fd1ba 13
b5efdb8a 14#include "alloc-util.h"
edfea9fe 15#include "analyze-condition.h"
ec16f3b6 16#include "analyze-security.h"
3f6fd1ba 17#include "analyze-verify.h"
048ecf5b 18#include "bus-error.h"
9b71e4ab 19#include "bus-locator.h"
807542be 20#include "bus-map-properties.h"
20b16441 21#include "bus-unit-util.h"
6d86f4bd 22#include "calendarspec.h"
b2af819b
LP
23#include "cap-list.h"
24#include "capability-util.h"
854a42fb 25#include "conf-files.h"
c0a1bfac 26#include "copy.h"
90bea744 27#include "def.h"
76ed04d9 28#include "exit-status.h"
da845dab 29#include "extract-word.h"
c0a1bfac 30#include "fd-util.h"
cdf6258c 31#include "fileio.h"
b41711cd 32#include "filesystems.h"
d8bfdbe1 33#include "format-table.h"
7d50b32a 34#include "glob-util.h"
bb150966 35#include "hashmap.h"
8752c575 36#include "locale-util.h"
3f6fd1ba 37#include "log.h"
d665c7b2 38#include "main-func.h"
e5ea5c3a 39#include "mount-util.h"
d8b4d14d 40#include "nulstr-util.h"
9ea9d4cf 41#include "pager.h"
599c7c54 42#include "parse-argument.h"
6bedfcbb 43#include "parse-util.h"
854a42fb 44#include "path-util.h"
294bf0c3 45#include "pretty-print.h"
da845dab 46#include "rm-rf.h"
349cc4a5 47#if HAVE_SECCOMP
294bf0c3 48# include "seccomp-util.h"
0f734bdc 49#endif
760877e9 50#include "sort-util.h"
3f6fd1ba 51#include "special.h"
b41711cd 52#include "stat-util.h"
3cc3dc77 53#include "string-table.h"
3f6fd1ba
LP
54#include "strv.h"
55#include "strxcpyx.h"
288a74cc 56#include "terminal-util.h"
760877e9 57#include "time-util.h"
da845dab 58#include "tmpfile-util.h"
3f6fd1ba
LP
59#include "unit-name.h"
60#include "util.h"
a87b151a 61#include "verb-log-control.h"
a6bcef29 62#include "verbs.h"
47350c5f 63#include "version.h"
2265fbf7 64
1ace223c 65#define SCALE_X (0.1 / 1000.0) /* pixels per us */
a213b7e9 66#define SCALE_Y (20.0)
2f6eb835 67
2265fbf7 68#define svg(...) printf(__VA_ARGS__)
c170f3a4
LP
69
70#define svg_bar(class, x1, x2, y) \
2265fbf7 71 svg(" <rect class=\"%s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n", \
c170f3a4 72 (class), \
2f6eb835
LP
73 SCALE_X * (x1), SCALE_Y * (y), \
74 SCALE_X * ((x2) - (x1)), SCALE_Y - 1.0)
c170f3a4
LP
75
76#define svg_text(b, x, y, format, ...) \
77 do { \
2f6eb835 78 svg(" <text class=\"%s\" x=\"%.03f\" y=\"%.03f\">", (b) ? "left" : "right", SCALE_X * (x) + (b ? 5.0 : -5.0), SCALE_Y * (y) + 14.0); \
c170f3a4
LP
79 svg(format, ## __VA_ARGS__); \
80 svg("</text>\n"); \
9ed794a3 81 } while (false)
2265fbf7 82
1700761b
SP
83static enum dot {
84 DEP_ALL,
85 DEP_ORDER,
86 DEP_REQUIRE
87} arg_dot = DEP_ALL;
1ace223c
SJ
88static char **arg_dot_from_patterns = NULL;
89static char **arg_dot_to_patterns = NULL;
9ea9d4cf 90static usec_t arg_fuzz = 0;
0221d68a 91static PagerFlags arg_pager_flags = 0;
3cd26e7c 92static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
f72b7018 93static const char *arg_host = NULL;
28b35ef2 94static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
3cc3dc77 95static RecursiveErrors arg_recursive_errors = RECURSIVE_ERRORS_YES;
1d3bc017 96static bool arg_man = true;
641c0fd1 97static bool arg_generators = false;
782671bc 98static char *arg_root = NULL;
e5ea5c3a 99static char *arg_image = NULL;
ecfd082b 100static char *arg_security_policy = NULL;
bb43d853 101static bool arg_offline = false;
dfbda879 102static unsigned arg_threshold = 100;
f2ccf832 103static unsigned arg_iterations = 1;
985c1880 104static usec_t arg_base_time = USEC_INFINITY;
8de7929d 105static char *arg_unit = NULL;
4b4a8ef7 106static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
bb150966 107
d665c7b2
YW
108STATIC_DESTRUCTOR_REGISTER(arg_dot_from_patterns, strv_freep);
109STATIC_DESTRUCTOR_REGISTER(arg_dot_to_patterns, strv_freep);
782671bc 110STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
e5ea5c3a 111STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
ecfd082b 112STATIC_DESTRUCTOR_REGISTER(arg_security_policy, freep);
8de7929d 113STATIC_DESTRUCTOR_REGISTER(arg_unit, freep);
d665c7b2 114
6aa601c5 115typedef struct BootTimes {
c170f3a4
LP
116 usec_t firmware_time;
117 usec_t loader_time;
118 usec_t kernel_time;
119 usec_t kernel_done_time;
120 usec_t initrd_time;
121 usec_t userspace_time;
122 usec_t finish_time;
c2e0d600
TA
123 usec_t security_start_time;
124 usec_t security_finish_time;
518d10e9
UTL
125 usec_t generators_start_time;
126 usec_t generators_finish_time;
d9acfb71
TA
127 usec_t unitsload_start_time;
128 usec_t unitsload_finish_time;
8c006565
YW
129 usec_t initrd_security_start_time;
130 usec_t initrd_security_finish_time;
131 usec_t initrd_generators_start_time;
132 usec_t initrd_generators_finish_time;
133 usec_t initrd_unitsload_start_time;
134 usec_t initrd_unitsload_finish_time;
06bef033
IS
135
136 /*
137 * If we're analyzing the user instance, all timestamps will be offset
138 * by its own start-up timestamp, which may be arbitrarily big.
139 * With "plot", this causes arbitrarily wide output SVG files which almost
140 * completely consist of empty space. Thus we cancel out this offset.
141 *
142 * This offset is subtracted from times above by acquire_boot_times(),
143 * but it still needs to be subtracted from unit-specific timestamps
144 * (so it is stored here for reference).
145 */
146 usec_t reverse_offset;
6aa601c5 147} BootTimes;
9ea9d4cf 148
6aa601c5 149typedef struct UnitTimes {
df560cf6 150 bool has_data;
2265fbf7 151 char *name;
cc27380c
TA
152 usec_t activating;
153 usec_t activated;
154 usec_t deactivated;
155 usec_t deactivating;
c170f3a4 156 usec_t time;
6aa601c5 157} UnitTimes;
2265fbf7 158
6aa601c5 159typedef struct HostInfo {
7e690cef
DH
160 char *hostname;
161 char *kernel_name;
162 char *kernel_release;
163 char *kernel_version;
164 char *os_pretty_name;
165 char *virtualization;
166 char *architecture;
6aa601c5 167} HostInfo;
7e690cef 168
f7e29336 169static int acquire_bus(sd_bus **bus, bool *use_full_bus) {
5c69b31c 170 bool user = arg_scope != UNIT_FILE_SYSTEM;
fb507898 171 int r;
5c69b31c 172
f7e29336 173 if (use_full_bus && *use_full_bus) {
fb507898
YW
174 r = bus_connect_transport(arg_transport, arg_host, user, bus);
175 if (IN_SET(r, 0, -EHOSTDOWN))
176 return r;
5c69b31c
GJ
177
178 *use_full_bus = false;
179 }
180
f7e29336 181 return bus_connect_transport_systemd(arg_transport, arg_host, user, bus);
bf0e0a4d
ZJS
182}
183
048ecf5b 184static int bus_get_uint64_property(sd_bus *bus, const char *path, const char *interface, const char *property, uint64_t *val) {
4afd3348 185 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
c170f3a4 186 int r;
2265fbf7 187
988b9df2
LP
188 assert(bus);
189 assert(path);
190 assert(interface);
191 assert(property);
192 assert(val);
193
a936124f
TA
194 r = sd_bus_get_property_trivial(
195 bus,
196 "org.freedesktop.systemd1",
197 path,
198 interface,
199 property,
200 &error,
201 't', val);
048ecf5b 202
4ae25393 203 if (r < 0)
0ed3da7c 204 return log_error_errno(r, "Failed to parse reply: %s", bus_error_message(&error, r));
2265fbf7 205
2265fbf7
SP
206 return 0;
207}
208
988b9df2 209static int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv) {
4afd3348 210 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
988b9df2
LP
211 int r;
212
213 assert(bus);
214 assert(path);
215 assert(property);
216 assert(strv);
217
218 r = sd_bus_get_property_strv(
219 bus,
220 "org.freedesktop.systemd1",
221 path,
222 "org.freedesktop.systemd1.Unit",
223 property,
224 &error,
225 strv);
4ae25393 226 if (r < 0)
0ed3da7c 227 return log_error_errno(r, "Failed to get unit property %s: %s", property, bus_error_message(&error, r));
988b9df2
LP
228
229 return 0;
230}
231
6aa601c5 232static int compare_unit_start(const UnitTimes *a, const UnitTimes *b) {
93bab288 233 return CMP(a->activating, b->activating);
2265fbf7
SP
234}
235
da845dab
AB
236static int process_aliases(char *argv[], char *tempdir, char ***ret) {
237 _cleanup_strv_free_ char **filenames = NULL;
238 char **filename;
239 int r;
240
241 assert(argv);
242 assert(tempdir);
243 assert(ret);
244
245 STRV_FOREACH(filename, strv_skip(argv, 1)) {
246 _cleanup_free_ char *src = NULL, *dst = NULL, *arg = NULL;
247 char *parse_arg;
248
249 arg = strdup(*filename);
250 if (!arg)
251 return -ENOMEM;
252
253 parse_arg = arg;
254 r = extract_first_word((const char **) &parse_arg, &src, ":", 0);
255 if (r < 0)
256 return r;
257
258 if (!parse_arg) {
259 r = strv_extend(&filenames, src);
260 if (r < 0)
261 return -ENOMEM;
262
263 continue;
264 }
265
266 dst = path_join(tempdir, basename(parse_arg));
267 if (!dst)
268 return -ENOMEM;
269
270 r = copy_file(src, dst, 0, 0644, 0, 0, COPY_REFLINK);
271 if (r < 0)
272 return r;
273
274 r = strv_consume(&filenames, TAKE_PTR(dst));
275 if (r < 0)
276 return -ENOMEM;
277 }
278
279 *ret = TAKE_PTR(filenames);
280 return 0;
281}
282
15567b3a 283static UnitTimes* unit_times_free_array(UnitTimes *t) {
75db809a 284 for (UnitTimes *p = t; p && p->has_data; p++)
c170f3a4 285 free(p->name);
75db809a 286 return mfree(t);
c170f3a4 287}
15567b3a 288DEFINE_TRIVIAL_CLEANUP_FUNC(UnitTimes*, unit_times_free_array);
df560cf6 289
06bef033
IS
290static void subtract_timestamp(usec_t *a, usec_t b) {
291 assert(a);
292
293 if (*a > 0) {
294 assert(*a >= b);
295 *a -= b;
296 }
297}
298
6aa601c5 299static int acquire_boot_times(sd_bus *bus, BootTimes **bt) {
cc0eb780 300 static const struct bus_properties_map property_map[] = {
6aa601c5
ZJS
301 { "FirmwareTimestampMonotonic", "t", NULL, offsetof(BootTimes, firmware_time) },
302 { "LoaderTimestampMonotonic", "t", NULL, offsetof(BootTimes, loader_time) },
303 { "KernelTimestamp", "t", NULL, offsetof(BootTimes, kernel_time) },
304 { "InitRDTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_time) },
305 { "UserspaceTimestampMonotonic", "t", NULL, offsetof(BootTimes, userspace_time) },
306 { "FinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, finish_time) },
307 { "SecurityStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, security_start_time) },
308 { "SecurityFinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, security_finish_time) },
309 { "GeneratorsStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, generators_start_time) },
310 { "GeneratorsFinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, generators_finish_time) },
311 { "UnitsLoadStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, unitsload_start_time) },
312 { "UnitsLoadFinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, unitsload_finish_time) },
313 { "InitRDSecurityStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_security_start_time) },
314 { "InitRDSecurityFinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_security_finish_time) },
315 { "InitRDGeneratorsStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_generators_start_time) },
316 { "InitRDGeneratorsFinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_generators_finish_time) },
317 { "InitRDUnitsLoadStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_unitsload_start_time) },
318 { "InitRDUnitsLoadFinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_unitsload_finish_time) },
cc0eb780
YW
319 {},
320 };
321 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
6aa601c5 322 static BootTimes times;
2265fbf7 323 static bool cached = false;
cc0eb780 324 int r;
c170f3a4 325
2265fbf7 326 if (cached)
c170f3a4
LP
327 goto finish;
328
329 assert_cc(sizeof(usec_t) == sizeof(uint64_t));
2265fbf7 330
cc0eb780
YW
331 r = bus_map_all_properties(
332 bus,
333 "org.freedesktop.systemd1",
334 "/org/freedesktop/systemd1",
335 property_map,
336 BUS_MAP_STRDUP,
337 &error,
338 NULL,
339 &times);
340 if (r < 0)
341 return log_error_errno(r, "Failed to get timestamp properties: %s", bus_error_message(&error, r));
8c006565 342
c2953e08
ZJS
343 if (times.finish_time <= 0)
344 return log_error_errno(SYNTHETIC_ERRNO(EINPROGRESS),
345 "Bootup is not yet finished (org.freedesktop.systemd1.Manager.FinishTimestampMonotonic=%"PRIu64").\n"
346 "Please try again later.\n"
347 "Hint: Use 'systemctl%s list-jobs' to see active jobs",
348 times.finish_time,
349 arg_scope == UNIT_FILE_SYSTEM ? "" : " --user");
2265fbf7 350
eddb5037
YW
351 if (arg_scope == UNIT_FILE_SYSTEM && times.security_start_time > 0) {
352 /* security_start_time is set when systemd is not running under container environment. */
baa4880b 353 if (times.initrd_time > 0)
28b35ef2
ZJS
354 times.kernel_done_time = times.initrd_time;
355 else
356 times.kernel_done_time = times.userspace_time;
357 } else {
06bef033 358 /*
eddb5037 359 * User-instance-specific or container-system-specific timestamps processing
6aa601c5 360 * (see comment to reverse_offset in BootTimes).
06bef033
IS
361 */
362 times.reverse_offset = times.userspace_time;
363
1ace223c
SJ
364 times.firmware_time = times.loader_time = times.kernel_time = times.initrd_time =
365 times.userspace_time = times.security_start_time = times.security_finish_time = 0;
06bef033 366
79ecaae4 367 subtract_timestamp(&times.finish_time, times.reverse_offset);
06bef033
IS
368
369 subtract_timestamp(&times.generators_start_time, times.reverse_offset);
370 subtract_timestamp(&times.generators_finish_time, times.reverse_offset);
371
372 subtract_timestamp(&times.unitsload_start_time, times.reverse_offset);
373 subtract_timestamp(&times.unitsload_finish_time, times.reverse_offset);
06bef033 374 }
2265fbf7
SP
375
376 cached = true;
c170f3a4
LP
377
378finish:
379 *bt = &times;
380 return 0;
2265fbf7
SP
381}
382
75db809a 383static HostInfo* free_host_info(HostInfo *hi) {
b1b533a0 384 if (!hi)
75db809a 385 return NULL;
b1b533a0 386
7e690cef
DH
387 free(hi->hostname);
388 free(hi->kernel_name);
389 free(hi->kernel_release);
390 free(hi->kernel_version);
391 free(hi->os_pretty_name);
392 free(hi->virtualization);
393 free(hi->architecture);
75db809a 394 return mfree(hi);
7e690cef 395}
b1b533a0 396
6aa601c5 397DEFINE_TRIVIAL_CLEANUP_FUNC(HostInfo *, free_host_info);
7e690cef 398
6aa601c5 399static int acquire_time_data(sd_bus *bus, UnitTimes **out) {
cc0eb780 400 static const struct bus_properties_map property_map[] = {
6aa601c5
ZJS
401 { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(UnitTimes, activating) },
402 { "ActiveEnterTimestampMonotonic", "t", NULL, offsetof(UnitTimes, activated) },
403 { "ActiveExitTimestampMonotonic", "t", NULL, offsetof(UnitTimes, deactivating) },
404 { "InactiveEnterTimestampMonotonic", "t", NULL, offsetof(UnitTimes, deactivated) },
cc0eb780
YW
405 {},
406 };
4afd3348
LP
407 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
408 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
15567b3a 409 _cleanup_(unit_times_free_arrayp) UnitTimes *unit_times = NULL;
6aa601c5 410 BootTimes *boot_times = NULL;
319a4f4b 411 size_t c = 0;
29b8b5ce 412 UnitInfo u;
97cec9ba 413 int r;
29b8b5ce 414
06bef033
IS
415 r = acquire_boot_times(bus, &boot_times);
416 if (r < 0)
df560cf6 417 return r;
06bef033 418
de770b60 419 r = bus_call_method(bus, bus_systemd_mgr, "ListUnits", &error, &reply, NULL);
4ae25393 420 if (r < 0)
0ed3da7c 421 return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r));
29b8b5ce
IS
422
423 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
df560cf6
FB
424 if (r < 0)
425 return bus_log_parse_error(r);
29b8b5ce
IS
426
427 while ((r = bus_parse_unit_info(reply, &u)) > 0) {
6aa601c5 428 UnitTimes *t;
29b8b5ce 429
319a4f4b 430 if (!GREEDY_REALLOC(unit_times, c + 2))
df560cf6 431 return log_oom();
29b8b5ce 432
1ace223c 433 unit_times[c + 1].has_data = false;
df560cf6 434 t = &unit_times[c];
29b8b5ce
IS
435 t->name = NULL;
436
437 assert_cc(sizeof(usec_t) == sizeof(uint64_t));
438
cc0eb780
YW
439 r = bus_map_all_properties(
440 bus,
441 "org.freedesktop.systemd1",
442 u.unit_path,
443 property_map,
444 BUS_MAP_STRDUP,
445 &error,
446 NULL,
447 t);
448 if (r < 0)
c2953e08
ZJS
449 return log_error_errno(r, "Failed to get timestamp properties of unit %s: %s",
450 u.id, bus_error_message(&error, r));
29b8b5ce 451
06bef033
IS
452 subtract_timestamp(&t->activating, boot_times->reverse_offset);
453 subtract_timestamp(&t->activated, boot_times->reverse_offset);
454 subtract_timestamp(&t->deactivating, boot_times->reverse_offset);
455 subtract_timestamp(&t->deactivated, boot_times->reverse_offset);
456
29b8b5ce
IS
457 if (t->activated >= t->activating)
458 t->time = t->activated - t->activating;
459 else if (t->deactivated >= t->activating)
460 t->time = t->deactivated - t->activating;
461 else
462 t->time = 0;
463
464 if (t->activating == 0)
465 continue;
466
467 t->name = strdup(u.id);
df560cf6
FB
468 if (!t->name)
469 return log_oom();
470
471 t->has_data = true;
29b8b5ce
IS
472 c++;
473 }
df560cf6
FB
474 if (r < 0)
475 return bus_log_parse_error(r);
29b8b5ce 476
df560cf6 477 *out = TAKE_PTR(unit_times);
29b8b5ce 478 return c;
29b8b5ce
IS
479}
480
6aa601c5 481static int acquire_host_info(sd_bus *bus, HostInfo **hi) {
7e690cef 482 static const struct bus_properties_map hostname_map[] = {
6aa601c5
ZJS
483 { "Hostname", "s", NULL, offsetof(HostInfo, hostname) },
484 { "KernelName", "s", NULL, offsetof(HostInfo, kernel_name) },
485 { "KernelRelease", "s", NULL, offsetof(HostInfo, kernel_release) },
486 { "KernelVersion", "s", NULL, offsetof(HostInfo, kernel_version) },
487 { "OperatingSystemPrettyName", "s", NULL, offsetof(HostInfo, os_pretty_name) },
7e690cef
DH
488 {}
489 };
490
491 static const struct bus_properties_map manager_map[] = {
6aa601c5
ZJS
492 { "Virtualization", "s", NULL, offsetof(HostInfo, virtualization) },
493 { "Architecture", "s", NULL, offsetof(HostInfo, architecture) },
7e690cef
DH
494 {}
495 };
496
4afd3348 497 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
4f481d76 498 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *system_bus = NULL;
75db809a 499 _cleanup_(free_host_infop) HostInfo *host = NULL;
b1b533a0
LP
500 int r;
501
6aa601c5 502 host = new0(HostInfo, 1);
7e690cef
DH
503 if (!host)
504 return log_oom();
505
4f481d76
YW
506 if (arg_scope != UNIT_FILE_SYSTEM) {
507 r = bus_connect_transport(arg_transport, arg_host, false, &system_bus);
508 if (r < 0) {
509 log_debug_errno(r, "Failed to connect to system bus, ignoring: %m");
510 goto manager;
511 }
512 }
513
1ace223c
SJ
514 r = bus_map_all_properties(
515 system_bus ?: bus,
516 "org.freedesktop.hostname1",
517 "/org/freedesktop/hostname1",
518 hostname_map,
519 BUS_MAP_STRDUP,
520 &error,
521 NULL,
522 host);
4f481d76 523 if (r < 0) {
c2953e08
ZJS
524 log_debug_errno(r, "Failed to get host information from systemd-hostnamed, ignoring: %s",
525 bus_error_message(&error, r));
4f481d76
YW
526 sd_bus_error_free(&error);
527 }
7e690cef 528
4f481d76 529manager:
1ace223c
SJ
530 r = bus_map_all_properties(
531 bus,
532 "org.freedesktop.systemd1",
533 "/org/freedesktop/systemd1",
534 manager_map,
535 BUS_MAP_STRDUP,
536 &error,
537 NULL,
538 host);
febda62a 539 if (r < 0)
c2953e08
ZJS
540 return log_error_errno(r, "Failed to get host information from systemd: %s",
541 bus_error_message(&error, r));
7e690cef 542
1cc6c93a 543 *hi = TAKE_PTR(host);
7e690cef 544 return 0;
7e690cef
DH
545}
546
048ecf5b 547static int pretty_boot_time(sd_bus *bus, char **_buf) {
6aa601c5 548 BootTimes *t;
2265fbf7 549 static char buf[4096];
c170f3a4
LP
550 size_t size;
551 char *ptr;
552 int r;
bd07d3d0 553 usec_t activated_time = USEC_INFINITY;
1ace223c 554 _cleanup_free_ char *path = NULL, *unit_id = NULL;
bd07d3d0 555 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
c170f3a4
LP
556
557 r = acquire_boot_times(bus, &t);
558 if (r < 0)
559 return r;
2265fbf7 560
bd07d3d0
JR
561 path = unit_dbus_path_from_name(SPECIAL_DEFAULT_TARGET);
562 if (!path)
563 return log_oom();
564
565 r = sd_bus_get_property_string(
566 bus,
567 "org.freedesktop.systemd1",
568 path,
569 "org.freedesktop.systemd1.Unit",
570 "Id",
571 &error,
572 &unit_id);
573 if (r < 0) {
574 log_error_errno(r, "default.target doesn't seem to exist: %s", bus_error_message(&error, r));
575 unit_id = NULL;
576 }
577
578 r = bus_get_uint64_property(bus, path,
579 "org.freedesktop.systemd1.Unit",
580 "ActiveEnterTimestampMonotonic",
581 &activated_time);
582 if (r < 0) {
0c753963 583 log_info_errno(r, "Could not get time to reach default.target, ignoring: %m");
bd07d3d0
JR
584 activated_time = USEC_INFINITY;
585 }
586
c170f3a4
LP
587 ptr = buf;
588 size = sizeof(buf);
2265fbf7
SP
589
590 size = strpcpyf(&ptr, size, "Startup finished in ");
baa4880b 591 if (t->firmware_time > 0)
5291f26d 592 size = strpcpyf(&ptr, size, "%s (firmware) + ", FORMAT_TIMESPAN(t->firmware_time - t->loader_time, USEC_PER_MSEC));
baa4880b 593 if (t->loader_time > 0)
5291f26d 594 size = strpcpyf(&ptr, size, "%s (loader) + ", FORMAT_TIMESPAN(t->loader_time, USEC_PER_MSEC));
02be0cca 595 if (t->kernel_done_time > 0)
5291f26d 596 size = strpcpyf(&ptr, size, "%s (kernel) + ", FORMAT_TIMESPAN(t->kernel_done_time, USEC_PER_MSEC));
2265fbf7 597 if (t->initrd_time > 0)
5291f26d 598 size = strpcpyf(&ptr, size, "%s (initrd) + ", FORMAT_TIMESPAN(t->userspace_time - t->initrd_time, USEC_PER_MSEC));
2265fbf7 599
5291f26d 600 size = strpcpyf(&ptr, size, "%s (userspace) ", FORMAT_TIMESPAN(t->finish_time - t->userspace_time, USEC_PER_MSEC));
02be0cca 601 if (t->kernel_done_time > 0)
5291f26d 602 strpcpyf(&ptr, size, "= %s ", FORMAT_TIMESPAN(t->firmware_time + t->finish_time, USEC_PER_MSEC));
2265fbf7 603
1f65fd49 604 if (unit_id && timestamp_is_set(activated_time)) {
b5d6f7ea
ZJS
605 usec_t base = t->userspace_time > 0 ? t->userspace_time : t->reverse_offset;
606
607 size = strpcpyf(&ptr, size, "\n%s reached after %s in userspace", unit_id,
5291f26d 608 FORMAT_TIMESPAN(activated_time - base, USEC_PER_MSEC));
eddb5037 609 } else if (unit_id && activated_time == 0)
da933f7d
JR
610 size = strpcpyf(&ptr, size, "\n%s was never reached", unit_id);
611 else if (unit_id && activated_time == USEC_INFINITY)
feb92776 612 size = strpcpyf(&ptr, size, "\nCould not get time to reach %s.", unit_id);
da933f7d
JR
613 else if (!unit_id)
614 size = strpcpyf(&ptr, size, "\ncould not find default.target");
615
c170f3a4
LP
616 ptr = strdup(buf);
617 if (!ptr)
618 return log_oom();
619
620 *_buf = ptr;
621 return 0;
2265fbf7
SP
622}
623
c170f3a4 624static void svg_graph_box(double height, double begin, double end) {
2265fbf7
SP
625 /* outside box, fill */
626 svg("<rect class=\"box\" x=\"0\" y=\"0\" width=\"%.03f\" height=\"%.03f\" />\n",
1ace223c
SJ
627 SCALE_X * (end - begin),
628 SCALE_Y * height);
2265fbf7 629
6aa601c5 630 for (long long i = ((long long) (begin / 100000)) * 100000; i <= end; i += 100000) {
2265fbf7 631 /* lines for each second */
c170f3a4 632 if (i % 5000000 == 0)
2265fbf7
SP
633 svg(" <line class=\"sec5\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
634 " <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
1ace223c
SJ
635 SCALE_X * i,
636 SCALE_X * i,
637 SCALE_Y * height,
638 SCALE_X * i,
639 -5.0,
640 0.000001 * i);
c170f3a4 641 else if (i % 1000000 == 0)
2265fbf7
SP
642 svg(" <line class=\"sec1\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
643 " <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
1ace223c
SJ
644 SCALE_X * i,
645 SCALE_X * i,
646 SCALE_Y * height,
647 SCALE_X * i,
648 -5.0,
649 0.000001 * i);
2265fbf7
SP
650 else
651 svg(" <line class=\"sec01\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
1ace223c
SJ
652 SCALE_X * i,
653 SCALE_X * i,
654 SCALE_Y * height);
2265fbf7
SP
655 }
656}
657
6aa601c5 658static int plot_unit_times(UnitTimes *u, double width, int y) {
7725fc11
YW
659 bool b;
660
661 if (!u->name)
662 return 0;
663
664 svg_bar("activating", u->activating, u->activated, y);
665 svg_bar("active", u->activated, u->deactivating, y);
666 svg_bar("deactivating", u->deactivating, u->deactivated, y);
667
668 /* place the text on the left if we have passed the half of the svg width */
669 b = u->activating * SCALE_X < width / 2;
670 if (u->time)
671 svg_text(b, u->activating, y, "%s (%s)",
5291f26d 672 u->name, FORMAT_TIMESPAN(u->time, USEC_PER_MSEC));
7725fc11
YW
673 else
674 svg_text(b, u->activating, y, "%s", u->name);
675
676 return 1;
677}
678
a6bcef29 679static int analyze_plot(int argc, char *argv[], void *userdata) {
6aa601c5 680 _cleanup_(free_host_infop) HostInfo *host = NULL;
a6bcef29 681 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
15567b3a 682 _cleanup_(unit_times_free_arrayp) UnitTimes *times = NULL;
4f481d76
YW
683 _cleanup_free_ char *pretty_times = NULL;
684 bool use_full_bus = arg_scope == UNIT_FILE_SYSTEM;
6aa601c5
ZJS
685 BootTimes *boot;
686 UnitTimes *u;
a6bcef29 687 int n, m = 1, y = 0, r;
2265fbf7 688 double width;
2265fbf7 689
f7e29336 690 r = acquire_bus(&bus, &use_full_bus);
a6bcef29 691 if (r < 0)
10a7340a 692 return bus_log_connect_error(r, arg_transport);
a6bcef29 693
c170f3a4
LP
694 n = acquire_boot_times(bus, &boot);
695 if (n < 0)
696 return n;
2265fbf7 697
c170f3a4
LP
698 n = pretty_boot_time(bus, &pretty_times);
699 if (n < 0)
700 return n;
2265fbf7 701
4f481d76 702 if (use_full_bus || arg_scope != UNIT_FILE_SYSTEM) {
5c69b31c
GJ
703 n = acquire_host_info(bus, &host);
704 if (n < 0)
705 return n;
706 }
2265fbf7
SP
707
708 n = acquire_time_data(bus, &times);
c170f3a4 709 if (n <= 0)
19f462f2 710 return n;
2265fbf7 711
93bab288 712 typesafe_qsort(times, n, compare_unit_start);
2265fbf7 713
2f6eb835 714 width = SCALE_X * (boot->firmware_time + boot->finish_time);
2265fbf7
SP
715 if (width < 800.0)
716 width = 800.0;
717
718 if (boot->firmware_time > boot->loader_time)
719 m++;
baa4880b 720 if (boot->loader_time > 0) {
2265fbf7
SP
721 m++;
722 if (width < 1000.0)
723 width = 1000.0;
724 }
baa4880b 725 if (boot->initrd_time > 0)
2265fbf7 726 m++;
02be0cca 727 if (boot->kernel_done_time > 0)
2265fbf7
SP
728 m++;
729
df560cf6 730 for (u = times; u->has_data; u++) {
95168f7d 731 double text_start, text_width;
c170f3a4 732
7725fc11 733 if (u->activating > boot->finish_time) {
a1e58e8e 734 u->name = mfree(u->name);
2265fbf7
SP
735 continue;
736 }
95168f7d
TA
737
738 /* If the text cannot fit on the left side then
739 * increase the svg width so it fits on the right.
740 * TODO: calculate the text width more accurately */
741 text_width = 8.0 * strlen(u->name);
cc27380c 742 text_start = (boot->firmware_time + u->activating) * SCALE_X;
95168f7d
TA
743 if (text_width > text_start && text_width + text_start > width)
744 width = text_width + text_start;
2265fbf7 745
efe6112d
YW
746 if (u->deactivated > u->activating &&
747 u->deactivated <= boot->finish_time &&
748 u->activated == 0 && u->deactivating == 0)
cc27380c
TA
749 u->activated = u->deactivating = u->deactivated;
750 if (u->activated < u->activating || u->activated > boot->finish_time)
751 u->activated = boot->finish_time;
efe6112d 752 if (u->deactivating < u->activated || u->deactivating > boot->finish_time)
cc27380c
TA
753 u->deactivating = boot->finish_time;
754 if (u->deactivated < u->deactivating || u->deactivated > boot->finish_time)
755 u->deactivated = boot->finish_time;
2265fbf7
SP
756 m++;
757 }
758
759 svg("<?xml version=\"1.0\" standalone=\"no\"?>\n"
760 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
761 "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
762
763 svg("<svg width=\"%.0fpx\" height=\"%.0fpx\" version=\"1.1\" "
764 "xmlns=\"http://www.w3.org/2000/svg\">\n\n",
518d10e9 765 80.0 + width, 150.0 + (m * SCALE_Y) +
d9acfb71 766 5 * SCALE_Y /* legend */);
2265fbf7
SP
767
768 /* write some basic info as a comment, including some help */
769 svg("<!-- This file is a systemd-analyze SVG file. It is best rendered in a -->\n"
770 "<!-- browser such as Chrome, Chromium or Firefox. Other applications -->\n"
771 "<!-- that render these files properly but much slower are ImageMagick, -->\n"
772 "<!-- gimp, inkscape, etc. To display the files on your system, just -->\n"
773 "<!-- point your browser to this file. -->\n\n"
681bd2c5 774 "<!-- This plot was generated by systemd-analyze version %-16.16s -->\n\n", GIT_VERSION);
2265fbf7
SP
775
776 /* style sheet */
777 svg("<defs>\n <style type=\"text/css\">\n <![CDATA[\n"
778 " rect { stroke-width: 1; stroke-opacity: 0; }\n"
418e3750 779 " rect.background { fill: rgb(255,255,255); }\n"
2265fbf7
SP
780 " rect.activating { fill: rgb(255,0,0); fill-opacity: 0.7; }\n"
781 " rect.active { fill: rgb(200,150,150); fill-opacity: 0.7; }\n"
782 " rect.deactivating { fill: rgb(150,100,100); fill-opacity: 0.7; }\n"
783 " rect.kernel { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
784 " rect.initrd { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
785 " rect.firmware { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
786 " rect.loader { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
787 " rect.userspace { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
c2e0d600 788 " rect.security { fill: rgb(144,238,144); fill-opacity: 0.7; }\n"
518d10e9 789 " rect.generators { fill: rgb(102,204,255); fill-opacity: 0.7; }\n"
d9acfb71 790 " rect.unitsload { fill: rgb( 82,184,255); fill-opacity: 0.7; }\n"
2265fbf7
SP
791 " rect.box { fill: rgb(240,240,240); stroke: rgb(192,192,192); }\n"
792 " line { stroke: rgb(64,64,64); stroke-width: 1; }\n"
793 "// line.sec1 { }\n"
794 " line.sec5 { stroke-width: 2; }\n"
795 " line.sec01 { stroke: rgb(224,224,224); stroke-width: 1; }\n"
2b7d6965
TA
796 " text { font-family: Verdana, Helvetica; font-size: 14px; }\n"
797 " text.left { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: start; }\n"
798 " text.right { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: end; }\n"
799 " text.sec { font-size: 10px; }\n"
2265fbf7
SP
800 " ]]>\n </style>\n</defs>\n\n");
801
418e3750 802 svg("<rect class=\"background\" width=\"100%%\" height=\"100%%\" />\n");
2265fbf7 803 svg("<text x=\"20\" y=\"50\">%s</text>", pretty_times);
4f481d76 804 if (host)
5c69b31c
GJ
805 svg("<text x=\"20\" y=\"30\">%s %s (%s %s %s) %s %s</text>",
806 isempty(host->os_pretty_name) ? "Linux" : host->os_pretty_name,
807 strempty(host->hostname),
808 strempty(host->kernel_name),
809 strempty(host->kernel_release),
810 strempty(host->kernel_version),
811 strempty(host->architecture),
812 strempty(host->virtualization));
2265fbf7 813
2f6eb835 814 svg("<g transform=\"translate(%.3f,100)\">\n", 20.0 + (SCALE_X * boot->firmware_time));
b5cfa740 815 svg_graph_box(m, -(double) boot->firmware_time, boot->finish_time);
2265fbf7 816
baa4880b 817 if (boot->firmware_time > 0) {
c170f3a4
LP
818 svg_bar("firmware", -(double) boot->firmware_time, -(double) boot->loader_time, y);
819 svg_text(true, -(double) boot->firmware_time, y, "firmware");
2265fbf7
SP
820 y++;
821 }
baa4880b 822 if (boot->loader_time > 0) {
c170f3a4
LP
823 svg_bar("loader", -(double) boot->loader_time, 0, y);
824 svg_text(true, -(double) boot->loader_time, y, "loader");
2265fbf7
SP
825 y++;
826 }
02be0cca 827 if (boot->kernel_done_time > 0) {
2265fbf7 828 svg_bar("kernel", 0, boot->kernel_done_time, y);
c170f3a4 829 svg_text(true, 0, y, "kernel");
2265fbf7
SP
830 y++;
831 }
baa4880b 832 if (boot->initrd_time > 0) {
2265fbf7 833 svg_bar("initrd", boot->initrd_time, boot->userspace_time, y);
8c006565
YW
834 if (boot->initrd_security_start_time < boot->initrd_security_finish_time)
835 svg_bar("security", boot->initrd_security_start_time, boot->initrd_security_finish_time, y);
836 if (boot->initrd_generators_start_time < boot->initrd_generators_finish_time)
837 svg_bar("generators", boot->initrd_generators_start_time, boot->initrd_generators_finish_time, y);
838 if (boot->initrd_unitsload_start_time < boot->initrd_unitsload_finish_time)
839 svg_bar("unitsload", boot->initrd_unitsload_start_time, boot->initrd_unitsload_finish_time, y);
c170f3a4 840 svg_text(true, boot->initrd_time, y, "initrd");
2265fbf7
SP
841 y++;
842 }
7725fc11
YW
843
844 for (u = times; u->has_data; u++) {
845 if (u->activating >= boot->userspace_time)
846 break;
847
848 y += plot_unit_times(u, width, y);
849 }
850
518d10e9 851 svg_bar("active", boot->userspace_time, boot->finish_time, y);
79ecaae4
YW
852 if (boot->security_start_time > 0)
853 svg_bar("security", boot->security_start_time, boot->security_finish_time, y);
518d10e9 854 svg_bar("generators", boot->generators_start_time, boot->generators_finish_time, y);
d9acfb71 855 svg_bar("unitsload", boot->unitsload_start_time, boot->unitsload_finish_time, y);
95168f7d 856 svg_text(true, boot->userspace_time, y, "systemd");
2265fbf7
SP
857 y++;
858
7725fc11
YW
859 for (; u->has_data; u++)
860 y += plot_unit_times(u, width, y);
518d10e9 861
b5cfa740
TA
862 svg("</g>\n");
863
518d10e9 864 /* Legend */
b5cfa740 865 svg("<g transform=\"translate(20,100)\">\n");
518d10e9
UTL
866 y++;
867 svg_bar("activating", 0, 300000, y);
95168f7d 868 svg_text(true, 400000, y, "Activating");
518d10e9
UTL
869 y++;
870 svg_bar("active", 0, 300000, y);
95168f7d 871 svg_text(true, 400000, y, "Active");
518d10e9
UTL
872 y++;
873 svg_bar("deactivating", 0, 300000, y);
95168f7d 874 svg_text(true, 400000, y, "Deactivating");
518d10e9 875 y++;
79ecaae4
YW
876 if (boot->security_start_time > 0) {
877 svg_bar("security", 0, 300000, y);
878 svg_text(true, 400000, y, "Setting up security module");
879 y++;
880 }
518d10e9 881 svg_bar("generators", 0, 300000, y);
95168f7d 882 svg_text(true, 400000, y, "Generators");
518d10e9 883 y++;
d9acfb71 884 svg_bar("unitsload", 0, 300000, y);
95168f7d 885 svg_text(true, 400000, y, "Loading unit files");
d9acfb71 886 y++;
518d10e9 887
2265fbf7
SP
888 svg("</g>\n\n");
889
988b9df2 890 svg("</svg>\n");
c170f3a4 891
df560cf6 892 return 0;
2265fbf7
SP
893}
894
1ace223c
SJ
895static int list_dependencies_print(
896 const char *name,
897 unsigned level,
898 unsigned branches,
899 bool last,
6aa601c5
ZJS
900 UnitTimes *times,
901 BootTimes *boot) {
1ace223c 902
6aa601c5 903 for (unsigned i = level; i != 0; i--)
9a6f746f 904 printf("%s", special_glyph(branches & (1 << (i-1)) ? SPECIAL_GLYPH_TREE_VERTICAL : SPECIAL_GLYPH_TREE_SPACE));
bb150966 905
9a6f746f 906 printf("%s", special_glyph(last ? SPECIAL_GLYPH_TREE_RIGHT : SPECIAL_GLYPH_TREE_BRANCH));
bb150966
HH
907
908 if (times) {
baa4880b 909 if (times->time > 0)
54f8c958 910 printf("%s%s @%s +%s%s", ansi_highlight_red(), name,
5291f26d
ZJS
911 FORMAT_TIMESPAN(times->activating - boot->userspace_time, USEC_PER_MSEC),
912 FORMAT_TIMESPAN(times->time, USEC_PER_MSEC), ansi_normal());
cc27380c 913 else if (times->activated > boot->userspace_time)
5291f26d 914 printf("%s @%s", name, FORMAT_TIMESPAN(times->activated - boot->userspace_time, USEC_PER_MSEC));
bb150966
HH
915 else
916 printf("%s", name);
988b9df2
LP
917 } else
918 printf("%s", name);
bb150966
HH
919 printf("\n");
920
921 return 0;
922}
923
048ecf5b 924static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) {
988b9df2 925 _cleanup_free_ char *path = NULL;
bb150966
HH
926
927 assert(bus);
928 assert(name);
929 assert(deps);
930
931 path = unit_dbus_path_from_name(name);
234519ae 932 if (!path)
988b9df2 933 return -ENOMEM;
bb150966 934
988b9df2 935 return bus_get_unit_property_strv(bus, path, "After", deps);
bb150966
HH
936}
937
938static Hashmap *unit_times_hashmap;
939
1ace223c 940static int list_dependencies_compare(char *const *a, char *const *b) {
bb150966 941 usec_t usa = 0, usb = 0;
6aa601c5 942 UnitTimes *times;
bb150966
HH
943
944 times = hashmap_get(unit_times_hashmap, *a);
945 if (times)
cc27380c 946 usa = times->activated;
bb150966
HH
947 times = hashmap_get(unit_times_hashmap, *b);
948 if (times)
cc27380c 949 usb = times->activated;
bb150966 950
93bab288 951 return CMP(usb, usa);
bb150966
HH
952}
953
6aa601c5 954static bool times_in_range(const UnitTimes *times, const BootTimes *boot) {
1ace223c 955 return times && times->activated > 0 && times->activated <= boot->finish_time;
230cc99a
ZJS
956}
957
1ace223c 958static int list_dependencies_one(sd_bus *bus, const char *name, unsigned level, char ***units, unsigned branches) {
bb150966
HH
959 _cleanup_strv_free_ char **deps = NULL;
960 char **c;
05f7a068 961 int r;
bb150966
HH
962 usec_t service_longest = 0;
963 int to_print = 0;
6aa601c5
ZJS
964 UnitTimes *times;
965 BootTimes *boot;
bb150966 966
988b9df2 967 if (strv_extend(units, name))
bb150966
HH
968 return log_oom();
969
970 r = list_dependencies_get_dependencies(bus, name, &deps);
971 if (r < 0)
972 return r;
973
93bab288 974 typesafe_qsort(deps, strv_length(deps), list_dependencies_compare);
bb150966
HH
975
976 r = acquire_boot_times(bus, &boot);
977 if (r < 0)
978 return r;
979
980 STRV_FOREACH(c, deps) {
981 times = hashmap_get(unit_times_hashmap, *c);
1ace223c 982 if (times_in_range(times, boot) && times->activated >= service_longest)
cc27380c 983 service_longest = times->activated;
bb150966
HH
984 }
985
234519ae 986 if (service_longest == 0)
bb150966
HH
987 return r;
988
989 STRV_FOREACH(c, deps) {
990 times = hashmap_get(unit_times_hashmap, *c);
1ace223c 991 if (times_in_range(times, boot) && service_longest - times->activated <= arg_fuzz)
bb150966 992 to_print++;
bb150966
HH
993 }
994
f168c273 995 if (!to_print)
bb150966
HH
996 return r;
997
998 STRV_FOREACH(c, deps) {
999 times = hashmap_get(unit_times_hashmap, *c);
1ace223c 1000 if (!times_in_range(times, boot) || service_longest - times->activated > arg_fuzz)
bb150966
HH
1001 continue;
1002
1003 to_print--;
1004
1005 r = list_dependencies_print(*c, level, branches, to_print == 0, times, boot);
1006 if (r < 0)
1007 return r;
1008
1009 if (strv_contains(*units, *c)) {
1010 r = list_dependencies_print("...", level + 1, (branches << 1) | (to_print ? 1 : 0),
1011 true, NULL, boot);
872c8faa
ZJS
1012 if (r < 0)
1013 return r;
bb150966
HH
1014 continue;
1015 }
1016
1ace223c 1017 r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (to_print ? 1 : 0));
872c8faa 1018 if (r < 0)
bb150966
HH
1019 return r;
1020
baa4880b 1021 if (to_print == 0)
bb150966 1022 break;
bb150966
HH
1023 }
1024 return 0;
1025}
1026
048ecf5b 1027static int list_dependencies(sd_bus *bus, const char *name) {
bb150966 1028 _cleanup_strv_free_ char **units = NULL;
6aa601c5 1029 UnitTimes *times;
bb150966 1030 int r;
0ee9613d
TA
1031 const char *id;
1032 _cleanup_free_ char *path = NULL;
4afd3348
LP
1033 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1034 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
6aa601c5 1035 BootTimes *boot;
bb150966
HH
1036
1037 assert(bus);
1038
805bf39c 1039 path = unit_dbus_path_from_name(name);
234519ae 1040 if (!path)
988b9df2 1041 return -ENOMEM;
bb150966 1042
a936124f
TA
1043 r = sd_bus_get_property(
1044 bus,
1045 "org.freedesktop.systemd1",
1046 path,
1047 "org.freedesktop.systemd1.Unit",
1048 "Id",
1049 &error,
1050 &reply,
1051 "s");
4ae25393 1052 if (r < 0)
0ed3da7c 1053 return log_error_errno(r, "Failed to get ID: %s", bus_error_message(&error, r));
bb150966 1054
048ecf5b 1055 r = sd_bus_message_read(reply, "s", &id);
5b30bef8
LP
1056 if (r < 0)
1057 return bus_log_parse_error(r);
bb150966 1058
bb150966
HH
1059 times = hashmap_get(unit_times_hashmap, id);
1060
1061 r = acquire_boot_times(bus, &boot);
1062 if (r < 0)
1063 return r;
1064
1065 if (times) {
1066 if (times->time)
54f8c958 1067 printf("%s%s +%s%s\n", ansi_highlight_red(), id,
5291f26d 1068 FORMAT_TIMESPAN(times->time, USEC_PER_MSEC), ansi_normal());
cc27380c 1069 else if (times->activated > boot->userspace_time)
5291f26d
ZJS
1070 printf("%s @%s\n", id,
1071 FORMAT_TIMESPAN(times->activated - boot->userspace_time, USEC_PER_MSEC));
bb150966
HH
1072 else
1073 printf("%s\n", id);
1074 }
1075
805bf39c 1076 return list_dependencies_one(bus, name, 0, &units, 0);
bb150966
HH
1077}
1078
a6bcef29
LP
1079static int analyze_critical_chain(int argc, char *argv[], void *userdata) {
1080 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
15567b3a 1081 _cleanup_(unit_times_free_arrayp) UnitTimes *times = NULL;
bb150966 1082 Hashmap *h;
988b9df2 1083 int n, r;
bb150966 1084
f7e29336 1085 r = acquire_bus(&bus, NULL);
a6bcef29 1086 if (r < 0)
10a7340a 1087 return bus_log_connect_error(r, arg_transport);
a6bcef29 1088
bb150966
HH
1089 n = acquire_time_data(bus, &times);
1090 if (n <= 0)
1091 return n;
1092
d5099efc 1093 h = hashmap_new(&string_hash_ops);
bb150966 1094 if (!h)
8efbce13 1095 return log_oom();
bb150966 1096
6aa601c5 1097 for (UnitTimes *u = times; u->has_data; u++) {
df560cf6 1098 r = hashmap_put(h, u->name, u);
bb150966 1099 if (r < 0)
8efbce13 1100 return log_error_errno(r, "Failed to add entry to hashmap: %m");
bb150966
HH
1101 }
1102 unit_times_hashmap = h;
1103
384c2c32 1104 pager_open(arg_pager_flags);
9ea9d4cf 1105
2fffb93b
ZJS
1106 puts("The time when unit became active or started is printed after the \"@\" character.\n"
1107 "The time the unit took to start is printed after the \"+\" character.\n");
bb150966 1108
a6bcef29 1109 if (argc > 1) {
805bf39c 1110 char **name;
a6bcef29 1111 STRV_FOREACH(name, strv_skip(argv, 1))
805bf39c 1112 list_dependencies(bus, *name);
9ea9d4cf 1113 } else
805bf39c 1114 list_dependencies(bus, SPECIAL_DEFAULT_TARGET);
bb150966 1115
a6bcef29 1116 h = hashmap_free(h);
bb150966
HH
1117 return 0;
1118}
1119
a6bcef29
LP
1120static int analyze_blame(int argc, char *argv[], void *userdata) {
1121 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
15567b3a 1122 _cleanup_(unit_times_free_arrayp) UnitTimes *times = NULL;
d8bfdbe1 1123 _cleanup_(table_unrefp) Table *table = NULL;
d8bfdbe1 1124 TableCell *cell;
a6bcef29
LP
1125 int n, r;
1126
f7e29336 1127 r = acquire_bus(&bus, NULL);
a6bcef29 1128 if (r < 0)
10a7340a 1129 return bus_log_connect_error(r, arg_transport);
c170f3a4
LP
1130
1131 n = acquire_time_data(bus, &times);
1132 if (n <= 0)
2265fbf7
SP
1133 return n;
1134
4252171a 1135 table = table_new("time", "unit");
d8bfdbe1
YW
1136 if (!table)
1137 return log_oom();
2265fbf7 1138
d8bfdbe1
YW
1139 table_set_header(table, false);
1140
1141 assert_se(cell = table_get_cell(table, 0, 0));
1142 r = table_set_ellipsize_percent(table, cell, 100);
1143 if (r < 0)
1144 return r;
1145
1146 r = table_set_align_percent(table, cell, 100);
1147 if (r < 0)
1148 return r;
1149
1150 assert_se(cell = table_get_cell(table, 0, 1));
1151 r = table_set_ellipsize_percent(table, cell, 100);
1152 if (r < 0)
1153 return r;
1154
ef1e0b9a 1155 r = table_set_sort(table, (size_t) 0);
d8bfdbe1
YW
1156 if (r < 0)
1157 return r;
1158
1159 r = table_set_reverse(table, 0, true);
1160 if (r < 0)
1161 return r;
9ea9d4cf 1162
6aa601c5 1163 for (UnitTimes *u = times; u->has_data; u++) {
d8bfdbe1
YW
1164 if (u->time <= 0)
1165 continue;
1166
9c46b437 1167 r = table_add_many(table,
47cc458e 1168 TABLE_TIMESPAN_MSEC, u->time,
9c46b437 1169 TABLE_STRING, u->name);
d8bfdbe1 1170 if (r < 0)
9c46b437 1171 return table_log_add_error(r);
2265fbf7 1172 }
c170f3a4 1173
384c2c32 1174 pager_open(arg_pager_flags);
d8bfdbe1
YW
1175
1176 return table_print(table, NULL);
2265fbf7
SP
1177}
1178
a6bcef29
LP
1179static int analyze_time(int argc, char *argv[], void *userdata) {
1180 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
c170f3a4
LP
1181 _cleanup_free_ char *buf = NULL;
1182 int r;
1183
f7e29336 1184 r = acquire_bus(&bus, NULL);
a6bcef29 1185 if (r < 0)
10a7340a 1186 return bus_log_connect_error(r, arg_transport);
a6bcef29 1187
c170f3a4
LP
1188 r = pretty_boot_time(bus, &buf);
1189 if (r < 0)
1190 return r;
1191
1192 puts(buf);
2265fbf7
SP
1193 return 0;
1194}
1195
1ace223c
SJ
1196static int graph_one_property(
1197 sd_bus *bus,
1198 const UnitInfo *u,
1199 const char *prop,
1200 const char *color,
1201 char *patterns[],
1202 char *from_patterns[],
1203 char *to_patterns[]) {
1204
048ecf5b 1205 _cleanup_strv_free_ char **units = NULL;
048ecf5b
TA
1206 char **unit;
1207 int r;
6ecb6cec 1208 bool match_patterns;
1700761b 1209
048ecf5b 1210 assert(u);
1700761b 1211 assert(prop);
048ecf5b
TA
1212 assert(color);
1213
191a3f16 1214 match_patterns = strv_fnmatch(patterns, u->id);
6ecb6cec 1215
191a3f16 1216 if (!strv_isempty(from_patterns) && !match_patterns && !strv_fnmatch(from_patterns, u->id))
1ace223c 1217 return 0;
6ecb6cec 1218
6e6ca4a5 1219 r = bus_get_unit_property_strv(bus, u->unit_path, prop, &units);
07d0eaa0 1220 if (r < 0)
988b9df2 1221 return r;
1700761b 1222
048ecf5b 1223 STRV_FOREACH(unit, units) {
6ecb6cec
ZJS
1224 bool match_patterns2;
1225
191a3f16 1226 match_patterns2 = strv_fnmatch(patterns, *unit);
816f25e8 1227
191a3f16 1228 if (!strv_isempty(to_patterns) && !match_patterns2 && !strv_fnmatch(to_patterns, *unit))
bceccd5e 1229 continue;
e55933db 1230
6ecb6cec 1231 if (!strv_isempty(patterns) && !match_patterns && !match_patterns2)
bceccd5e 1232 continue;
048ecf5b
TA
1233
1234 printf("\t\"%s\"->\"%s\" [color=\"%s\"];\n", u->id, *unit, color);
1700761b
SP
1235 }
1236
1237 return 0;
1238}
1239
4387795e 1240static int graph_one(sd_bus *bus, const UnitInfo *u, char *patterns[], char *from_patterns[], char *to_patterns[]) {
1700761b 1241 int r;
1700761b
SP
1242
1243 assert(bus);
1244 assert(u);
1245
8901b405 1246 if (IN_SET(arg_dot, DEP_ORDER, DEP_ALL)) {
4387795e 1247 r = graph_one_property(bus, u, "After", "green", patterns, from_patterns, to_patterns);
048ecf5b
TA
1248 if (r < 0)
1249 return r;
1700761b
SP
1250 }
1251
8901b405 1252 if (IN_SET(arg_dot, DEP_REQUIRE, DEP_ALL)) {
4387795e 1253 r = graph_one_property(bus, u, "Requires", "black", patterns, from_patterns, to_patterns);
8901b405
MS
1254 if (r < 0)
1255 return r;
1256 r = graph_one_property(bus, u, "Requisite", "darkblue", patterns, from_patterns, to_patterns);
048ecf5b
TA
1257 if (r < 0)
1258 return r;
4387795e 1259 r = graph_one_property(bus, u, "Wants", "grey66", patterns, from_patterns, to_patterns);
048ecf5b
TA
1260 if (r < 0)
1261 return r;
4387795e 1262 r = graph_one_property(bus, u, "Conflicts", "red", patterns, from_patterns, to_patterns);
048ecf5b
TA
1263 if (r < 0)
1264 return r;
1700761b
SP
1265 }
1266
1267 return 0;
1268}
1269
83efb7c2
EV
1270static int expand_patterns(sd_bus *bus, char **patterns, char ***ret) {
1271 _cleanup_strv_free_ char **expanded_patterns = NULL;
1272 char **pattern;
1273 int r;
1274
1275 STRV_FOREACH(pattern, patterns) {
4afd3348 1276 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
83efb7c2
EV
1277 _cleanup_free_ char *unit = NULL, *unit_id = NULL;
1278
1279 if (strv_extend(&expanded_patterns, *pattern) < 0)
1280 return log_oom();
1281
1282 if (string_is_glob(*pattern))
1283 continue;
1284
1285 unit = unit_dbus_path_from_name(*pattern);
1286 if (!unit)
1287 return log_oom();
1288
1289 r = sd_bus_get_property_string(
1290 bus,
1291 "org.freedesktop.systemd1",
1292 unit,
1293 "org.freedesktop.systemd1.Unit",
1294 "Id",
1295 &error,
1296 &unit_id);
1297 if (r < 0)
1298 return log_error_errno(r, "Failed to get ID: %s", bus_error_message(&error, r));
1299
1300 if (!streq(*pattern, unit_id)) {
1301 if (strv_extend(&expanded_patterns, unit_id) < 0)
1302 return log_oom();
1303 }
1304 }
1305
d7a0f1f4 1306 *ret = TAKE_PTR(expanded_patterns); /* do not free */
83efb7c2
EV
1307
1308 return 0;
1309}
1310
a6bcef29 1311static int dot(int argc, char *argv[], void *userdata) {
4afd3348
LP
1312 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1313 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
a6bcef29 1314 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
83efb7c2 1315 _cleanup_strv_free_ char **expanded_patterns = NULL;
4387795e
EV
1316 _cleanup_strv_free_ char **expanded_from_patterns = NULL;
1317 _cleanup_strv_free_ char **expanded_to_patterns = NULL;
1700761b 1318 int r;
f459b602 1319 UnitInfo u;
048ecf5b 1320
f7e29336 1321 r = acquire_bus(&bus, NULL);
a6bcef29 1322 if (r < 0)
10a7340a 1323 return bus_log_connect_error(r, arg_transport);
a6bcef29
LP
1324
1325 r = expand_patterns(bus, strv_skip(argv, 1), &expanded_patterns);
83efb7c2
EV
1326 if (r < 0)
1327 return r;
1328
4387795e
EV
1329 r = expand_patterns(bus, arg_dot_from_patterns, &expanded_from_patterns);
1330 if (r < 0)
1331 return r;
1332
1333 r = expand_patterns(bus, arg_dot_to_patterns, &expanded_to_patterns);
1334 if (r < 0)
1335 return r;
1336
41b88bb8 1337 r = bus_call_method(bus, bus_systemd_mgr, "ListUnits", &error, &reply, NULL);
4ae25393 1338 if (r < 0)
0ed3da7c 1339 log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r));
1700761b 1340
048ecf5b 1341 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
1700761b 1342 if (r < 0)
988b9df2 1343 return bus_log_parse_error(r);
1700761b
SP
1344
1345 printf("digraph systemd {\n");
1346
048ecf5b 1347 while ((r = bus_parse_unit_info(reply, &u)) > 0) {
f459b602 1348
4387795e 1349 r = graph_one(bus, &u, expanded_patterns, expanded_from_patterns, expanded_to_patterns);
1700761b
SP
1350 if (r < 0)
1351 return r;
1352 }
f459b602
MAP
1353 if (r < 0)
1354 return bus_log_parse_error(r);
1700761b
SP
1355
1356 printf("}\n");
1357
1358 log_info(" Color legend: black = Requires\n"
1359 " dark blue = Requisite\n"
1360 " dark grey = Wants\n"
1361 " red = Conflicts\n"
1362 " green = After\n");
1363
1364 if (on_tty())
1365 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
1366 "-- Try a shell pipeline like 'systemd-analyze dot | dot -Tsvg > systemd.svg'!\n");
1367
1368 return 0;
1369}
c8a8806e 1370
c0a1bfac
DT
1371static int dump_fallback(sd_bus *bus) {
1372 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1373 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1374 const char *text = NULL;
1375 int r;
1376
1377 assert(bus);
1378
de770b60 1379 r = bus_call_method(bus, bus_systemd_mgr, "Dump", &error, &reply, NULL);
c0a1bfac
DT
1380 if (r < 0)
1381 return log_error_errno(r, "Failed to issue method call Dump: %s", bus_error_message(&error, r));
1382
1383 r = sd_bus_message_read(reply, "s", &text);
1384 if (r < 0)
1385 return bus_log_parse_error(r);
1386
1387 fputs(text, stdout);
1388 return 0;
1389}
1390
a6bcef29 1391static int dump(int argc, char *argv[], void *userdata) {
4afd3348 1392 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
a6bcef29
LP
1393 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1394 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
c0a1bfac 1395 int fd = -1;
9ea9d4cf 1396 int r;
9ea9d4cf 1397
f7e29336 1398 r = acquire_bus(&bus, NULL);
a6bcef29 1399 if (r < 0)
10a7340a 1400 return bus_log_connect_error(r, arg_transport);
a65615ca 1401
384c2c32 1402 pager_open(arg_pager_flags);
9ea9d4cf 1403
c0a1bfac
DT
1404 if (!sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD))
1405 return dump_fallback(bus);
1406
de770b60 1407 r = bus_call_method(bus, bus_systemd_mgr, "DumpByFileDescriptor", &error, &reply, NULL);
c0a1bfac
DT
1408 if (r < 0) {
1409 /* fall back to Dump if DumpByFileDescriptor is not supported */
1410 if (!IN_SET(r, -EACCES, -EBADR))
c2953e08
ZJS
1411 return log_error_errno(r, "Failed to issue method call DumpByFileDescriptor: %s",
1412 bus_error_message(&error, r));
9ea9d4cf 1413
c0a1bfac
DT
1414 return dump_fallback(bus);
1415 }
1416
1417 r = sd_bus_message_read(reply, "h", &fd);
5b30bef8
LP
1418 if (r < 0)
1419 return bus_log_parse_error(r);
9ea9d4cf 1420
c0a1bfac 1421 fflush(stdout);
f5fbe71d 1422 return copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0);
9ea9d4cf
LP
1423}
1424
854a42fb 1425static int cat_config(int argc, char *argv[], void *userdata) {
2987225c 1426 char **arg, **list;
854a42fb
ZJS
1427 int r;
1428
384c2c32 1429 pager_open(arg_pager_flags);
854a42fb 1430
2987225c
LP
1431 list = strv_skip(argv, 1);
1432 STRV_FOREACH(arg, list) {
971f6ea5
ZJS
1433 const char *t = NULL;
1434
2987225c 1435 if (arg != list)
cb91deaf 1436 print_separator();
854a42fb
ZJS
1437
1438 if (path_is_absolute(*arg)) {
971f6ea5
ZJS
1439 const char *dir;
1440
1441 NULSTR_FOREACH(dir, CONF_PATHS_NULSTR("")) {
1442 t = path_startswith(*arg, dir);
1443 if (t)
1444 break;
1445 }
1446
baaa35ad
ZJS
1447 if (!t)
1448 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
c2953e08 1449 "Path %s does not start with any known prefix.", *arg);
971f6ea5
ZJS
1450 } else
1451 t = *arg;
1452
1453 r = conf_files_cat(arg_root, t);
854a42fb
ZJS
1454 if (r < 0)
1455 return r;
1456 }
1457
1458 return 0;
1459}
1460
a87b151a 1461static int verb_log_control(int argc, char *argv[], void *userdata) {
a6bcef29 1462 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2ca2a91c
LP
1463 int r;
1464
b98416e1 1465 assert(IN_SET(argc, 1, 2));
2ca2a91c 1466
f7e29336 1467 r = acquire_bus(&bus, NULL);
a6bcef29 1468 if (r < 0)
10a7340a 1469 return bus_log_connect_error(r, arg_transport);
a65615ca 1470
a87b151a 1471 return verb_log_control_common(bus, "org.freedesktop.systemd1", argv[0], argc == 2 ? argv[1] : NULL);
90657286
YW
1472}
1473
e67cd21d
ZJS
1474static bool strv_fnmatch_strv_or_empty(char* const* patterns, char **strv, int flags) {
1475 char **s;
1476 STRV_FOREACH(s, strv)
1477 if (strv_fnmatch_or_empty(patterns, *s, flags))
1478 return true;
1479
1480 return false;
1481}
1482
1483static int do_unit_files(int argc, char *argv[], void *userdata) {
1484 _cleanup_(lookup_paths_free) LookupPaths lp = {};
1485 _cleanup_hashmap_free_ Hashmap *unit_ids = NULL;
1486 _cleanup_hashmap_free_ Hashmap *unit_names = NULL;
1487 char **patterns = strv_skip(argv, 1);
e67cd21d
ZJS
1488 const char *k, *dst;
1489 char **v;
1490 int r;
1491
1492 r = lookup_paths_init(&lp, arg_scope, 0, NULL);
1493 if (r < 0)
1494 return log_error_errno(r, "lookup_paths_init() failed: %m");
1495
91e0ee5f 1496 r = unit_file_build_name_map(&lp, NULL, &unit_ids, &unit_names, NULL);
e67cd21d
ZJS
1497 if (r < 0)
1498 return log_error_errno(r, "unit_file_build_name_map() failed: %m");
1499
90e74a66 1500 HASHMAP_FOREACH_KEY(dst, k, unit_ids) {
e67cd21d
ZJS
1501 if (!strv_fnmatch_or_empty(patterns, k, FNM_NOESCAPE) &&
1502 !strv_fnmatch_or_empty(patterns, dst, FNM_NOESCAPE))
1503 continue;
1504
1505 printf("ids: %s → %s\n", k, dst);
1506 }
1507
90e74a66 1508 HASHMAP_FOREACH_KEY(v, k, unit_names) {
e67cd21d
ZJS
1509 if (!strv_fnmatch_or_empty(patterns, k, FNM_NOESCAPE) &&
1510 !strv_fnmatch_strv_or_empty(patterns, v, FNM_NOESCAPE))
1511 continue;
1512
1513 _cleanup_free_ char *j = strv_join(v, ", ");
1514 printf("aliases: %s ← %s\n", k, j);
1515 }
1516
1517 return 0;
1518}
1519
31a5924e 1520static int dump_unit_paths(int argc, char *argv[], void *userdata) {
8e766630 1521 _cleanup_(lookup_paths_free) LookupPaths paths = {};
31a5924e
ZJS
1522 int r;
1523 char **p;
1524
1525 r = lookup_paths_init(&paths, arg_scope, 0, NULL);
1526 if (r < 0)
1527 return log_error_errno(r, "lookup_paths_init() failed: %m");
1528
1529 STRV_FOREACH(p, paths.search_path)
1530 puts(*p);
1531
1532 return 0;
1533}
1534
417b82e1
MG
1535static int dump_exit_status(int argc, char *argv[], void *userdata) {
1536 _cleanup_(table_unrefp) Table *table = NULL;
1537 int r;
1538
1539 table = table_new("name", "status", "class");
1540 if (!table)
1541 return log_oom();
1542
1543 r = table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
1544 if (r < 0)
1545 return log_error_errno(r, "Failed to right-align status: %m");
1546
1547 if (strv_isempty(strv_skip(argv, 1)))
1548 for (size_t i = 0; i < ELEMENTSOF(exit_status_mappings); i++) {
1549 if (!exit_status_mappings[i].name)
1550 continue;
1551
1552 r = table_add_many(table,
1553 TABLE_STRING, exit_status_mappings[i].name,
1554 TABLE_INT, (int) i,
1555 TABLE_STRING, exit_status_class(i));
1556 if (r < 0)
9c46b437 1557 return table_log_add_error(r);
417b82e1
MG
1558 }
1559 else
1560 for (int i = 1; i < argc; i++) {
1561 int status;
1562
1563 status = exit_status_from_string(argv[i]);
1564 if (status < 0)
7211c853 1565 return log_error_errno(status, "Invalid exit status \"%s\".", argv[i]);
417b82e1
MG
1566
1567 assert(status >= 0 && (size_t) status < ELEMENTSOF(exit_status_mappings));
1568 r = table_add_many(table,
1569 TABLE_STRING, exit_status_mappings[status].name ?: "-",
1570 TABLE_INT, status,
1571 TABLE_STRING, exit_status_class(status) ?: "-");
1572 if (r < 0)
9c46b437 1573 return table_log_add_error(r);
417b82e1
MG
1574 }
1575
384c2c32 1576 pager_open(arg_pager_flags);
417b82e1
MG
1577
1578 return table_print(table, NULL);
1579}
1580
b2af819b
LP
1581static int dump_capabilities(int argc, char *argv[], void *userdata) {
1582 _cleanup_(table_unrefp) Table *table = NULL;
1583 unsigned last_cap;
1584 int r;
1585
1586 table = table_new("name", "number");
1587 if (!table)
1588 return log_oom();
1589
1590 (void) table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
1591
1592 /* Determine the maximum of the last cap known by the kernel and by us */
1593 last_cap = MAX((unsigned) CAP_LAST_CAP, cap_last_cap());
1594
1595 if (strv_isempty(strv_skip(argv, 1)))
1596 for (unsigned c = 0; c <= last_cap; c++) {
1597 r = table_add_many(table,
1598 TABLE_STRING, capability_to_name(c) ?: "cap_???",
1599 TABLE_UINT, c);
1600 if (r < 0)
1601 return table_log_add_error(r);
1602 }
1603 else {
1604 for (int i = 1; i < argc; i++) {
1605 int c;
1606
1607 c = capability_from_name(argv[i]);
1608 if (c < 0 || (unsigned) c > last_cap)
1609 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Capability \"%s\" not known.", argv[i]);
1610
1611 r = table_add_many(table,
1612 TABLE_STRING, capability_to_name(c) ?: "cap_???",
1613 TABLE_UINT, (unsigned) c);
1614 if (r < 0)
1615 return table_log_add_error(r);
1616 }
1617
ef1e0b9a 1618 (void) table_set_sort(table, (size_t) 1);
b2af819b
LP
1619 }
1620
384c2c32 1621 pager_open(arg_pager_flags);
b2af819b
LP
1622
1623 return table_print(table, NULL);
1624}
1625
349cc4a5 1626#if HAVE_SECCOMP
cdf6258c
LP
1627
1628static int load_kernel_syscalls(Set **ret) {
be327321 1629 _cleanup_set_free_ Set *syscalls = NULL;
cdf6258c
LP
1630 _cleanup_fclose_ FILE *f = NULL;
1631 int r;
1632
1ace223c
SJ
1633 /* Let's read the available system calls from the list of available tracing events. Slightly dirty,
1634 * but good enough for analysis purposes. */
cdf6258c 1635
196f3067
LP
1636 f = fopen("/sys/kernel/tracing/available_events", "re");
1637 if (!f) {
1638 /* We tried the non-debugfs mount point and that didn't work. If it wasn't mounted, maybe the
1639 * old debugfs mount point works? */
1640 f = fopen("/sys/kernel/debug/tracing/available_events", "re");
1641 if (!f)
1642 return log_full_errno(IN_SET(errno, EPERM, EACCES, ENOENT) ? LOG_DEBUG : LOG_WARNING, errno,
1643 "Can't read open tracefs' available_events file: %m");
1644 }
cdf6258c
LP
1645
1646 for (;;) {
1647 _cleanup_free_ char *line = NULL;
1648 const char *e;
1649
1650 r = read_line(f, LONG_LINE_MAX, &line);
1651 if (r < 0)
1652 return log_error_errno(r, "Failed to read system call list: %m");
1653 if (r == 0)
1654 break;
1655
1656 e = startswith(line, "syscalls:sys_enter_");
1657 if (!e)
1658 continue;
1659
1ace223c
SJ
1660 /* These are named differently inside the kernel than their external name for historical
1661 * reasons. Let's hide them here. */
cdf6258c
LP
1662 if (STR_IN_SET(e, "newuname", "newfstat", "newstat", "newlstat", "sysctl"))
1663 continue;
1664
be327321 1665 r = set_put_strdup(&syscalls, e);
cdf6258c
LP
1666 if (r < 0)
1667 return log_error_errno(r, "Failed to add system call to list: %m");
1668 }
1669
1670 *ret = TAKE_PTR(syscalls);
1671 return 0;
1672}
1673
a748b122 1674static void syscall_set_remove(Set *s, const SyscallFilterSet *set) {
cdf6258c
LP
1675 const char *syscall;
1676
b41711cd
ILG
1677 if (!set)
1678 return;
1679
cdf6258c
LP
1680 NULSTR_FOREACH(syscall, set->value) {
1681 if (syscall[0] == '@')
1682 continue;
1683
81610e96 1684 free(set_remove(s, syscall));
cdf6258c
LP
1685 }
1686}
1687
869feb33
ZJS
1688static void dump_syscall_filter(const SyscallFilterSet *set) {
1689 const char *syscall;
1690
a9bfb3ef
LP
1691 printf("%s%s%s\n"
1692 " # %s\n",
1693 ansi_highlight(),
1694 set->name,
1695 ansi_normal(),
1696 set->help);
1697
869feb33 1698 NULSTR_FOREACH(syscall, set->value)
1ace223c 1699 printf(" %s%s%s\n", syscall[0] == '@' ? ansi_underline() : "", syscall, ansi_normal());
869feb33
ZJS
1700}
1701
a6bcef29 1702static int dump_syscall_filters(int argc, char *argv[], void *userdata) {
869feb33
ZJS
1703 bool first = true;
1704
384c2c32 1705 pager_open(arg_pager_flags);
869feb33 1706
a6bcef29 1707 if (strv_isempty(strv_skip(argv, 1))) {
a748b122
LP
1708 _cleanup_set_free_ Set *kernel = NULL, *known = NULL;
1709 const char *sys;
6aa601c5 1710 int k;
cdf6258c 1711
a748b122
LP
1712 NULSTR_FOREACH(sys, syscall_filter_sets[SYSCALL_FILTER_SET_KNOWN].value)
1713 if (set_put_strdup(&known, sys) < 0)
1714 return log_oom();
1715
cdf6258c 1716 k = load_kernel_syscalls(&kernel);
869feb33 1717
6aa601c5 1718 for (int i = 0; i < _SYSCALL_FILTER_SET_MAX; i++) {
cdf6258c 1719 const SyscallFilterSet *set = syscall_filter_sets + i;
869feb33
ZJS
1720 if (!first)
1721 puts("");
cdf6258c
LP
1722
1723 dump_syscall_filter(set);
a748b122
LP
1724 syscall_set_remove(kernel, set);
1725 if (i != SYSCALL_FILTER_SET_KNOWN)
1726 syscall_set_remove(known, set);
869feb33
ZJS
1727 first = false;
1728 }
cdf6258c 1729
a748b122
LP
1730 if (!set_isempty(known)) {
1731 _cleanup_free_ char **l = NULL;
1732 char **syscall;
1733
1734 printf("\n"
1735 "# %sUngrouped System Calls%s (known but not included in any of the groups except @known):\n",
1736 ansi_highlight(), ansi_normal());
1737
1738 l = set_get_strv(known);
1739 if (!l)
1740 return log_oom();
1741
1742 strv_sort(l);
1743
1744 STRV_FOREACH(syscall, l)
1745 printf("# %s\n", *syscall);
1746 }
1747
cdf6258c
LP
1748 if (k < 0) {
1749 fputc('\n', stdout);
1750 fflush(stdout);
1751 log_notice_errno(k, "# Not showing unlisted system calls, couldn't retrieve kernel system call list: %m");
1752 } else if (!set_isempty(kernel)) {
ea334dc3
LP
1753 _cleanup_free_ char **l = NULL;
1754 char **syscall;
cdf6258c
LP
1755
1756 printf("\n"
a9bfb3ef
LP
1757 "# %sUnlisted System Calls%s (supported by the local kernel, but not included in any of the groups listed above):\n",
1758 ansi_highlight(), ansi_normal());
cdf6258c 1759
ea334dc3
LP
1760 l = set_get_strv(kernel);
1761 if (!l)
1762 return log_oom();
1763
1764 strv_sort(l);
1765
1766 STRV_FOREACH(syscall, l)
1767 printf("# %s\n", *syscall);
cdf6258c 1768 }
869feb33
ZJS
1769 } else {
1770 char **name;
1771
a6bcef29 1772 STRV_FOREACH(name, strv_skip(argv, 1)) {
869feb33
ZJS
1773 const SyscallFilterSet *set;
1774
1775 if (!first)
1776 puts("");
1777
1778 set = syscall_filter_set_find(*name);
1779 if (!set) {
1780 /* make sure the error appears below normal output */
1781 fflush(stdout);
1782
c2953e08
ZJS
1783 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
1784 "Filter set \"%s\" not found.", *name);
869feb33
ZJS
1785 }
1786
1787 dump_syscall_filter(set);
1788 first = false;
1789 }
1790 }
1791
1792 return 0;
1793}
1794
1720590b 1795#else
d3340e6f 1796static int dump_syscall_filters(int argc, char *argv[], void *userdata) {
1ace223c 1797 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Not compiled with syscall filters, sorry.");
d3340e6f 1798}
1720590b
ZJS
1799#endif
1800
b41711cd
ILG
1801static int load_available_kernel_filesystems(Set **ret) {
1802 _cleanup_set_free_ Set *filesystems = NULL;
1803 int r;
1804 char *t;
1805
1806 assert(ret);
1807
1808 /* Let's read the available filesystems */
1809
1810 r = read_virtual_file("/proc/filesystems", SIZE_MAX, &t, NULL);
1811 if (r < 0)
1812 return r;
1813
1814 for (int i = 0;;) {
1815 _cleanup_free_ char *line = NULL;
1816 const char *p;
1817
1818 r = string_extract_line(t, i++, &line);
1819 if (r < 0)
1820 return log_oom();
1821 if (r == 0)
1822 break;
1823
1824 if (!line)
1825 line = t;
1826
1827 p = strchr(line, '\t');
1828 if (!p)
1829 continue;
1830
1831 p += strspn(p, WHITESPACE);
1832
1833 r = set_put_strdup(&filesystems, p);
1834 if (r < 0)
1835 return log_error_errno(r, "Failed to add filesystem to list: %m");
1836 }
1837
1838 *ret = TAKE_PTR(filesystems);
1839 return 0;
1840}
1841
1842static void filesystem_set_remove(Set *s, const FilesystemSet *set) {
1843 const char *filesystem;
1844
1845 NULSTR_FOREACH(filesystem, set->value) {
1846 if (filesystem[0] == '@')
1847 continue;
1848
1849 free(set_remove(s, filesystem));
1850 }
1851}
1852
1853static void dump_filesystem(const FilesystemSet *set) {
1854 const char *filesystem;
1855
1856 if (!set)
1857 return;
1858
1859 printf("%s%s%s\n"
1860 " # %s\n",
1861 ansi_highlight(),
1862 set->name,
1863 ansi_normal(),
1864 set->help);
1865
1866 NULSTR_FOREACH(filesystem, set->value)
1867 printf(" %s%s%s\n", filesystem[0] == '@' ? ansi_underline() : "", filesystem, ansi_normal());
1868}
1869
1870static int dump_filesystems(int argc, char *argv[], void *userdata) {
1871 bool first = true;
1872
1873#if ! HAVE_LIBBPF
1874 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Not compiled with libbpf support, sorry.");
1875#endif
1876
384c2c32 1877 pager_open(arg_pager_flags);
b41711cd
ILG
1878
1879 if (strv_isempty(strv_skip(argv, 1))) {
1880 _cleanup_set_free_ Set *kernel = NULL, *known = NULL;
1881 const char *fs;
1882 int k;
1883
1884 NULSTR_FOREACH(fs, filesystem_sets[FILESYSTEM_SET_KNOWN].value)
1885 if (set_put_strdup(&known, fs) < 0)
1886 return log_oom();
1887
1888 k = load_available_kernel_filesystems(&kernel);
1889
1890 for (FilesystemGroups i = 0; i < _FILESYSTEM_SET_MAX; i++) {
1891 const FilesystemSet *set = filesystem_sets + i;
1892 if (!first)
1893 puts("");
1894
1895 dump_filesystem(set);
1896 filesystem_set_remove(kernel, set);
1897 if (i != FILESYSTEM_SET_KNOWN)
1898 filesystem_set_remove(known, set);
1899 first = false;
1900 }
1901
1902 if (!set_isempty(known)) {
1903 _cleanup_free_ char **l = NULL;
1904 char **filesystem;
1905
1906 printf("\n"
1907 "# %sUngrouped filesystems%s (known but not included in any of the groups except @known):\n",
1908 ansi_highlight(), ansi_normal());
1909
1910 l = set_get_strv(known);
1911 if (!l)
1912 return log_oom();
1913
1914 strv_sort(l);
1915
1916 STRV_FOREACH(filesystem, l)
1917 printf("# %s\n", *filesystem);
1918 }
1919
1920 if (k < 0) {
1921 fputc('\n', stdout);
1922 fflush(stdout);
1923 log_notice_errno(k, "# Not showing unlisted filesystems, couldn't retrieve kernel filesystem list: %m");
1924 } else if (!set_isempty(kernel)) {
1925 _cleanup_free_ char **l = NULL;
1926 char **filesystem;
1927
1928 printf("\n"
1929 "# %sUnlisted filesystems%s (available to the local kernel, but not included in any of the groups listed above):\n",
1930 ansi_highlight(), ansi_normal());
1931
1932 l = set_get_strv(kernel);
1933 if (!l)
1934 return log_oom();
1935
1936 strv_sort(l);
1937
1938 STRV_FOREACH(filesystem, l)
1939 printf("# %s\n", *filesystem);
1940 }
1941 } else {
1942 char **name;
1943
1944 STRV_FOREACH(name, strv_skip(argv, 1)) {
1945 const FilesystemSet *set;
1946
1947 if (!first)
1948 puts("");
1949
1950 set = filesystem_set_find(*name);
1951 if (!set) {
1952 /* make sure the error appears below normal output */
1953 fflush(stdout);
1954
1955 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
1956 "Filesystem set \"%s\" not found.", *name);
1957 }
1958
1959 dump_filesystem(set);
1960 first = false;
1961 }
1962 }
1963
1964 return 0;
1965}
1966
c269607f
ZJS
1967static void parsing_hint(const char *p, bool calendar, bool timestamp, bool timespan) {
1968 if (calendar && calendar_spec_from_string(p, NULL) >= 0)
1969 log_notice("Hint: this expression is a valid calendar specification. "
1970 "Use 'systemd-analyze calendar \"%s\"' instead?", p);
1971 if (timestamp && parse_timestamp(p, NULL) >= 0)
1972 log_notice("Hint: this expression is a valid timestamp. "
1973 "Use 'systemd-analyze timestamp \"%s\"' instead?", p);
1974 if (timespan && parse_time(p, NULL, USEC_PER_SEC) >= 0)
1975 log_notice("Hint: this expression is a valid timespan. "
1976 "Use 'systemd-analyze timespan \"%s\"' instead?", p);
1977}
1978
3f1c1287
CD
1979static int dump_timespan(int argc, char *argv[], void *userdata) {
1980 char **input_timespan;
1981
1982 STRV_FOREACH(input_timespan, strv_skip(argv, 1)) {
7503c6e8
YW
1983 _cleanup_(table_unrefp) Table *table = NULL;
1984 usec_t output_usecs;
1985 TableCell *cell;
3f1c1287 1986 int r;
3f1c1287
CD
1987
1988 r = parse_time(*input_timespan, &output_usecs, USEC_PER_SEC);
c269607f
ZJS
1989 if (r < 0) {
1990 log_error_errno(r, "Failed to parse time span '%s': %m", *input_timespan);
1991 parsing_hint(*input_timespan, true, true, false);
1992 return r;
1993 }
3f1c1287 1994
4252171a 1995 table = table_new("name", "value");
7503c6e8
YW
1996 if (!table)
1997 return log_oom();
1998
1999 table_set_header(table, false);
2000
2001 assert_se(cell = table_get_cell(table, 0, 0));
2002 r = table_set_ellipsize_percent(table, cell, 100);
2003 if (r < 0)
2004 return r;
2005
2006 r = table_set_align_percent(table, cell, 100);
2007 if (r < 0)
2008 return r;
2009
2010 assert_se(cell = table_get_cell(table, 0, 1));
2011 r = table_set_ellipsize_percent(table, cell, 100);
2012 if (r < 0)
2013 return r;
2014
9c46b437
YW
2015 r = table_add_many(table,
2016 TABLE_STRING, "Original:",
2017 TABLE_STRING, *input_timespan);
7503c6e8 2018 if (r < 0)
9c46b437 2019 return table_log_add_error(r);
7503c6e8
YW
2020
2021 r = table_add_cell_stringf(table, NULL, "%ss:", special_glyph(SPECIAL_GLYPH_MU));
2022 if (r < 0)
9c46b437 2023 return table_log_add_error(r);
7503c6e8 2024
9c46b437 2025 r = table_add_many(table,
47cc458e 2026 TABLE_UINT64, output_usecs,
9c46b437 2027 TABLE_STRING, "Human:",
47cc458e 2028 TABLE_TIMESPAN, output_usecs,
9c46b437 2029 TABLE_SET_COLOR, ansi_highlight());
7503c6e8 2030 if (r < 0)
9c46b437 2031 return table_log_add_error(r);
7503c6e8
YW
2032
2033 r = table_print(table, NULL);
2034 if (r < 0)
2035 return r;
3f1c1287
CD
2036
2037 if (input_timespan[1])
2038 putchar('\n');
2039 }
2040
2041 return EXIT_SUCCESS;
2042}
2043
2cae4711 2044static int test_timestamp_one(const char *p) {
af2d6d47
YW
2045 _cleanup_(table_unrefp) Table *table = NULL;
2046 TableCell *cell;
2cae4711 2047 usec_t usec;
2cae4711
ZJS
2048 int r;
2049
2050 r = parse_timestamp(p, &usec);
c269607f
ZJS
2051 if (r < 0) {
2052 log_error_errno(r, "Failed to parse \"%s\": %m", p);
2053 parsing_hint(p, true, false, true);
2054 return r;
2055 }
2cae4711 2056
4252171a 2057 table = table_new("name", "value");
af2d6d47
YW
2058 if (!table)
2059 return log_oom();
2cae4711 2060
af2d6d47 2061 table_set_header(table, false);
2cae4711 2062
af2d6d47
YW
2063 assert_se(cell = table_get_cell(table, 0, 0));
2064 r = table_set_ellipsize_percent(table, cell, 100);
2065 if (r < 0)
2066 return r;
ea62aa24 2067
af2d6d47
YW
2068 r = table_set_align_percent(table, cell, 100);
2069 if (r < 0)
2070 return r;
2cae4711 2071
af2d6d47
YW
2072 assert_se(cell = table_get_cell(table, 0, 1));
2073 r = table_set_ellipsize_percent(table, cell, 100);
2074 if (r < 0)
2075 return r;
2076
9c46b437
YW
2077 r = table_add_many(table,
2078 TABLE_STRING, "Original form:",
2079 TABLE_STRING, p,
2080 TABLE_STRING, "Normalized form:",
47cc458e 2081 TABLE_TIMESTAMP, usec,
9c46b437 2082 TABLE_SET_COLOR, ansi_highlight_blue());
af2d6d47 2083 if (r < 0)
9c46b437 2084 return table_log_add_error(r);
af2d6d47
YW
2085
2086 if (!in_utc_timezone()) {
9c46b437
YW
2087 r = table_add_many(table,
2088 TABLE_STRING, "(in UTC):",
47cc458e 2089 TABLE_TIMESTAMP_UTC, usec);
af2d6d47 2090 if (r < 0)
9c46b437 2091 return table_log_add_error(r);
af2d6d47
YW
2092 }
2093
2094 r = table_add_cell(table, NULL, TABLE_STRING, "UNIX seconds:");
2095 if (r < 0)
9c46b437 2096 return table_log_add_error(r);
af2d6d47 2097
08f105df 2098 if (usec % USEC_PER_SEC == 0)
9c46b437 2099 r = table_add_cell_stringf(table, NULL, "@%"PRI_USEC,
08f105df
ZJS
2100 usec / USEC_PER_SEC);
2101 else
9c46b437 2102 r = table_add_cell_stringf(table, NULL, "@%"PRI_USEC".%06"PRI_USEC"",
08f105df
ZJS
2103 usec / USEC_PER_SEC,
2104 usec % USEC_PER_SEC);
af2d6d47
YW
2105 if (r < 0)
2106 return r;
2107
9c46b437
YW
2108 r = table_add_many(table,
2109 TABLE_STRING, "From now:",
47cc458e 2110 TABLE_TIMESTAMP_RELATIVE, usec);
af2d6d47 2111 if (r < 0)
9c46b437 2112 return table_log_add_error(r);
af2d6d47
YW
2113
2114 return table_print(table, NULL);
2cae4711
ZJS
2115}
2116
2117static int test_timestamp(int argc, char *argv[], void *userdata) {
2118 int ret = 0, r;
2119 char **p;
2120
2121 STRV_FOREACH(p, strv_skip(argv, 1)) {
2122 r = test_timestamp_one(*p);
2123 if (ret == 0 && r < 0)
2124 ret = r;
2125
2126 if (*(p + 1))
2127 putchar('\n');
2128 }
2129
2130 return ret;
2131}
2132
84653d99
ZJS
2133static int test_calendar_one(usec_t n, const char *p) {
2134 _cleanup_(calendar_spec_freep) CalendarSpec *spec = NULL;
9c712cf2 2135 _cleanup_(table_unrefp) Table *table = NULL;
84653d99 2136 _cleanup_free_ char *t = NULL;
9c712cf2 2137 TableCell *cell;
84653d99 2138 int r;
6d86f4bd 2139
84653d99 2140 r = calendar_spec_from_string(p, &spec);
c269607f
ZJS
2141 if (r < 0) {
2142 log_error_errno(r, "Failed to parse calendar specification '%s': %m", p);
2143 parsing_hint(p, false, true, true);
2144 return r;
2145 }
6d86f4bd 2146
84653d99
ZJS
2147 r = calendar_spec_to_string(spec, &t);
2148 if (r < 0)
2149 return log_error_errno(r, "Failed to format calendar specification '%s': %m", p);
6d86f4bd 2150
4252171a 2151 table = table_new("name", "value");
9c712cf2
YW
2152 if (!table)
2153 return log_oom();
2154
2155 table_set_header(table, false);
2156
2157 assert_se(cell = table_get_cell(table, 0, 0));
2158 r = table_set_ellipsize_percent(table, cell, 100);
2159 if (r < 0)
2160 return r;
2161
2162 r = table_set_align_percent(table, cell, 100);
2163 if (r < 0)
2164 return r;
2165
2166 assert_se(cell = table_get_cell(table, 0, 1));
2167 r = table_set_ellipsize_percent(table, cell, 100);
2168 if (r < 0)
2169 return r;
2170
2171 if (!streq(t, p)) {
9c46b437
YW
2172 r = table_add_many(table,
2173 TABLE_STRING, "Original form:",
2174 TABLE_STRING, p);
9c712cf2 2175 if (r < 0)
9c46b437 2176 return table_log_add_error(r);
9c712cf2
YW
2177 }
2178
9c46b437
YW
2179 r = table_add_many(table,
2180 TABLE_STRING, "Normalized form:",
2181 TABLE_STRING, t);
9c712cf2 2182 if (r < 0)
9c46b437 2183 return table_log_add_error(r);
6d86f4bd 2184
84653d99 2185 for (unsigned i = 0; i < arg_iterations; i++) {
84653d99
ZJS
2186 usec_t next;
2187
2188 r = calendar_spec_next_usec(spec, n, &next);
2189 if (r == -ENOENT) {
9c712cf2 2190 if (i == 0) {
9c46b437
YW
2191 r = table_add_many(table,
2192 TABLE_STRING, "Next elapse:",
2193 TABLE_STRING, "never",
2194 TABLE_SET_COLOR, ansi_highlight_yellow());
9c712cf2 2195 if (r < 0)
9c46b437 2196 return table_log_add_error(r);
9c712cf2
YW
2197 }
2198 break;
84653d99
ZJS
2199 }
2200 if (r < 0)
2201 return log_error_errno(r, "Failed to determine next elapse for '%s': %m", p);
6d86f4bd 2202
9c712cf2 2203 if (i == 0) {
9c46b437
YW
2204 r = table_add_many(table,
2205 TABLE_STRING, "Next elapse:",
47cc458e 2206 TABLE_TIMESTAMP, next,
9c46b437 2207 TABLE_SET_COLOR, ansi_highlight_blue());
9c712cf2 2208 if (r < 0)
9c46b437 2209 return table_log_add_error(r);
9c712cf2 2210 } else {
1ace223c 2211 int k = DECIMAL_STR_WIDTH(i + 1);
6d86f4bd 2212
84653d99
ZJS
2213 if (k < 8)
2214 k = 8 - k;
2215 else
2216 k = 0;
f2ccf832 2217
9c712cf2
YW
2218 r = table_add_cell_stringf(table, NULL, "Iter. #%u:", i+1);
2219 if (r < 0)
9c46b437 2220 return table_log_add_error(r);
9c712cf2 2221
9c46b437 2222 r = table_add_many(table,
47cc458e 2223 TABLE_TIMESTAMP, next,
9c46b437 2224 TABLE_SET_COLOR, ansi_highlight_blue());
9c712cf2 2225 if (r < 0)
9c46b437 2226 return table_log_add_error(r);
84653d99 2227 }
f2ccf832 2228
9c712cf2 2229 if (!in_utc_timezone()) {
9c46b437
YW
2230 r = table_add_many(table,
2231 TABLE_STRING, "(in UTC):",
47cc458e 2232 TABLE_TIMESTAMP_UTC, next);
9c712cf2 2233 if (r < 0)
9c46b437 2234 return table_log_add_error(r);
9c712cf2
YW
2235 }
2236
9c46b437
YW
2237 r = table_add_many(table,
2238 TABLE_STRING, "From now:",
47cc458e 2239 TABLE_TIMESTAMP_RELATIVE, next);
9c712cf2 2240 if (r < 0)
9c46b437 2241 return table_log_add_error(r);
6d86f4bd 2242
84653d99
ZJS
2243 n = next;
2244 }
f2ccf832 2245
9c712cf2 2246 return table_print(table, NULL);
84653d99 2247}
6d86f4bd 2248
84653d99
ZJS
2249static int test_calendar(int argc, char *argv[], void *userdata) {
2250 int ret = 0, r;
2251 char **p;
2252 usec_t n;
6d86f4bd 2253
985c1880
LP
2254 if (arg_base_time != USEC_INFINITY)
2255 n = arg_base_time;
2256 else
2257 n = now(CLOCK_REALTIME); /* We want to use the same "base" for all expressions */
f2ccf832 2258
84653d99
ZJS
2259 STRV_FOREACH(p, strv_skip(argv, 1)) {
2260 r = test_calendar_one(n, *p);
2261 if (ret == 0 && r < 0)
2262 ret = r;
6d86f4bd 2263
1ace223c 2264 if (*(p + 1))
6d86f4bd
LP
2265 putchar('\n');
2266 }
2267
2268 return ret;
2269}
2270
889d695d
JK
2271static int service_watchdogs(int argc, char *argv[], void *userdata) {
2272 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2273 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2274 int b, r;
2275
90657286 2276 assert(IN_SET(argc, 1, 2));
889d695d
JK
2277 assert(argv);
2278
f7e29336 2279 r = acquire_bus(&bus, NULL);
90657286 2280 if (r < 0)
10a7340a 2281 return bus_log_connect_error(r, arg_transport);
90657286 2282
90657286 2283 if (argc == 1) {
6ab86319 2284 /* get ServiceWatchdogs */
de770b60 2285 r = bus_get_property_trivial(bus, bus_systemd_mgr, "ServiceWatchdogs", &error, 'b', &b);
90657286
YW
2286 if (r < 0)
2287 return log_error_errno(r, "Failed to get service-watchdog state: %s", bus_error_message(&error, r));
2288
2289 printf("%s\n", yes_no(!!b));
2290
6ab86319
ZJS
2291 } else {
2292 /* set ServiceWatchdogs */
2293 b = parse_boolean(argv[1]);
2294 if (b < 0)
2295 return log_error_errno(b, "Failed to parse service-watchdogs argument: %m");
90657286 2296
de770b60 2297 r = bus_set_property(bus, bus_systemd_mgr, "ServiceWatchdogs", &error, "b", b);
6ab86319
ZJS
2298 if (r < 0)
2299 return log_error_errno(r, "Failed to set service-watchdog state: %s", bus_error_message(&error, r));
889d695d
JK
2300 }
2301
889d695d
JK
2302 return 0;
2303}
2304
edfea9fe 2305static int do_condition(int argc, char *argv[], void *userdata) {
8de7929d 2306 return verify_conditions(strv_skip(argv, 1), arg_scope, arg_unit, arg_root);
edfea9fe
ZJS
2307}
2308
a6bcef29 2309static int do_verify(int argc, char *argv[], void *userdata) {
da845dab
AB
2310 _cleanup_strv_free_ char **filenames = NULL;
2311 _cleanup_(rm_rf_physical_and_freep) char *tempdir = NULL;
2312 int r;
2313
2314 r = mkdtemp_malloc("/tmp/systemd-analyze-XXXXXX", &tempdir);
2315 if (r < 0)
2316 return log_error_errno(r, "Failed to setup working directory: %m");
2317
2318 r = process_aliases(argv, tempdir, &filenames);
2319 if (r < 0)
2320 return log_error_errno(r, "Couldn't process aliases: %m");
2321
2322 return verify_units(filenames, arg_scope, arg_man, arg_generators, arg_recursive_errors, arg_root);
a6bcef29
LP
2323}
2324
ec16f3b6
LP
2325static int do_security(int argc, char *argv[], void *userdata) {
2326 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
ecfd082b 2327 _cleanup_(json_variant_unrefp) JsonVariant *policy = NULL;
ec16f3b6 2328 int r;
ecfd082b 2329 unsigned line, column;
ec16f3b6
LP
2330
2331 r = acquire_bus(&bus, NULL);
2332 if (r < 0)
10a7340a 2333 return bus_log_connect_error(r, arg_transport);
ec16f3b6 2334
384c2c32 2335 pager_open(arg_pager_flags);
ec16f3b6 2336
ecfd082b
MG
2337 if (arg_security_policy) {
2338 r = json_parse_file(/*f=*/ NULL, arg_security_policy, /*flags=*/ 0, &policy, &line, &column);
2339 if (r < 0)
2340 return log_error_errno(r, "Failed to parse '%s' at %u:%u: %m", arg_security_policy, line, column);
2341 } else {
2342 _cleanup_fclose_ FILE *f = NULL;
2343 _cleanup_free_ char *pp = NULL;
2344
2345 r = search_and_fopen_nulstr("systemd-analyze-security.policy", "re", /*root=*/ NULL, CONF_PATHS_NULSTR("systemd"), &f, &pp);
2346 if (r < 0 && r != -ENOENT)
2347 return r;
2348
b98416e1 2349 if (f) {
ecfd082b
MG
2350 r = json_parse_file(f, pp, /*flags=*/ 0, &policy, &line, &column);
2351 if (r < 0)
2352 return log_error_errno(r, "[%s:%u:%u] Failed to parse JSON policy: %m", pp, line, column);
2353 }
2354 }
2355
2356 return analyze_security(bus,
2357 strv_skip(argv, 1),
2358 policy,
2359 arg_scope,
2360 arg_man,
2361 arg_generators,
2362 arg_offline,
2363 arg_threshold,
2364 arg_root,
4b4a8ef7
MG
2365 arg_json_format_flags,
2366 arg_pager_flags,
ecfd082b 2367 /*flags=*/ 0);
ec16f3b6
LP
2368}
2369
a6bcef29 2370static int help(int argc, char *argv[], void *userdata) {
49139a5d 2371 _cleanup_free_ char *link = NULL, *dot_link = NULL;
37ec0fdd 2372 int r;
9ea9d4cf 2373
384c2c32 2374 pager_open(arg_pager_flags);
9ea9d4cf 2375
37ec0fdd
LP
2376 r = terminal_urlify_man("systemd-analyze", "1", &link);
2377 if (r < 0)
2378 return log_oom();
2379
49139a5d
LP
2380 /* Not using terminal_urlify_man() for this, since we don't want the "man page" text suffix in this case. */
2381 r = terminal_urlify("man:dot(1)", "dot(1)", &dot_link);
2382 if (r < 0)
2383 return log_oom();
2384
353b2baa
LP
2385 printf("%s [OPTIONS...] COMMAND ...\n\n"
2386 "%sProfile systemd, show unit dependencies, check unit files.%s\n"
20a51f6a 2387 "\nCommands:\n"
3cc3dc77
MG
2388 " [time] Print time required to boot the machine\n"
2389 " blame Print list of running units ordered by\n"
2390 " time to init\n"
2391 " critical-chain [UNIT...] Print a tree of the time critical chain\n"
2392 " of units\n"
2393 " plot Output SVG graphic showing service\n"
2394 " initialization\n"
2395 " dot [UNIT...] Output dependency graph in %s format\n"
2396 " dump Output state serialization of service\n"
2397 " manager\n"
2398 " cat-config Show configuration file and drop-ins\n"
2399 " unit-files List files and symlinks for units\n"
2400 " unit-paths List load directories for units\n"
2401 " exit-status [STATUS...] List exit status definitions\n"
2402 " capability [CAP...] List capability definitions\n"
2403 " syscall-filter [NAME...] Print list of syscalls in seccomp\n"
2404 " filter\n"
b41711cd 2405 " filesystems [NAME...] Print list of filesystems\n"
3cc3dc77
MG
2406 " condition CONDITION... Evaluate conditions and asserts\n"
2407 " verify FILE... Check unit files for correctness\n"
2408 " calendar SPEC... Validate repetitive calendar time\n"
2409 " events\n"
2410 " timestamp TIMESTAMP... Validate a timestamp\n"
2411 " timespan SPAN... Validate a time span\n"
2412 " security [UNIT...] Analyze security of unit\n"
353b2baa 2413 "\nOptions:\n"
3cc3dc77
MG
2414 " -h --help Show this help\n"
2415 " --recursive-errors=MODE Control which units are verified\n"
bb43d853 2416 " --offline=BOOL Perform a security review on unit file(s)\n"
dfbda879
MG
2417 " --threshold=N Exit with a non-zero status when overall\n"
2418 " exposure level is over threshold value\n"
3cc3dc77 2419 " --version Show package version\n"
ecfd082b
MG
2420 " --security-policy=PATH Use custom JSON security policy instead\n"
2421 " of built-in one\n"
4b4a8ef7
MG
2422 " --json=pretty|short|off Generate JSON output of the security\n"
2423 " analysis table\n"
3cc3dc77
MG
2424 " --no-pager Do not pipe output into a pager\n"
2425 " --system Operate on system systemd instance\n"
2426 " --user Operate on user systemd instance\n"
2427 " --global Operate on global user configuration\n"
2428 " -H --host=[USER@]HOST Operate on remote host\n"
2429 " -M --machine=CONTAINER Operate on local container\n"
2430 " --order Show only order in the graph\n"
2431 " --require Show only requirement in the graph\n"
2432 " --from-pattern=GLOB Show only origins in the graph\n"
2433 " --to-pattern=GLOB Show only destinations in the graph\n"
2434 " --fuzz=SECONDS Also print services which finished SECONDS\n"
2435 " earlier than the latest in the branch\n"
2436 " --man[=BOOL] Do [not] check for existence of man pages\n"
2437 " --generators[=BOOL] Do [not] run unit generators\n"
2438 " (requires privileges)\n"
2439 " --iterations=N Show the specified number of iterations\n"
2440 " --base-time=TIMESTAMP Calculate calendar times relative to\n"
2441 " specified time\n"
bc556335
DDM
2442 "\nSee the %s for details.\n",
2443 program_invocation_short_name,
2444 ansi_highlight(),
2445 ansi_normal(),
2446 dot_link,
2447 link);
96de7c04 2448
1ace223c
SJ
2449 /* When updating this list, including descriptions, apply changes to
2450 * shell-completion/bash/systemd-analyze and shell-completion/zsh/_systemd-analyze too. */
a6bcef29
LP
2451
2452 return 0;
2265fbf7
SP
2453}
2454
9ea9d4cf 2455static int parse_argv(int argc, char *argv[]) {
2265fbf7
SP
2456 enum {
2457 ARG_VERSION = 0x100,
1700761b
SP
2458 ARG_ORDER,
2459 ARG_REQUIRE,
46d8646a 2460 ARG_ROOT,
e5ea5c3a 2461 ARG_IMAGE,
e55933db 2462 ARG_SYSTEM,
28b35ef2
ZJS
2463 ARG_USER,
2464 ARG_GLOBAL,
e55933db 2465 ARG_DOT_FROM_PATTERN,
bb150966 2466 ARG_DOT_TO_PATTERN,
9ea9d4cf 2467 ARG_FUZZ,
1d3bc017 2468 ARG_NO_PAGER,
dad29dff 2469 ARG_MAN,
641c0fd1 2470 ARG_GENERATORS,
f2ccf832 2471 ARG_ITERATIONS,
985c1880 2472 ARG_BASE_TIME,
3cc3dc77 2473 ARG_RECURSIVE_ERRORS,
bb43d853 2474 ARG_OFFLINE,
dfbda879 2475 ARG_THRESHOLD,
ecfd082b 2476 ARG_SECURITY_POLICY,
4b4a8ef7 2477 ARG_JSON,
2265fbf7
SP
2478 };
2479
2480 static const struct option options[] = {
3cc3dc77
MG
2481 { "help", no_argument, NULL, 'h' },
2482 { "version", no_argument, NULL, ARG_VERSION },
2483 { "order", no_argument, NULL, ARG_ORDER },
2484 { "require", no_argument, NULL, ARG_REQUIRE },
2485 { "root", required_argument, NULL, ARG_ROOT },
2486 { "image", required_argument, NULL, ARG_IMAGE },
2487 { "recursive-errors", required_argument, NULL, ARG_RECURSIVE_ERRORS },
bb43d853 2488 { "offline", required_argument, NULL, ARG_OFFLINE },
dfbda879 2489 { "threshold", required_argument, NULL, ARG_THRESHOLD },
ecfd082b 2490 { "security-policy", required_argument, NULL, ARG_SECURITY_POLICY },
3cc3dc77
MG
2491 { "system", no_argument, NULL, ARG_SYSTEM },
2492 { "user", no_argument, NULL, ARG_USER },
2493 { "global", no_argument, NULL, ARG_GLOBAL },
2494 { "from-pattern", required_argument, NULL, ARG_DOT_FROM_PATTERN },
2495 { "to-pattern", required_argument, NULL, ARG_DOT_TO_PATTERN },
2496 { "fuzz", required_argument, NULL, ARG_FUZZ },
2497 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
2498 { "man", optional_argument, NULL, ARG_MAN },
2499 { "generators", optional_argument, NULL, ARG_GENERATORS },
2500 { "host", required_argument, NULL, 'H' },
2501 { "machine", required_argument, NULL, 'M' },
2502 { "iterations", required_argument, NULL, ARG_ITERATIONS },
2503 { "base-time", required_argument, NULL, ARG_BASE_TIME },
8de7929d 2504 { "unit", required_argument, NULL, 'U' },
4b4a8ef7 2505 { "json", required_argument, NULL, ARG_JSON },
eb9da376 2506 {}
2265fbf7
SP
2507 };
2508
eb9da376
LP
2509 int r, c;
2510
2265fbf7
SP
2511 assert(argc >= 0);
2512 assert(argv);
2513
8de7929d 2514 while ((c = getopt_long(argc, argv, "hH:M:U:", options, NULL)) >= 0)
eb9da376 2515 switch (c) {
c170f3a4
LP
2516
2517 case 'h':
a6bcef29 2518 return help(0, NULL, NULL);
c170f3a4 2519
3cc3dc77
MG
2520 case ARG_RECURSIVE_ERRORS:
2521 if (streq(optarg, "help")) {
2522 DUMP_STRING_TABLE(recursive_errors, RecursiveErrors, _RECURSIVE_ERRORS_MAX);
2523 return 0;
2524 }
2525 r = recursive_errors_from_string(optarg);
2526 if (r < 0)
2527 return log_error_errno(r, "Unknown mode passed to --recursive-errors='%s'.", optarg);
2528
2529 arg_recursive_errors = r;
2530 break;
2531
c170f3a4 2532 case ARG_VERSION:
3f6fd1ba 2533 return version();
c170f3a4 2534
46d8646a 2535 case ARG_ROOT:
782671bc
MG
2536 r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root);
2537 if (r < 0)
2538 return r;
46d8646a
ZJS
2539 break;
2540
e5ea5c3a
MG
2541 case ARG_IMAGE:
2542 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
2543 if (r < 0)
2544 return r;
2545 break;
2546
28b35ef2
ZJS
2547 case ARG_SYSTEM:
2548 arg_scope = UNIT_FILE_SYSTEM;
2549 break;
2550
c170f3a4 2551 case ARG_USER:
28b35ef2 2552 arg_scope = UNIT_FILE_USER;
c170f3a4
LP
2553 break;
2554
28b35ef2
ZJS
2555 case ARG_GLOBAL:
2556 arg_scope = UNIT_FILE_GLOBAL;
c170f3a4
LP
2557 break;
2558
2559 case ARG_ORDER:
2560 arg_dot = DEP_ORDER;
2561 break;
2562
2563 case ARG_REQUIRE:
2564 arg_dot = DEP_REQUIRE;
2565 break;
2566
e55933db 2567 case ARG_DOT_FROM_PATTERN:
903a0b07
LP
2568 if (strv_extend(&arg_dot_from_patterns, optarg) < 0)
2569 return log_oom();
2570
e55933db
ŁS
2571 break;
2572
2573 case ARG_DOT_TO_PATTERN:
903a0b07
LP
2574 if (strv_extend(&arg_dot_to_patterns, optarg) < 0)
2575 return log_oom();
2576
e55933db
ŁS
2577 break;
2578
bb150966
HH
2579 case ARG_FUZZ:
2580 r = parse_sec(optarg, &arg_fuzz);
2581 if (r < 0)
2582 return r;
2583 break;
2584
9ea9d4cf 2585 case ARG_NO_PAGER:
0221d68a 2586 arg_pager_flags |= PAGER_DISABLE;
9ea9d4cf
LP
2587 break;
2588
3cd26e7c
LP
2589 case 'H':
2590 arg_transport = BUS_TRANSPORT_REMOTE;
2591 arg_host = optarg;
2592 break;
2593
2594 case 'M':
de33fc62 2595 arg_transport = BUS_TRANSPORT_MACHINE;
3cd26e7c
LP
2596 arg_host = optarg;
2597 break;
2598
dad29dff 2599 case ARG_MAN:
599c7c54
ZJS
2600 r = parse_boolean_argument("--man", optarg, &arg_man);
2601 if (r < 0)
2602 return r;
1d3bc017
ZJS
2603 break;
2604
641c0fd1 2605 case ARG_GENERATORS:
599c7c54
ZJS
2606 r = parse_boolean_argument("--generators", optarg, &arg_generators);
2607 if (r < 0)
2608 return r;
641c0fd1
ZJS
2609 break;
2610
bb43d853
MG
2611 case ARG_OFFLINE:
2612 r = parse_boolean_argument("--offline", optarg, &arg_offline);
2613 if (r < 0)
2614 return r;
2615 break;
2616
dfbda879
MG
2617 case ARG_THRESHOLD:
2618 r = safe_atou(optarg, &arg_threshold);
2619 if (r < 0 || arg_threshold > 100)
2620 return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL), "Failed to parse threshold: %s", optarg);
2621
2622 break;
2623
ecfd082b
MG
2624 case ARG_SECURITY_POLICY:
2625 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_security_policy);
2626 if (r < 0)
2627 return r;
2628 break;
2629
4b4a8ef7
MG
2630 case ARG_JSON:
2631 r = parse_json_argument(optarg, &arg_json_format_flags);
2632 if (r <= 0)
2633 return r;
2634 break;
2635
f2ccf832
LP
2636 case ARG_ITERATIONS:
2637 r = safe_atou(optarg, &arg_iterations);
2638 if (r < 0)
2639 return log_error_errno(r, "Failed to parse iterations: %s", optarg);
2640
2641 break;
2642
985c1880
LP
2643 case ARG_BASE_TIME:
2644 r = parse_timestamp(optarg, &arg_base_time);
2645 if (r < 0)
2646 return log_error_errno(r, "Failed to parse --base-time= parameter: %s", optarg);
2647
2648 break;
2649
8de7929d
DDM
2650 case 'U': {
2651 _cleanup_free_ char *mangled = NULL;
2652
2653 r = unit_name_mangle(optarg, UNIT_NAME_MANGLE_WARN, &mangled);
2654 if (r < 0)
2655 return log_error_errno(r, "Failed to mangle unit name %s: %m", optarg);
2656
2657 free_and_replace(arg_unit, mangled);
2658 break;
2659 }
c170f3a4
LP
2660 case '?':
2661 return -EINVAL;
2662
2663 default:
04499a70 2664 assert_not_reached();
2265fbf7 2665 }
eb9da376 2666
bb43d853
MG
2667 if (arg_offline && !streq_ptr(argv[optind], "security"))
2668 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2669 "Option --offline= is only supported for security right now.");
2670
4b4a8ef7
MG
2671 if (arg_json_format_flags != JSON_FORMAT_OFF && !streq_ptr(argv[optind], "security"))
2672 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2673 "Option --json= is only supported for security right now.");
2674
dfbda879
MG
2675 if (arg_threshold != 100 && !streq_ptr(argv[optind], "security"))
2676 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2677 "Option --threshold= is only supported for security right now.");
2678
31a5924e 2679 if (arg_scope == UNIT_FILE_GLOBAL &&
baaa35ad
ZJS
2680 !STR_IN_SET(argv[optind] ?: "time", "dot", "unit-paths", "verify"))
2681 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2682 "Option --global only makes sense with verbs dot, unit-paths, verify.");
31a5924e 2683
f1d9d36a
ZJS
2684 if (streq_ptr(argv[optind], "cat-config") && arg_scope == UNIT_FILE_USER)
2685 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2686 "Option --user is not supported for cat-config right now.");
2687
ecfd082b
MG
2688 if (arg_security_policy && !streq_ptr(argv[optind], "security"))
2689 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2690 "Option --security-policy= is only supported for security.");
2691
8de7929d 2692 if ((arg_root || arg_image) && (!STRPTR_IN_SET(argv[optind], "cat-config", "verify", "condition")) &&
bb43d853 2693 (!(streq_ptr(argv[optind], "security") && arg_offline)))
baaa35ad 2694 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
8de7929d 2695 "Options --root= and --image= are only supported for cat-config, verify, condition and security when used with --offline= right now.");
e5ea5c3a
MG
2696
2697 /* Having both an image and a root is not supported by the code */
2698 if (arg_root && arg_image)
2699 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
46d8646a 2700
8de7929d
DDM
2701 if (arg_unit && !streq_ptr(argv[optind], "condition"))
2702 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --unit= is only supported for condition");
2703
2704 if (streq_ptr(argv[optind], "condition") && !arg_unit && optind >= argc - 1)
2705 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too few arguments for condition");
2706
2707 if (streq_ptr(argv[optind], "condition") && arg_unit && optind < argc - 1)
2708 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No conditions can be passed if --unit= is used.");
2709
1d3bc017 2710 return 1; /* work to do */
2265fbf7
SP
2711}
2712
d665c7b2 2713static int run(int argc, char *argv[]) {
e5ea5c3a
MG
2714 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
2715 _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
2716 _cleanup_(umount_and_rmdir_and_freep) char *unlink_dir = NULL;
a6bcef29
LP
2717
2718 static const Verb verbs[] = {
889d695d
JK
2719 { "help", VERB_ANY, VERB_ANY, 0, help },
2720 { "time", VERB_ANY, 1, VERB_DEFAULT, analyze_time },
2721 { "blame", VERB_ANY, 1, 0, analyze_blame },
2722 { "critical-chain", VERB_ANY, VERB_ANY, 0, analyze_critical_chain },
2723 { "plot", VERB_ANY, 1, 0, analyze_plot },
2724 { "dot", VERB_ANY, VERB_ANY, 0, dot },
26e1e973 2725 /* The following seven verbs are deprecated */
a87b151a
DDM
2726 { "log-level", VERB_ANY, 2, 0, verb_log_control },
2727 { "log-target", VERB_ANY, 2, 0, verb_log_control },
2728 { "set-log-level", 2, 2, 0, verb_log_control },
2729 { "get-log-level", VERB_ANY, 1, 0, verb_log_control },
2730 { "set-log-target", 2, 2, 0, verb_log_control },
2731 { "get-log-target", VERB_ANY, 1, 0, verb_log_control },
26e1e973 2732 { "service-watchdogs", VERB_ANY, 2, 0, service_watchdogs },
889d695d 2733 { "dump", VERB_ANY, 1, 0, dump },
854a42fb 2734 { "cat-config", 2, VERB_ANY, 0, cat_config },
e67cd21d 2735 { "unit-files", VERB_ANY, VERB_ANY, 0, do_unit_files },
31a5924e 2736 { "unit-paths", 1, 1, 0, dump_unit_paths },
5238d9a8 2737 { "exit-status", VERB_ANY, VERB_ANY, 0, dump_exit_status },
889d695d 2738 { "syscall-filter", VERB_ANY, VERB_ANY, 0, dump_syscall_filters },
b2af819b 2739 { "capability", VERB_ANY, VERB_ANY, 0, dump_capabilities },
b41711cd 2740 { "filesystems", VERB_ANY, VERB_ANY, 0, dump_filesystems },
8de7929d 2741 { "condition", VERB_ANY, VERB_ANY, 0, do_condition },
889d695d
JK
2742 { "verify", 2, VERB_ANY, 0, do_verify },
2743 { "calendar", 2, VERB_ANY, 0, test_calendar },
2cae4711 2744 { "timestamp", 2, VERB_ANY, 0, test_timestamp },
3f1c1287 2745 { "timespan", 2, VERB_ANY, 0, dump_timespan },
ec16f3b6 2746 { "security", VERB_ANY, VERB_ANY, 0, do_security },
a6bcef29
LP
2747 {}
2748 };
2749
5220a6f3 2750 int r;
2265fbf7
SP
2751
2752 setlocale(LC_ALL, "");
c170f3a4 2753 setlocale(LC_NUMERIC, "C"); /* we want to format/parse floats in C style */
a6bcef29 2754
d2acb93d 2755 log_setup();
2265fbf7
SP
2756
2757 r = parse_argv(argc, argv);
9ea9d4cf 2758 if (r <= 0)
d665c7b2 2759 return r;
c170f3a4 2760
e5ea5c3a
MG
2761 /* Open up and mount the image */
2762 if (arg_image) {
2763 assert(!arg_root);
2764
2765 r = mount_image_privately_interactively(
2766 arg_image,
2767 DISSECT_IMAGE_GENERIC_ROOT |
2768 DISSECT_IMAGE_RELAX_VAR_CHECK |
2769 DISSECT_IMAGE_READ_ONLY,
2770 &unlink_dir,
2771 &loop_device,
2772 &decrypted_image);
2773 if (r < 0)
2774 return r;
2775
2776 arg_root = strdup(unlink_dir);
2777 if (!arg_root)
2778 return log_oom();
2779 }
2780
d665c7b2 2781 return dispatch_verb(argc, argv, verbs, NULL);
2265fbf7 2782}
d665c7b2
YW
2783
2784DEFINE_MAIN_FUNCTION(run);