1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2013 Simon Peeters
13 #include "alloc-util.h"
14 #include "analyze-verify.h"
15 #include "bus-error.h"
16 #include "bus-unit-util.h"
18 #include "calendarspec.h"
20 #include "conf-files.h"
23 #include "glob-util.h"
25 #include "locale-util.h"
28 #include "parse-util.h"
29 #include "path-util.h"
31 #include "seccomp-util.h"
36 #include "terminal-util.h"
37 #include "unit-name.h"
41 #define SCALE_X (0.1 / 1000.0) /* pixels per us */
42 #define SCALE_Y (20.0)
44 #define compare(a, b) (((a) > (b))? 1 : (((b) > (a))? -1 : 0))
46 #define svg(...) printf(__VA_ARGS__)
48 #define svg_bar(class, x1, x2, y) \
49 svg(" <rect class=\"%s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n", \
51 SCALE_X * (x1), SCALE_Y * (y), \
52 SCALE_X * ((x2) - (x1)), SCALE_Y - 1.0)
54 #define svg_text(b, x, y, format, ...) \
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__); \
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
;
81 usec_t kernel_done_time
;
83 usec_t userspace_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 usec_t initrd_security_start_time
;
92 usec_t initrd_security_finish_time
;
93 usec_t initrd_generators_start_time
;
94 usec_t initrd_generators_finish_time
;
95 usec_t initrd_unitsload_start_time
;
96 usec_t initrd_unitsload_finish_time
;
99 * If we're analyzing the user instance, all timestamps will be offset
100 * by its own start-up timestamp, which may be arbitrarily big.
101 * With "plot", this causes arbitrarily wide output SVG files which almost
102 * completely consist of empty space. Thus we cancel out this offset.
104 * This offset is subtracted from times above by acquire_boot_times(),
105 * but it still needs to be subtracted from unit-specific timestamps
106 * (so it is stored here for reference).
108 usec_t reverse_offset
;
124 char *kernel_release
;
125 char *kernel_version
;
126 char *os_pretty_name
;
127 char *virtualization
;
131 static int acquire_bus(sd_bus
**bus
, bool *use_full_bus
) {
132 bool user
= arg_scope
!= UNIT_FILE_SYSTEM
;
135 if (use_full_bus
&& *use_full_bus
) {
136 r
= bus_connect_transport(arg_transport
, arg_host
, user
, bus
);
137 if (IN_SET(r
, 0, -EHOSTDOWN
))
140 *use_full_bus
= false;
143 return bus_connect_transport_systemd(arg_transport
, arg_host
, user
, bus
);
146 static int bus_get_uint64_property(sd_bus
*bus
, const char *path
, const char *interface
, const char *property
, uint64_t *val
) {
147 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
156 r
= sd_bus_get_property_trivial(
158 "org.freedesktop.systemd1",
166 log_error("Failed to parse reply: %s", bus_error_message(&error
, -r
));
173 static int bus_get_unit_property_strv(sd_bus
*bus
, const char *path
, const char *property
, char ***strv
) {
174 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
182 r
= sd_bus_get_property_strv(
184 "org.freedesktop.systemd1",
186 "org.freedesktop.systemd1.Unit",
191 log_error("Failed to get unit property %s: %s", property
, bus_error_message(&error
, -r
));
198 static int compare_unit_time(const void *a
, const void *b
) {
199 return compare(((struct unit_times
*)b
)->time
,
200 ((struct unit_times
*)a
)->time
);
203 static int compare_unit_start(const void *a
, const void *b
) {
204 return compare(((struct unit_times
*)a
)->activating
,
205 ((struct unit_times
*)b
)->activating
);
208 static void unit_times_free(struct unit_times
*t
) {
209 struct unit_times
*p
;
211 for (p
= t
; p
->has_data
; p
++)
216 DEFINE_TRIVIAL_CLEANUP_FUNC(struct unit_times
*, unit_times_free
);
218 static void subtract_timestamp(usec_t
*a
, usec_t b
) {
227 static int acquire_boot_times(sd_bus
*bus
, struct boot_times
**bt
) {
228 static const struct bus_properties_map property_map
[] = {
229 { "FirmwareTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, firmware_time
) },
230 { "LoaderTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, loader_time
) },
231 { "KernelTimestamp", "t", NULL
, offsetof(struct boot_times
, kernel_time
) },
232 { "InitRDTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, initrd_time
) },
233 { "UserspaceTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, userspace_time
) },
234 { "FinishTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, finish_time
) },
235 { "SecurityStartTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, security_start_time
) },
236 { "SecurityFinishTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, security_finish_time
) },
237 { "GeneratorsStartTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, generators_start_time
) },
238 { "GeneratorsFinishTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, generators_finish_time
) },
239 { "UnitsLoadStartTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, unitsload_start_time
) },
240 { "UnitsLoadFinishTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, unitsload_finish_time
) },
241 { "InitRDSecurityStartTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, initrd_security_start_time
) },
242 { "InitRDSecurityFinishTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, initrd_security_finish_time
) },
243 { "InitRDGeneratorsStartTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, initrd_generators_start_time
) },
244 { "InitRDGeneratorsFinishTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, initrd_generators_finish_time
) },
245 { "InitRDUnitsLoadStartTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, initrd_unitsload_start_time
) },
246 { "InitRDUnitsLoadFinishTimestampMonotonic", "t", NULL
, offsetof(struct boot_times
, initrd_unitsload_finish_time
) },
249 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
250 static struct boot_times times
;
251 static bool cached
= false;
257 assert_cc(sizeof(usec_t
) == sizeof(uint64_t));
259 r
= bus_map_all_properties(
261 "org.freedesktop.systemd1",
262 "/org/freedesktop/systemd1",
269 return log_error_errno(r
, "Failed to get timestamp properties: %s", bus_error_message(&error
, r
));
271 if (times
.finish_time
<= 0) {
272 log_error("Bootup is not yet finished (org.freedesktop.systemd1.Manager.FinishTimestampMonotonic=%"PRIu64
").\n"
273 "Please try again later.\n"
274 "Hint: Use 'systemctl%s list-jobs' to see active jobs",
276 arg_scope
== UNIT_FILE_SYSTEM
? "" : " --user");
280 if (arg_scope
== UNIT_FILE_SYSTEM
&& times
.security_start_time
> 0) {
281 /* security_start_time is set when systemd is not running under container environment. */
282 if (times
.initrd_time
> 0)
283 times
.kernel_done_time
= times
.initrd_time
;
285 times
.kernel_done_time
= times
.userspace_time
;
288 * User-instance-specific or container-system-specific timestamps processing
289 * (see comment to reverse_offset in struct boot_times).
291 times
.reverse_offset
= times
.userspace_time
;
293 times
.firmware_time
= times
.loader_time
= times
.kernel_time
= times
.initrd_time
= times
.userspace_time
=
294 times
.security_start_time
= times
.security_finish_time
= 0;
296 subtract_timestamp(×
.finish_time
, times
.reverse_offset
);
298 subtract_timestamp(×
.generators_start_time
, times
.reverse_offset
);
299 subtract_timestamp(×
.generators_finish_time
, times
.reverse_offset
);
301 subtract_timestamp(×
.unitsload_start_time
, times
.reverse_offset
);
302 subtract_timestamp(×
.unitsload_finish_time
, times
.reverse_offset
);
312 static void free_host_info(struct host_info
*hi
) {
318 free(hi
->kernel_name
);
319 free(hi
->kernel_release
);
320 free(hi
->kernel_version
);
321 free(hi
->os_pretty_name
);
322 free(hi
->virtualization
);
323 free(hi
->architecture
);
327 DEFINE_TRIVIAL_CLEANUP_FUNC(struct host_info
*, free_host_info
);
329 static int acquire_time_data(sd_bus
*bus
, struct unit_times
**out
) {
330 static const struct bus_properties_map property_map
[] = {
331 { "InactiveExitTimestampMonotonic", "t", NULL
, offsetof(struct unit_times
, activating
) },
332 { "ActiveEnterTimestampMonotonic", "t", NULL
, offsetof(struct unit_times
, activated
) },
333 { "ActiveExitTimestampMonotonic", "t", NULL
, offsetof(struct unit_times
, deactivating
) },
334 { "InactiveEnterTimestampMonotonic", "t", NULL
, offsetof(struct unit_times
, deactivated
) },
337 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
338 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
339 _cleanup_(unit_times_freep
) struct unit_times
*unit_times
= NULL
;
340 struct boot_times
*boot_times
= NULL
;
341 size_t allocated
= 0, c
= 0;
345 r
= acquire_boot_times(bus
, &boot_times
);
349 r
= sd_bus_call_method(
351 "org.freedesktop.systemd1",
352 "/org/freedesktop/systemd1",
353 "org.freedesktop.systemd1.Manager",
358 log_error("Failed to list units: %s", bus_error_message(&error
, -r
));
362 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssssouso)");
364 return bus_log_parse_error(r
);
366 while ((r
= bus_parse_unit_info(reply
, &u
)) > 0) {
367 struct unit_times
*t
;
369 if (!GREEDY_REALLOC(unit_times
, allocated
, c
+2))
372 unit_times
[c
+1].has_data
= false;
376 assert_cc(sizeof(usec_t
) == sizeof(uint64_t));
378 r
= bus_map_all_properties(
380 "org.freedesktop.systemd1",
388 return log_error_errno(r
, "Failed to get timestamp properties of unit %s: %s", u
.id
, bus_error_message(&error
, r
));
390 subtract_timestamp(&t
->activating
, boot_times
->reverse_offset
);
391 subtract_timestamp(&t
->activated
, boot_times
->reverse_offset
);
392 subtract_timestamp(&t
->deactivating
, boot_times
->reverse_offset
);
393 subtract_timestamp(&t
->deactivated
, boot_times
->reverse_offset
);
395 if (t
->activated
>= t
->activating
)
396 t
->time
= t
->activated
- t
->activating
;
397 else if (t
->deactivated
>= t
->activating
)
398 t
->time
= t
->deactivated
- t
->activating
;
402 if (t
->activating
== 0)
405 t
->name
= strdup(u
.id
);
413 return bus_log_parse_error(r
);
415 *out
= TAKE_PTR(unit_times
);
419 static int acquire_host_info(sd_bus
*bus
, struct host_info
**hi
) {
420 static const struct bus_properties_map hostname_map
[] = {
421 { "Hostname", "s", NULL
, offsetof(struct host_info
, hostname
) },
422 { "KernelName", "s", NULL
, offsetof(struct host_info
, kernel_name
) },
423 { "KernelRelease", "s", NULL
, offsetof(struct host_info
, kernel_release
) },
424 { "KernelVersion", "s", NULL
, offsetof(struct host_info
, kernel_version
) },
425 { "OperatingSystemPrettyName", "s", NULL
, offsetof(struct host_info
, os_pretty_name
) },
429 static const struct bus_properties_map manager_map
[] = {
430 { "Virtualization", "s", NULL
, offsetof(struct host_info
, virtualization
) },
431 { "Architecture", "s", NULL
, offsetof(struct host_info
, architecture
) },
435 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
436 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*system_bus
= NULL
;
437 _cleanup_(free_host_infop
) struct host_info
*host
;
440 host
= new0(struct host_info
, 1);
444 if (arg_scope
!= UNIT_FILE_SYSTEM
) {
445 r
= bus_connect_transport(arg_transport
, arg_host
, false, &system_bus
);
447 log_debug_errno(r
, "Failed to connect to system bus, ignoring: %m");
452 r
= bus_map_all_properties(system_bus
?: bus
,
453 "org.freedesktop.hostname1",
454 "/org/freedesktop/hostname1",
461 log_debug_errno(r
, "Failed to get host information from systemd-hostnamed, ignoring: %s", bus_error_message(&error
, r
));
462 sd_bus_error_free(&error
);
466 r
= bus_map_all_properties(bus
,
467 "org.freedesktop.systemd1",
468 "/org/freedesktop/systemd1",
475 return log_error_errno(r
, "Failed to get host information from systemd: %s", bus_error_message(&error
, r
));
477 *hi
= TAKE_PTR(host
);
482 static int pretty_boot_time(sd_bus
*bus
, char **_buf
) {
483 char ts
[FORMAT_TIMESPAN_MAX
];
484 struct boot_times
*t
;
485 static char buf
[4096];
489 usec_t activated_time
= USEC_INFINITY
;
490 _cleanup_free_
char* path
= NULL
, *unit_id
= NULL
;
491 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
493 r
= acquire_boot_times(bus
, &t
);
497 path
= unit_dbus_path_from_name(SPECIAL_DEFAULT_TARGET
);
501 r
= sd_bus_get_property_string(
503 "org.freedesktop.systemd1",
505 "org.freedesktop.systemd1.Unit",
510 log_error_errno(r
, "default.target doesn't seem to exist: %s", bus_error_message(&error
, r
));
514 r
= bus_get_uint64_property(bus
, path
,
515 "org.freedesktop.systemd1.Unit",
516 "ActiveEnterTimestampMonotonic",
519 log_info_errno(r
, "Could not get time to reach default.target. Continuing...");
520 activated_time
= USEC_INFINITY
;
526 size
= strpcpyf(&ptr
, size
, "Startup finished in ");
527 if (t
->firmware_time
> 0)
528 size
= strpcpyf(&ptr
, size
, "%s (firmware) + ", format_timespan(ts
, sizeof(ts
), t
->firmware_time
- t
->loader_time
, USEC_PER_MSEC
));
529 if (t
->loader_time
> 0)
530 size
= strpcpyf(&ptr
, size
, "%s (loader) + ", format_timespan(ts
, sizeof(ts
), t
->loader_time
, USEC_PER_MSEC
));
531 if (t
->kernel_time
> 0)
532 size
= strpcpyf(&ptr
, size
, "%s (kernel) + ", format_timespan(ts
, sizeof(ts
), t
->kernel_done_time
, USEC_PER_MSEC
));
533 if (t
->initrd_time
> 0)
534 size
= strpcpyf(&ptr
, size
, "%s (initrd) + ", format_timespan(ts
, sizeof(ts
), t
->userspace_time
- t
->initrd_time
, USEC_PER_MSEC
));
536 size
= strpcpyf(&ptr
, size
, "%s (userspace) ", format_timespan(ts
, sizeof(ts
), t
->finish_time
- t
->userspace_time
, USEC_PER_MSEC
));
537 if (t
->kernel_time
> 0)
538 strpcpyf(&ptr
, size
, "= %s ", format_timespan(ts
, sizeof(ts
), t
->firmware_time
+ t
->finish_time
, USEC_PER_MSEC
));
540 if (unit_id
&& activated_time
> 0 && activated_time
!= USEC_INFINITY
) {
541 if (t
->userspace_time
> 0)
542 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
));
544 size
= strpcpyf(&ptr
, size
, "\n%s reached after %s in userspace", unit_id
, format_timespan(ts
, sizeof(ts
), activated_time
- t
->reverse_offset
, USEC_PER_MSEC
));
545 } else if (unit_id
&& activated_time
== 0)
546 size
= strpcpyf(&ptr
, size
, "\n%s was never reached", unit_id
);
547 else if (unit_id
&& activated_time
== USEC_INFINITY
)
548 size
= strpcpyf(&ptr
, size
, "\nCould not get time to reach %s.", unit_id
);
550 size
= strpcpyf(&ptr
, size
, "\ncould not find default.target");
560 static void svg_graph_box(double height
, double begin
, double end
) {
563 /* outside box, fill */
564 svg("<rect class=\"box\" x=\"0\" y=\"0\" width=\"%.03f\" height=\"%.03f\" />\n",
565 SCALE_X
* (end
- begin
), SCALE_Y
* height
);
567 for (i
= ((long long) (begin
/ 100000)) * 100000; i
<= end
; i
+=100000) {
568 /* lines for each second */
569 if (i
% 5000000 == 0)
570 svg(" <line class=\"sec5\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
571 " <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
572 SCALE_X
* i
, SCALE_X
* i
, SCALE_Y
* height
, SCALE_X
* i
, -5.0, 0.000001 * i
);
573 else if (i
% 1000000 == 0)
574 svg(" <line class=\"sec1\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
575 " <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
576 SCALE_X
* i
, SCALE_X
* i
, SCALE_Y
* height
, SCALE_X
* i
, -5.0, 0.000001 * i
);
578 svg(" <line class=\"sec01\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
579 SCALE_X
* i
, SCALE_X
* i
, SCALE_Y
* height
);
583 static int plot_unit_times(struct unit_times
*u
, double width
, int y
) {
584 char ts
[FORMAT_TIMESPAN_MAX
];
590 svg_bar("activating", u
->activating
, u
->activated
, y
);
591 svg_bar("active", u
->activated
, u
->deactivating
, y
);
592 svg_bar("deactivating", u
->deactivating
, u
->deactivated
, y
);
594 /* place the text on the left if we have passed the half of the svg width */
595 b
= u
->activating
* SCALE_X
< width
/ 2;
597 svg_text(b
, u
->activating
, y
, "%s (%s)",
598 u
->name
, format_timespan(ts
, sizeof(ts
), u
->time
, USEC_PER_MSEC
));
600 svg_text(b
, u
->activating
, y
, "%s", u
->name
);
605 static int analyze_plot(int argc
, char *argv
[], void *userdata
) {
606 _cleanup_(free_host_infop
) struct host_info
*host
= NULL
;
607 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
608 _cleanup_(unit_times_freep
) struct unit_times
*times
= NULL
;
609 _cleanup_free_
char *pretty_times
= NULL
;
610 bool use_full_bus
= arg_scope
== UNIT_FILE_SYSTEM
;
611 struct boot_times
*boot
;
612 struct unit_times
*u
;
613 int n
, m
= 1, y
= 0, r
;
616 r
= acquire_bus(&bus
, &use_full_bus
);
618 return log_error_errno(r
, "Failed to create bus connection: %m");
620 n
= acquire_boot_times(bus
, &boot
);
624 n
= pretty_boot_time(bus
, &pretty_times
);
628 if (use_full_bus
|| arg_scope
!= UNIT_FILE_SYSTEM
) {
629 n
= acquire_host_info(bus
, &host
);
634 n
= acquire_time_data(bus
, ×
);
638 qsort(times
, n
, sizeof(struct unit_times
), compare_unit_start
);
640 width
= SCALE_X
* (boot
->firmware_time
+ boot
->finish_time
);
644 if (boot
->firmware_time
> boot
->loader_time
)
646 if (boot
->loader_time
> 0) {
651 if (boot
->initrd_time
> 0)
653 if (boot
->kernel_time
> 0)
656 for (u
= times
; u
->has_data
; u
++) {
657 double text_start
, text_width
;
659 if (u
->activating
> boot
->finish_time
) {
660 u
->name
= mfree(u
->name
);
664 /* If the text cannot fit on the left side then
665 * increase the svg width so it fits on the right.
666 * TODO: calculate the text width more accurately */
667 text_width
= 8.0 * strlen(u
->name
);
668 text_start
= (boot
->firmware_time
+ u
->activating
) * SCALE_X
;
669 if (text_width
> text_start
&& text_width
+ text_start
> width
)
670 width
= text_width
+ text_start
;
672 if (u
->deactivated
> u
->activating
&&
673 u
->deactivated
<= boot
->finish_time
&&
674 u
->activated
== 0 && u
->deactivating
== 0)
675 u
->activated
= u
->deactivating
= u
->deactivated
;
676 if (u
->activated
< u
->activating
|| u
->activated
> boot
->finish_time
)
677 u
->activated
= boot
->finish_time
;
678 if (u
->deactivating
< u
->activated
|| u
->deactivating
> boot
->finish_time
)
679 u
->deactivating
= boot
->finish_time
;
680 if (u
->deactivated
< u
->deactivating
|| u
->deactivated
> boot
->finish_time
)
681 u
->deactivated
= boot
->finish_time
;
685 svg("<?xml version=\"1.0\" standalone=\"no\"?>\n"
686 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
687 "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
689 svg("<svg width=\"%.0fpx\" height=\"%.0fpx\" version=\"1.1\" "
690 "xmlns=\"http://www.w3.org/2000/svg\">\n\n",
691 80.0 + width
, 150.0 + (m
* SCALE_Y
) +
692 5 * SCALE_Y
/* legend */);
694 /* write some basic info as a comment, including some help */
695 svg("<!-- This file is a systemd-analyze SVG file. It is best rendered in a -->\n"
696 "<!-- browser such as Chrome, Chromium or Firefox. Other applications -->\n"
697 "<!-- that render these files properly but much slower are ImageMagick, -->\n"
698 "<!-- gimp, inkscape, etc. To display the files on your system, just -->\n"
699 "<!-- point your browser to this file. -->\n\n"
700 "<!-- This plot was generated by systemd-analyze version %-16.16s -->\n\n", PACKAGE_VERSION
);
703 svg("<defs>\n <style type=\"text/css\">\n <![CDATA[\n"
704 " rect { stroke-width: 1; stroke-opacity: 0; }\n"
705 " rect.background { fill: rgb(255,255,255); }\n"
706 " rect.activating { fill: rgb(255,0,0); fill-opacity: 0.7; }\n"
707 " rect.active { fill: rgb(200,150,150); fill-opacity: 0.7; }\n"
708 " rect.deactivating { fill: rgb(150,100,100); fill-opacity: 0.7; }\n"
709 " rect.kernel { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
710 " rect.initrd { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
711 " rect.firmware { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
712 " rect.loader { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
713 " rect.userspace { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
714 " rect.security { fill: rgb(144,238,144); fill-opacity: 0.7; }\n"
715 " rect.generators { fill: rgb(102,204,255); fill-opacity: 0.7; }\n"
716 " rect.unitsload { fill: rgb( 82,184,255); fill-opacity: 0.7; }\n"
717 " rect.box { fill: rgb(240,240,240); stroke: rgb(192,192,192); }\n"
718 " line { stroke: rgb(64,64,64); stroke-width: 1; }\n"
720 " line.sec5 { stroke-width: 2; }\n"
721 " line.sec01 { stroke: rgb(224,224,224); stroke-width: 1; }\n"
722 " text { font-family: Verdana, Helvetica; font-size: 14px; }\n"
723 " text.left { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: start; }\n"
724 " text.right { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: end; }\n"
725 " text.sec { font-size: 10px; }\n"
726 " ]]>\n </style>\n</defs>\n\n");
728 svg("<rect class=\"background\" width=\"100%%\" height=\"100%%\" />\n");
729 svg("<text x=\"20\" y=\"50\">%s</text>", pretty_times
);
731 svg("<text x=\"20\" y=\"30\">%s %s (%s %s %s) %s %s</text>",
732 isempty(host
->os_pretty_name
) ? "Linux" : host
->os_pretty_name
,
733 strempty(host
->hostname
),
734 strempty(host
->kernel_name
),
735 strempty(host
->kernel_release
),
736 strempty(host
->kernel_version
),
737 strempty(host
->architecture
),
738 strempty(host
->virtualization
));
740 svg("<g transform=\"translate(%.3f,100)\">\n", 20.0 + (SCALE_X
* boot
->firmware_time
));
741 svg_graph_box(m
, -(double) boot
->firmware_time
, boot
->finish_time
);
743 if (boot
->firmware_time
> 0) {
744 svg_bar("firmware", -(double) boot
->firmware_time
, -(double) boot
->loader_time
, y
);
745 svg_text(true, -(double) boot
->firmware_time
, y
, "firmware");
748 if (boot
->loader_time
> 0) {
749 svg_bar("loader", -(double) boot
->loader_time
, 0, y
);
750 svg_text(true, -(double) boot
->loader_time
, y
, "loader");
753 if (boot
->kernel_time
> 0) {
754 svg_bar("kernel", 0, boot
->kernel_done_time
, y
);
755 svg_text(true, 0, y
, "kernel");
758 if (boot
->initrd_time
> 0) {
759 svg_bar("initrd", boot
->initrd_time
, boot
->userspace_time
, y
);
760 if (boot
->initrd_security_start_time
< boot
->initrd_security_finish_time
)
761 svg_bar("security", boot
->initrd_security_start_time
, boot
->initrd_security_finish_time
, y
);
762 if (boot
->initrd_generators_start_time
< boot
->initrd_generators_finish_time
)
763 svg_bar("generators", boot
->initrd_generators_start_time
, boot
->initrd_generators_finish_time
, y
);
764 if (boot
->initrd_unitsload_start_time
< boot
->initrd_unitsload_finish_time
)
765 svg_bar("unitsload", boot
->initrd_unitsload_start_time
, boot
->initrd_unitsload_finish_time
, y
);
766 svg_text(true, boot
->initrd_time
, y
, "initrd");
770 for (u
= times
; u
->has_data
; u
++) {
771 if (u
->activating
>= boot
->userspace_time
)
774 y
+= plot_unit_times(u
, width
, y
);
777 svg_bar("active", boot
->userspace_time
, boot
->finish_time
, y
);
778 if (boot
->security_start_time
> 0)
779 svg_bar("security", boot
->security_start_time
, boot
->security_finish_time
, y
);
780 svg_bar("generators", boot
->generators_start_time
, boot
->generators_finish_time
, y
);
781 svg_bar("unitsload", boot
->unitsload_start_time
, boot
->unitsload_finish_time
, y
);
782 svg_text(true, boot
->userspace_time
, y
, "systemd");
785 for (; u
->has_data
; u
++)
786 y
+= plot_unit_times(u
, width
, y
);
791 svg("<g transform=\"translate(20,100)\">\n");
793 svg_bar("activating", 0, 300000, y
);
794 svg_text(true, 400000, y
, "Activating");
796 svg_bar("active", 0, 300000, y
);
797 svg_text(true, 400000, y
, "Active");
799 svg_bar("deactivating", 0, 300000, y
);
800 svg_text(true, 400000, y
, "Deactivating");
802 if (boot
->security_start_time
> 0) {
803 svg_bar("security", 0, 300000, y
);
804 svg_text(true, 400000, y
, "Setting up security module");
807 svg_bar("generators", 0, 300000, y
);
808 svg_text(true, 400000, y
, "Generators");
810 svg_bar("unitsload", 0, 300000, y
);
811 svg_text(true, 400000, y
, "Loading unit files");
821 static int list_dependencies_print(const char *name
, unsigned int level
, unsigned int branches
,
822 bool last
, struct unit_times
*times
, struct boot_times
*boot
) {
824 char ts
[FORMAT_TIMESPAN_MAX
], ts2
[FORMAT_TIMESPAN_MAX
];
826 for (i
= level
; i
!= 0; i
--)
827 printf("%s", special_glyph(branches
& (1 << (i
-1)) ? TREE_VERTICAL
: TREE_SPACE
));
829 printf("%s", special_glyph(last
? TREE_RIGHT
: TREE_BRANCH
));
833 printf("%s%s @%s +%s%s", ansi_highlight_red(), name
,
834 format_timespan(ts
, sizeof(ts
), times
->activating
- boot
->userspace_time
, USEC_PER_MSEC
),
835 format_timespan(ts2
, sizeof(ts2
), times
->time
, USEC_PER_MSEC
), ansi_normal());
836 else if (times
->activated
> boot
->userspace_time
)
837 printf("%s @%s", name
, format_timespan(ts
, sizeof(ts
), times
->activated
- boot
->userspace_time
, USEC_PER_MSEC
));
847 static int list_dependencies_get_dependencies(sd_bus
*bus
, const char *name
, char ***deps
) {
848 _cleanup_free_
char *path
= NULL
;
854 path
= unit_dbus_path_from_name(name
);
858 return bus_get_unit_property_strv(bus
, path
, "After", deps
);
861 static Hashmap
*unit_times_hashmap
;
863 static int list_dependencies_compare(const void *_a
, const void *_b
) {
864 const char **a
= (const char**) _a
, **b
= (const char**) _b
;
865 usec_t usa
= 0, usb
= 0;
866 struct unit_times
*times
;
868 times
= hashmap_get(unit_times_hashmap
, *a
);
870 usa
= times
->activated
;
871 times
= hashmap_get(unit_times_hashmap
, *b
);
873 usb
= times
->activated
;
878 static bool times_in_range(const struct unit_times
*times
, const struct boot_times
*boot
) {
880 times
->activated
> 0 && times
->activated
<= boot
->finish_time
;
883 static int list_dependencies_one(sd_bus
*bus
, const char *name
, unsigned int level
, char ***units
,
884 unsigned int branches
) {
885 _cleanup_strv_free_
char **deps
= NULL
;
888 usec_t service_longest
= 0;
890 struct unit_times
*times
;
891 struct boot_times
*boot
;
893 if (strv_extend(units
, name
))
896 r
= list_dependencies_get_dependencies(bus
, name
, &deps
);
900 qsort_safe(deps
, strv_length(deps
), sizeof (char*), list_dependencies_compare
);
902 r
= acquire_boot_times(bus
, &boot
);
906 STRV_FOREACH(c
, deps
) {
907 times
= hashmap_get(unit_times_hashmap
, *c
);
908 if (times_in_range(times
, boot
) &&
909 times
->activated
>= service_longest
)
910 service_longest
= times
->activated
;
913 if (service_longest
== 0)
916 STRV_FOREACH(c
, deps
) {
917 times
= hashmap_get(unit_times_hashmap
, *c
);
918 if (times_in_range(times
, boot
) &&
919 service_longest
- times
->activated
<= arg_fuzz
)
926 STRV_FOREACH(c
, deps
) {
927 times
= hashmap_get(unit_times_hashmap
, *c
);
928 if (!times_in_range(times
, boot
) ||
929 service_longest
- times
->activated
> arg_fuzz
)
934 r
= list_dependencies_print(*c
, level
, branches
, to_print
== 0, times
, boot
);
938 if (strv_contains(*units
, *c
)) {
939 r
= list_dependencies_print("...", level
+ 1, (branches
<< 1) | (to_print
? 1 : 0),
946 r
= list_dependencies_one(bus
, *c
, level
+ 1, units
,
947 (branches
<< 1) | (to_print
? 1 : 0));
957 static int list_dependencies(sd_bus
*bus
, const char *name
) {
958 _cleanup_strv_free_
char **units
= NULL
;
959 char ts
[FORMAT_TIMESPAN_MAX
];
960 struct unit_times
*times
;
963 _cleanup_free_
char *path
= NULL
;
964 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
965 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
966 struct boot_times
*boot
;
970 path
= unit_dbus_path_from_name(name
);
974 r
= sd_bus_get_property(
976 "org.freedesktop.systemd1",
978 "org.freedesktop.systemd1.Unit",
984 log_error("Failed to get ID: %s", bus_error_message(&error
, -r
));
988 r
= sd_bus_message_read(reply
, "s", &id
);
990 return bus_log_parse_error(r
);
992 times
= hashmap_get(unit_times_hashmap
, id
);
994 r
= acquire_boot_times(bus
, &boot
);
1000 printf("%s%s +%s%s\n", ansi_highlight_red(), id
,
1001 format_timespan(ts
, sizeof(ts
), times
->time
, USEC_PER_MSEC
), ansi_normal());
1002 else if (times
->activated
> boot
->userspace_time
)
1003 printf("%s @%s\n", id
, format_timespan(ts
, sizeof(ts
), times
->activated
- boot
->userspace_time
, USEC_PER_MSEC
));
1008 return list_dependencies_one(bus
, name
, 0, &units
, 0);
1011 static int analyze_critical_chain(int argc
, char *argv
[], void *userdata
) {
1012 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1013 _cleanup_(unit_times_freep
) struct unit_times
*times
= NULL
;
1014 struct unit_times
*u
;
1018 r
= acquire_bus(&bus
, NULL
);
1020 return log_error_errno(r
, "Failed to create bus connection: %m");
1022 n
= acquire_time_data(bus
, ×
);
1026 h
= hashmap_new(&string_hash_ops
);
1030 for (u
= times
; u
->has_data
; u
++) {
1031 r
= hashmap_put(h
, u
->name
, u
);
1033 return log_error_errno(r
, "Failed to add entry to hashmap: %m");
1035 unit_times_hashmap
= h
;
1037 (void) pager_open(arg_no_pager
, false);
1039 puts("The time after the unit is active or started is printed after the \"@\" character.\n"
1040 "The time the unit takes to start is printed after the \"+\" character.\n");
1044 STRV_FOREACH(name
, strv_skip(argv
, 1))
1045 list_dependencies(bus
, *name
);
1047 list_dependencies(bus
, SPECIAL_DEFAULT_TARGET
);
1049 h
= hashmap_free(h
);
1053 static int analyze_blame(int argc
, char *argv
[], void *userdata
) {
1054 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1055 _cleanup_(unit_times_freep
) struct unit_times
*times
= NULL
;
1056 struct unit_times
*u
;
1059 r
= acquire_bus(&bus
, NULL
);
1061 return log_error_errno(r
, "Failed to create bus connection: %m");
1063 n
= acquire_time_data(bus
, ×
);
1067 qsort(times
, n
, sizeof(struct unit_times
), compare_unit_time
);
1069 (void) pager_open(arg_no_pager
, false);
1071 for (u
= times
; u
->has_data
; u
++) {
1072 char ts
[FORMAT_TIMESPAN_MAX
];
1075 printf("%16s %s\n", format_timespan(ts
, sizeof(ts
), u
->time
, USEC_PER_MSEC
), u
->name
);
1081 static int analyze_time(int argc
, char *argv
[], void *userdata
) {
1082 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1083 _cleanup_free_
char *buf
= NULL
;
1086 r
= acquire_bus(&bus
, NULL
);
1088 return log_error_errno(r
, "Failed to create bus connection: %m");
1090 r
= pretty_boot_time(bus
, &buf
);
1098 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
[]) {
1099 _cleanup_strv_free_
char **units
= NULL
;
1102 bool match_patterns
;
1108 match_patterns
= strv_fnmatch(patterns
, u
->id
, 0);
1110 if (!strv_isempty(from_patterns
) &&
1112 !strv_fnmatch(from_patterns
, u
->id
, 0))
1115 r
= bus_get_unit_property_strv(bus
, u
->unit_path
, prop
, &units
);
1119 STRV_FOREACH(unit
, units
) {
1120 bool match_patterns2
;
1122 match_patterns2
= strv_fnmatch(patterns
, *unit
, 0);
1124 if (!strv_isempty(to_patterns
) &&
1126 !strv_fnmatch(to_patterns
, *unit
, 0))
1129 if (!strv_isempty(patterns
) && !match_patterns
&& !match_patterns2
)
1132 printf("\t\"%s\"->\"%s\" [color=\"%s\"];\n", u
->id
, *unit
, color
);
1138 static int graph_one(sd_bus
*bus
, const UnitInfo
*u
, char *patterns
[], char *from_patterns
[], char *to_patterns
[]) {
1144 if (IN_SET(arg_dot
, DEP_ORDER
, DEP_ALL
)) {
1145 r
= graph_one_property(bus
, u
, "After", "green", patterns
, from_patterns
, to_patterns
);
1150 if (IN_SET(arg_dot
, DEP_REQUIRE
, DEP_ALL
)) {
1151 r
= graph_one_property(bus
, u
, "Requires", "black", patterns
, from_patterns
, to_patterns
);
1154 r
= graph_one_property(bus
, u
, "Requisite", "darkblue", patterns
, from_patterns
, to_patterns
);
1157 r
= graph_one_property(bus
, u
, "Wants", "grey66", patterns
, from_patterns
, to_patterns
);
1160 r
= graph_one_property(bus
, u
, "Conflicts", "red", patterns
, from_patterns
, to_patterns
);
1168 static int expand_patterns(sd_bus
*bus
, char **patterns
, char ***ret
) {
1169 _cleanup_strv_free_
char **expanded_patterns
= NULL
;
1173 STRV_FOREACH(pattern
, patterns
) {
1174 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1175 _cleanup_free_
char *unit
= NULL
, *unit_id
= NULL
;
1177 if (strv_extend(&expanded_patterns
, *pattern
) < 0)
1180 if (string_is_glob(*pattern
))
1183 unit
= unit_dbus_path_from_name(*pattern
);
1187 r
= sd_bus_get_property_string(
1189 "org.freedesktop.systemd1",
1191 "org.freedesktop.systemd1.Unit",
1196 return log_error_errno(r
, "Failed to get ID: %s", bus_error_message(&error
, r
));
1198 if (!streq(*pattern
, unit_id
)) {
1199 if (strv_extend(&expanded_patterns
, unit_id
) < 0)
1204 *ret
= expanded_patterns
;
1205 expanded_patterns
= NULL
; /* do not free */
1210 static int dot(int argc
, char *argv
[], void *userdata
) {
1211 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1212 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1213 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1214 _cleanup_strv_free_
char **expanded_patterns
= NULL
;
1215 _cleanup_strv_free_
char **expanded_from_patterns
= NULL
;
1216 _cleanup_strv_free_
char **expanded_to_patterns
= NULL
;
1220 r
= acquire_bus(&bus
, NULL
);
1222 return log_error_errno(r
, "Failed to create bus connection: %m");
1224 r
= expand_patterns(bus
, strv_skip(argv
, 1), &expanded_patterns
);
1228 r
= expand_patterns(bus
, arg_dot_from_patterns
, &expanded_from_patterns
);
1232 r
= expand_patterns(bus
, arg_dot_to_patterns
, &expanded_to_patterns
);
1236 r
= sd_bus_call_method(
1238 "org.freedesktop.systemd1",
1239 "/org/freedesktop/systemd1",
1240 "org.freedesktop.systemd1.Manager",
1246 log_error("Failed to list units: %s", bus_error_message(&error
, -r
));
1250 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssssouso)");
1252 return bus_log_parse_error(r
);
1254 printf("digraph systemd {\n");
1256 while ((r
= bus_parse_unit_info(reply
, &u
)) > 0) {
1258 r
= graph_one(bus
, &u
, expanded_patterns
, expanded_from_patterns
, expanded_to_patterns
);
1263 return bus_log_parse_error(r
);
1267 log_info(" Color legend: black = Requires\n"
1268 " dark blue = Requisite\n"
1269 " dark grey = Wants\n"
1270 " red = Conflicts\n"
1271 " green = After\n");
1274 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
1275 "-- Try a shell pipeline like 'systemd-analyze dot | dot -Tsvg > systemd.svg'!\n");
1280 static int dump_fallback(sd_bus
*bus
) {
1281 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1282 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1283 const char *text
= NULL
;
1288 r
= sd_bus_call_method(
1290 "org.freedesktop.systemd1",
1291 "/org/freedesktop/systemd1",
1292 "org.freedesktop.systemd1.Manager",
1298 return log_error_errno(r
, "Failed to issue method call Dump: %s", bus_error_message(&error
, r
));
1300 r
= sd_bus_message_read(reply
, "s", &text
);
1302 return bus_log_parse_error(r
);
1304 fputs(text
, stdout
);
1308 static int dump(int argc
, char *argv
[], void *userdata
) {
1309 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1310 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1311 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1315 r
= acquire_bus(&bus
, NULL
);
1317 return log_error_errno(r
, "Failed to create bus connection: %m");
1319 (void) pager_open(arg_no_pager
, false);
1321 if (!sd_bus_can_send(bus
, SD_BUS_TYPE_UNIX_FD
))
1322 return dump_fallback(bus
);
1324 r
= sd_bus_call_method(
1326 "org.freedesktop.systemd1",
1327 "/org/freedesktop/systemd1",
1328 "org.freedesktop.systemd1.Manager",
1329 "DumpByFileDescriptor",
1334 /* fall back to Dump if DumpByFileDescriptor is not supported */
1335 if (!IN_SET(r
, -EACCES
, -EBADR
))
1336 return log_error_errno(r
, "Failed to issue method call DumpByFileDescriptor: %s", bus_error_message(&error
, r
));
1338 return dump_fallback(bus
);
1341 r
= sd_bus_message_read(reply
, "h", &fd
);
1343 return bus_log_parse_error(r
);
1346 return copy_bytes(fd
, STDOUT_FILENO
, (uint64_t) -1, 0);
1349 static int cat_config(int argc
, char *argv
[], void *userdata
) {
1353 (void) pager_open(arg_no_pager
, false);
1355 STRV_FOREACH(arg
, argv
+ 1) {
1356 const char *t
= NULL
;
1358 if (arg
!= argv
+ 1)
1361 if (path_is_absolute(*arg
)) {
1364 NULSTR_FOREACH(dir
, CONF_PATHS_NULSTR("")) {
1365 t
= path_startswith(*arg
, dir
);
1371 log_error("Path %s does not start with any known prefix.", *arg
);
1377 r
= conf_files_cat(arg_root
, t
);
1385 static int set_log_level(int argc
, char *argv
[], void *userdata
) {
1386 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1387 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1393 r
= acquire_bus(&bus
, NULL
);
1395 return log_error_errno(r
, "Failed to create bus connection: %m");
1397 r
= sd_bus_set_property(
1399 "org.freedesktop.systemd1",
1400 "/org/freedesktop/systemd1",
1401 "org.freedesktop.systemd1.Manager",
1407 return log_error_errno(r
, "Failed to issue method call: %s", bus_error_message(&error
, r
));
1412 static int get_log_level(int argc
, char *argv
[], void *userdata
) {
1413 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1414 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1415 _cleanup_free_
char *level
= NULL
;
1418 r
= acquire_bus(&bus
, NULL
);
1420 return log_error_errno(r
, "Failed to create bus connection: %m");
1422 r
= sd_bus_get_property_string(
1424 "org.freedesktop.systemd1",
1425 "/org/freedesktop/systemd1",
1426 "org.freedesktop.systemd1.Manager",
1431 return log_error_errno(r
, "Failed to get log level: %s", bus_error_message(&error
, r
));
1437 static 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
);
1441 static int set_log_target(int argc
, char *argv
[], void *userdata
) {
1442 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1443 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1449 r
= acquire_bus(&bus
, NULL
);
1451 return log_error_errno(r
, "Failed to create bus connection: %m");
1453 r
= sd_bus_set_property(
1455 "org.freedesktop.systemd1",
1456 "/org/freedesktop/systemd1",
1457 "org.freedesktop.systemd1.Manager",
1463 return log_error_errno(r
, "Failed to issue method call: %s", bus_error_message(&error
, r
));
1468 static int get_log_target(int argc
, char *argv
[], void *userdata
) {
1469 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1470 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1471 _cleanup_free_
char *target
= NULL
;
1474 r
= acquire_bus(&bus
, NULL
);
1476 return log_error_errno(r
, "Failed to create bus connection: %m");
1478 r
= sd_bus_get_property_string(
1480 "org.freedesktop.systemd1",
1481 "/org/freedesktop/systemd1",
1482 "org.freedesktop.systemd1.Manager",
1487 return log_error_errno(r
, "Failed to get log target: %s", bus_error_message(&error
, r
));
1493 static 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
);
1497 static int dump_unit_paths(int argc
, char *argv
[], void *userdata
) {
1498 _cleanup_(lookup_paths_free
) LookupPaths paths
= {};
1502 r
= lookup_paths_init(&paths
, arg_scope
, 0, NULL
);
1504 return log_error_errno(r
, "lookup_paths_init() failed: %m");
1506 STRV_FOREACH(p
, paths
.search_path
)
1513 static void dump_syscall_filter(const SyscallFilterSet
*set
) {
1514 const char *syscall
;
1516 printf("%s\n", set
->name
);
1517 printf(" # %s\n", set
->help
);
1518 NULSTR_FOREACH(syscall
, set
->value
)
1519 printf(" %s\n", syscall
);
1522 static int dump_syscall_filters(int argc
, char *argv
[], void *userdata
) {
1525 (void) pager_open(arg_no_pager
, false);
1527 if (strv_isempty(strv_skip(argv
, 1))) {
1530 for (i
= 0; i
< _SYSCALL_FILTER_SET_MAX
; i
++) {
1533 dump_syscall_filter(syscall_filter_sets
+ i
);
1539 STRV_FOREACH(name
, strv_skip(argv
, 1)) {
1540 const SyscallFilterSet
*set
;
1545 set
= syscall_filter_set_find(*name
);
1547 /* make sure the error appears below normal output */
1550 log_error("Filter set \"%s\" not found.", *name
);
1554 dump_syscall_filter(set
);
1563 static int dump_syscall_filters(int argc
, char *argv
[], void *userdata
) {
1564 log_error("Not compiled with syscall filters, sorry.");
1569 static int test_calendar(int argc
, char *argv
[], void *userdata
) {
1574 n
= now(CLOCK_REALTIME
);
1576 STRV_FOREACH(p
, strv_skip(argv
, 1)) {
1577 _cleanup_(calendar_spec_freep
) CalendarSpec
*spec
= NULL
;
1578 _cleanup_free_
char *t
= NULL
;
1581 r
= calendar_spec_from_string(*p
, &spec
);
1583 ret
= log_error_errno(r
, "Failed to parse calendar specification '%s': %m", *p
);
1587 r
= calendar_spec_normalize(spec
);
1589 ret
= log_error_errno(r
, "Failed to normalize calendar specification '%s': %m", *p
);
1593 r
= calendar_spec_to_string(spec
, &t
);
1595 ret
= log_error_errno(r
, "Failed to format calendar specification '%s': %m", *p
);
1600 printf(" Original form: %s\n", *p
);
1602 printf("Normalized form: %s\n", t
);
1604 r
= calendar_spec_next_usec(spec
, n
, &next
);
1606 printf(" Next elapse: never\n");
1608 ret
= log_error_errno(r
, "Failed to determine next elapse for '%s': %m", *p
);
1611 char buffer
[CONST_MAX(FORMAT_TIMESTAMP_MAX
, FORMAT_TIMESTAMP_RELATIVE_MAX
)];
1613 printf(" Next elapse: %s\n", format_timestamp(buffer
, sizeof(buffer
), next
));
1615 if (!in_utc_timezone())
1616 printf(" (in UTC): %s\n", format_timestamp_utc(buffer
, sizeof(buffer
), next
));
1618 printf(" From now: %s\n", format_timestamp_relative(buffer
, sizeof(buffer
), next
));
1628 static int service_watchdogs(int argc
, char *argv
[], void *userdata
) {
1629 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1630 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1633 assert(IN_SET(argc
, 1, 2));
1636 r
= acquire_bus(&bus
, NULL
);
1638 return log_error_errno(r
, "Failed to create bus connection: %m");
1640 /* get ServiceWatchdogs */
1642 r
= sd_bus_get_property_trivial(
1644 "org.freedesktop.systemd1",
1645 "/org/freedesktop/systemd1",
1646 "org.freedesktop.systemd1.Manager",
1652 return log_error_errno(r
, "Failed to get service-watchdog state: %s", bus_error_message(&error
, r
));
1654 printf("%s\n", yes_no(!!b
));
1659 /* set ServiceWatchdogs */
1660 b
= parse_boolean(argv
[1]);
1662 log_error("Failed to parse service-watchdogs argument.");
1666 r
= sd_bus_set_property(
1668 "org.freedesktop.systemd1",
1669 "/org/freedesktop/systemd1",
1670 "org.freedesktop.systemd1.Manager",
1676 return log_error_errno(r
, "Failed to set service-watchdog state: %s", bus_error_message(&error
, r
));
1681 static int do_verify(int argc
, char *argv
[], void *userdata
) {
1682 return verify_units(strv_skip(argv
, 1), arg_scope
, arg_man
, arg_generators
);
1685 static int help(int argc
, char *argv
[], void *userdata
) {
1687 (void) pager_open(arg_no_pager
, false);
1689 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1690 "Profile systemd, show unit dependencies, check unit files.\n\n"
1691 " -h --help Show this help\n"
1692 " --version Show package version\n"
1693 " --no-pager Do not pipe output into a pager\n"
1694 " --system Operate on system systemd instance\n"
1695 " --user Operate on user systemd instance\n"
1696 " --global Operate on global user configuration\n"
1697 " -H --host=[USER@]HOST Operate on remote host\n"
1698 " -M --machine=CONTAINER Operate on local container\n"
1699 " --order Show only order in the graph\n"
1700 " --require Show only requirement in the graph\n"
1701 " --from-pattern=GLOB Show only origins in the graph\n"
1702 " --to-pattern=GLOB Show only destinations in the graph\n"
1703 " --fuzz=SECONDS Also print also services which finished SECONDS\n"
1704 " earlier than the latest in the branch\n"
1705 " --man[=BOOL] Do [not] check for existence of man pages\n\n"
1706 " --generators[=BOOL] Do [not] run unit generators (requires privileges)\n\n"
1708 " time Print time spent in the kernel\n"
1709 " blame Print list of running units ordered by time to init\n"
1710 " critical-chain [UNIT...] Print a tree of the time critical chain of units\n"
1711 " plot Output SVG graphic showing service initialization\n"
1712 " dot [UNIT...] Output dependency graph in man:dot(1) format\n"
1713 " log-level [LEVEL] Get/set logging threshold for manager\n"
1714 " log-target [TARGET] Get/set logging target for manager\n"
1715 " dump Output state serialization of service manager\n"
1716 " cat-config Show configuration file and drop-ins\n"
1717 " unit-paths List load directories for units\n"
1718 " syscall-filter [NAME...] Print list of syscalls in seccomp filter\n"
1719 " verify FILE... Check unit files for correctness\n"
1720 " calendar SPEC... Validate repetitive calendar time events\n"
1721 " service-watchdogs [BOOL] Get/set service watchdog state\n"
1722 , program_invocation_short_name
);
1724 /* When updating this list, including descriptions, apply
1725 * changes to shell-completion/bash/systemd-analyze and
1726 * shell-completion/zsh/_systemd-analyze too. */
1731 static int parse_argv(int argc
, char *argv
[]) {
1733 ARG_VERSION
= 0x100,
1740 ARG_DOT_FROM_PATTERN
,
1748 static const struct option options
[] = {
1749 { "help", no_argument
, NULL
, 'h' },
1750 { "version", no_argument
, NULL
, ARG_VERSION
},
1751 { "order", no_argument
, NULL
, ARG_ORDER
},
1752 { "require", no_argument
, NULL
, ARG_REQUIRE
},
1753 { "root", required_argument
, NULL
, ARG_ROOT
},
1754 { "system", no_argument
, NULL
, ARG_SYSTEM
},
1755 { "user", no_argument
, NULL
, ARG_USER
},
1756 { "global", no_argument
, NULL
, ARG_GLOBAL
},
1757 { "from-pattern", required_argument
, NULL
, ARG_DOT_FROM_PATTERN
},
1758 { "to-pattern", required_argument
, NULL
, ARG_DOT_TO_PATTERN
},
1759 { "fuzz", required_argument
, NULL
, ARG_FUZZ
},
1760 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1761 { "man", optional_argument
, NULL
, ARG_MAN
},
1762 { "generators", optional_argument
, NULL
, ARG_GENERATORS
},
1763 { "host", required_argument
, NULL
, 'H' },
1764 { "machine", required_argument
, NULL
, 'M' },
1773 while ((c
= getopt_long(argc
, argv
, "hH:M:", options
, NULL
)) >= 0)
1777 return help(0, NULL
, NULL
);
1787 arg_scope
= UNIT_FILE_SYSTEM
;
1791 arg_scope
= UNIT_FILE_USER
;
1795 arg_scope
= UNIT_FILE_GLOBAL
;
1799 arg_dot
= DEP_ORDER
;
1803 arg_dot
= DEP_REQUIRE
;
1806 case ARG_DOT_FROM_PATTERN
:
1807 if (strv_extend(&arg_dot_from_patterns
, optarg
) < 0)
1812 case ARG_DOT_TO_PATTERN
:
1813 if (strv_extend(&arg_dot_to_patterns
, optarg
) < 0)
1819 r
= parse_sec(optarg
, &arg_fuzz
);
1825 arg_no_pager
= true;
1829 arg_transport
= BUS_TRANSPORT_REMOTE
;
1834 arg_transport
= BUS_TRANSPORT_MACHINE
;
1840 r
= parse_boolean(optarg
);
1842 log_error("Failed to parse --man= argument.");
1852 case ARG_GENERATORS
:
1854 r
= parse_boolean(optarg
);
1856 log_error("Failed to parse --generators= argument.");
1862 arg_generators
= true;
1870 assert_not_reached("Unhandled option code.");
1873 if (arg_scope
== UNIT_FILE_GLOBAL
&&
1874 !STR_IN_SET(argv
[optind
] ?: "time", "dot", "unit-paths", "verify")) {
1875 log_error("Option --global only makes sense with verbs dot, unit-paths, verify.");
1879 if (arg_root
&& !streq_ptr(argv
[optind
], "cat-config")) {
1880 log_error("Option --root is only supported for cat-config right now.");
1884 return 1; /* work to do */
1887 int main(int argc
, char *argv
[]) {
1889 static const Verb verbs
[] = {
1890 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1891 { "time", VERB_ANY
, 1, VERB_DEFAULT
, analyze_time
},
1892 { "blame", VERB_ANY
, 1, 0, analyze_blame
},
1893 { "critical-chain", VERB_ANY
, VERB_ANY
, 0, analyze_critical_chain
},
1894 { "plot", VERB_ANY
, 1, 0, analyze_plot
},
1895 { "dot", VERB_ANY
, VERB_ANY
, 0, dot
},
1896 { "log-level", VERB_ANY
, 2, 0, get_or_set_log_level
},
1897 { "log-target", VERB_ANY
, 2, 0, get_or_set_log_target
},
1898 /* The following four verbs are deprecated aliases */
1899 { "set-log-level", 2, 2, 0, set_log_level
},
1900 { "get-log-level", VERB_ANY
, 1, 0, get_log_level
},
1901 { "set-log-target", 2, 2, 0, set_log_target
},
1902 { "get-log-target", VERB_ANY
, 1, 0, get_log_target
},
1903 { "dump", VERB_ANY
, 1, 0, dump
},
1904 { "cat-config", 2, VERB_ANY
, 0, cat_config
},
1905 { "unit-paths", 1, 1, 0, dump_unit_paths
},
1906 { "syscall-filter", VERB_ANY
, VERB_ANY
, 0, dump_syscall_filters
},
1907 { "verify", 2, VERB_ANY
, 0, do_verify
},
1908 { "calendar", 2, VERB_ANY
, 0, test_calendar
},
1909 { "service-watchdogs", VERB_ANY
, 2, 0, service_watchdogs
},
1915 setlocale(LC_ALL
, "");
1916 setlocale(LC_NUMERIC
, "C"); /* we want to format/parse floats in C style */
1918 log_parse_environment();
1921 r
= parse_argv(argc
, argv
);
1925 r
= dispatch_verb(argc
, argv
, verbs
, NULL
);
1930 strv_free(arg_dot_from_patterns
);
1931 strv_free(arg_dot_to_patterns
);
1933 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;