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