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