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