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