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