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