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-condition.h"
21 #include "analyze-critical-chain.h"
22 #include "analyze-dot.h"
23 #include "analyze-dump.h"
24 #include "analyze-exit-status.h"
25 #include "analyze-filesystems.h"
26 #include "analyze-inspect-elf.h"
27 #include "analyze-log-control.h"
28 #include "analyze-plot.h"
29 #include "analyze-security.h"
30 #include "analyze-service-watchdogs.h"
31 #include "analyze-syscall-filter.h"
32 #include "analyze-time.h"
33 #include "analyze-time-data.h"
34 #include "analyze-timespan.h"
35 #include "analyze-timestamp.h"
36 #include "analyze-unit-files.h"
37 #include "analyze-unit-paths.h"
38 #include "analyze-compare-versions.h"
39 #include "analyze-verify.h"
41 #include "bus-error.h"
42 #include "bus-locator.h"
43 #include "bus-map-properties.h"
44 #include "bus-unit-util.h"
45 #include "calendarspec.h"
47 #include "capability-util.h"
48 #include "conf-files.h"
51 #include "exit-status.h"
52 #include "extract-word.h"
55 #include "filesystems.h"
56 #include "format-table.h"
57 #include "glob-util.h"
59 #include "locale-util.h"
61 #include "main-func.h"
62 #include "mount-util.h"
63 #include "nulstr-util.h"
65 #include "parse-argument.h"
66 #include "parse-util.h"
67 #include "path-util.h"
68 #include "pretty-print.h"
71 # include "seccomp-util.h"
73 #include "sort-util.h"
75 #include "stat-util.h"
76 #include "string-table.h"
79 #include "terminal-util.h"
80 #include "time-util.h"
81 #include "tmpfile-util.h"
82 #include "unit-name.h"
83 #include "verb-log-control.h"
87 DotMode arg_dot
= DEP_ALL
;
88 char **arg_dot_from_patterns
= NULL
, **arg_dot_to_patterns
= NULL
;
90 PagerFlags arg_pager_flags
= 0;
91 BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
92 const char *arg_host
= NULL
;
93 LookupScope arg_scope
= LOOKUP_SCOPE_SYSTEM
;
94 RecursiveErrors arg_recursive_errors
= _RECURSIVE_ERRORS_INVALID
;
96 bool arg_generators
= false;
97 char *arg_root
= NULL
;
98 static char *arg_image
= NULL
;
99 char *arg_security_policy
= NULL
;
100 bool arg_offline
= false;
101 unsigned arg_threshold
= 100;
102 unsigned arg_iterations
= 1;
103 usec_t arg_base_time
= USEC_INFINITY
;
104 char *arg_unit
= NULL
;
105 JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
106 bool arg_quiet
= false;
107 char *arg_profile
= NULL
;
109 STATIC_DESTRUCTOR_REGISTER(arg_dot_from_patterns
, strv_freep
);
110 STATIC_DESTRUCTOR_REGISTER(arg_dot_to_patterns
, strv_freep
);
111 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
112 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
113 STATIC_DESTRUCTOR_REGISTER(arg_security_policy
, freep
);
114 STATIC_DESTRUCTOR_REGISTER(arg_unit
, freep
);
115 STATIC_DESTRUCTOR_REGISTER(arg_profile
, freep
);
117 int acquire_bus(sd_bus
**bus
, bool *use_full_bus
) {
118 bool user
= arg_scope
!= LOOKUP_SCOPE_SYSTEM
;
121 if (use_full_bus
&& *use_full_bus
) {
122 r
= bus_connect_transport(arg_transport
, arg_host
, user
, bus
);
123 if (IN_SET(r
, 0, -EHOSTDOWN
))
126 *use_full_bus
= false;
129 return bus_connect_transport_systemd(arg_transport
, arg_host
, user
, bus
);
132 int bus_get_unit_property_strv(sd_bus
*bus
, const char *path
, const char *property
, char ***strv
) {
133 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
141 r
= sd_bus_get_property_strv(
143 "org.freedesktop.systemd1",
145 "org.freedesktop.systemd1.Unit",
150 return log_error_errno(r
, "Failed to get unit property %s: %s", property
, bus_error_message(&error
, r
));
155 void time_parsing_hint(const char *p
, bool calendar
, bool timestamp
, bool timespan
) {
156 if (calendar
&& calendar_spec_from_string(p
, NULL
) >= 0)
157 log_notice("Hint: this expression is a valid calendar specification. "
158 "Use 'systemd-analyze calendar \"%s\"' instead?", p
);
159 if (timestamp
&& parse_timestamp(p
, NULL
) >= 0)
160 log_notice("Hint: this expression is a valid timestamp. "
161 "Use 'systemd-analyze timestamp \"%s\"' instead?", p
);
162 if (timespan
&& parse_time(p
, NULL
, USEC_PER_SEC
) >= 0)
163 log_notice("Hint: this expression is a valid timespan. "
164 "Use 'systemd-analyze timespan \"%s\"' instead?", p
);
167 static int help(int argc
, char *argv
[], void *userdata
) {
168 _cleanup_free_
char *link
= NULL
, *dot_link
= NULL
;
171 pager_open(arg_pager_flags
);
173 r
= terminal_urlify_man("systemd-analyze", "1", &link
);
177 /* Not using terminal_urlify_man() for this, since we don't want the "man page" text suffix in this case. */
178 r
= terminal_urlify("man:dot(1)", "dot(1)", &dot_link
);
182 printf("%s [OPTIONS...] COMMAND ...\n\n"
183 "%sProfile systemd, show unit dependencies, check unit files.%s\n"
185 " [time] Print time required to boot the machine\n"
186 " blame Print list of running units ordered by\n"
188 " critical-chain [UNIT...] Print a tree of the time critical chain\n"
190 " plot Output SVG graphic showing service\n"
192 " dot [UNIT...] Output dependency graph in %s format\n"
193 " dump [PATTERN...] Output state serialization of service\n"
195 " cat-config Show configuration file and drop-ins\n"
196 " unit-files List files and symlinks for units\n"
197 " unit-paths List load directories for units\n"
198 " exit-status [STATUS...] List exit status definitions\n"
199 " capability [CAP...] List capability definitions\n"
200 " syscall-filter [NAME...] List syscalls in seccomp filters\n"
201 " filesystems [NAME...] List known filesystems\n"
202 " condition CONDITION... Evaluate conditions and asserts\n"
203 " compare-versions VERSION1 [OP] VERSION2\n"
204 " Compare two version strings\n"
205 " verify FILE... Check unit files for correctness\n"
206 " calendar SPEC... Validate repetitive calendar time\n"
208 " timestamp TIMESTAMP... Validate a timestamp\n"
209 " timespan SPAN... Validate a time span\n"
210 " security [UNIT...] Analyze security of unit\n"
211 " inspect-elf FILE... Parse and print ELF package metadata\n"
213 " --recursive-errors=MODE Control which units are verified\n"
214 " --offline=BOOL Perform a security review on unit file(s)\n"
215 " --threshold=N Exit with a non-zero status when overall\n"
216 " exposure level is over threshold value\n"
217 " --security-policy=PATH Use custom JSON security policy instead\n"
219 " --json=pretty|short|off Generate JSON output of the security\n"
221 " --no-pager Do not pipe output into a pager\n"
222 " --system Operate on system systemd instance\n"
223 " --user Operate on user systemd instance\n"
224 " --global Operate on global user configuration\n"
225 " -H --host=[USER@]HOST Operate on remote host\n"
226 " -M --machine=CONTAINER Operate on local container\n"
227 " --order Show only order in the graph\n"
228 " --require Show only requirement in the graph\n"
229 " --from-pattern=GLOB Show only origins in the graph\n"
230 " --to-pattern=GLOB Show only destinations in the graph\n"
231 " --fuzz=SECONDS Also print services which finished SECONDS\n"
232 " earlier than the latest in the branch\n"
233 " --man[=BOOL] Do [not] check for existence of man pages\n"
234 " --generators[=BOOL] Do [not] run unit generators\n"
235 " (requires privileges)\n"
236 " --iterations=N Show the specified number of iterations\n"
237 " --base-time=TIMESTAMP Calculate calendar times relative to\n"
239 " --profile=name|PATH Include the specified profile in the\n"
240 " security review of the unit(s)\n"
241 " -h --help Show this help\n"
242 " --version Show package version\n"
243 " -q --quiet Do not emit hints\n"
244 " --root=PATH Operate on an alternate filesystem root\n"
245 " --image=PATH Operate on disk image as filesystem root\n"
246 "\nSee the %s for details.\n",
247 program_invocation_short_name
,
253 /* When updating this list, including descriptions, apply changes to
254 * shell-completion/bash/systemd-analyze and shell-completion/zsh/_systemd-analyze too. */
259 static int parse_argv(int argc
, char *argv
[]) {
269 ARG_DOT_FROM_PATTERN
,
277 ARG_RECURSIVE_ERRORS
,
285 static const struct option options
[] = {
286 { "help", no_argument
, NULL
, 'h' },
287 { "version", no_argument
, NULL
, ARG_VERSION
},
288 { "quiet", no_argument
, NULL
, 'q' },
289 { "order", no_argument
, NULL
, ARG_ORDER
},
290 { "require", no_argument
, NULL
, ARG_REQUIRE
},
291 { "root", required_argument
, NULL
, ARG_ROOT
},
292 { "image", required_argument
, NULL
, ARG_IMAGE
},
293 { "recursive-errors", required_argument
, NULL
, ARG_RECURSIVE_ERRORS
},
294 { "offline", required_argument
, NULL
, ARG_OFFLINE
},
295 { "threshold", required_argument
, NULL
, ARG_THRESHOLD
},
296 { "security-policy", required_argument
, NULL
, ARG_SECURITY_POLICY
},
297 { "system", no_argument
, NULL
, ARG_SYSTEM
},
298 { "user", no_argument
, NULL
, ARG_USER
},
299 { "global", no_argument
, NULL
, ARG_GLOBAL
},
300 { "from-pattern", required_argument
, NULL
, ARG_DOT_FROM_PATTERN
},
301 { "to-pattern", required_argument
, NULL
, ARG_DOT_TO_PATTERN
},
302 { "fuzz", required_argument
, NULL
, ARG_FUZZ
},
303 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
304 { "man", optional_argument
, NULL
, ARG_MAN
},
305 { "generators", optional_argument
, NULL
, ARG_GENERATORS
},
306 { "host", required_argument
, NULL
, 'H' },
307 { "machine", required_argument
, NULL
, 'M' },
308 { "iterations", required_argument
, NULL
, ARG_ITERATIONS
},
309 { "base-time", required_argument
, NULL
, ARG_BASE_TIME
},
310 { "unit", required_argument
, NULL
, 'U' },
311 { "json", required_argument
, NULL
, ARG_JSON
},
312 { "profile", required_argument
, NULL
, ARG_PROFILE
},
321 while ((c
= getopt_long(argc
, argv
, "hH:M:U:", options
, NULL
)) >= 0)
325 return help(0, NULL
, NULL
);
334 case ARG_RECURSIVE_ERRORS
:
335 if (streq(optarg
, "help")) {
336 DUMP_STRING_TABLE(recursive_errors
, RecursiveErrors
, _RECURSIVE_ERRORS_MAX
);
339 r
= recursive_errors_from_string(optarg
);
341 return log_error_errno(r
, "Unknown mode passed to --recursive-errors='%s'.", optarg
);
343 arg_recursive_errors
= r
;
347 r
= parse_path_argument(optarg
, /* suppress_root= */ true, &arg_root
);
353 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_image
);
359 arg_scope
= LOOKUP_SCOPE_SYSTEM
;
363 arg_scope
= LOOKUP_SCOPE_USER
;
367 arg_scope
= LOOKUP_SCOPE_GLOBAL
;
375 arg_dot
= DEP_REQUIRE
;
378 case ARG_DOT_FROM_PATTERN
:
379 if (strv_extend(&arg_dot_from_patterns
, optarg
) < 0)
384 case ARG_DOT_TO_PATTERN
:
385 if (strv_extend(&arg_dot_to_patterns
, optarg
) < 0)
391 r
= parse_sec(optarg
, &arg_fuzz
);
397 arg_pager_flags
|= PAGER_DISABLE
;
401 arg_transport
= BUS_TRANSPORT_REMOTE
;
406 arg_transport
= BUS_TRANSPORT_MACHINE
;
411 r
= parse_boolean_argument("--man", optarg
, &arg_man
);
417 r
= parse_boolean_argument("--generators", optarg
, &arg_generators
);
423 r
= parse_boolean_argument("--offline", optarg
, &arg_offline
);
429 r
= safe_atou(optarg
, &arg_threshold
);
430 if (r
< 0 || arg_threshold
> 100)
431 return log_error_errno(r
< 0 ? r
: SYNTHETIC_ERRNO(EINVAL
), "Failed to parse threshold: %s", optarg
);
435 case ARG_SECURITY_POLICY
:
436 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_security_policy
);
442 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
448 r
= safe_atou(optarg
, &arg_iterations
);
450 return log_error_errno(r
, "Failed to parse iterations: %s", optarg
);
455 r
= parse_timestamp(optarg
, &arg_base_time
);
457 return log_error_errno(r
, "Failed to parse --base-time= parameter: %s", optarg
);
463 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Profile file name is empty");
465 if (is_path(optarg
)) {
466 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_profile
);
469 if (!endswith(arg_profile
, ".conf"))
470 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Profile file name must end with .conf: %s", arg_profile
);
472 r
= free_and_strdup(&arg_profile
, optarg
);
480 _cleanup_free_
char *mangled
= NULL
;
482 r
= unit_name_mangle(optarg
, UNIT_NAME_MANGLE_WARN
, &mangled
);
484 return log_error_errno(r
, "Failed to mangle unit name %s: %m", optarg
);
486 free_and_replace(arg_unit
, mangled
);
493 assert_not_reached();
496 if (arg_offline
&& !streq_ptr(argv
[optind
], "security"))
497 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
498 "Option --offline= is only supported for security right now.");
500 if (arg_json_format_flags
!= JSON_FORMAT_OFF
&& !STRPTR_IN_SET(argv
[optind
], "security", "inspect-elf"))
501 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
502 "Option --json= is only supported for security and inspect-elf right now.");
504 if (arg_threshold
!= 100 && !streq_ptr(argv
[optind
], "security"))
505 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
506 "Option --threshold= is only supported for security right now.");
508 if (arg_scope
== LOOKUP_SCOPE_GLOBAL
&&
509 !STR_IN_SET(argv
[optind
] ?: "time", "dot", "unit-paths", "verify"))
510 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
511 "Option --global only makes sense with verbs dot, unit-paths, verify.");
513 if (streq_ptr(argv
[optind
], "cat-config") && arg_scope
== LOOKUP_SCOPE_USER
)
514 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
515 "Option --user is not supported for cat-config right now.");
517 if (arg_security_policy
&& !streq_ptr(argv
[optind
], "security"))
518 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
519 "Option --security-policy= is only supported for security.");
521 if ((arg_root
|| arg_image
) && (!STRPTR_IN_SET(argv
[optind
], "cat-config", "verify", "condition")) &&
522 (!(streq_ptr(argv
[optind
], "security") && arg_offline
)))
523 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
524 "Options --root= and --image= are only supported for cat-config, verify, condition and security when used with --offline= right now.");
526 /* Having both an image and a root is not supported by the code */
527 if (arg_root
&& arg_image
)
528 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Please specify either --root= or --image=, the combination of both is not supported.");
530 if (arg_unit
&& !streq_ptr(argv
[optind
], "condition"))
531 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Option --unit= is only supported for condition");
533 if (streq_ptr(argv
[optind
], "condition") && !arg_unit
&& optind
>= argc
- 1)
534 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Too few arguments for condition");
536 if (streq_ptr(argv
[optind
], "condition") && arg_unit
&& optind
< argc
- 1)
537 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No conditions can be passed if --unit= is used.");
539 return 1; /* work to do */
542 static int run(int argc
, char *argv
[]) {
543 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
544 _cleanup_(umount_and_rmdir_and_freep
) char *unlink_dir
= NULL
;
546 static const Verb verbs
[] = {
547 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
548 { "time", VERB_ANY
, 1, VERB_DEFAULT
, verb_time
},
549 { "blame", VERB_ANY
, 1, 0, verb_blame
},
550 { "critical-chain", VERB_ANY
, VERB_ANY
, 0, verb_critical_chain
},
551 { "plot", VERB_ANY
, 1, 0, verb_plot
},
552 { "dot", VERB_ANY
, VERB_ANY
, 0, verb_dot
},
553 /* ↓ The following seven verbs are deprecated, from here … ↓ */
554 { "log-level", VERB_ANY
, 2, 0, verb_log_control
},
555 { "log-target", VERB_ANY
, 2, 0, verb_log_control
},
556 { "set-log-level", 2, 2, 0, verb_log_control
},
557 { "get-log-level", VERB_ANY
, 1, 0, verb_log_control
},
558 { "set-log-target", 2, 2, 0, verb_log_control
},
559 { "get-log-target", VERB_ANY
, 1, 0, verb_log_control
},
560 { "service-watchdogs", VERB_ANY
, 2, 0, verb_service_watchdogs
},
561 /* ↑ … until here ↑ */
562 { "dump", VERB_ANY
, VERB_ANY
, 0, verb_dump
},
563 { "cat-config", 2, VERB_ANY
, 0, verb_cat_config
},
564 { "unit-files", VERB_ANY
, VERB_ANY
, 0, verb_unit_files
},
565 { "unit-paths", 1, 1, 0, verb_unit_paths
},
566 { "exit-status", VERB_ANY
, VERB_ANY
, 0, verb_exit_status
},
567 { "syscall-filter", VERB_ANY
, VERB_ANY
, 0, verb_syscall_filters
},
568 { "capability", VERB_ANY
, VERB_ANY
, 0, verb_capabilities
},
569 { "filesystems", VERB_ANY
, VERB_ANY
, 0, verb_filesystems
},
570 { "condition", VERB_ANY
, VERB_ANY
, 0, verb_condition
},
571 { "compare-versions", 3, 4, 0, verb_compare_versions
},
572 { "verify", 2, VERB_ANY
, 0, verb_verify
},
573 { "calendar", 2, VERB_ANY
, 0, verb_calendar
},
574 { "timestamp", 2, VERB_ANY
, 0, verb_timestamp
},
575 { "timespan", 2, VERB_ANY
, 0, verb_timespan
},
576 { "security", VERB_ANY
, VERB_ANY
, 0, verb_security
},
577 { "inspect-elf", 2, VERB_ANY
, 0, verb_elf_inspection
},
583 setlocale(LC_ALL
, "");
584 setlocale(LC_NUMERIC
, "C"); /* we want to format/parse floats in C style */
588 r
= parse_argv(argc
, argv
);
592 /* Open up and mount the image */
596 r
= mount_image_privately_interactively(
598 DISSECT_IMAGE_GENERIC_ROOT
|
599 DISSECT_IMAGE_RELAX_VAR_CHECK
|
600 DISSECT_IMAGE_READ_ONLY
,
606 arg_root
= strdup(unlink_dir
);
611 return dispatch_verb(argc
, argv
, verbs
, NULL
);
614 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);