1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 Copyright © 2013 Simon Peeters
14 #include "alloc-util.h"
16 #include "analyze-blame.h"
17 #include "analyze-calendar.h"
18 #include "analyze-capability.h"
19 #include "analyze-cat-config.h"
20 #include "analyze-compare-versions.h"
21 #include "analyze-condition.h"
22 #include "analyze-critical-chain.h"
23 #include "analyze-dot.h"
24 #include "analyze-dump.h"
25 #include "analyze-exit-status.h"
26 #include "analyze-fdstore.h"
27 #include "analyze-filesystems.h"
28 #include "analyze-inspect-elf.h"
29 #include "analyze-log-control.h"
30 #include "analyze-malloc.h"
31 #include "analyze-pcrs.h"
32 #include "analyze-plot.h"
33 #include "analyze-security.h"
34 #include "analyze-service-watchdogs.h"
35 #include "analyze-srk.h"
36 #include "analyze-syscall-filter.h"
37 #include "analyze-time.h"
38 #include "analyze-time-data.h"
39 #include "analyze-timespan.h"
40 #include "analyze-timestamp.h"
41 #include "analyze-unit-files.h"
42 #include "analyze-unit-paths.h"
43 #include "analyze-verify.h"
44 #include "analyze-image-policy.h"
46 #include "bus-error.h"
47 #include "bus-locator.h"
48 #include "bus-map-properties.h"
49 #include "bus-unit-util.h"
50 #include "calendarspec.h"
52 #include "capability-util.h"
53 #include "conf-files.h"
55 #include "constants.h"
56 #include "exit-status.h"
57 #include "extract-word.h"
60 #include "filesystems.h"
61 #include "format-table.h"
62 #include "glob-util.h"
64 #include "locale-util.h"
66 #include "main-func.h"
67 #include "mount-util.h"
68 #include "nulstr-util.h"
70 #include "parse-argument.h"
71 #include "parse-util.h"
72 #include "path-util.h"
73 #include "pretty-print.h"
76 # include "seccomp-util.h"
78 #include "sort-util.h"
80 #include "stat-util.h"
81 #include "string-table.h"
84 #include "terminal-util.h"
85 #include "time-util.h"
86 #include "tmpfile-util.h"
87 #include "unit-name.h"
88 #include "verb-log-control.h"
91 DotMode arg_dot
= DEP_ALL
;
92 char **arg_dot_from_patterns
= NULL
, **arg_dot_to_patterns
= NULL
;
94 PagerFlags arg_pager_flags
= 0;
95 CatFlags arg_cat_flags
= 0;
96 BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
97 const char *arg_host
= NULL
;
98 RuntimeScope arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
99 RecursiveErrors arg_recursive_errors
= _RECURSIVE_ERRORS_INVALID
;
101 bool arg_generators
= false;
102 char *arg_root
= NULL
;
103 static char *arg_image
= NULL
;
104 char *arg_security_policy
= NULL
;
105 bool arg_offline
= false;
106 unsigned arg_threshold
= 100;
107 unsigned arg_iterations
= 1;
108 usec_t arg_base_time
= USEC_INFINITY
;
109 char *arg_unit
= NULL
;
110 JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
111 bool arg_quiet
= false;
112 char *arg_profile
= NULL
;
113 bool arg_legend
= true;
114 bool arg_table
= false;
115 ImagePolicy
*arg_image_policy
= NULL
;
117 STATIC_DESTRUCTOR_REGISTER(arg_dot_from_patterns
, strv_freep
);
118 STATIC_DESTRUCTOR_REGISTER(arg_dot_to_patterns
, strv_freep
);
119 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
120 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
121 STATIC_DESTRUCTOR_REGISTER(arg_security_policy
, freep
);
122 STATIC_DESTRUCTOR_REGISTER(arg_unit
, freep
);
123 STATIC_DESTRUCTOR_REGISTER(arg_profile
, freep
);
124 STATIC_DESTRUCTOR_REGISTER(arg_image_policy
, image_policy_freep
);
126 int acquire_bus(sd_bus
**bus
, bool *use_full_bus
) {
129 if (use_full_bus
&& *use_full_bus
) {
130 r
= bus_connect_transport(arg_transport
, arg_host
, arg_runtime_scope
, bus
);
131 if (IN_SET(r
, 0, -EHOSTDOWN
))
134 *use_full_bus
= false;
137 return bus_connect_transport_systemd(arg_transport
, arg_host
, arg_runtime_scope
, bus
);
140 int bus_get_unit_property_strv(sd_bus
*bus
, const char *path
, const char *property
, char ***strv
) {
141 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
149 r
= sd_bus_get_property_strv(
151 "org.freedesktop.systemd1",
153 "org.freedesktop.systemd1.Unit",
158 return log_error_errno(r
, "Failed to get unit property %s: %s", property
, bus_error_message(&error
, r
));
163 void time_parsing_hint(const char *p
, bool calendar
, bool timestamp
, bool timespan
) {
164 if (calendar
&& calendar_spec_from_string(p
, NULL
) >= 0)
165 log_notice("Hint: this expression is a valid calendar specification. "
166 "Use 'systemd-analyze calendar \"%s\"' instead?", p
);
167 if (timestamp
&& parse_timestamp(p
, NULL
) >= 0)
168 log_notice("Hint: this expression is a valid timestamp. "
169 "Use 'systemd-analyze timestamp \"%s\"' instead?", p
);
170 if (timespan
&& parse_time(p
, NULL
, USEC_PER_SEC
) >= 0)
171 log_notice("Hint: this expression is a valid timespan. "
172 "Use 'systemd-analyze timespan \"%s\"' instead?", p
);
175 int dump_fd_reply(sd_bus_message
*message
) {
180 r
= sd_bus_message_read(message
, "h", &fd
);
182 return bus_log_parse_error(r
);
185 r
= copy_bytes(fd
, STDOUT_FILENO
, UINT64_MAX
, 0);
189 return 1; /* Success */
192 static int help(int argc
, char *argv
[], void *userdata
) {
193 _cleanup_free_
char *link
= NULL
, *dot_link
= NULL
;
196 pager_open(arg_pager_flags
);
198 r
= terminal_urlify_man("systemd-analyze", "1", &link
);
202 /* Not using terminal_urlify_man() for this, since we don't want the "man page" text suffix in this case. */
203 r
= terminal_urlify("man:dot(1)", "dot(1)", &dot_link
);
207 printf("%s [OPTIONS...] COMMAND ...\n\n"
208 "%sProfile systemd, show unit dependencies, check unit files.%s\n"
210 " [time] Print time required to boot the machine\n"
211 " blame Print list of running units ordered by\n"
213 " critical-chain [UNIT...] Print a tree of the time critical chain\n"
215 " plot Output SVG graphic showing service\n"
217 " dot [UNIT...] Output dependency graph in %s format\n"
218 " dump [PATTERN...] Output state serialization of service\n"
220 " cat-config NAME|PATH... Show configuration file and drop-ins\n"
221 " unit-files List files and symlinks for units\n"
222 " unit-paths List load directories for units\n"
223 " exit-status [STATUS...] List exit status definitions\n"
224 " capability [CAP...] List capability definitions\n"
225 " syscall-filter [NAME...] List syscalls in seccomp filters\n"
226 " filesystems [NAME...] List known filesystems\n"
227 " condition CONDITION... Evaluate conditions and asserts\n"
228 " compare-versions VERSION1 [OP] VERSION2\n"
229 " Compare two version strings\n"
230 " verify FILE... Check unit files for correctness\n"
231 " calendar SPEC... Validate repetitive calendar time\n"
233 " timestamp TIMESTAMP... Validate a timestamp\n"
234 " timespan SPAN... Validate a time span\n"
235 " security [UNIT...] Analyze security of unit\n"
236 " inspect-elf FILE... Parse and print ELF package metadata\n"
237 " malloc [D-BUS SERVICE...] Dump malloc stats of a D-Bus service\n"
238 " fdstore SERVICE... Show file descriptor store contents of service\n"
239 " image-policy POLICY... Analyze image policy string\n"
240 " pcrs [PCR...] Show TPM2 PCRs and their names\n"
241 " srk > FILE Write TPM2 SRK to stdout\n"
243 " --recursive-errors=MODE Control which units are verified\n"
244 " --offline=BOOL Perform a security review on unit file(s)\n"
245 " --threshold=N Exit with a non-zero status when overall\n"
246 " exposure level is over threshold value\n"
247 " --security-policy=PATH Use custom JSON security policy instead\n"
249 " --json=pretty|short|off Generate JSON output of the security\n"
250 " analysis table, or plot's raw time data\n"
251 " --no-pager Do not pipe output into a pager\n"
252 " --no-legend Disable column headers and hints in plot\n"
253 " with either --table or --json=\n"
254 " --system Operate on system systemd instance\n"
255 " --user Operate on user systemd instance\n"
256 " --global Operate on global user configuration\n"
257 " -H --host=[USER@]HOST Operate on remote host\n"
258 " -M --machine=CONTAINER Operate on local container\n"
259 " --order Show only order in the graph\n"
260 " --require Show only requirement in the graph\n"
261 " --from-pattern=GLOB Show only origins in the graph\n"
262 " --to-pattern=GLOB Show only destinations in the graph\n"
263 " --fuzz=SECONDS Also print services which finished SECONDS\n"
264 " earlier than the latest in the branch\n"
265 " --man[=BOOL] Do [not] check for existence of man pages\n"
266 " --generators[=BOOL] Do [not] run unit generators\n"
267 " (requires privileges)\n"
268 " --iterations=N Show the specified number of iterations\n"
269 " --base-time=TIMESTAMP Calculate calendar times relative to\n"
271 " --profile=name|PATH Include the specified profile in the\n"
272 " security review of the unit(s)\n"
273 " --table Output plot's raw time data as a table\n"
274 " -h --help Show this help\n"
275 " --version Show package version\n"
276 " -q --quiet Do not emit hints\n"
277 " --tldr Skip comments and empty lines\n"
278 " --root=PATH Operate on an alternate filesystem root\n"
279 " --image=PATH Operate on disk image as filesystem root\n"
280 " --image-policy=POLICY Specify disk image dissection policy\n"
281 "\nSee the %s for details.\n",
282 program_invocation_short_name
,
288 /* When updating this list, including descriptions, apply changes to
289 * shell-completion/bash/systemd-analyze and shell-completion/zsh/_systemd-analyze too. */
294 static int parse_argv(int argc
, char *argv
[]) {
305 ARG_DOT_FROM_PATTERN
,
313 ARG_RECURSIVE_ERRORS
,
324 static const struct option options
[] = {
325 { "help", no_argument
, NULL
, 'h' },
326 { "version", no_argument
, NULL
, ARG_VERSION
},
327 { "quiet", no_argument
, NULL
, 'q' },
328 { "order", no_argument
, NULL
, ARG_ORDER
},
329 { "require", no_argument
, NULL
, ARG_REQUIRE
},
330 { "root", required_argument
, NULL
, ARG_ROOT
},
331 { "image", required_argument
, NULL
, ARG_IMAGE
},
332 { "image-policy", required_argument
, NULL
, ARG_IMAGE_POLICY
},
333 { "recursive-errors", required_argument
, NULL
, ARG_RECURSIVE_ERRORS
},
334 { "offline", required_argument
, NULL
, ARG_OFFLINE
},
335 { "threshold", required_argument
, NULL
, ARG_THRESHOLD
},
336 { "security-policy", required_argument
, NULL
, ARG_SECURITY_POLICY
},
337 { "system", no_argument
, NULL
, ARG_SYSTEM
},
338 { "user", no_argument
, NULL
, ARG_USER
},
339 { "global", no_argument
, NULL
, ARG_GLOBAL
},
340 { "from-pattern", required_argument
, NULL
, ARG_DOT_FROM_PATTERN
},
341 { "to-pattern", required_argument
, NULL
, ARG_DOT_TO_PATTERN
},
342 { "fuzz", required_argument
, NULL
, ARG_FUZZ
},
343 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
344 { "man", optional_argument
, NULL
, ARG_MAN
},
345 { "generators", optional_argument
, NULL
, ARG_GENERATORS
},
346 { "host", required_argument
, NULL
, 'H' },
347 { "machine", required_argument
, NULL
, 'M' },
348 { "iterations", required_argument
, NULL
, ARG_ITERATIONS
},
349 { "base-time", required_argument
, NULL
, ARG_BASE_TIME
},
350 { "unit", required_argument
, NULL
, 'U' },
351 { "json", required_argument
, NULL
, ARG_JSON
},
352 { "profile", required_argument
, NULL
, ARG_PROFILE
},
353 { "table", optional_argument
, NULL
, ARG_TABLE
},
354 { "no-legend", optional_argument
, NULL
, ARG_NO_LEGEND
},
355 { "tldr", no_argument
, NULL
, ARG_TLDR
},
364 while ((c
= getopt_long(argc
, argv
, "hH:M:U:q", options
, NULL
)) >= 0)
368 return help(0, NULL
, NULL
);
377 case ARG_RECURSIVE_ERRORS
:
378 if (streq(optarg
, "help")) {
379 DUMP_STRING_TABLE(recursive_errors
, RecursiveErrors
, _RECURSIVE_ERRORS_MAX
);
382 r
= recursive_errors_from_string(optarg
);
384 return log_error_errno(r
, "Unknown mode passed to --recursive-errors='%s'.", optarg
);
386 arg_recursive_errors
= r
;
390 r
= parse_path_argument(optarg
, /* suppress_root= */ true, &arg_root
);
396 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_image
);
401 case ARG_IMAGE_POLICY
:
402 r
= parse_image_policy_argument(optarg
, &arg_image_policy
);
408 arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
412 arg_runtime_scope
= RUNTIME_SCOPE_USER
;
416 arg_runtime_scope
= RUNTIME_SCOPE_GLOBAL
;
424 arg_dot
= DEP_REQUIRE
;
427 case ARG_DOT_FROM_PATTERN
:
428 if (strv_extend(&arg_dot_from_patterns
, optarg
) < 0)
433 case ARG_DOT_TO_PATTERN
:
434 if (strv_extend(&arg_dot_to_patterns
, optarg
) < 0)
440 r
= parse_sec(optarg
, &arg_fuzz
);
446 arg_pager_flags
|= PAGER_DISABLE
;
450 arg_transport
= BUS_TRANSPORT_REMOTE
;
455 arg_transport
= BUS_TRANSPORT_MACHINE
;
460 r
= parse_boolean_argument("--man", optarg
, &arg_man
);
466 r
= parse_boolean_argument("--generators", optarg
, &arg_generators
);
472 r
= parse_boolean_argument("--offline", optarg
, &arg_offline
);
478 r
= safe_atou(optarg
, &arg_threshold
);
479 if (r
< 0 || arg_threshold
> 100)
480 return log_error_errno(r
< 0 ? r
: SYNTHETIC_ERRNO(EINVAL
), "Failed to parse threshold: %s", optarg
);
484 case ARG_SECURITY_POLICY
:
485 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_security_policy
);
491 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
497 r
= safe_atou(optarg
, &arg_iterations
);
499 return log_error_errno(r
, "Failed to parse iterations: %s", optarg
);
503 r
= parse_timestamp(optarg
, &arg_base_time
);
505 return log_error_errno(r
, "Failed to parse --base-time= parameter: %s", optarg
);
510 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Profile file name is empty");
512 if (is_path(optarg
)) {
513 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_profile
);
516 if (!endswith(arg_profile
, ".conf"))
517 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Profile file name must end with .conf: %s", arg_profile
);
519 r
= free_and_strdup(&arg_profile
, optarg
);
527 _cleanup_free_
char *mangled
= NULL
;
529 r
= unit_name_mangle(optarg
, UNIT_NAME_MANGLE_WARN
, &mangled
);
531 return log_error_errno(r
, "Failed to mangle unit name %s: %m", optarg
);
533 free_and_replace(arg_unit
, mangled
);
546 arg_cat_flags
= CAT_TLDR
;
553 assert_not_reached();
556 if (arg_offline
&& !streq_ptr(argv
[optind
], "security"))
557 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
558 "Option --offline= is only supported for security right now.");
560 if (arg_json_format_flags
!= JSON_FORMAT_OFF
&& !STRPTR_IN_SET(argv
[optind
], "security", "inspect-elf", "plot", "fdstore", "pcrs"))
561 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
562 "Option --json= is only supported for security, inspect-elf, plot, fdstore, pcrs right now.");
564 if (arg_threshold
!= 100 && !streq_ptr(argv
[optind
], "security"))
565 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
566 "Option --threshold= is only supported for security right now.");
568 if (arg_runtime_scope
== RUNTIME_SCOPE_GLOBAL
&&
569 !STR_IN_SET(argv
[optind
] ?: "time", "dot", "unit-paths", "verify"))
570 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
571 "Option --global only makes sense with verbs dot, unit-paths, verify.");
573 if (streq_ptr(argv
[optind
], "cat-config") && arg_runtime_scope
== RUNTIME_SCOPE_USER
)
574 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
575 "Option --user is not supported for cat-config right now.");
577 if (arg_security_policy
&& !streq_ptr(argv
[optind
], "security"))
578 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
579 "Option --security-policy= is only supported for security.");
581 if ((arg_root
|| arg_image
) && (!STRPTR_IN_SET(argv
[optind
], "cat-config", "verify", "condition")) &&
582 (!(streq_ptr(argv
[optind
], "security") && arg_offline
)))
583 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
584 "Options --root= and --image= are only supported for cat-config, verify, condition and security when used with --offline= right now.");
586 /* Having both an image and a root is not supported by the code */
587 if (arg_root
&& arg_image
)
588 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Please specify either --root= or --image=, the combination of both is not supported.");
590 if (arg_unit
&& !streq_ptr(argv
[optind
], "condition"))
591 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Option --unit= is only supported for condition");
593 if (streq_ptr(argv
[optind
], "condition") && !arg_unit
&& optind
>= argc
- 1)
594 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Too few arguments for condition");
596 if (streq_ptr(argv
[optind
], "condition") && arg_unit
&& optind
< argc
- 1)
597 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No conditions can be passed if --unit= is used.");
599 if ((!arg_legend
&& !streq_ptr(argv
[optind
], "plot")) ||
600 (streq_ptr(argv
[optind
], "plot") && !arg_legend
&& !arg_table
&& FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
)))
601 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Option --no-legend is only supported for plot with either --table or --json=.");
603 if (arg_table
&& !streq_ptr(argv
[optind
], "plot"))
604 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Option --table is only supported for plot right now.");
606 if (arg_table
&& !FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
))
607 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--table and --json= are mutually exclusive.");
609 return 1; /* work to do */
612 static int run(int argc
, char *argv
[]) {
613 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
614 _cleanup_(umount_and_freep
) char *mounted_dir
= NULL
;
616 static const Verb verbs
[] = {
617 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
618 { "time", VERB_ANY
, 1, VERB_DEFAULT
, verb_time
},
619 { "blame", VERB_ANY
, 1, 0, verb_blame
},
620 { "critical-chain", VERB_ANY
, VERB_ANY
, 0, verb_critical_chain
},
621 { "plot", VERB_ANY
, 1, 0, verb_plot
},
622 { "dot", VERB_ANY
, VERB_ANY
, 0, verb_dot
},
623 /* ↓ The following seven verbs are deprecated, from here … ↓ */
624 { "log-level", VERB_ANY
, 2, 0, verb_log_control
},
625 { "log-target", VERB_ANY
, 2, 0, verb_log_control
},
626 { "set-log-level", 2, 2, 0, verb_log_control
},
627 { "get-log-level", VERB_ANY
, 1, 0, verb_log_control
},
628 { "set-log-target", 2, 2, 0, verb_log_control
},
629 { "get-log-target", VERB_ANY
, 1, 0, verb_log_control
},
630 { "service-watchdogs", VERB_ANY
, 2, 0, verb_service_watchdogs
},
631 /* ↑ … until here ↑ */
632 { "dump", VERB_ANY
, VERB_ANY
, 0, verb_dump
},
633 { "cat-config", 2, VERB_ANY
, 0, verb_cat_config
},
634 { "unit-files", VERB_ANY
, VERB_ANY
, 0, verb_unit_files
},
635 { "unit-paths", 1, 1, 0, verb_unit_paths
},
636 { "exit-status", VERB_ANY
, VERB_ANY
, 0, verb_exit_status
},
637 { "syscall-filter", VERB_ANY
, VERB_ANY
, 0, verb_syscall_filters
},
638 { "capability", VERB_ANY
, VERB_ANY
, 0, verb_capabilities
},
639 { "filesystems", VERB_ANY
, VERB_ANY
, 0, verb_filesystems
},
640 { "condition", VERB_ANY
, VERB_ANY
, 0, verb_condition
},
641 { "compare-versions", 3, 4, 0, verb_compare_versions
},
642 { "verify", 2, VERB_ANY
, 0, verb_verify
},
643 { "calendar", 2, VERB_ANY
, 0, verb_calendar
},
644 { "timestamp", 2, VERB_ANY
, 0, verb_timestamp
},
645 { "timespan", 2, VERB_ANY
, 0, verb_timespan
},
646 { "security", VERB_ANY
, VERB_ANY
, 0, verb_security
},
647 { "inspect-elf", 2, VERB_ANY
, 0, verb_elf_inspection
},
648 { "malloc", VERB_ANY
, VERB_ANY
, 0, verb_malloc
},
649 { "fdstore", 2, VERB_ANY
, 0, verb_fdstore
},
650 { "image-policy", 2, 2, 0, verb_image_policy
},
651 { "pcrs", VERB_ANY
, VERB_ANY
, 0, verb_pcrs
},
652 { "srk", VERB_ANY
, 1, 0, verb_srk
},
658 setlocale(LC_ALL
, "");
659 setlocale(LC_NUMERIC
, "C"); /* we want to format/parse floats in C style */
663 r
= parse_argv(argc
, argv
);
667 /* Open up and mount the image */
671 r
= mount_image_privately_interactively(
674 DISSECT_IMAGE_GENERIC_ROOT
|
675 DISSECT_IMAGE_RELAX_VAR_CHECK
|
676 DISSECT_IMAGE_READ_ONLY
,
678 /* ret_dir_fd= */ NULL
,
683 arg_root
= strdup(mounted_dir
);
688 return dispatch_verb(argc
, argv
, verbs
, NULL
);
691 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);