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-syscall-filter.h"
36 #include "analyze-time.h"
37 #include "analyze-time-data.h"
38 #include "analyze-timespan.h"
39 #include "analyze-timestamp.h"
40 #include "analyze-unit-files.h"
41 #include "analyze-unit-paths.h"
42 #include "analyze-verify.h"
43 #include "analyze-image-policy.h"
45 #include "bus-error.h"
46 #include "bus-locator.h"
47 #include "bus-map-properties.h"
48 #include "bus-unit-util.h"
49 #include "calendarspec.h"
51 #include "capability-util.h"
52 #include "conf-files.h"
54 #include "constants.h"
55 #include "exit-status.h"
56 #include "extract-word.h"
59 #include "filesystems.h"
60 #include "format-table.h"
61 #include "glob-util.h"
63 #include "locale-util.h"
65 #include "main-func.h"
66 #include "mount-util.h"
67 #include "nulstr-util.h"
69 #include "parse-argument.h"
70 #include "parse-util.h"
71 #include "path-util.h"
72 #include "pretty-print.h"
75 # include "seccomp-util.h"
77 #include "sort-util.h"
79 #include "stat-util.h"
80 #include "string-table.h"
83 #include "terminal-util.h"
84 #include "time-util.h"
85 #include "tmpfile-util.h"
86 #include "unit-name.h"
87 #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 BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
96 const char *arg_host
= NULL
;
97 RuntimeScope arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
98 RecursiveErrors arg_recursive_errors
= _RECURSIVE_ERRORS_INVALID
;
100 bool arg_generators
= false;
101 char *arg_root
= NULL
;
102 static char *arg_image
= NULL
;
103 char *arg_security_policy
= NULL
;
104 bool arg_offline
= false;
105 unsigned arg_threshold
= 100;
106 unsigned arg_iterations
= 1;
107 usec_t arg_base_time
= USEC_INFINITY
;
108 char *arg_unit
= NULL
;
109 JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
110 bool arg_quiet
= false;
111 char *arg_profile
= NULL
;
112 bool arg_legend
= true;
113 bool arg_table
= false;
114 ImagePolicy
*arg_image_policy
= NULL
;
116 STATIC_DESTRUCTOR_REGISTER(arg_dot_from_patterns
, strv_freep
);
117 STATIC_DESTRUCTOR_REGISTER(arg_dot_to_patterns
, strv_freep
);
118 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
119 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
120 STATIC_DESTRUCTOR_REGISTER(arg_security_policy
, freep
);
121 STATIC_DESTRUCTOR_REGISTER(arg_unit
, freep
);
122 STATIC_DESTRUCTOR_REGISTER(arg_profile
, freep
);
123 STATIC_DESTRUCTOR_REGISTER(arg_image_policy
, image_policy_freep
);
125 int acquire_bus(sd_bus
**bus
, bool *use_full_bus
) {
128 if (use_full_bus
&& *use_full_bus
) {
129 r
= bus_connect_transport(arg_transport
, arg_host
, arg_runtime_scope
, bus
);
130 if (IN_SET(r
, 0, -EHOSTDOWN
))
133 *use_full_bus
= false;
136 return bus_connect_transport_systemd(arg_transport
, arg_host
, arg_runtime_scope
, bus
);
139 int bus_get_unit_property_strv(sd_bus
*bus
, const char *path
, const char *property
, char ***strv
) {
140 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
148 r
= sd_bus_get_property_strv(
150 "org.freedesktop.systemd1",
152 "org.freedesktop.systemd1.Unit",
157 return log_error_errno(r
, "Failed to get unit property %s: %s", property
, bus_error_message(&error
, r
));
162 void time_parsing_hint(const char *p
, bool calendar
, bool timestamp
, bool timespan
) {
163 if (calendar
&& calendar_spec_from_string(p
, NULL
) >= 0)
164 log_notice("Hint: this expression is a valid calendar specification. "
165 "Use 'systemd-analyze calendar \"%s\"' instead?", p
);
166 if (timestamp
&& parse_timestamp(p
, NULL
) >= 0)
167 log_notice("Hint: this expression is a valid timestamp. "
168 "Use 'systemd-analyze timestamp \"%s\"' instead?", p
);
169 if (timespan
&& parse_time(p
, NULL
, USEC_PER_SEC
) >= 0)
170 log_notice("Hint: this expression is a valid timespan. "
171 "Use 'systemd-analyze timespan \"%s\"' instead?", p
);
174 int dump_fd_reply(sd_bus_message
*message
) {
179 r
= sd_bus_message_read(message
, "h", &fd
);
181 return bus_log_parse_error(r
);
184 r
= copy_bytes(fd
, STDOUT_FILENO
, UINT64_MAX
, 0);
188 return 1; /* Success */
191 static int help(int argc
, char *argv
[], void *userdata
) {
192 _cleanup_free_
char *link
= NULL
, *dot_link
= NULL
;
195 pager_open(arg_pager_flags
);
197 r
= terminal_urlify_man("systemd-analyze", "1", &link
);
201 /* Not using terminal_urlify_man() for this, since we don't want the "man page" text suffix in this case. */
202 r
= terminal_urlify("man:dot(1)", "dot(1)", &dot_link
);
206 printf("%s [OPTIONS...] COMMAND ...\n\n"
207 "%sProfile systemd, show unit dependencies, check unit files.%s\n"
209 " [time] Print time required to boot the machine\n"
210 " blame Print list of running units ordered by\n"
212 " critical-chain [UNIT...] Print a tree of the time critical chain\n"
214 " plot Output SVG graphic showing service\n"
216 " dot [UNIT...] Output dependency graph in %s format\n"
217 " dump [PATTERN...] Output state serialization of service\n"
219 " cat-config Show configuration file and drop-ins\n"
220 " unit-files List files and symlinks for units\n"
221 " unit-paths List load directories for units\n"
222 " exit-status [STATUS...] List exit status definitions\n"
223 " capability [CAP...] List capability definitions\n"
224 " syscall-filter [NAME...] List syscalls in seccomp filters\n"
225 " filesystems [NAME...] List known filesystems\n"
226 " condition CONDITION... Evaluate conditions and asserts\n"
227 " compare-versions VERSION1 [OP] VERSION2\n"
228 " Compare two version strings\n"
229 " verify FILE... Check unit files for correctness\n"
230 " calendar SPEC... Validate repetitive calendar time\n"
232 " timestamp TIMESTAMP... Validate a timestamp\n"
233 " timespan SPAN... Validate a time span\n"
234 " security [UNIT...] Analyze security of unit\n"
235 " inspect-elf FILE... Parse and print ELF package metadata\n"
236 " malloc [D-BUS SERVICE...] Dump malloc stats of a D-Bus service\n"
237 " fdstore SERVICE... Show file descriptor store contents of service\n"
238 " pcrs [PCR...] Show TPM2 PCRs and their names\n"
240 " --recursive-errors=MODE Control which units are verified\n"
241 " --offline=BOOL Perform a security review on unit file(s)\n"
242 " --threshold=N Exit with a non-zero status when overall\n"
243 " exposure level is over threshold value\n"
244 " --security-policy=PATH Use custom JSON security policy instead\n"
246 " --json=pretty|short|off Generate JSON output of the security\n"
247 " analysis table, or plot's raw time data\n"
248 " --no-pager Do not pipe output into a pager\n"
249 " --no-legend Disable column headers and hints in plot\n"
250 " with either --table or --json=\n"
251 " --system Operate on system systemd instance\n"
252 " --user Operate on user systemd instance\n"
253 " --global Operate on global user configuration\n"
254 " -H --host=[USER@]HOST Operate on remote host\n"
255 " -M --machine=CONTAINER Operate on local container\n"
256 " --order Show only order in the graph\n"
257 " --require Show only requirement in the graph\n"
258 " --from-pattern=GLOB Show only origins in the graph\n"
259 " --to-pattern=GLOB Show only destinations in the graph\n"
260 " --fuzz=SECONDS Also print services which finished SECONDS\n"
261 " earlier than the latest in the branch\n"
262 " --man[=BOOL] Do [not] check for existence of man pages\n"
263 " --generators[=BOOL] Do [not] run unit generators\n"
264 " (requires privileges)\n"
265 " --iterations=N Show the specified number of iterations\n"
266 " --base-time=TIMESTAMP Calculate calendar times relative to\n"
268 " --profile=name|PATH Include the specified profile in the\n"
269 " security review of the unit(s)\n"
270 " --table Output plot's raw time data as a table\n"
271 " -h --help Show this help\n"
272 " --version Show package version\n"
273 " -q --quiet Do not emit hints\n"
274 " --root=PATH Operate on an alternate filesystem root\n"
275 " --image=PATH Operate on disk image as filesystem root\n"
276 " --image-policy=POLICY Specify disk image dissection policy\n"
277 "\nSee the %s for details.\n",
278 program_invocation_short_name
,
284 /* When updating this list, including descriptions, apply changes to
285 * shell-completion/bash/systemd-analyze and shell-completion/zsh/_systemd-analyze too. */
290 static int parse_argv(int argc
, char *argv
[]) {
301 ARG_DOT_FROM_PATTERN
,
309 ARG_RECURSIVE_ERRORS
,
319 static const struct option options
[] = {
320 { "help", no_argument
, NULL
, 'h' },
321 { "version", no_argument
, NULL
, ARG_VERSION
},
322 { "quiet", no_argument
, NULL
, 'q' },
323 { "order", no_argument
, NULL
, ARG_ORDER
},
324 { "require", no_argument
, NULL
, ARG_REQUIRE
},
325 { "root", required_argument
, NULL
, ARG_ROOT
},
326 { "image", required_argument
, NULL
, ARG_IMAGE
},
327 { "image-policy", required_argument
, NULL
, ARG_IMAGE_POLICY
},
328 { "recursive-errors", required_argument
, NULL
, ARG_RECURSIVE_ERRORS
},
329 { "offline", required_argument
, NULL
, ARG_OFFLINE
},
330 { "threshold", required_argument
, NULL
, ARG_THRESHOLD
},
331 { "security-policy", required_argument
, NULL
, ARG_SECURITY_POLICY
},
332 { "system", no_argument
, NULL
, ARG_SYSTEM
},
333 { "user", no_argument
, NULL
, ARG_USER
},
334 { "global", no_argument
, NULL
, ARG_GLOBAL
},
335 { "from-pattern", required_argument
, NULL
, ARG_DOT_FROM_PATTERN
},
336 { "to-pattern", required_argument
, NULL
, ARG_DOT_TO_PATTERN
},
337 { "fuzz", required_argument
, NULL
, ARG_FUZZ
},
338 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
339 { "man", optional_argument
, NULL
, ARG_MAN
},
340 { "generators", optional_argument
, NULL
, ARG_GENERATORS
},
341 { "host", required_argument
, NULL
, 'H' },
342 { "machine", required_argument
, NULL
, 'M' },
343 { "iterations", required_argument
, NULL
, ARG_ITERATIONS
},
344 { "base-time", required_argument
, NULL
, ARG_BASE_TIME
},
345 { "unit", required_argument
, NULL
, 'U' },
346 { "json", required_argument
, NULL
, ARG_JSON
},
347 { "profile", required_argument
, NULL
, ARG_PROFILE
},
348 { "table", optional_argument
, NULL
, ARG_TABLE
},
349 { "no-legend", optional_argument
, NULL
, ARG_NO_LEGEND
},
358 while ((c
= getopt_long(argc
, argv
, "hH:M:U:", options
, NULL
)) >= 0)
362 return help(0, NULL
, NULL
);
371 case ARG_RECURSIVE_ERRORS
:
372 if (streq(optarg
, "help")) {
373 DUMP_STRING_TABLE(recursive_errors
, RecursiveErrors
, _RECURSIVE_ERRORS_MAX
);
376 r
= recursive_errors_from_string(optarg
);
378 return log_error_errno(r
, "Unknown mode passed to --recursive-errors='%s'.", optarg
);
380 arg_recursive_errors
= r
;
384 r
= parse_path_argument(optarg
, /* suppress_root= */ true, &arg_root
);
390 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_image
);
395 case ARG_IMAGE_POLICY
:
396 r
= parse_image_policy_argument(optarg
, &arg_image_policy
);
402 arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
406 arg_runtime_scope
= RUNTIME_SCOPE_USER
;
410 arg_runtime_scope
= RUNTIME_SCOPE_GLOBAL
;
418 arg_dot
= DEP_REQUIRE
;
421 case ARG_DOT_FROM_PATTERN
:
422 if (strv_extend(&arg_dot_from_patterns
, optarg
) < 0)
427 case ARG_DOT_TO_PATTERN
:
428 if (strv_extend(&arg_dot_to_patterns
, optarg
) < 0)
434 r
= parse_sec(optarg
, &arg_fuzz
);
440 arg_pager_flags
|= PAGER_DISABLE
;
444 arg_transport
= BUS_TRANSPORT_REMOTE
;
449 arg_transport
= BUS_TRANSPORT_MACHINE
;
454 r
= parse_boolean_argument("--man", optarg
, &arg_man
);
460 r
= parse_boolean_argument("--generators", optarg
, &arg_generators
);
466 r
= parse_boolean_argument("--offline", optarg
, &arg_offline
);
472 r
= safe_atou(optarg
, &arg_threshold
);
473 if (r
< 0 || arg_threshold
> 100)
474 return log_error_errno(r
< 0 ? r
: SYNTHETIC_ERRNO(EINVAL
), "Failed to parse threshold: %s", optarg
);
478 case ARG_SECURITY_POLICY
:
479 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_security_policy
);
485 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
491 r
= safe_atou(optarg
, &arg_iterations
);
493 return log_error_errno(r
, "Failed to parse iterations: %s", optarg
);
497 r
= parse_timestamp(optarg
, &arg_base_time
);
499 return log_error_errno(r
, "Failed to parse --base-time= parameter: %s", optarg
);
504 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Profile file name is empty");
506 if (is_path(optarg
)) {
507 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_profile
);
510 if (!endswith(arg_profile
, ".conf"))
511 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Profile file name must end with .conf: %s", arg_profile
);
513 r
= free_and_strdup(&arg_profile
, optarg
);
521 _cleanup_free_
char *mangled
= NULL
;
523 r
= unit_name_mangle(optarg
, UNIT_NAME_MANGLE_WARN
, &mangled
);
525 return log_error_errno(r
, "Failed to mangle unit name %s: %m", optarg
);
527 free_and_replace(arg_unit
, mangled
);
543 assert_not_reached();
546 if (arg_offline
&& !streq_ptr(argv
[optind
], "security"))
547 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
548 "Option --offline= is only supported for security right now.");
550 if (arg_json_format_flags
!= JSON_FORMAT_OFF
&& !STRPTR_IN_SET(argv
[optind
], "security", "inspect-elf", "plot", "fdstore", "pcrs"))
551 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
552 "Option --json= is only supported for security, inspect-elf, plot, fdstore, pcrs right now.");
554 if (arg_threshold
!= 100 && !streq_ptr(argv
[optind
], "security"))
555 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
556 "Option --threshold= is only supported for security right now.");
558 if (arg_runtime_scope
== RUNTIME_SCOPE_GLOBAL
&&
559 !STR_IN_SET(argv
[optind
] ?: "time", "dot", "unit-paths", "verify"))
560 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
561 "Option --global only makes sense with verbs dot, unit-paths, verify.");
563 if (streq_ptr(argv
[optind
], "cat-config") && arg_runtime_scope
== RUNTIME_SCOPE_USER
)
564 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
565 "Option --user is not supported for cat-config right now.");
567 if (arg_security_policy
&& !streq_ptr(argv
[optind
], "security"))
568 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
569 "Option --security-policy= is only supported for security.");
571 if ((arg_root
|| arg_image
) && (!STRPTR_IN_SET(argv
[optind
], "cat-config", "verify", "condition")) &&
572 (!(streq_ptr(argv
[optind
], "security") && arg_offline
)))
573 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
574 "Options --root= and --image= are only supported for cat-config, verify, condition and security when used with --offline= right now.");
576 /* Having both an image and a root is not supported by the code */
577 if (arg_root
&& arg_image
)
578 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Please specify either --root= or --image=, the combination of both is not supported.");
580 if (arg_unit
&& !streq_ptr(argv
[optind
], "condition"))
581 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Option --unit= is only supported for condition");
583 if (streq_ptr(argv
[optind
], "condition") && !arg_unit
&& optind
>= argc
- 1)
584 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Too few arguments for condition");
586 if (streq_ptr(argv
[optind
], "condition") && arg_unit
&& optind
< argc
- 1)
587 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No conditions can be passed if --unit= is used.");
589 if ((!arg_legend
&& !streq_ptr(argv
[optind
], "plot")) ||
590 (streq_ptr(argv
[optind
], "plot") && !arg_legend
&& !arg_table
&& FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
)))
591 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Option --no-legend is only supported for plot with either --table or --json=.");
593 if (arg_table
&& !streq_ptr(argv
[optind
], "plot"))
594 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Option --table is only supported for plot right now.");
596 if (arg_table
&& !FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
))
597 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--table and --json= are mutually exclusive.");
599 return 1; /* work to do */
602 static int run(int argc
, char *argv
[]) {
603 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
604 _cleanup_(umount_and_freep
) char *mounted_dir
= NULL
;
606 static const Verb verbs
[] = {
607 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
608 { "time", VERB_ANY
, 1, VERB_DEFAULT
, verb_time
},
609 { "blame", VERB_ANY
, 1, 0, verb_blame
},
610 { "critical-chain", VERB_ANY
, VERB_ANY
, 0, verb_critical_chain
},
611 { "plot", VERB_ANY
, 1, 0, verb_plot
},
612 { "dot", VERB_ANY
, VERB_ANY
, 0, verb_dot
},
613 /* ↓ The following seven verbs are deprecated, from here … ↓ */
614 { "log-level", VERB_ANY
, 2, 0, verb_log_control
},
615 { "log-target", VERB_ANY
, 2, 0, verb_log_control
},
616 { "set-log-level", 2, 2, 0, verb_log_control
},
617 { "get-log-level", VERB_ANY
, 1, 0, verb_log_control
},
618 { "set-log-target", 2, 2, 0, verb_log_control
},
619 { "get-log-target", VERB_ANY
, 1, 0, verb_log_control
},
620 { "service-watchdogs", VERB_ANY
, 2, 0, verb_service_watchdogs
},
621 /* ↑ … until here ↑ */
622 { "dump", VERB_ANY
, VERB_ANY
, 0, verb_dump
},
623 { "cat-config", 2, VERB_ANY
, 0, verb_cat_config
},
624 { "unit-files", VERB_ANY
, VERB_ANY
, 0, verb_unit_files
},
625 { "unit-paths", 1, 1, 0, verb_unit_paths
},
626 { "exit-status", VERB_ANY
, VERB_ANY
, 0, verb_exit_status
},
627 { "syscall-filter", VERB_ANY
, VERB_ANY
, 0, verb_syscall_filters
},
628 { "capability", VERB_ANY
, VERB_ANY
, 0, verb_capabilities
},
629 { "filesystems", VERB_ANY
, VERB_ANY
, 0, verb_filesystems
},
630 { "condition", VERB_ANY
, VERB_ANY
, 0, verb_condition
},
631 { "compare-versions", 3, 4, 0, verb_compare_versions
},
632 { "verify", 2, VERB_ANY
, 0, verb_verify
},
633 { "calendar", 2, VERB_ANY
, 0, verb_calendar
},
634 { "timestamp", 2, VERB_ANY
, 0, verb_timestamp
},
635 { "timespan", 2, VERB_ANY
, 0, verb_timespan
},
636 { "security", VERB_ANY
, VERB_ANY
, 0, verb_security
},
637 { "inspect-elf", 2, VERB_ANY
, 0, verb_elf_inspection
},
638 { "malloc", VERB_ANY
, VERB_ANY
, 0, verb_malloc
},
639 { "fdstore", 2, VERB_ANY
, 0, verb_fdstore
},
640 { "image-policy", 2, 2, 0, verb_image_policy
},
641 { "pcrs", VERB_ANY
, VERB_ANY
, 0, verb_pcrs
},
647 setlocale(LC_ALL
, "");
648 setlocale(LC_NUMERIC
, "C"); /* we want to format/parse floats in C style */
652 r
= parse_argv(argc
, argv
);
656 /* Open up and mount the image */
660 r
= mount_image_privately_interactively(
663 DISSECT_IMAGE_GENERIC_ROOT
|
664 DISSECT_IMAGE_RELAX_VAR_CHECK
|
665 DISSECT_IMAGE_READ_ONLY
,
667 /* ret_dir_fd= */ NULL
,
672 arg_root
= strdup(mounted_dir
);
677 return dispatch_verb(argc
, argv
, verbs
, NULL
);
680 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);