]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/analyze/analyze.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / analyze / analyze.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2013 Simon Peeters
4 ***/
5
6 #include <getopt.h>
7 #include <locale.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include "sd-bus.h"
12
13 #include "alloc-util.h"
14 #include "analyze-verify.h"
15 #include "bus-error.h"
16 #include "bus-unit-util.h"
17 #include "bus-util.h"
18 #include "calendarspec.h"
19 #include "def.h"
20 #include "conf-files.h"
21 #include "copy.h"
22 #include "fd-util.h"
23 #include "glob-util.h"
24 #include "hashmap.h"
25 #include "locale-util.h"
26 #include "log.h"
27 #include "pager.h"
28 #include "parse-util.h"
29 #include "path-util.h"
30 #if HAVE_SECCOMP
31 #include "seccomp-util.h"
32 #endif
33 #include "special.h"
34 #include "strv.h"
35 #include "strxcpyx.h"
36 #include "terminal-util.h"
37 #include "unit-name.h"
38 #include "util.h"
39 #include "verbs.h"
40
41 #define SCALE_X (0.1 / 1000.0) /* pixels per us */
42 #define SCALE_Y (20.0)
43
44 #define compare(a, b) (((a) > (b))? 1 : (((b) > (a))? -1 : 0))
45
46 #define svg(...) printf(__VA_ARGS__)
47
48 #define svg_bar(class, x1, x2, y) \
49 svg(" <rect class=\"%s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n", \
50 (class), \
51 SCALE_X * (x1), SCALE_Y * (y), \
52 SCALE_X * ((x2) - (x1)), SCALE_Y - 1.0)
53
54 #define svg_text(b, x, y, format, ...) \
55 do { \
56 svg(" <text class=\"%s\" x=\"%.03f\" y=\"%.03f\">", (b) ? "left" : "right", SCALE_X * (x) + (b ? 5.0 : -5.0), SCALE_Y * (y) + 14.0); \
57 svg(format, ## __VA_ARGS__); \
58 svg("</text>\n"); \
59 } while (false)
60
61 static enum dot {
62 DEP_ALL,
63 DEP_ORDER,
64 DEP_REQUIRE
65 } arg_dot = DEP_ALL;
66 static char** arg_dot_from_patterns = NULL;
67 static char** arg_dot_to_patterns = NULL;
68 static usec_t arg_fuzz = 0;
69 static bool arg_no_pager = false;
70 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
71 static const char *arg_host = NULL;
72 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
73 static bool arg_man = true;
74 static bool arg_generators = false;
75 static const char *arg_root = NULL;
76
77 struct boot_times {
78 usec_t firmware_time;
79 usec_t loader_time;
80 usec_t kernel_time;
81 usec_t kernel_done_time;
82 usec_t initrd_time;
83 usec_t userspace_time;
84 usec_t finish_time;
85 usec_t security_start_time;
86 usec_t security_finish_time;
87 usec_t generators_start_time;
88 usec_t generators_finish_time;
89 usec_t unitsload_start_time;
90 usec_t unitsload_finish_time;
91
92 /*
93 * If we're analyzing the user instance, all timestamps will be offset
94 * by its own start-up timestamp, which may be arbitrarily big.
95 * With "plot", this causes arbitrarily wide output SVG files which almost
96 * completely consist of empty space. Thus we cancel out this offset.
97 *
98 * This offset is subtracted from times above by acquire_boot_times(),
99 * but it still needs to be subtracted from unit-specific timestamps
100 * (so it is stored here for reference).
101 */
102 usec_t reverse_offset;
103 };
104
105 struct unit_times {
106 bool has_data;
107 char *name;
108 usec_t activating;
109 usec_t activated;
110 usec_t deactivated;
111 usec_t deactivating;
112 usec_t time;
113 };
114
115 struct host_info {
116 char *hostname;
117 char *kernel_name;
118 char *kernel_release;
119 char *kernel_version;
120 char *os_pretty_name;
121 char *virtualization;
122 char *architecture;
123 };
124
125 static int acquire_bus(sd_bus **bus, bool *use_full_bus) {
126 bool user = arg_scope != UNIT_FILE_SYSTEM;
127 int r;
128
129 if (use_full_bus && *use_full_bus) {
130 r = bus_connect_transport(arg_transport, arg_host, user, bus);
131 if (IN_SET(r, 0, -EHOSTDOWN))
132 return r;
133
134 *use_full_bus = false;
135 }
136
137 return bus_connect_transport_systemd(arg_transport, arg_host, user, bus);
138 }
139
140 static int bus_get_uint64_property(sd_bus *bus, const char *path, const char *interface, const char *property, uint64_t *val) {
141 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
142 int r;
143
144 assert(bus);
145 assert(path);
146 assert(interface);
147 assert(property);
148 assert(val);
149
150 r = sd_bus_get_property_trivial(
151 bus,
152 "org.freedesktop.systemd1",
153 path,
154 interface,
155 property,
156 &error,
157 't', val);
158
159 if (r < 0) {
160 log_error("Failed to parse reply: %s", bus_error_message(&error, -r));
161 return r;
162 }
163
164 return 0;
165 }
166
167 static int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv) {
168 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
169 int r;
170
171 assert(bus);
172 assert(path);
173 assert(property);
174 assert(strv);
175
176 r = sd_bus_get_property_strv(
177 bus,
178 "org.freedesktop.systemd1",
179 path,
180 "org.freedesktop.systemd1.Unit",
181 property,
182 &error,
183 strv);
184 if (r < 0) {
185 log_error("Failed to get unit property %s: %s", property, bus_error_message(&error, -r));
186 return r;
187 }
188
189 return 0;
190 }
191
192 static int compare_unit_time(const void *a, const void *b) {
193 return compare(((struct unit_times *)b)->time,
194 ((struct unit_times *)a)->time);
195 }
196
197 static int compare_unit_start(const void *a, const void *b) {
198 return compare(((struct unit_times *)a)->activating,
199 ((struct unit_times *)b)->activating);
200 }
201
202 static void unit_times_free(struct unit_times *t) {
203 struct unit_times *p;
204
205 for (p = t; p->has_data; p++)
206 free(p->name);
207 free(t);
208 }
209
210 DEFINE_TRIVIAL_CLEANUP_FUNC(struct unit_times *, unit_times_free);
211
212 static void subtract_timestamp(usec_t *a, usec_t b) {
213 assert(a);
214
215 if (*a > 0) {
216 assert(*a >= b);
217 *a -= b;
218 }
219 }
220
221 static int acquire_boot_times(sd_bus *bus, struct boot_times **bt) {
222 static struct boot_times times;
223 static bool cached = false;
224
225 if (cached)
226 goto finish;
227
228 assert_cc(sizeof(usec_t) == sizeof(uint64_t));
229
230 if (bus_get_uint64_property(bus,
231 "/org/freedesktop/systemd1",
232 "org.freedesktop.systemd1.Manager",
233 "FirmwareTimestampMonotonic",
234 &times.firmware_time) < 0 ||
235 bus_get_uint64_property(bus,
236 "/org/freedesktop/systemd1",
237 "org.freedesktop.systemd1.Manager",
238 "LoaderTimestampMonotonic",
239 &times.loader_time) < 0 ||
240 bus_get_uint64_property(bus,
241 "/org/freedesktop/systemd1",
242 "org.freedesktop.systemd1.Manager",
243 "KernelTimestamp",
244 &times.kernel_time) < 0 ||
245 bus_get_uint64_property(bus,
246 "/org/freedesktop/systemd1",
247 "org.freedesktop.systemd1.Manager",
248 "InitRDTimestampMonotonic",
249 &times.initrd_time) < 0 ||
250 bus_get_uint64_property(bus,
251 "/org/freedesktop/systemd1",
252 "org.freedesktop.systemd1.Manager",
253 "UserspaceTimestampMonotonic",
254 &times.userspace_time) < 0 ||
255 bus_get_uint64_property(bus,
256 "/org/freedesktop/systemd1",
257 "org.freedesktop.systemd1.Manager",
258 "FinishTimestampMonotonic",
259 &times.finish_time) < 0 ||
260 bus_get_uint64_property(bus,
261 "/org/freedesktop/systemd1",
262 "org.freedesktop.systemd1.Manager",
263 "SecurityStartTimestampMonotonic",
264 &times.security_start_time) < 0 ||
265 bus_get_uint64_property(bus,
266 "/org/freedesktop/systemd1",
267 "org.freedesktop.systemd1.Manager",
268 "SecurityFinishTimestampMonotonic",
269 &times.security_finish_time) < 0 ||
270 bus_get_uint64_property(bus,
271 "/org/freedesktop/systemd1",
272 "org.freedesktop.systemd1.Manager",
273 "GeneratorsStartTimestampMonotonic",
274 &times.generators_start_time) < 0 ||
275 bus_get_uint64_property(bus,
276 "/org/freedesktop/systemd1",
277 "org.freedesktop.systemd1.Manager",
278 "GeneratorsFinishTimestampMonotonic",
279 &times.generators_finish_time) < 0 ||
280 bus_get_uint64_property(bus,
281 "/org/freedesktop/systemd1",
282 "org.freedesktop.systemd1.Manager",
283 "UnitsLoadStartTimestampMonotonic",
284 &times.unitsload_start_time) < 0 ||
285 bus_get_uint64_property(bus,
286 "/org/freedesktop/systemd1",
287 "org.freedesktop.systemd1.Manager",
288 "UnitsLoadFinishTimestampMonotonic",
289 &times.unitsload_finish_time) < 0)
290 return -EIO;
291
292 if (times.finish_time <= 0) {
293 log_error("Bootup is not yet finished (org.freedesktop.systemd1.Manager.FinishTimestampMonotonic=%"PRIu64").\n"
294 "Please try again later.\n"
295 "Hint: Use 'systemctl%s list-jobs' to see active jobs",
296 times.finish_time,
297 arg_scope == UNIT_FILE_SYSTEM ? "" : " --user");
298 return -EINPROGRESS;
299 }
300
301 if (arg_scope == UNIT_FILE_SYSTEM) {
302 if (times.initrd_time > 0)
303 times.kernel_done_time = times.initrd_time;
304 else
305 times.kernel_done_time = times.userspace_time;
306 } else {
307 /*
308 * User-instance-specific timestamps processing
309 * (see comment to reverse_offset in struct boot_times).
310 */
311 times.reverse_offset = times.userspace_time;
312
313 times.firmware_time = times.loader_time = times.kernel_time = times.initrd_time = times.userspace_time = 0;
314 subtract_timestamp(&times.finish_time, times.reverse_offset);
315
316 subtract_timestamp(&times.security_start_time, times.reverse_offset);
317 subtract_timestamp(&times.security_finish_time, times.reverse_offset);
318
319 subtract_timestamp(&times.generators_start_time, times.reverse_offset);
320 subtract_timestamp(&times.generators_finish_time, times.reverse_offset);
321
322 subtract_timestamp(&times.unitsload_start_time, times.reverse_offset);
323 subtract_timestamp(&times.unitsload_finish_time, times.reverse_offset);
324 }
325
326 cached = true;
327
328 finish:
329 *bt = &times;
330 return 0;
331 }
332
333 static void free_host_info(struct host_info *hi) {
334
335 if (!hi)
336 return;
337
338 free(hi->hostname);
339 free(hi->kernel_name);
340 free(hi->kernel_release);
341 free(hi->kernel_version);
342 free(hi->os_pretty_name);
343 free(hi->virtualization);
344 free(hi->architecture);
345 free(hi);
346 }
347
348 DEFINE_TRIVIAL_CLEANUP_FUNC(struct host_info*, free_host_info);
349
350 static int acquire_time_data(sd_bus *bus, struct unit_times **out) {
351 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
352 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
353 int r, c = 0;
354 struct boot_times *boot_times = NULL;
355 _cleanup_(unit_times_freep) struct unit_times *unit_times = NULL;
356 size_t size = 0;
357 UnitInfo u;
358
359 r = acquire_boot_times(bus, &boot_times);
360 if (r < 0)
361 return r;
362
363 r = sd_bus_call_method(
364 bus,
365 "org.freedesktop.systemd1",
366 "/org/freedesktop/systemd1",
367 "org.freedesktop.systemd1.Manager",
368 "ListUnits",
369 &error, &reply,
370 NULL);
371 if (r < 0) {
372 log_error("Failed to list units: %s", bus_error_message(&error, -r));
373 return r;
374 }
375
376 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
377 if (r < 0)
378 return bus_log_parse_error(r);
379
380 while ((r = bus_parse_unit_info(reply, &u)) > 0) {
381 struct unit_times *t;
382
383 if (!GREEDY_REALLOC(unit_times, size, c+2))
384 return log_oom();
385
386 unit_times[c+1].has_data = false;
387 t = &unit_times[c];
388 t->name = NULL;
389
390 assert_cc(sizeof(usec_t) == sizeof(uint64_t));
391
392 if (bus_get_uint64_property(bus, u.unit_path,
393 "org.freedesktop.systemd1.Unit",
394 "InactiveExitTimestampMonotonic",
395 &t->activating) < 0 ||
396 bus_get_uint64_property(bus, u.unit_path,
397 "org.freedesktop.systemd1.Unit",
398 "ActiveEnterTimestampMonotonic",
399 &t->activated) < 0 ||
400 bus_get_uint64_property(bus, u.unit_path,
401 "org.freedesktop.systemd1.Unit",
402 "ActiveExitTimestampMonotonic",
403 &t->deactivating) < 0 ||
404 bus_get_uint64_property(bus, u.unit_path,
405 "org.freedesktop.systemd1.Unit",
406 "InactiveEnterTimestampMonotonic",
407 &t->deactivated) < 0)
408 return -EIO;
409
410 subtract_timestamp(&t->activating, boot_times->reverse_offset);
411 subtract_timestamp(&t->activated, boot_times->reverse_offset);
412 subtract_timestamp(&t->deactivating, boot_times->reverse_offset);
413 subtract_timestamp(&t->deactivated, boot_times->reverse_offset);
414
415 if (t->activated >= t->activating)
416 t->time = t->activated - t->activating;
417 else if (t->deactivated >= t->activating)
418 t->time = t->deactivated - t->activating;
419 else
420 t->time = 0;
421
422 if (t->activating == 0)
423 continue;
424
425 t->name = strdup(u.id);
426 if (!t->name)
427 return log_oom();
428
429 t->has_data = true;
430 c++;
431 }
432 if (r < 0)
433 return bus_log_parse_error(r);
434
435 *out = TAKE_PTR(unit_times);
436 return c;
437 }
438
439 static int acquire_host_info(sd_bus *bus, struct host_info **hi) {
440 static const struct bus_properties_map hostname_map[] = {
441 { "Hostname", "s", NULL, offsetof(struct host_info, hostname) },
442 { "KernelName", "s", NULL, offsetof(struct host_info, kernel_name) },
443 { "KernelRelease", "s", NULL, offsetof(struct host_info, kernel_release) },
444 { "KernelVersion", "s", NULL, offsetof(struct host_info, kernel_version) },
445 { "OperatingSystemPrettyName", "s", NULL, offsetof(struct host_info, os_pretty_name) },
446 {}
447 };
448
449 static const struct bus_properties_map manager_map[] = {
450 { "Virtualization", "s", NULL, offsetof(struct host_info, virtualization) },
451 { "Architecture", "s", NULL, offsetof(struct host_info, architecture) },
452 {}
453 };
454
455 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
456 _cleanup_(free_host_infop) struct host_info *host;
457 int r;
458
459 host = new0(struct host_info, 1);
460 if (!host)
461 return log_oom();
462
463 r = bus_map_all_properties(bus,
464 "org.freedesktop.hostname1",
465 "/org/freedesktop/hostname1",
466 hostname_map,
467 BUS_MAP_STRDUP,
468 &error,
469 NULL,
470 host);
471 if (r < 0)
472 log_debug_errno(r, "Failed to get host information from systemd-hostnamed: %s", bus_error_message(&error, r));
473
474 r = bus_map_all_properties(bus,
475 "org.freedesktop.systemd1",
476 "/org/freedesktop/systemd1",
477 manager_map,
478 BUS_MAP_STRDUP,
479 &error,
480 NULL,
481 host);
482 if (r < 0)
483 return log_error_errno(r, "Failed to get host information from systemd: %s", bus_error_message(&error, r));
484
485 *hi = TAKE_PTR(host);
486
487 return 0;
488 }
489
490 static int pretty_boot_time(sd_bus *bus, char **_buf) {
491 char ts[FORMAT_TIMESPAN_MAX];
492 struct boot_times *t;
493 static char buf[4096];
494 size_t size;
495 char *ptr;
496 int r;
497 usec_t activated_time = USEC_INFINITY;
498 _cleanup_free_ char* path = NULL, *unit_id = NULL;
499 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
500
501 r = acquire_boot_times(bus, &t);
502 if (r < 0)
503 return r;
504
505 path = unit_dbus_path_from_name(SPECIAL_DEFAULT_TARGET);
506 if (!path)
507 return log_oom();
508
509 r = sd_bus_get_property_string(
510 bus,
511 "org.freedesktop.systemd1",
512 path,
513 "org.freedesktop.systemd1.Unit",
514 "Id",
515 &error,
516 &unit_id);
517 if (r < 0) {
518 log_error_errno(r, "default.target doesn't seem to exist: %s", bus_error_message(&error, r));
519 unit_id = NULL;
520 }
521
522 r = bus_get_uint64_property(bus, path,
523 "org.freedesktop.systemd1.Unit",
524 "ActiveEnterTimestampMonotonic",
525 &activated_time);
526 if (r < 0) {
527 log_info_errno(r, "Could not get time to reach default.target. Continuing...");
528 activated_time = USEC_INFINITY;
529 }
530
531 ptr = buf;
532 size = sizeof(buf);
533
534 size = strpcpyf(&ptr, size, "Startup finished in ");
535 if (t->firmware_time > 0)
536 size = strpcpyf(&ptr, size, "%s (firmware) + ", format_timespan(ts, sizeof(ts), t->firmware_time - t->loader_time, USEC_PER_MSEC));
537 if (t->loader_time > 0)
538 size = strpcpyf(&ptr, size, "%s (loader) + ", format_timespan(ts, sizeof(ts), t->loader_time, USEC_PER_MSEC));
539 if (t->kernel_time > 0)
540 size = strpcpyf(&ptr, size, "%s (kernel) + ", format_timespan(ts, sizeof(ts), t->kernel_done_time, USEC_PER_MSEC));
541 if (t->initrd_time > 0)
542 size = strpcpyf(&ptr, size, "%s (initrd) + ", format_timespan(ts, sizeof(ts), t->userspace_time - t->initrd_time, USEC_PER_MSEC));
543
544 size = strpcpyf(&ptr, size, "%s (userspace) ", format_timespan(ts, sizeof(ts), t->finish_time - t->userspace_time, USEC_PER_MSEC));
545 if (t->kernel_time > 0)
546 strpcpyf(&ptr, size, "= %s", format_timespan(ts, sizeof(ts), t->firmware_time + t->finish_time, USEC_PER_MSEC));
547
548 if (unit_id && activated_time > 0 && activated_time != USEC_INFINITY)
549 size = strpcpyf(&ptr, size, "\n%s reached after %s in userspace", unit_id, format_timespan(ts, sizeof(ts), activated_time - t->userspace_time, USEC_PER_MSEC));
550 else if (unit_id && activated_time == 0)
551 size = strpcpyf(&ptr, size, "\n%s was never reached", unit_id);
552 else if (unit_id && activated_time == USEC_INFINITY)
553 size = strpcpyf(&ptr, size, "\nCould not get time to reach %s.",unit_id);
554 else if (!unit_id)
555 size = strpcpyf(&ptr, size, "\ncould not find default.target");
556
557 ptr = strdup(buf);
558 if (!ptr)
559 return log_oom();
560
561 *_buf = ptr;
562 return 0;
563 }
564
565 static void svg_graph_box(double height, double begin, double end) {
566 long long i;
567
568 /* outside box, fill */
569 svg("<rect class=\"box\" x=\"0\" y=\"0\" width=\"%.03f\" height=\"%.03f\" />\n",
570 SCALE_X * (end - begin), SCALE_Y * height);
571
572 for (i = ((long long) (begin / 100000)) * 100000; i <= end; i+=100000) {
573 /* lines for each second */
574 if (i % 5000000 == 0)
575 svg(" <line class=\"sec5\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
576 " <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
577 SCALE_X * i, SCALE_X * i, SCALE_Y * height, SCALE_X * i, -5.0, 0.000001 * i);
578 else if (i % 1000000 == 0)
579 svg(" <line class=\"sec1\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
580 " <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
581 SCALE_X * i, SCALE_X * i, SCALE_Y * height, SCALE_X * i, -5.0, 0.000001 * i);
582 else
583 svg(" <line class=\"sec01\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
584 SCALE_X * i, SCALE_X * i, SCALE_Y * height);
585 }
586 }
587
588 static int analyze_plot(int argc, char *argv[], void *userdata) {
589 _cleanup_(free_host_infop) struct host_info *host = NULL;
590 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
591 _cleanup_(unit_times_freep) struct unit_times *times = NULL;
592 struct boot_times *boot;
593 int n, m = 1, y = 0, r;
594 bool use_full_bus = true;
595 double width;
596 _cleanup_free_ char *pretty_times = NULL;
597 struct unit_times *u;
598
599 r = acquire_bus(&bus, &use_full_bus);
600 if (r < 0)
601 return log_error_errno(r, "Failed to create bus connection: %m");
602
603 n = acquire_boot_times(bus, &boot);
604 if (n < 0)
605 return n;
606
607 n = pretty_boot_time(bus, &pretty_times);
608 if (n < 0)
609 return n;
610
611 if (use_full_bus) {
612 n = acquire_host_info(bus, &host);
613 if (n < 0)
614 return n;
615 }
616
617 n = acquire_time_data(bus, &times);
618 if (n <= 0)
619 return n;
620
621 qsort(times, n, sizeof(struct unit_times), compare_unit_start);
622
623 width = SCALE_X * (boot->firmware_time + boot->finish_time);
624 if (width < 800.0)
625 width = 800.0;
626
627 if (boot->firmware_time > boot->loader_time)
628 m++;
629 if (boot->loader_time > 0) {
630 m++;
631 if (width < 1000.0)
632 width = 1000.0;
633 }
634 if (boot->initrd_time > 0)
635 m++;
636 if (boot->kernel_time > 0)
637 m++;
638
639 for (u = times; u->has_data; u++) {
640 double text_start, text_width;
641
642 if (u->activating < boot->userspace_time ||
643 u->activating > boot->finish_time) {
644 u->name = mfree(u->name);
645 continue;
646 }
647
648 /* If the text cannot fit on the left side then
649 * increase the svg width so it fits on the right.
650 * TODO: calculate the text width more accurately */
651 text_width = 8.0 * strlen(u->name);
652 text_start = (boot->firmware_time + u->activating) * SCALE_X;
653 if (text_width > text_start && text_width + text_start > width)
654 width = text_width + text_start;
655
656 if (u->deactivated > u->activating && u->deactivated <= boot->finish_time
657 && u->activated == 0 && u->deactivating == 0)
658 u->activated = u->deactivating = u->deactivated;
659 if (u->activated < u->activating || u->activated > boot->finish_time)
660 u->activated = boot->finish_time;
661 if (u->deactivating < u->activated || u->activated > boot->finish_time)
662 u->deactivating = boot->finish_time;
663 if (u->deactivated < u->deactivating || u->deactivated > boot->finish_time)
664 u->deactivated = boot->finish_time;
665 m++;
666 }
667
668 svg("<?xml version=\"1.0\" standalone=\"no\"?>\n"
669 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
670 "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
671
672 svg("<svg width=\"%.0fpx\" height=\"%.0fpx\" version=\"1.1\" "
673 "xmlns=\"http://www.w3.org/2000/svg\">\n\n",
674 80.0 + width, 150.0 + (m * SCALE_Y) +
675 5 * SCALE_Y /* legend */);
676
677 /* write some basic info as a comment, including some help */
678 svg("<!-- This file is a systemd-analyze SVG file. It is best rendered in a -->\n"
679 "<!-- browser such as Chrome, Chromium or Firefox. Other applications -->\n"
680 "<!-- that render these files properly but much slower are ImageMagick, -->\n"
681 "<!-- gimp, inkscape, etc. To display the files on your system, just -->\n"
682 "<!-- point your browser to this file. -->\n\n"
683 "<!-- This plot was generated by systemd-analyze version %-16.16s -->\n\n", PACKAGE_VERSION);
684
685 /* style sheet */
686 svg("<defs>\n <style type=\"text/css\">\n <![CDATA[\n"
687 " rect { stroke-width: 1; stroke-opacity: 0; }\n"
688 " rect.background { fill: rgb(255,255,255); }\n"
689 " rect.activating { fill: rgb(255,0,0); fill-opacity: 0.7; }\n"
690 " rect.active { fill: rgb(200,150,150); fill-opacity: 0.7; }\n"
691 " rect.deactivating { fill: rgb(150,100,100); fill-opacity: 0.7; }\n"
692 " rect.kernel { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
693 " rect.initrd { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
694 " rect.firmware { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
695 " rect.loader { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
696 " rect.userspace { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
697 " rect.security { fill: rgb(144,238,144); fill-opacity: 0.7; }\n"
698 " rect.generators { fill: rgb(102,204,255); fill-opacity: 0.7; }\n"
699 " rect.unitsload { fill: rgb( 82,184,255); fill-opacity: 0.7; }\n"
700 " rect.box { fill: rgb(240,240,240); stroke: rgb(192,192,192); }\n"
701 " line { stroke: rgb(64,64,64); stroke-width: 1; }\n"
702 "// line.sec1 { }\n"
703 " line.sec5 { stroke-width: 2; }\n"
704 " line.sec01 { stroke: rgb(224,224,224); stroke-width: 1; }\n"
705 " text { font-family: Verdana, Helvetica; font-size: 14px; }\n"
706 " text.left { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: start; }\n"
707 " text.right { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: end; }\n"
708 " text.sec { font-size: 10px; }\n"
709 " ]]>\n </style>\n</defs>\n\n");
710
711 svg("<rect class=\"background\" width=\"100%%\" height=\"100%%\" />\n");
712 svg("<text x=\"20\" y=\"50\">%s</text>", pretty_times);
713 if (use_full_bus)
714 svg("<text x=\"20\" y=\"30\">%s %s (%s %s %s) %s %s</text>",
715 isempty(host->os_pretty_name) ? "Linux" : host->os_pretty_name,
716 strempty(host->hostname),
717 strempty(host->kernel_name),
718 strempty(host->kernel_release),
719 strempty(host->kernel_version),
720 strempty(host->architecture),
721 strempty(host->virtualization));
722
723 svg("<g transform=\"translate(%.3f,100)\">\n", 20.0 + (SCALE_X * boot->firmware_time));
724 svg_graph_box(m, -(double) boot->firmware_time, boot->finish_time);
725
726 if (boot->firmware_time > 0) {
727 svg_bar("firmware", -(double) boot->firmware_time, -(double) boot->loader_time, y);
728 svg_text(true, -(double) boot->firmware_time, y, "firmware");
729 y++;
730 }
731 if (boot->loader_time > 0) {
732 svg_bar("loader", -(double) boot->loader_time, 0, y);
733 svg_text(true, -(double) boot->loader_time, y, "loader");
734 y++;
735 }
736 if (boot->kernel_time > 0) {
737 svg_bar("kernel", 0, boot->kernel_done_time, y);
738 svg_text(true, 0, y, "kernel");
739 y++;
740 }
741 if (boot->initrd_time > 0) {
742 svg_bar("initrd", boot->initrd_time, boot->userspace_time, y);
743 svg_text(true, boot->initrd_time, y, "initrd");
744 y++;
745 }
746 svg_bar("active", boot->userspace_time, boot->finish_time, y);
747 svg_bar("security", boot->security_start_time, boot->security_finish_time, y);
748 svg_bar("generators", boot->generators_start_time, boot->generators_finish_time, y);
749 svg_bar("unitsload", boot->unitsload_start_time, boot->unitsload_finish_time, y);
750 svg_text(true, boot->userspace_time, y, "systemd");
751 y++;
752
753 for (u = times; u->has_data; u++) {
754 char ts[FORMAT_TIMESPAN_MAX];
755 bool b;
756
757 if (!u->name)
758 continue;
759
760 svg_bar("activating", u->activating, u->activated, y);
761 svg_bar("active", u->activated, u->deactivating, y);
762 svg_bar("deactivating", u->deactivating, u->deactivated, y);
763
764 /* place the text on the left if we have passed the half of the svg width */
765 b = u->activating * SCALE_X < width / 2;
766 if (u->time)
767 svg_text(b, u->activating, y, "%s (%s)",
768 u->name, format_timespan(ts, sizeof(ts), u->time, USEC_PER_MSEC));
769 else
770 svg_text(b, u->activating, y, "%s", u->name);
771 y++;
772 }
773
774 svg("</g>\n");
775
776 /* Legend */
777 svg("<g transform=\"translate(20,100)\">\n");
778 y++;
779 svg_bar("activating", 0, 300000, y);
780 svg_text(true, 400000, y, "Activating");
781 y++;
782 svg_bar("active", 0, 300000, y);
783 svg_text(true, 400000, y, "Active");
784 y++;
785 svg_bar("deactivating", 0, 300000, y);
786 svg_text(true, 400000, y, "Deactivating");
787 y++;
788 svg_bar("security", 0, 300000, y);
789 svg_text(true, 400000, y, "Setting up security module");
790 y++;
791 svg_bar("generators", 0, 300000, y);
792 svg_text(true, 400000, y, "Generators");
793 y++;
794 svg_bar("unitsload", 0, 300000, y);
795 svg_text(true, 400000, y, "Loading unit files");
796 y++;
797
798 svg("</g>\n\n");
799
800 svg("</svg>\n");
801
802 return 0;
803 }
804
805 static int list_dependencies_print(const char *name, unsigned int level, unsigned int branches,
806 bool last, struct unit_times *times, struct boot_times *boot) {
807 unsigned int i;
808 char ts[FORMAT_TIMESPAN_MAX], ts2[FORMAT_TIMESPAN_MAX];
809
810 for (i = level; i != 0; i--)
811 printf("%s", special_glyph(branches & (1 << (i-1)) ? TREE_VERTICAL : TREE_SPACE));
812
813 printf("%s", special_glyph(last ? TREE_RIGHT : TREE_BRANCH));
814
815 if (times) {
816 if (times->time > 0)
817 printf("%s%s @%s +%s%s", ansi_highlight_red(), name,
818 format_timespan(ts, sizeof(ts), times->activating - boot->userspace_time, USEC_PER_MSEC),
819 format_timespan(ts2, sizeof(ts2), times->time, USEC_PER_MSEC), ansi_normal());
820 else if (times->activated > boot->userspace_time)
821 printf("%s @%s", name, format_timespan(ts, sizeof(ts), times->activated - boot->userspace_time, USEC_PER_MSEC));
822 else
823 printf("%s", name);
824 } else
825 printf("%s", name);
826 printf("\n");
827
828 return 0;
829 }
830
831 static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) {
832 _cleanup_free_ char *path = NULL;
833
834 assert(bus);
835 assert(name);
836 assert(deps);
837
838 path = unit_dbus_path_from_name(name);
839 if (!path)
840 return -ENOMEM;
841
842 return bus_get_unit_property_strv(bus, path, "After", deps);
843 }
844
845 static Hashmap *unit_times_hashmap;
846
847 static int list_dependencies_compare(const void *_a, const void *_b) {
848 const char **a = (const char**) _a, **b = (const char**) _b;
849 usec_t usa = 0, usb = 0;
850 struct unit_times *times;
851
852 times = hashmap_get(unit_times_hashmap, *a);
853 if (times)
854 usa = times->activated;
855 times = hashmap_get(unit_times_hashmap, *b);
856 if (times)
857 usb = times->activated;
858
859 return usb - usa;
860 }
861
862 static bool times_in_range(const struct unit_times *times, const struct boot_times *boot) {
863 return times &&
864 times->activated > 0 && times->activated <= boot->finish_time;
865 }
866
867 static int list_dependencies_one(sd_bus *bus, const char *name, unsigned int level, char ***units,
868 unsigned int branches) {
869 _cleanup_strv_free_ char **deps = NULL;
870 char **c;
871 int r = 0;
872 usec_t service_longest = 0;
873 int to_print = 0;
874 struct unit_times *times;
875 struct boot_times *boot;
876
877 if (strv_extend(units, name))
878 return log_oom();
879
880 r = list_dependencies_get_dependencies(bus, name, &deps);
881 if (r < 0)
882 return r;
883
884 qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
885
886 r = acquire_boot_times(bus, &boot);
887 if (r < 0)
888 return r;
889
890 STRV_FOREACH(c, deps) {
891 times = hashmap_get(unit_times_hashmap, *c);
892 if (times_in_range(times, boot) &&
893 (times->activated >= service_longest
894 || service_longest == 0)) {
895 service_longest = times->activated;
896 break;
897 }
898 }
899
900 if (service_longest == 0)
901 return r;
902
903 STRV_FOREACH(c, deps) {
904 times = hashmap_get(unit_times_hashmap, *c);
905 if (times_in_range(times, boot) &&
906 service_longest - times->activated <= arg_fuzz)
907 to_print++;
908 }
909
910 if (!to_print)
911 return r;
912
913 STRV_FOREACH(c, deps) {
914 times = hashmap_get(unit_times_hashmap, *c);
915 if (!times_in_range(times, boot) ||
916 service_longest - times->activated > arg_fuzz)
917 continue;
918
919 to_print--;
920
921 r = list_dependencies_print(*c, level, branches, to_print == 0, times, boot);
922 if (r < 0)
923 return r;
924
925 if (strv_contains(*units, *c)) {
926 r = list_dependencies_print("...", level + 1, (branches << 1) | (to_print ? 1 : 0),
927 true, NULL, boot);
928 if (r < 0)
929 return r;
930 continue;
931 }
932
933 r = list_dependencies_one(bus, *c, level + 1, units,
934 (branches << 1) | (to_print ? 1 : 0));
935 if (r < 0)
936 return r;
937
938 if (to_print == 0)
939 break;
940 }
941 return 0;
942 }
943
944 static int list_dependencies(sd_bus *bus, const char *name) {
945 _cleanup_strv_free_ char **units = NULL;
946 char ts[FORMAT_TIMESPAN_MAX];
947 struct unit_times *times;
948 int r;
949 const char *id;
950 _cleanup_free_ char *path = NULL;
951 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
952 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
953 struct boot_times *boot;
954
955 assert(bus);
956
957 path = unit_dbus_path_from_name(name);
958 if (!path)
959 return -ENOMEM;
960
961 r = sd_bus_get_property(
962 bus,
963 "org.freedesktop.systemd1",
964 path,
965 "org.freedesktop.systemd1.Unit",
966 "Id",
967 &error,
968 &reply,
969 "s");
970 if (r < 0) {
971 log_error("Failed to get ID: %s", bus_error_message(&error, -r));
972 return r;
973 }
974
975 r = sd_bus_message_read(reply, "s", &id);
976 if (r < 0)
977 return bus_log_parse_error(r);
978
979 times = hashmap_get(unit_times_hashmap, id);
980
981 r = acquire_boot_times(bus, &boot);
982 if (r < 0)
983 return r;
984
985 if (times) {
986 if (times->time)
987 printf("%s%s +%s%s\n", ansi_highlight_red(), id,
988 format_timespan(ts, sizeof(ts), times->time, USEC_PER_MSEC), ansi_normal());
989 else if (times->activated > boot->userspace_time)
990 printf("%s @%s\n", id, format_timespan(ts, sizeof(ts), times->activated - boot->userspace_time, USEC_PER_MSEC));
991 else
992 printf("%s\n", id);
993 }
994
995 return list_dependencies_one(bus, name, 0, &units, 0);
996 }
997
998 static int analyze_critical_chain(int argc, char *argv[], void *userdata) {
999 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1000 _cleanup_(unit_times_freep) struct unit_times *times = NULL;
1001 struct unit_times *u;
1002 Hashmap *h;
1003 int n, r;
1004
1005 r = acquire_bus(&bus, NULL);
1006 if (r < 0)
1007 return log_error_errno(r, "Failed to create bus connection: %m");
1008
1009 n = acquire_time_data(bus, &times);
1010 if (n <= 0)
1011 return n;
1012
1013 h = hashmap_new(&string_hash_ops);
1014 if (!h)
1015 return log_oom();
1016
1017 for (u = times; u->has_data; u++) {
1018 r = hashmap_put(h, u->name, u);
1019 if (r < 0)
1020 return log_error_errno(r, "Failed to add entry to hashmap: %m");
1021 }
1022 unit_times_hashmap = h;
1023
1024 (void) pager_open(arg_no_pager, false);
1025
1026 puts("The time after the unit is active or started is printed after the \"@\" character.\n"
1027 "The time the unit takes to start is printed after the \"+\" character.\n");
1028
1029 if (argc > 1) {
1030 char **name;
1031 STRV_FOREACH(name, strv_skip(argv, 1))
1032 list_dependencies(bus, *name);
1033 } else
1034 list_dependencies(bus, SPECIAL_DEFAULT_TARGET);
1035
1036 h = hashmap_free(h);
1037 return 0;
1038 }
1039
1040 static int analyze_blame(int argc, char *argv[], void *userdata) {
1041 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1042 _cleanup_(unit_times_freep) struct unit_times *times = NULL;
1043 struct unit_times *u;
1044 int n, r;
1045
1046 r = acquire_bus(&bus, NULL);
1047 if (r < 0)
1048 return log_error_errno(r, "Failed to create bus connection: %m");
1049
1050 n = acquire_time_data(bus, &times);
1051 if (n <= 0)
1052 return n;
1053
1054 qsort(times, n, sizeof(struct unit_times), compare_unit_time);
1055
1056 (void) pager_open(arg_no_pager, false);
1057
1058 for (u = times; u->has_data; u++) {
1059 char ts[FORMAT_TIMESPAN_MAX];
1060
1061 if (u->time > 0)
1062 printf("%16s %s\n", format_timespan(ts, sizeof(ts), u->time, USEC_PER_MSEC), u->name);
1063 }
1064
1065 return 0;
1066 }
1067
1068 static int analyze_time(int argc, char *argv[], void *userdata) {
1069 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1070 _cleanup_free_ char *buf = NULL;
1071 int r;
1072
1073 r = acquire_bus(&bus, NULL);
1074 if (r < 0)
1075 return log_error_errno(r, "Failed to create bus connection: %m");
1076
1077 r = pretty_boot_time(bus, &buf);
1078 if (r < 0)
1079 return r;
1080
1081 puts(buf);
1082 return 0;
1083 }
1084
1085 static int graph_one_property(sd_bus *bus, const UnitInfo *u, const char* prop, const char *color, char* patterns[], char* from_patterns[], char* to_patterns[]) {
1086 _cleanup_strv_free_ char **units = NULL;
1087 char **unit;
1088 int r;
1089 bool match_patterns;
1090
1091 assert(u);
1092 assert(prop);
1093 assert(color);
1094
1095 match_patterns = strv_fnmatch(patterns, u->id, 0);
1096
1097 if (!strv_isempty(from_patterns) &&
1098 !match_patterns &&
1099 !strv_fnmatch(from_patterns, u->id, 0))
1100 return 0;
1101
1102 r = bus_get_unit_property_strv(bus, u->unit_path, prop, &units);
1103 if (r < 0)
1104 return r;
1105
1106 STRV_FOREACH(unit, units) {
1107 bool match_patterns2;
1108
1109 match_patterns2 = strv_fnmatch(patterns, *unit, 0);
1110
1111 if (!strv_isempty(to_patterns) &&
1112 !match_patterns2 &&
1113 !strv_fnmatch(to_patterns, *unit, 0))
1114 continue;
1115
1116 if (!strv_isempty(patterns) && !match_patterns && !match_patterns2)
1117 continue;
1118
1119 printf("\t\"%s\"->\"%s\" [color=\"%s\"];\n", u->id, *unit, color);
1120 }
1121
1122 return 0;
1123 }
1124
1125 static int graph_one(sd_bus *bus, const UnitInfo *u, char *patterns[], char *from_patterns[], char *to_patterns[]) {
1126 int r;
1127
1128 assert(bus);
1129 assert(u);
1130
1131 if (IN_SET(arg_dot, DEP_ORDER, DEP_ALL)) {
1132 r = graph_one_property(bus, u, "After", "green", patterns, from_patterns, to_patterns);
1133 if (r < 0)
1134 return r;
1135 }
1136
1137 if (IN_SET(arg_dot, DEP_REQUIRE, DEP_ALL)) {
1138 r = graph_one_property(bus, u, "Requires", "black", patterns, from_patterns, to_patterns);
1139 if (r < 0)
1140 return r;
1141 r = graph_one_property(bus, u, "Requisite", "darkblue", patterns, from_patterns, to_patterns);
1142 if (r < 0)
1143 return r;
1144 r = graph_one_property(bus, u, "Wants", "grey66", patterns, from_patterns, to_patterns);
1145 if (r < 0)
1146 return r;
1147 r = graph_one_property(bus, u, "Conflicts", "red", patterns, from_patterns, to_patterns);
1148 if (r < 0)
1149 return r;
1150 }
1151
1152 return 0;
1153 }
1154
1155 static int expand_patterns(sd_bus *bus, char **patterns, char ***ret) {
1156 _cleanup_strv_free_ char **expanded_patterns = NULL;
1157 char **pattern;
1158 int r;
1159
1160 STRV_FOREACH(pattern, patterns) {
1161 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1162 _cleanup_free_ char *unit = NULL, *unit_id = NULL;
1163
1164 if (strv_extend(&expanded_patterns, *pattern) < 0)
1165 return log_oom();
1166
1167 if (string_is_glob(*pattern))
1168 continue;
1169
1170 unit = unit_dbus_path_from_name(*pattern);
1171 if (!unit)
1172 return log_oom();
1173
1174 r = sd_bus_get_property_string(
1175 bus,
1176 "org.freedesktop.systemd1",
1177 unit,
1178 "org.freedesktop.systemd1.Unit",
1179 "Id",
1180 &error,
1181 &unit_id);
1182 if (r < 0)
1183 return log_error_errno(r, "Failed to get ID: %s", bus_error_message(&error, r));
1184
1185 if (!streq(*pattern, unit_id)) {
1186 if (strv_extend(&expanded_patterns, unit_id) < 0)
1187 return log_oom();
1188 }
1189 }
1190
1191 *ret = expanded_patterns;
1192 expanded_patterns = NULL; /* do not free */
1193
1194 return 0;
1195 }
1196
1197 static int dot(int argc, char *argv[], void *userdata) {
1198 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1199 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1200 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1201 _cleanup_strv_free_ char **expanded_patterns = NULL;
1202 _cleanup_strv_free_ char **expanded_from_patterns = NULL;
1203 _cleanup_strv_free_ char **expanded_to_patterns = NULL;
1204 int r;
1205 UnitInfo u;
1206
1207 r = acquire_bus(&bus, NULL);
1208 if (r < 0)
1209 return log_error_errno(r, "Failed to create bus connection: %m");
1210
1211 r = expand_patterns(bus, strv_skip(argv, 1), &expanded_patterns);
1212 if (r < 0)
1213 return r;
1214
1215 r = expand_patterns(bus, arg_dot_from_patterns, &expanded_from_patterns);
1216 if (r < 0)
1217 return r;
1218
1219 r = expand_patterns(bus, arg_dot_to_patterns, &expanded_to_patterns);
1220 if (r < 0)
1221 return r;
1222
1223 r = sd_bus_call_method(
1224 bus,
1225 "org.freedesktop.systemd1",
1226 "/org/freedesktop/systemd1",
1227 "org.freedesktop.systemd1.Manager",
1228 "ListUnits",
1229 &error,
1230 &reply,
1231 "");
1232 if (r < 0) {
1233 log_error("Failed to list units: %s", bus_error_message(&error, -r));
1234 return r;
1235 }
1236
1237 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
1238 if (r < 0)
1239 return bus_log_parse_error(r);
1240
1241 printf("digraph systemd {\n");
1242
1243 while ((r = bus_parse_unit_info(reply, &u)) > 0) {
1244
1245 r = graph_one(bus, &u, expanded_patterns, expanded_from_patterns, expanded_to_patterns);
1246 if (r < 0)
1247 return r;
1248 }
1249 if (r < 0)
1250 return bus_log_parse_error(r);
1251
1252 printf("}\n");
1253
1254 log_info(" Color legend: black = Requires\n"
1255 " dark blue = Requisite\n"
1256 " dark grey = Wants\n"
1257 " red = Conflicts\n"
1258 " green = After\n");
1259
1260 if (on_tty())
1261 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
1262 "-- Try a shell pipeline like 'systemd-analyze dot | dot -Tsvg > systemd.svg'!\n");
1263
1264 return 0;
1265 }
1266
1267 static int dump_fallback(sd_bus *bus) {
1268 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1269 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1270 const char *text = NULL;
1271 int r;
1272
1273 assert(bus);
1274
1275 r = sd_bus_call_method(
1276 bus,
1277 "org.freedesktop.systemd1",
1278 "/org/freedesktop/systemd1",
1279 "org.freedesktop.systemd1.Manager",
1280 "Dump",
1281 &error,
1282 &reply,
1283 NULL);
1284 if (r < 0)
1285 return log_error_errno(r, "Failed to issue method call Dump: %s", bus_error_message(&error, r));
1286
1287 r = sd_bus_message_read(reply, "s", &text);
1288 if (r < 0)
1289 return bus_log_parse_error(r);
1290
1291 fputs(text, stdout);
1292 return 0;
1293 }
1294
1295 static int dump(int argc, char *argv[], void *userdata) {
1296 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1297 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1298 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1299 int fd = -1;
1300 int r;
1301
1302 r = acquire_bus(&bus, NULL);
1303 if (r < 0)
1304 return log_error_errno(r, "Failed to create bus connection: %m");
1305
1306 (void) pager_open(arg_no_pager, false);
1307
1308 if (!sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD))
1309 return dump_fallback(bus);
1310
1311 r = sd_bus_call_method(
1312 bus,
1313 "org.freedesktop.systemd1",
1314 "/org/freedesktop/systemd1",
1315 "org.freedesktop.systemd1.Manager",
1316 "DumpByFileDescriptor",
1317 &error,
1318 &reply,
1319 NULL);
1320 if (r < 0) {
1321 /* fall back to Dump if DumpByFileDescriptor is not supported */
1322 if (!IN_SET(r, -EACCES, -EBADR))
1323 return log_error_errno(r, "Failed to issue method call DumpByFileDescriptor: %s", bus_error_message(&error, r));
1324
1325 return dump_fallback(bus);
1326 }
1327
1328 r = sd_bus_message_read(reply, "h", &fd);
1329 if (r < 0)
1330 return bus_log_parse_error(r);
1331
1332 fflush(stdout);
1333 return copy_bytes(fd, STDOUT_FILENO, (uint64_t) -1, 0);
1334 }
1335
1336 static int cat_config(int argc, char *argv[], void *userdata) {
1337 char **arg;
1338 int r;
1339
1340 (void) pager_open(arg_no_pager, false);
1341
1342 STRV_FOREACH(arg, argv + 1) {
1343 const char *t = NULL;
1344
1345 if (arg != argv + 1)
1346 print_separator();
1347
1348 if (path_is_absolute(*arg)) {
1349 const char *dir;
1350
1351 NULSTR_FOREACH(dir, CONF_PATHS_NULSTR("")) {
1352 t = path_startswith(*arg, dir);
1353 if (t)
1354 break;
1355 }
1356
1357 if (!t) {
1358 log_error("Path %s does not start with any known prefix.", *arg);
1359 return -EINVAL;
1360 }
1361 } else
1362 t = *arg;
1363
1364 r = conf_files_cat(arg_root, t);
1365 if (r < 0)
1366 return r;
1367 }
1368
1369 return 0;
1370 }
1371
1372 static int set_log_level(int argc, char *argv[], void *userdata) {
1373 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1374 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1375 int r;
1376
1377 assert(argc == 2);
1378 assert(argv);
1379
1380 r = acquire_bus(&bus, NULL);
1381 if (r < 0)
1382 return log_error_errno(r, "Failed to create bus connection: %m");
1383
1384 r = sd_bus_set_property(
1385 bus,
1386 "org.freedesktop.systemd1",
1387 "/org/freedesktop/systemd1",
1388 "org.freedesktop.systemd1.Manager",
1389 "LogLevel",
1390 &error,
1391 "s",
1392 argv[1]);
1393 if (r < 0)
1394 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
1395
1396 return 0;
1397 }
1398
1399 static int get_log_level(int argc, char *argv[], void *userdata) {
1400 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1401 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1402 _cleanup_free_ char *level = NULL;
1403 int r;
1404
1405 r = acquire_bus(&bus, NULL);
1406 if (r < 0)
1407 return log_error_errno(r, "Failed to create bus connection: %m");
1408
1409 r = sd_bus_get_property_string(
1410 bus,
1411 "org.freedesktop.systemd1",
1412 "/org/freedesktop/systemd1",
1413 "org.freedesktop.systemd1.Manager",
1414 "LogLevel",
1415 &error,
1416 &level);
1417 if (r < 0)
1418 return log_error_errno(r, "Failed to get log level: %s", bus_error_message(&error, r));
1419
1420 puts(level);
1421 return 0;
1422 }
1423
1424 static int get_or_set_log_level(int argc, char *argv[], void *userdata) {
1425 return (argc == 1) ? get_log_level(argc, argv, userdata) : set_log_level(argc, argv, userdata);
1426 }
1427
1428 static int set_log_target(int argc, char *argv[], void *userdata) {
1429 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1430 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1431 int r;
1432
1433 assert(argc == 2);
1434 assert(argv);
1435
1436 r = acquire_bus(&bus, NULL);
1437 if (r < 0)
1438 return log_error_errno(r, "Failed to create bus connection: %m");
1439
1440 r = sd_bus_set_property(
1441 bus,
1442 "org.freedesktop.systemd1",
1443 "/org/freedesktop/systemd1",
1444 "org.freedesktop.systemd1.Manager",
1445 "LogTarget",
1446 &error,
1447 "s",
1448 argv[1]);
1449 if (r < 0)
1450 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
1451
1452 return 0;
1453 }
1454
1455 static int get_log_target(int argc, char *argv[], void *userdata) {
1456 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1457 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1458 _cleanup_free_ char *target = NULL;
1459 int r;
1460
1461 r = acquire_bus(&bus, NULL);
1462 if (r < 0)
1463 return log_error_errno(r, "Failed to create bus connection: %m");
1464
1465 r = sd_bus_get_property_string(
1466 bus,
1467 "org.freedesktop.systemd1",
1468 "/org/freedesktop/systemd1",
1469 "org.freedesktop.systemd1.Manager",
1470 "LogTarget",
1471 &error,
1472 &target);
1473 if (r < 0)
1474 return log_error_errno(r, "Failed to get log target: %s", bus_error_message(&error, r));
1475
1476 puts(target);
1477 return 0;
1478 }
1479
1480 static int get_or_set_log_target(int argc, char *argv[], void *userdata) {
1481 return (argc == 1) ? get_log_target(argc, argv, userdata) : set_log_target(argc, argv, userdata);
1482 }
1483
1484 static int dump_unit_paths(int argc, char *argv[], void *userdata) {
1485 _cleanup_(lookup_paths_free) LookupPaths paths = {};
1486 int r;
1487 char **p;
1488
1489 r = lookup_paths_init(&paths, arg_scope, 0, NULL);
1490 if (r < 0)
1491 return log_error_errno(r, "lookup_paths_init() failed: %m");
1492
1493 STRV_FOREACH(p, paths.search_path)
1494 puts(*p);
1495
1496 return 0;
1497 }
1498
1499 #if HAVE_SECCOMP
1500 static void dump_syscall_filter(const SyscallFilterSet *set) {
1501 const char *syscall;
1502
1503 printf("%s\n", set->name);
1504 printf(" # %s\n", set->help);
1505 NULSTR_FOREACH(syscall, set->value)
1506 printf(" %s\n", syscall);
1507 }
1508
1509 static int dump_syscall_filters(int argc, char *argv[], void *userdata) {
1510 bool first = true;
1511
1512 (void) pager_open(arg_no_pager, false);
1513
1514 if (strv_isempty(strv_skip(argv, 1))) {
1515 int i;
1516
1517 for (i = 0; i < _SYSCALL_FILTER_SET_MAX; i++) {
1518 if (!first)
1519 puts("");
1520 dump_syscall_filter(syscall_filter_sets + i);
1521 first = false;
1522 }
1523 } else {
1524 char **name;
1525
1526 STRV_FOREACH(name, strv_skip(argv, 1)) {
1527 const SyscallFilterSet *set;
1528
1529 if (!first)
1530 puts("");
1531
1532 set = syscall_filter_set_find(*name);
1533 if (!set) {
1534 /* make sure the error appears below normal output */
1535 fflush(stdout);
1536
1537 log_error("Filter set \"%s\" not found.", *name);
1538 return -ENOENT;
1539 }
1540
1541 dump_syscall_filter(set);
1542 first = false;
1543 }
1544 }
1545
1546 return 0;
1547 }
1548
1549 #else
1550 static int dump_syscall_filters(int argc, char *argv[], void *userdata) {
1551 log_error("Not compiled with syscall filters, sorry.");
1552 return -EOPNOTSUPP;
1553 }
1554 #endif
1555
1556 static int test_calendar(int argc, char *argv[], void *userdata) {
1557 int ret = 0, r;
1558 char **p;
1559 usec_t n;
1560
1561 n = now(CLOCK_REALTIME);
1562
1563 STRV_FOREACH(p, strv_skip(argv, 1)) {
1564 _cleanup_(calendar_spec_freep) CalendarSpec *spec = NULL;
1565 _cleanup_free_ char *t = NULL;
1566 usec_t next;
1567
1568 r = calendar_spec_from_string(*p, &spec);
1569 if (r < 0) {
1570 ret = log_error_errno(r, "Failed to parse calendar specification '%s': %m", *p);
1571 continue;
1572 }
1573
1574 r = calendar_spec_normalize(spec);
1575 if (r < 0) {
1576 ret = log_error_errno(r, "Failed to normalize calendar specification '%s': %m", *p);
1577 continue;
1578 }
1579
1580 r = calendar_spec_to_string(spec, &t);
1581 if (r < 0) {
1582 ret = log_error_errno(r, "Failed to format calendar specification '%s': %m", *p);
1583 continue;
1584 }
1585
1586 if (!streq(t, *p))
1587 printf(" Original form: %s\n", *p);
1588
1589 printf("Normalized form: %s\n", t);
1590
1591 r = calendar_spec_next_usec(spec, n, &next);
1592 if (r == -ENOENT)
1593 printf(" Next elapse: never\n");
1594 else if (r < 0) {
1595 ret = log_error_errno(r, "Failed to determine next elapse for '%s': %m", *p);
1596 continue;
1597 } else {
1598 char buffer[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESTAMP_RELATIVE_MAX)];
1599
1600 printf(" Next elapse: %s\n", format_timestamp(buffer, sizeof(buffer), next));
1601
1602 if (!in_utc_timezone())
1603 printf(" (in UTC): %s\n", format_timestamp_utc(buffer, sizeof(buffer), next));
1604
1605 printf(" From now: %s\n", format_timestamp_relative(buffer, sizeof(buffer), next));
1606 }
1607
1608 if (*(p+1))
1609 putchar('\n');
1610 }
1611
1612 return ret;
1613 }
1614
1615 static int service_watchdogs(int argc, char *argv[], void *userdata) {
1616 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1617 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1618 int b, r;
1619
1620 assert(IN_SET(argc, 1, 2));
1621 assert(argv);
1622
1623 r = acquire_bus(&bus, NULL);
1624 if (r < 0)
1625 return log_error_errno(r, "Failed to create bus connection: %m");
1626
1627 /* get ServiceWatchdogs */
1628 if (argc == 1) {
1629 r = sd_bus_get_property_trivial(
1630 bus,
1631 "org.freedesktop.systemd1",
1632 "/org/freedesktop/systemd1",
1633 "org.freedesktop.systemd1.Manager",
1634 "ServiceWatchdogs",
1635 &error,
1636 'b',
1637 &b);
1638 if (r < 0)
1639 return log_error_errno(r, "Failed to get service-watchdog state: %s", bus_error_message(&error, r));
1640
1641 printf("%s\n", yes_no(!!b));
1642
1643 return 0;
1644 }
1645
1646 /* set ServiceWatchdogs */
1647 b = parse_boolean(argv[1]);
1648 if (b < 0) {
1649 log_error("Failed to parse service-watchdogs argument.");
1650 return -EINVAL;
1651 }
1652
1653 r = sd_bus_set_property(
1654 bus,
1655 "org.freedesktop.systemd1",
1656 "/org/freedesktop/systemd1",
1657 "org.freedesktop.systemd1.Manager",
1658 "ServiceWatchdogs",
1659 &error,
1660 "b",
1661 b);
1662 if (r < 0)
1663 return log_error_errno(r, "Failed to set service-watchdog state: %s", bus_error_message(&error, r));
1664
1665 return 0;
1666 }
1667
1668 static int do_verify(int argc, char *argv[], void *userdata) {
1669 return verify_units(strv_skip(argv, 1), arg_scope, arg_man, arg_generators);
1670 }
1671
1672 static int help(int argc, char *argv[], void *userdata) {
1673
1674 (void) pager_open(arg_no_pager, false);
1675
1676 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1677 "Profile systemd, show unit dependencies, check unit files.\n\n"
1678 " -h --help Show this help\n"
1679 " --version Show package version\n"
1680 " --no-pager Do not pipe output into a pager\n"
1681 " --system Operate on system systemd instance\n"
1682 " --user Operate on user systemd instance\n"
1683 " --global Operate on global user configuration\n"
1684 " -H --host=[USER@]HOST Operate on remote host\n"
1685 " -M --machine=CONTAINER Operate on local container\n"
1686 " --order Show only order in the graph\n"
1687 " --require Show only requirement in the graph\n"
1688 " --from-pattern=GLOB Show only origins in the graph\n"
1689 " --to-pattern=GLOB Show only destinations in the graph\n"
1690 " --fuzz=SECONDS Also print also services which finished SECONDS\n"
1691 " earlier than the latest in the branch\n"
1692 " --man[=BOOL] Do [not] check for existence of man pages\n\n"
1693 " --generators[=BOOL] Do [not] run unit generators (requires privileges)\n\n"
1694 "Commands:\n"
1695 " time Print time spent in the kernel\n"
1696 " blame Print list of running units ordered by time to init\n"
1697 " critical-chain [UNIT...] Print a tree of the time critical chain of units\n"
1698 " plot Output SVG graphic showing service initialization\n"
1699 " dot [UNIT...] Output dependency graph in man:dot(1) format\n"
1700 " log-level [LEVEL] Get/set logging threshold for manager\n"
1701 " log-target [TARGET] Get/set logging target for manager\n"
1702 " dump Output state serialization of service manager\n"
1703 " cat-config Show configuration file and drop-ins\n"
1704 " unit-paths List load directories for units\n"
1705 " syscall-filter [NAME...] Print list of syscalls in seccomp filter\n"
1706 " verify FILE... Check unit files for correctness\n"
1707 " calendar SPEC... Validate repetitive calendar time events\n"
1708 " service-watchdogs [BOOL] Get/set service watchdog state\n"
1709 , program_invocation_short_name);
1710
1711 /* When updating this list, including descriptions, apply
1712 * changes to shell-completion/bash/systemd-analyze and
1713 * shell-completion/zsh/_systemd-analyze too. */
1714
1715 return 0;
1716 }
1717
1718 static int parse_argv(int argc, char *argv[]) {
1719 enum {
1720 ARG_VERSION = 0x100,
1721 ARG_ORDER,
1722 ARG_REQUIRE,
1723 ARG_ROOT,
1724 ARG_SYSTEM,
1725 ARG_USER,
1726 ARG_GLOBAL,
1727 ARG_DOT_FROM_PATTERN,
1728 ARG_DOT_TO_PATTERN,
1729 ARG_FUZZ,
1730 ARG_NO_PAGER,
1731 ARG_MAN,
1732 ARG_GENERATORS,
1733 };
1734
1735 static const struct option options[] = {
1736 { "help", no_argument, NULL, 'h' },
1737 { "version", no_argument, NULL, ARG_VERSION },
1738 { "order", no_argument, NULL, ARG_ORDER },
1739 { "require", no_argument, NULL, ARG_REQUIRE },
1740 { "root", required_argument, NULL, ARG_ROOT },
1741 { "system", no_argument, NULL, ARG_SYSTEM },
1742 { "user", no_argument, NULL, ARG_USER },
1743 { "global", no_argument, NULL, ARG_GLOBAL },
1744 { "from-pattern", required_argument, NULL, ARG_DOT_FROM_PATTERN },
1745 { "to-pattern", required_argument, NULL, ARG_DOT_TO_PATTERN },
1746 { "fuzz", required_argument, NULL, ARG_FUZZ },
1747 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1748 { "man", optional_argument, NULL, ARG_MAN },
1749 { "generators", optional_argument, NULL, ARG_GENERATORS },
1750 { "host", required_argument, NULL, 'H' },
1751 { "machine", required_argument, NULL, 'M' },
1752 {}
1753 };
1754
1755 int r, c;
1756
1757 assert(argc >= 0);
1758 assert(argv);
1759
1760 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
1761 switch (c) {
1762
1763 case 'h':
1764 return help(0, NULL, NULL);
1765
1766 case ARG_VERSION:
1767 return version();
1768
1769 case ARG_ROOT:
1770 arg_root = optarg;
1771 break;
1772
1773 case ARG_SYSTEM:
1774 arg_scope = UNIT_FILE_SYSTEM;
1775 break;
1776
1777 case ARG_USER:
1778 arg_scope = UNIT_FILE_USER;
1779 break;
1780
1781 case ARG_GLOBAL:
1782 arg_scope = UNIT_FILE_GLOBAL;
1783 break;
1784
1785 case ARG_ORDER:
1786 arg_dot = DEP_ORDER;
1787 break;
1788
1789 case ARG_REQUIRE:
1790 arg_dot = DEP_REQUIRE;
1791 break;
1792
1793 case ARG_DOT_FROM_PATTERN:
1794 if (strv_extend(&arg_dot_from_patterns, optarg) < 0)
1795 return log_oom();
1796
1797 break;
1798
1799 case ARG_DOT_TO_PATTERN:
1800 if (strv_extend(&arg_dot_to_patterns, optarg) < 0)
1801 return log_oom();
1802
1803 break;
1804
1805 case ARG_FUZZ:
1806 r = parse_sec(optarg, &arg_fuzz);
1807 if (r < 0)
1808 return r;
1809 break;
1810
1811 case ARG_NO_PAGER:
1812 arg_no_pager = true;
1813 break;
1814
1815 case 'H':
1816 arg_transport = BUS_TRANSPORT_REMOTE;
1817 arg_host = optarg;
1818 break;
1819
1820 case 'M':
1821 arg_transport = BUS_TRANSPORT_MACHINE;
1822 arg_host = optarg;
1823 break;
1824
1825 case ARG_MAN:
1826 if (optarg) {
1827 r = parse_boolean(optarg);
1828 if (r < 0) {
1829 log_error("Failed to parse --man= argument.");
1830 return -EINVAL;
1831 }
1832
1833 arg_man = r;
1834 } else
1835 arg_man = true;
1836
1837 break;
1838
1839 case ARG_GENERATORS:
1840 if (optarg) {
1841 r = parse_boolean(optarg);
1842 if (r < 0) {
1843 log_error("Failed to parse --generators= argument.");
1844 return -EINVAL;
1845 }
1846
1847 arg_generators = r;
1848 } else
1849 arg_generators = true;
1850
1851 break;
1852
1853 case '?':
1854 return -EINVAL;
1855
1856 default:
1857 assert_not_reached("Unhandled option code.");
1858 }
1859
1860 if (arg_scope == UNIT_FILE_GLOBAL &&
1861 !STR_IN_SET(argv[optind] ?: "time", "dot", "unit-paths", "verify")) {
1862 log_error("Option --global only makes sense with verbs dot, unit-paths, verify.");
1863 return -EINVAL;
1864 }
1865
1866 if (arg_root && !streq_ptr(argv[optind], "cat-config")) {
1867 log_error("Option --root is only supported for cat-config right now.");
1868 return -EINVAL;
1869 }
1870
1871 return 1; /* work to do */
1872 }
1873
1874 int main(int argc, char *argv[]) {
1875
1876 static const Verb verbs[] = {
1877 { "help", VERB_ANY, VERB_ANY, 0, help },
1878 { "time", VERB_ANY, 1, VERB_DEFAULT, analyze_time },
1879 { "blame", VERB_ANY, 1, 0, analyze_blame },
1880 { "critical-chain", VERB_ANY, VERB_ANY, 0, analyze_critical_chain },
1881 { "plot", VERB_ANY, 1, 0, analyze_plot },
1882 { "dot", VERB_ANY, VERB_ANY, 0, dot },
1883 { "log-level", VERB_ANY, 2, 0, get_or_set_log_level },
1884 { "log-target", VERB_ANY, 2, 0, get_or_set_log_target },
1885 /* The following four verbs are deprecated aliases */
1886 { "set-log-level", 2, 2, 0, set_log_level },
1887 { "get-log-level", VERB_ANY, 1, 0, get_log_level },
1888 { "set-log-target", 2, 2, 0, set_log_target },
1889 { "get-log-target", VERB_ANY, 1, 0, get_log_target },
1890 { "dump", VERB_ANY, 1, 0, dump },
1891 { "cat-config", 2, VERB_ANY, 0, cat_config },
1892 { "unit-paths", 1, 1, 0, dump_unit_paths },
1893 { "syscall-filter", VERB_ANY, VERB_ANY, 0, dump_syscall_filters },
1894 { "verify", 2, VERB_ANY, 0, do_verify },
1895 { "calendar", 2, VERB_ANY, 0, test_calendar },
1896 { "service-watchdogs", VERB_ANY, 2, 0, service_watchdogs },
1897 {}
1898 };
1899
1900 int r;
1901
1902 setlocale(LC_ALL, "");
1903 setlocale(LC_NUMERIC, "C"); /* we want to format/parse floats in C style */
1904
1905 log_parse_environment();
1906 log_open();
1907
1908 r = parse_argv(argc, argv);
1909 if (r <= 0)
1910 goto finish;
1911
1912 r = dispatch_verb(argc, argv, verbs, NULL);
1913
1914 finish:
1915 pager_close();
1916
1917 strv_free(arg_dot_from_patterns);
1918 strv_free(arg_dot_to_patterns);
1919
1920 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1921 }