]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/analyze/analyze.c
core: gracefully ignore PrivateBPF=yes if the kernel does not support it (#38238)
[thirdparty/systemd.git] / src / analyze / analyze.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2265fbf7 2/***
96b2fb93 3 Copyright © 2013 Simon Peeters
2265fbf7
SP
4***/
5
2265fbf7 6#include <getopt.h>
4bde8743 7#include <locale.h>
3f6fd1ba
LP
8#include <stdio.h>
9#include <stdlib.h>
2265fbf7 10
048ecf5b 11#include "sd-bus.h"
4bde8743 12#include "sd-json.h"
3f6fd1ba 13
b5efdb8a 14#include "alloc-util.h"
30bddc06 15#include "analyze.h"
fb8cc599 16#include "analyze-architectures.h"
113dd9cb 17#include "analyze-blame.h"
5229b03c 18#include "analyze-calendar.h"
6488e844 19#include "analyze-capability.h"
c649b343 20#include "analyze-cat-config.h"
8f114904 21#include "analyze-chid.h"
5f43c97c 22#include "analyze-compare-versions.h"
edfea9fe 23#include "analyze-condition.h"
ef215fa7 24#include "analyze-critical-chain.h"
cccd2af6 25#include "analyze-dot.h"
25eb70af 26#include "analyze-dump.h"
73cb64c4 27#include "analyze-exit-status.h"
5f43c97c 28#include "analyze-fdstore.h"
08e36480 29#include "analyze-filesystems.h"
58e35960 30#include "analyze-has-tpm2.h"
fb8cc599 31#include "analyze-image-policy.h"
2acfc252 32#include "analyze-inspect-elf.h"
faaa6ea0 33#include "analyze-log-control.h"
f50535af 34#include "analyze-malloc.h"
f70c90f5 35#include "analyze-pcrs.h"
ba474dad 36#include "analyze-plot.h"
ec16f3b6 37#include "analyze-security.h"
2b04e72c 38#include "analyze-service-watchdogs.h"
8c5045f9 39#include "analyze-smbios11.h"
d30693f3 40#include "analyze-srk.h"
389638d3 41#include "analyze-syscall-filter.h"
f24f8e37 42#include "analyze-time.h"
30bddc06 43#include "analyze-timespan.h"
503ccaaa 44#include "analyze-timestamp.h"
0c2d69df 45#include "analyze-unit-files.h"
ad6e02e7 46#include "analyze-unit-gdb.h"
36258fbe 47#include "analyze-unit-paths.h"
9a08000d 48#include "analyze-unit-shell.h"
3f6fd1ba 49#include "analyze-verify.h"
a839c4b7 50#include "analyze-verify-util.h"
d6b4d1c7 51#include "build.h"
048ecf5b 52#include "bus-error.h"
a839c4b7 53#include "bus-unit-util.h"
4bde8743 54#include "bus-util.h"
6d86f4bd 55#include "calendarspec.h"
4bde8743 56#include "dissect-image.h"
ad6e02e7 57#include "extract-word.h"
8aa304d3 58#include "image-policy.h"
3f6fd1ba 59#include "log.h"
4bde8743 60#include "loop-util.h"
d665c7b2 61#include "main-func.h"
e5ea5c3a 62#include "mount-util.h"
9ea9d4cf 63#include "pager.h"
599c7c54 64#include "parse-argument.h"
6bedfcbb 65#include "parse-util.h"
854a42fb 66#include "path-util.h"
294bf0c3 67#include "pretty-print.h"
4bde8743 68#include "runtime-scope.h"
3cc3dc77 69#include "string-table.h"
4bde8743 70#include "string-util.h"
3f6fd1ba 71#include "strv.h"
760877e9 72#include "time-util.h"
a839c4b7 73#include "unit-def.h"
3f6fd1ba 74#include "unit-name.h"
a6bcef29 75#include "verbs.h"
2265fbf7 76
cccd2af6 77DotMode arg_dot = DEP_ALL;
3e7a029c 78CapabilityMode arg_capability = CAPABILITY_LITERAL;
cccd2af6 79char **arg_dot_from_patterns = NULL, **arg_dot_to_patterns = NULL;
ef215fa7 80usec_t arg_fuzz = 0;
08e36480 81PagerFlags arg_pager_flags = 0;
063c8382 82CatFlags arg_cat_flags = 0;
25eb70af 83BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
3bfc3414
YW
84char *arg_debugger = NULL;
85char **arg_debugger_args = NULL;
ba474dad 86const char *arg_host = NULL;
4870133b 87RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
cae7c282 88RecursiveErrors arg_recursive_errors = _RECURSIVE_ERRORS_INVALID;
57a22a3f
LP
89bool arg_man = true;
90bool arg_generators = false;
6e4918a9 91const char *arg_instance = "test_instance";
1592d2f9 92double arg_svg_timescale = 1.0;
93bool arg_detailed_svg = false;
c649b343 94char *arg_root = NULL;
e5ea5c3a 95static char *arg_image = NULL;
57a22a3f
LP
96char *arg_security_policy = NULL;
97bool arg_offline = false;
98unsigned arg_threshold = 100;
5229b03c
LP
99unsigned arg_iterations = 1;
100usec_t arg_base_time = USEC_INFINITY;
e82116e5 101char *arg_unit = NULL;
309a747f 102sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
08e36480 103bool arg_quiet = false;
57a22a3f 104char *arg_profile = NULL;
ff46b2f9
JZ
105bool arg_legend = true;
106bool arg_table = false;
84be0c71 107ImagePolicy *arg_image_policy = NULL;
9fed4ec2 108char *arg_drm_device_path = NULL;
bb150966 109
d665c7b2
YW
110STATIC_DESTRUCTOR_REGISTER(arg_dot_from_patterns, strv_freep);
111STATIC_DESTRUCTOR_REGISTER(arg_dot_to_patterns, strv_freep);
3bfc3414
YW
112STATIC_DESTRUCTOR_REGISTER(arg_debugger, freep);
113STATIC_DESTRUCTOR_REGISTER(arg_debugger_args, strv_freep);
782671bc 114STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
e5ea5c3a 115STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
ecfd082b 116STATIC_DESTRUCTOR_REGISTER(arg_security_policy, freep);
9fed4ec2 117STATIC_DESTRUCTOR_REGISTER(arg_drm_device_path, freep);
8de7929d 118STATIC_DESTRUCTOR_REGISTER(arg_unit, freep);
04469211 119STATIC_DESTRUCTOR_REGISTER(arg_profile, freep);
84be0c71 120STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
d665c7b2 121
25eb70af 122int acquire_bus(sd_bus **bus, bool *use_full_bus) {
fb507898 123 int r;
5c69b31c 124
f7e29336 125 if (use_full_bus && *use_full_bus) {
4870133b 126 r = bus_connect_transport(arg_transport, arg_host, arg_runtime_scope, bus);
fb507898
YW
127 if (IN_SET(r, 0, -EHOSTDOWN))
128 return r;
5c69b31c
GJ
129
130 *use_full_bus = false;
131 }
132
4870133b 133 return bus_connect_transport_systemd(arg_transport, arg_host, arg_runtime_scope, bus);
bf0e0a4d
ZJS
134}
135
cccd2af6 136int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv) {
4afd3348 137 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
988b9df2
LP
138 int r;
139
140 assert(bus);
141 assert(path);
142 assert(property);
143 assert(strv);
144
145 r = sd_bus_get_property_strv(
146 bus,
147 "org.freedesktop.systemd1",
148 path,
149 "org.freedesktop.systemd1.Unit",
150 property,
151 &error,
152 strv);
4ae25393 153 if (r < 0)
0ed3da7c 154 return log_error_errno(r, "Failed to get unit property %s: %s", property, bus_error_message(&error, r));
988b9df2
LP
155
156 return 0;
157}
158
30bddc06 159void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timespan) {
c269607f
ZJS
160 if (calendar && calendar_spec_from_string(p, NULL) >= 0)
161 log_notice("Hint: this expression is a valid calendar specification. "
162 "Use 'systemd-analyze calendar \"%s\"' instead?", p);
163 if (timestamp && parse_timestamp(p, NULL) >= 0)
164 log_notice("Hint: this expression is a valid timestamp. "
165 "Use 'systemd-analyze timestamp \"%s\"' instead?", p);
166 if (timespan && parse_time(p, NULL, USEC_PER_SEC) >= 0)
167 log_notice("Hint: this expression is a valid timespan. "
168 "Use 'systemd-analyze timespan \"%s\"' instead?", p);
6d86f4bd
LP
169}
170
a839c4b7
ZJS
171static int verb_transient_settings(int argc, char *argv[], void *userdata) {
172 assert(argc >= 2);
173
174 pager_open(arg_pager_flags);
175
176 bool first = true;
177 STRV_FOREACH(arg, strv_skip(argv, 1)) {
178 UnitType t;
179
180 t = unit_type_from_string(*arg);
181 if (t < 0)
182 return log_error_errno(t, "Invalid unit type '%s'.", *arg);
183
184 if (!first)
185 puts("");
186
187 bus_dump_transient_settings(t);
188 first = false;
189 }
190
191 return 0;
192}
193
a6bcef29 194static int help(int argc, char *argv[], void *userdata) {
49139a5d 195 _cleanup_free_ char *link = NULL, *dot_link = NULL;
37ec0fdd 196 int r;
9ea9d4cf 197
384c2c32 198 pager_open(arg_pager_flags);
9ea9d4cf 199
37ec0fdd
LP
200 r = terminal_urlify_man("systemd-analyze", "1", &link);
201 if (r < 0)
202 return log_oom();
203
49139a5d
LP
204 /* Not using terminal_urlify_man() for this, since we don't want the "man page" text suffix in this case. */
205 r = terminal_urlify("man:dot(1)", "dot(1)", &dot_link);
206 if (r < 0)
207 return log_oom();
208
fd4b8484
LP
209 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
210 "%5$sProfile systemd, show unit dependencies, check unit files.%6$s\n"
645a3fa9 211 "\n%3$sBoot Analysis:%4$s\n"
3cc3dc77
MG
212 " [time] Print time required to boot the machine\n"
213 " blame Print list of running units ordered by\n"
214 " time to init\n"
215 " critical-chain [UNIT...] Print a tree of the time critical chain\n"
216 " of units\n"
645a3fa9 217 "\n%3$sDependency Analysis:%4$s\n"
3cc3dc77
MG
218 " plot Output SVG graphic showing service\n"
219 " initialization\n"
fd4b8484 220 " dot [UNIT...] Output dependency graph in %7$s format\n"
d1d8786c 221 " dump [PATTERN...] Output state serialization of service\n"
3cc3dc77 222 " manager\n"
645a3fa9 223 "\n%3$sConfiguration Files and Search Paths:%4$s\n"
ca029693 224 " cat-config NAME|PATH... Show configuration file and drop-ins\n"
3cc3dc77
MG
225 " unit-files List files and symlinks for units\n"
226 " unit-paths List load directories for units\n"
645a3fa9 227 "\n%3$sEnumerate OS Concepts:%4$s\n"
3cc3dc77
MG
228 " exit-status [STATUS...] List exit status definitions\n"
229 " capability [CAP...] List capability definitions\n"
52117f5a
ZJS
230 " syscall-filter [NAME...] List syscalls in seccomp filters\n"
231 " filesystems [NAME...] List known filesystems\n"
fb8cc599 232 " architectures [NAME...] List known architectures\n"
645a3fa9 233 " smbios11 List strings passed via SMBIOS Type #11\n"
8f114904 234 " chid List local CHIDs\n"
a839c4b7 235 " transient-settings TYPE... List transient settings for unit TYPE\n"
645a3fa9 236 "\n%3$sExpression Evaluation:%4$s\n"
3cc3dc77 237 " condition CONDITION... Evaluate conditions and asserts\n"
bc012a3e
ZJS
238 " compare-versions VERSION1 [OP] VERSION2\n"
239 " Compare two version strings\n"
645a3fa9
LP
240 " image-policy POLICY... Analyze image policy string\n"
241 "\n%3$sClock & Time:%4$s\n"
3cc3dc77
MG
242 " calendar SPEC... Validate repetitive calendar time\n"
243 " events\n"
244 " timestamp TIMESTAMP... Validate a timestamp\n"
245 " timespan SPAN... Validate a time span\n"
645a3fa9
LP
246 "\n%3$sUnit & Service Analysis:%4$s\n"
247 " verify FILE... Check unit files for correctness\n"
3cc3dc77 248 " security [UNIT...] Analyze security of unit\n"
5f43c97c 249 " fdstore SERVICE... Show file descriptor store contents of service\n"
645a3fa9 250 " malloc [D-BUS SERVICE...] Dump malloc stats of a D-Bus service\n"
ad6e02e7 251 " unit-gdb SERVICE Attach a debugger to the given running service\n"
9a08000d
Z
252 " unit-shell SERVICE [Command]\n"
253 " Run command on the namespace of the service\n"
645a3fa9
LP
254 "\n%3$sExecutable Analysis:%4$s\n"
255 " inspect-elf FILE... Parse and print ELF package metadata\n"
256 "\n%3$sTPM Operations:%4$s\n"
58e35960 257 " has-tpm2 Report whether TPM2 support is available\n"
f70c90f5 258 " pcrs [PCR...] Show TPM2 PCRs and their names\n"
0615abef 259 " srk [>FILE] Write TPM2 SRK (to FILE)\n"
fd4b8484 260 "\n%3$sOptions:%4$s\n"
3cc3dc77 261 " --recursive-errors=MODE Control which units are verified\n"
bb43d853 262 " --offline=BOOL Perform a security review on unit file(s)\n"
dfbda879
MG
263 " --threshold=N Exit with a non-zero status when overall\n"
264 " exposure level is over threshold value\n"
ecfd082b
MG
265 " --security-policy=PATH Use custom JSON security policy instead\n"
266 " of built-in one\n"
4b4a8ef7 267 " --json=pretty|short|off Generate JSON output of the security\n"
ff46b2f9 268 " analysis table, or plot's raw time data\n"
3cc3dc77 269 " --no-pager Do not pipe output into a pager\n"
ff46b2f9
JZ
270 " --no-legend Disable column headers and hints in plot\n"
271 " with either --table or --json=\n"
3cc3dc77
MG
272 " --system Operate on system systemd instance\n"
273 " --user Operate on user systemd instance\n"
274 " --global Operate on global user configuration\n"
275 " -H --host=[USER@]HOST Operate on remote host\n"
276 " -M --machine=CONTAINER Operate on local container\n"
277 " --order Show only order in the graph\n"
278 " --require Show only requirement in the graph\n"
279 " --from-pattern=GLOB Show only origins in the graph\n"
280 " --to-pattern=GLOB Show only destinations in the graph\n"
281 " --fuzz=SECONDS Also print services which finished SECONDS\n"
282 " earlier than the latest in the branch\n"
283 " --man[=BOOL] Do [not] check for existence of man pages\n"
284 " --generators[=BOOL] Do [not] run unit generators\n"
285 " (requires privileges)\n"
6e4918a9 286 " --instance=NAME Specify fallback instance name for template units\n"
3cc3dc77
MG
287 " --iterations=N Show the specified number of iterations\n"
288 " --base-time=TIMESTAMP Calculate calendar times relative to\n"
289 " specified time\n"
04469211
LB
290 " --profile=name|PATH Include the specified profile in the\n"
291 " security review of the unit(s)\n"
5e163285 292 " --unit=UNIT Evaluate conditions and asserts of unit\n"
ff46b2f9 293 " --table Output plot's raw time data as a table\n"
1592d2f9 294 " --scale-svg=FACTOR Stretch x-axis of plot by FACTOR (default: 1.0)\n"
295 " --detailed Add more details to SVG plot,\n"
296 " e.g. show activation timestamps\n"
52117f5a
ZJS
297 " -h --help Show this help\n"
298 " --version Show package version\n"
299 " -q --quiet Do not emit hints\n"
063c8382 300 " --tldr Skip comments and empty lines\n"
9f5b68d6
LP
301 " --root=PATH Operate on an alternate filesystem root\n"
302 " --image=PATH Operate on disk image as filesystem root\n"
84be0c71 303 " --image-policy=POLICY Specify disk image dissection policy\n"
4d09f976 304 " -m --mask Parse parameter as numeric capability mask\n"
9fed4ec2 305 " --drm-device=PATH Use this DRM device sysfs path to get EDID\n"
ad6e02e7
Z
306 " --debugger=DEBUGGER Use the given debugger\n"
307 " -A --debugger-arguments=ARGS\n"
308 " Pass the given arguments to the debugger\n"
9fed4ec2 309
fd4b8484 310 "\nSee the %2$s for details.\n",
bc556335 311 program_invocation_short_name,
fd4b8484
LP
312 link,
313 ansi_underline(),
314 ansi_normal(),
bc556335
DDM
315 ansi_highlight(),
316 ansi_normal(),
fd4b8484 317 dot_link);
96de7c04 318
1ace223c
SJ
319 /* When updating this list, including descriptions, apply changes to
320 * shell-completion/bash/systemd-analyze and shell-completion/zsh/_systemd-analyze too. */
a6bcef29
LP
321
322 return 0;
2265fbf7
SP
323}
324
9ea9d4cf 325static int parse_argv(int argc, char *argv[]) {
2265fbf7
SP
326 enum {
327 ARG_VERSION = 0x100,
1700761b
SP
328 ARG_ORDER,
329 ARG_REQUIRE,
46d8646a 330 ARG_ROOT,
e5ea5c3a 331 ARG_IMAGE,
06e78680 332 ARG_IMAGE_POLICY,
e55933db 333 ARG_SYSTEM,
28b35ef2
ZJS
334 ARG_USER,
335 ARG_GLOBAL,
e55933db 336 ARG_DOT_FROM_PATTERN,
bb150966 337 ARG_DOT_TO_PATTERN,
9ea9d4cf 338 ARG_FUZZ,
1d3bc017 339 ARG_NO_PAGER,
dad29dff 340 ARG_MAN,
641c0fd1 341 ARG_GENERATORS,
6e4918a9 342 ARG_INSTANCE,
f2ccf832 343 ARG_ITERATIONS,
985c1880 344 ARG_BASE_TIME,
3cc3dc77 345 ARG_RECURSIVE_ERRORS,
bb43d853 346 ARG_OFFLINE,
dfbda879 347 ARG_THRESHOLD,
ecfd082b 348 ARG_SECURITY_POLICY,
4b4a8ef7 349 ARG_JSON,
04469211 350 ARG_PROFILE,
ff46b2f9
JZ
351 ARG_TABLE,
352 ARG_NO_LEGEND,
063c8382 353 ARG_TLDR,
1592d2f9 354 ARG_SCALE_FACTOR_SVG,
355 ARG_DETAILED_SVG,
9fed4ec2 356 ARG_DRM_DEVICE_PATH,
ad6e02e7 357 ARG_DEBUGGER,
2265fbf7
SP
358 };
359
360 static const struct option options[] = {
ad6e02e7
Z
361 { "help", no_argument, NULL, 'h' },
362 { "version", no_argument, NULL, ARG_VERSION },
363 { "quiet", no_argument, NULL, 'q' },
364 { "order", no_argument, NULL, ARG_ORDER },
365 { "require", no_argument, NULL, ARG_REQUIRE },
366 { "root", required_argument, NULL, ARG_ROOT },
367 { "image", required_argument, NULL, ARG_IMAGE },
368 { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
369 { "recursive-errors" , required_argument, NULL, ARG_RECURSIVE_ERRORS },
370 { "offline", required_argument, NULL, ARG_OFFLINE },
371 { "threshold", required_argument, NULL, ARG_THRESHOLD },
372 { "security-policy", required_argument, NULL, ARG_SECURITY_POLICY },
373 { "system", no_argument, NULL, ARG_SYSTEM },
374 { "user", no_argument, NULL, ARG_USER },
375 { "global", no_argument, NULL, ARG_GLOBAL },
376 { "from-pattern", required_argument, NULL, ARG_DOT_FROM_PATTERN },
377 { "to-pattern", required_argument, NULL, ARG_DOT_TO_PATTERN },
378 { "fuzz", required_argument, NULL, ARG_FUZZ },
379 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
380 { "man", optional_argument, NULL, ARG_MAN },
381 { "generators", optional_argument, NULL, ARG_GENERATORS },
382 { "instance", required_argument, NULL, ARG_INSTANCE },
383 { "host", required_argument, NULL, 'H' },
384 { "machine", required_argument, NULL, 'M' },
385 { "iterations", required_argument, NULL, ARG_ITERATIONS },
386 { "base-time", required_argument, NULL, ARG_BASE_TIME },
387 { "unit", required_argument, NULL, 'U' },
388 { "json", required_argument, NULL, ARG_JSON },
389 { "profile", required_argument, NULL, ARG_PROFILE },
390 { "table", optional_argument, NULL, ARG_TABLE },
391 { "no-legend", optional_argument, NULL, ARG_NO_LEGEND },
392 { "tldr", no_argument, NULL, ARG_TLDR },
393 { "mask", no_argument, NULL, 'm' },
394 { "scale-svg", required_argument, NULL, ARG_SCALE_FACTOR_SVG },
395 { "detailed", no_argument, NULL, ARG_DETAILED_SVG },
396 { "drm-device", required_argument, NULL, ARG_DRM_DEVICE_PATH },
397 { "debugger", required_argument, NULL, ARG_DEBUGGER },
398 { "debugger-arguments", required_argument, NULL, 'A' },
eb9da376 399 {}
2265fbf7
SP
400 };
401
9a08000d
Z
402 bool reorder = false;
403 int r, c, unit_shell = -1;
eb9da376 404
2265fbf7
SP
405 assert(argc >= 0);
406 assert(argv);
407
9a08000d
Z
408 /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
409 * that checks for GNU extensions in optstring ('-' or '+; at the beginning). */
410 optind = 0;
411
412 for (;;) {
ad6e02e7 413 static const char option_string[] = "-hqH:M:U:mA:";
9a08000d
Z
414
415 c = getopt_long(argc, argv, option_string + reorder, options, NULL);
416 if (c < 0)
417 break;
418
eb9da376 419 switch (c) {
c170f3a4 420
9a08000d
Z
421 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
422 * non-option argument was discovered. */
423
424 assert(!reorder);
425
426 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
427 * for switches after the main verb. However, for "unit-shell" we really don't want that, since we
428 * want that switches specified after the service name are passed to the program to execute,
429 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
430 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
431 * non-option parameter. If it's the verb "unit-shell" we remember its position and continue
432 * processing options. In this case, as soon as we hit the next non-option argument we found
433 * the service name, and stop further processing. If the first non-option argument is any other
434 * verb than "unit-shell" we switch to normal reordering mode and continue processing arguments
435 * normally. */
436
437 if (unit_shell >= 0) {
f89a52e3 438 optind--; /* don't process this argument, go one step back */
9a08000d
Z
439 goto done;
440 }
441 if (streq(optarg, "unit-shell"))
442 /* Remember the position of the "unit_shell" verb, and continue processing normally. */
443 unit_shell = optind - 1;
444 else {
445 int saved_optind;
446
447 /* Ok, this is some other verb. In this case, turn on reordering again, and continue
448 * processing normally. */
449 reorder = true;
450
451 /* We changed the option string. getopt_long() only looks at it again if we invoke it
452 * at least once with a reset option index. Hence, let's reset the option index here,
453 * then invoke getopt_long() again (ignoring what it has to say, after all we most
454 * likely already processed it), and the bump the option index so that we read the
455 * intended argument again. */
456 saved_optind = optind;
457 optind = 0;
458 (void) getopt_long(argc, argv, option_string + reorder, options, NULL);
459 optind = saved_optind - 1; /* go one step back, process this argument again */
460 }
461
462 break;
463
c170f3a4 464 case 'h':
a6bcef29 465 return help(0, NULL, NULL);
c170f3a4 466
52117f5a
ZJS
467 case ARG_VERSION:
468 return version();
469
470 case 'q':
471 arg_quiet = true;
472 break;
473
3cc3dc77
MG
474 case ARG_RECURSIVE_ERRORS:
475 if (streq(optarg, "help")) {
476 DUMP_STRING_TABLE(recursive_errors, RecursiveErrors, _RECURSIVE_ERRORS_MAX);
477 return 0;
478 }
479 r = recursive_errors_from_string(optarg);
480 if (r < 0)
481 return log_error_errno(r, "Unknown mode passed to --recursive-errors='%s'.", optarg);
482
483 arg_recursive_errors = r;
484 break;
485
46d8646a 486 case ARG_ROOT:
782671bc
MG
487 r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root);
488 if (r < 0)
489 return r;
46d8646a
ZJS
490 break;
491
e5ea5c3a
MG
492 case ARG_IMAGE:
493 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
494 if (r < 0)
495 return r;
496 break;
497
06e78680
YW
498 case ARG_IMAGE_POLICY:
499 r = parse_image_policy_argument(optarg, &arg_image_policy);
500 if (r < 0)
501 return r;
502 break;
503
28b35ef2 504 case ARG_SYSTEM:
4870133b 505 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
28b35ef2
ZJS
506 break;
507
c170f3a4 508 case ARG_USER:
4870133b 509 arg_runtime_scope = RUNTIME_SCOPE_USER;
c170f3a4
LP
510 break;
511
28b35ef2 512 case ARG_GLOBAL:
4870133b 513 arg_runtime_scope = RUNTIME_SCOPE_GLOBAL;
c170f3a4
LP
514 break;
515
516 case ARG_ORDER:
517 arg_dot = DEP_ORDER;
518 break;
519
520 case ARG_REQUIRE:
521 arg_dot = DEP_REQUIRE;
522 break;
523
e55933db 524 case ARG_DOT_FROM_PATTERN:
903a0b07
LP
525 if (strv_extend(&arg_dot_from_patterns, optarg) < 0)
526 return log_oom();
527
e55933db
ŁS
528 break;
529
530 case ARG_DOT_TO_PATTERN:
903a0b07
LP
531 if (strv_extend(&arg_dot_to_patterns, optarg) < 0)
532 return log_oom();
533
e55933db
ŁS
534 break;
535
bb150966
HH
536 case ARG_FUZZ:
537 r = parse_sec(optarg, &arg_fuzz);
538 if (r < 0)
539 return r;
540 break;
541
9ea9d4cf 542 case ARG_NO_PAGER:
0221d68a 543 arg_pager_flags |= PAGER_DISABLE;
9ea9d4cf
LP
544 break;
545
3cd26e7c
LP
546 case 'H':
547 arg_transport = BUS_TRANSPORT_REMOTE;
548 arg_host = optarg;
549 break;
550
551 case 'M':
a59cc386
MY
552 r = parse_machine_argument(optarg, &arg_host, &arg_transport);
553 if (r < 0)
554 return r;
3cd26e7c
LP
555 break;
556
dad29dff 557 case ARG_MAN:
599c7c54
ZJS
558 r = parse_boolean_argument("--man", optarg, &arg_man);
559 if (r < 0)
560 return r;
1d3bc017
ZJS
561 break;
562
641c0fd1 563 case ARG_GENERATORS:
599c7c54
ZJS
564 r = parse_boolean_argument("--generators", optarg, &arg_generators);
565 if (r < 0)
566 return r;
641c0fd1
ZJS
567 break;
568
6e4918a9
YW
569 case ARG_INSTANCE:
570 arg_instance = optarg;
571 break;
572
bb43d853
MG
573 case ARG_OFFLINE:
574 r = parse_boolean_argument("--offline", optarg, &arg_offline);
575 if (r < 0)
576 return r;
577 break;
578
dfbda879
MG
579 case ARG_THRESHOLD:
580 r = safe_atou(optarg, &arg_threshold);
581 if (r < 0 || arg_threshold > 100)
582 return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL), "Failed to parse threshold: %s", optarg);
583
584 break;
585
ecfd082b
MG
586 case ARG_SECURITY_POLICY:
587 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_security_policy);
588 if (r < 0)
589 return r;
590 break;
591
4b4a8ef7
MG
592 case ARG_JSON:
593 r = parse_json_argument(optarg, &arg_json_format_flags);
594 if (r <= 0)
595 return r;
596 break;
597
f2ccf832
LP
598 case ARG_ITERATIONS:
599 r = safe_atou(optarg, &arg_iterations);
600 if (r < 0)
601 return log_error_errno(r, "Failed to parse iterations: %s", optarg);
f2ccf832
LP
602 break;
603
985c1880
LP
604 case ARG_BASE_TIME:
605 r = parse_timestamp(optarg, &arg_base_time);
606 if (r < 0)
607 return log_error_errno(r, "Failed to parse --base-time= parameter: %s", optarg);
985c1880
LP
608 break;
609
04469211
LB
610 case ARG_PROFILE:
611 if (isempty(optarg))
612 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Profile file name is empty");
613
614 if (is_path(optarg)) {
615 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_profile);
616 if (r < 0)
617 return r;
618 if (!endswith(arg_profile, ".conf"))
619 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Profile file name must end with .conf: %s", arg_profile);
620 } else {
621 r = free_and_strdup(&arg_profile, optarg);
622 if (r < 0)
623 return log_oom();
624 }
625
626 break;
627
8de7929d
DDM
628 case 'U': {
629 _cleanup_free_ char *mangled = NULL;
630
631 r = unit_name_mangle(optarg, UNIT_NAME_MANGLE_WARN, &mangled);
632 if (r < 0)
633 return log_error_errno(r, "Failed to mangle unit name %s: %m", optarg);
634
635 free_and_replace(arg_unit, mangled);
636 break;
637 }
ff46b2f9
JZ
638
639 case ARG_TABLE:
640 arg_table = true;
641 break;
642
643 case ARG_NO_LEGEND:
644 arg_legend = false;
645 break;
646
063c8382
ZJS
647 case ARG_TLDR:
648 arg_cat_flags = CAT_TLDR;
649 break;
650
3e7a029c
IS
651 case 'm':
652 arg_capability = CAPABILITY_MASK;
653 break;
654
1592d2f9 655 case ARG_SCALE_FACTOR_SVG:
656 arg_svg_timescale = strtod(optarg, NULL);
657 break;
658
659 case ARG_DETAILED_SVG:
660 arg_detailed_svg = true;
661 break;
662
9fed4ec2 663 case ARG_DRM_DEVICE_PATH:
664 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_drm_device_path);
665 if (r < 0)
666 return r;
667 break;
668
ad6e02e7 669 case ARG_DEBUGGER:
3bfc3414
YW
670 r = free_and_strdup_warn(&arg_debugger, optarg);
671 if (r < 0)
672 return r;
ad6e02e7
Z
673 break;
674
675 case 'A': {
676 _cleanup_strv_free_ char **l = NULL;
677 r = strv_split_full(&l, optarg, WHITESPACE, EXTRACT_UNQUOTE);
678 if (r < 0)
679 return log_error_errno(r, "Failed to parse debugger arguments '%s': %m", optarg);
680 strv_free_and_replace(arg_debugger_args, l);
681 break;
682 }
683
c170f3a4
LP
684 case '?':
685 return -EINVAL;
686
687 default:
04499a70 688 assert_not_reached();
2265fbf7 689 }
9a08000d
Z
690 }
691
692done:
693 if (unit_shell >= 0) {
694 char *t;
695
696 /* We found the "unit-shell" verb while processing the argument list. Since we turned off reordering of the
697 * argument list initially let's readjust it now, and move the "unit-shell" verb to the back. */
698
699 optind -= 1; /* place the option index where the "unit-shell" verb will be placed */
700
701 t = argv[unit_shell];
702 for (int i = unit_shell; i < optind; i++)
703 argv[i] = argv[i+1];
704 argv[optind] = t;
705 }
eb9da376 706
bb43d853
MG
707 if (arg_offline && !streq_ptr(argv[optind], "security"))
708 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
709 "Option --offline= is only supported for security right now.");
710
5e163285
AAF
711 if (arg_offline && optind >= argc - 1)
712 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
713 "Option --offline= requires one or more units to perform a security review.");
714
dfbda879
MG
715 if (arg_threshold != 100 && !streq_ptr(argv[optind], "security"))
716 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
717 "Option --threshold= is only supported for security right now.");
718
ac970536 719 if (arg_runtime_scope == RUNTIME_SCOPE_GLOBAL && !streq_ptr(argv[optind], "unit-paths"))
baaa35ad 720 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
ac970536 721 "Option --global only makes sense with verb unit-paths.");
31a5924e 722
4870133b 723 if (streq_ptr(argv[optind], "cat-config") && arg_runtime_scope == RUNTIME_SCOPE_USER)
f1d9d36a
ZJS
724 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
725 "Option --user is not supported for cat-config right now.");
726
ecfd082b
MG
727 if (arg_security_policy && !streq_ptr(argv[optind], "security"))
728 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
729 "Option --security-policy= is only supported for security.");
730
ad6e02e7 731 if ((arg_root || arg_image) && (!STRPTR_IN_SET(argv[optind], "cat-config", "verify", "condition", "inspect-elf", "unit-gdb")) &&
bb43d853 732 (!(streq_ptr(argv[optind], "security") && arg_offline)))
baaa35ad 733 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
ad6e02e7 734 "Options --root= and --image= are only supported for cat-config, verify, condition, unit-gdb, and security when used with --offline= right now.");
e5ea5c3a
MG
735
736 /* Having both an image and a root is not supported by the code */
737 if (arg_root && arg_image)
738 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
46d8646a 739
8de7929d
DDM
740 if (arg_unit && !streq_ptr(argv[optind], "condition"))
741 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --unit= is only supported for condition");
742
743 if (streq_ptr(argv[optind], "condition") && !arg_unit && optind >= argc - 1)
744 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too few arguments for condition");
745
746 if (streq_ptr(argv[optind], "condition") && arg_unit && optind < argc - 1)
747 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No conditions can be passed if --unit= is used.");
748
ff46b2f9
JZ
749 if (arg_table && !streq_ptr(argv[optind], "plot"))
750 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --table is only supported for plot right now.");
751
23441a3d 752 if (arg_table && sd_json_format_enabled(arg_json_format_flags))
ff46b2f9
JZ
753 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--table and --json= are mutually exclusive.");
754
3e7a029c
IS
755 if (arg_capability != CAPABILITY_LITERAL && !streq_ptr(argv[optind], "capability"))
756 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --mask is only supported for capability.");
757
9fed4ec2 758 if (arg_drm_device_path && !streq_ptr(argv[optind], "chid"))
759 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --drm-device is only supported for chid right now.");
760
1d3bc017 761 return 1; /* work to do */
2265fbf7
SP
762}
763
d665c7b2 764static int run(int argc, char *argv[]) {
e5ea5c3a 765 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
a4b3e942 766 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
a6bcef29
LP
767
768 static const Verb verbs[] = {
a839c4b7
ZJS
769 { "help", VERB_ANY, VERB_ANY, 0, help },
770 { "time", VERB_ANY, 1, VERB_DEFAULT, verb_time },
771 { "blame", VERB_ANY, 1, 0, verb_blame },
772 { "critical-chain", VERB_ANY, VERB_ANY, 0, verb_critical_chain },
773 { "plot", VERB_ANY, 1, 0, verb_plot },
774 { "dot", VERB_ANY, VERB_ANY, 0, verb_dot },
ab86ccba 775 /* ↓ The following seven verbs are deprecated, from here … ↓ */
a839c4b7
ZJS
776 { "log-level", VERB_ANY, 2, 0, verb_log_control },
777 { "log-target", VERB_ANY, 2, 0, verb_log_control },
778 { "set-log-level", 2, 2, 0, verb_log_control },
779 { "get-log-level", VERB_ANY, 1, 0, verb_log_control },
780 { "set-log-target", 2, 2, 0, verb_log_control },
781 { "get-log-target", VERB_ANY, 1, 0, verb_log_control },
782 { "service-watchdogs", VERB_ANY, 2, 0, verb_service_watchdogs },
ab86ccba 783 /* ↑ … until here ↑ */
a839c4b7
ZJS
784 { "dump", VERB_ANY, VERB_ANY, 0, verb_dump },
785 { "cat-config", 2, VERB_ANY, 0, verb_cat_config },
786 { "unit-files", VERB_ANY, VERB_ANY, 0, verb_unit_files },
ad6e02e7 787 { "unit-gdb", 2, VERB_ANY, 0, verb_unit_gdb },
a839c4b7 788 { "unit-paths", 1, 1, 0, verb_unit_paths },
9a08000d 789 { "unit-shell", 2, VERB_ANY, 0, verb_unit_shell },
a839c4b7
ZJS
790 { "exit-status", VERB_ANY, VERB_ANY, 0, verb_exit_status },
791 { "syscall-filter", VERB_ANY, VERB_ANY, 0, verb_syscall_filters },
792 { "capability", VERB_ANY, VERB_ANY, 0, verb_capabilities },
793 { "filesystems", VERB_ANY, VERB_ANY, 0, verb_filesystems },
794 { "condition", VERB_ANY, VERB_ANY, 0, verb_condition },
795 { "compare-versions", 3, 4, 0, verb_compare_versions },
796 { "verify", 2, VERB_ANY, 0, verb_verify },
797 { "calendar", 2, VERB_ANY, 0, verb_calendar },
798 { "timestamp", 2, VERB_ANY, 0, verb_timestamp },
799 { "timespan", 2, VERB_ANY, 0, verb_timespan },
800 { "security", VERB_ANY, VERB_ANY, 0, verb_security },
801 { "inspect-elf", 2, VERB_ANY, 0, verb_elf_inspection },
802 { "malloc", VERB_ANY, VERB_ANY, 0, verb_malloc },
803 { "fdstore", 2, VERB_ANY, 0, verb_fdstore },
804 { "image-policy", 2, 2, 0, verb_image_policy },
805 { "has-tpm2", VERB_ANY, 1, 0, verb_has_tpm2 },
806 { "pcrs", VERB_ANY, VERB_ANY, 0, verb_pcrs },
807 { "srk", VERB_ANY, 1, 0, verb_srk },
808 { "architectures", VERB_ANY, VERB_ANY, 0, verb_architectures },
809 { "smbios11", VERB_ANY, 1, 0, verb_smbios11 },
810 { "chid", VERB_ANY, VERB_ANY, 0, verb_chid },
811 { "transient-settings", 2, VERB_ANY, 0, verb_transient_settings },
a6bcef29
LP
812 {}
813 };
814
5220a6f3 815 int r;
2265fbf7
SP
816
817 setlocale(LC_ALL, "");
c170f3a4 818 setlocale(LC_NUMERIC, "C"); /* we want to format/parse floats in C style */
a6bcef29 819
d2acb93d 820 log_setup();
2265fbf7
SP
821
822 r = parse_argv(argc, argv);
9ea9d4cf 823 if (r <= 0)
d665c7b2 824 return r;
c170f3a4 825
e5ea5c3a
MG
826 /* Open up and mount the image */
827 if (arg_image) {
828 assert(!arg_root);
829
830 r = mount_image_privately_interactively(
831 arg_image,
84be0c71 832 arg_image_policy,
e5ea5c3a
MG
833 DISSECT_IMAGE_GENERIC_ROOT |
834 DISSECT_IMAGE_RELAX_VAR_CHECK |
f4a63ce2
LP
835 DISSECT_IMAGE_READ_ONLY |
836 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
a4b3e942 837 &mounted_dir,
a133d2c3 838 /* ret_dir_fd= */ NULL,
e330f97a 839 &loop_device);
e5ea5c3a
MG
840 if (r < 0)
841 return r;
842
a4b3e942 843 arg_root = strdup(mounted_dir);
e5ea5c3a
MG
844 if (!arg_root)
845 return log_oom();
846 }
847
d665c7b2 848 return dispatch_verb(argc, argv, verbs, NULL);
2265fbf7 849}
d665c7b2 850
fddad5f4 851DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);