]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/analyze/analyze.c
man: systemd-measure. Remove 'tpm2-pcrs=' from cryptenroll command (#39590)
[thirdparty/systemd.git] / src / analyze / analyze.c
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/***
3 Copyright © 2013 Simon Peeters
4***/
5
6#include <getopt.h>
7#include <locale.h>
8#include <stdio.h>
9#include <stdlib.h>
10
11#include "sd-bus.h"
12#include "sd-json.h"
13
14#include "alloc-util.h"
15#include "analyze.h"
16#include "analyze-architectures.h"
17#include "analyze-blame.h"
18#include "analyze-calendar.h"
19#include "analyze-capability.h"
20#include "analyze-cat-config.h"
21#include "analyze-chid.h"
22#include "analyze-compare-versions.h"
23#include "analyze-condition.h"
24#include "analyze-critical-chain.h"
25#include "analyze-dlopen-metadata.h"
26#include "analyze-dot.h"
27#include "analyze-dump.h"
28#include "analyze-exit-status.h"
29#include "analyze-fdstore.h"
30#include "analyze-filesystems.h"
31#include "analyze-has-tpm2.h"
32#include "analyze-image-policy.h"
33#include "analyze-inspect-elf.h"
34#include "analyze-log-control.h"
35#include "analyze-malloc.h"
36#include "analyze-nvpcrs.h"
37#include "analyze-pcrs.h"
38#include "analyze-plot.h"
39#include "analyze-security.h"
40#include "analyze-service-watchdogs.h"
41#include "analyze-smbios11.h"
42#include "analyze-srk.h"
43#include "analyze-syscall-filter.h"
44#include "analyze-time.h"
45#include "analyze-timespan.h"
46#include "analyze-timestamp.h"
47#include "analyze-unit-files.h"
48#include "analyze-unit-gdb.h"
49#include "analyze-unit-paths.h"
50#include "analyze-unit-shell.h"
51#include "analyze-verify.h"
52#include "analyze-verify-util.h"
53#include "build.h"
54#include "bus-error.h"
55#include "bus-unit-util.h"
56#include "bus-util.h"
57#include "calendarspec.h"
58#include "dissect-image.h"
59#include "extract-word.h"
60#include "image-policy.h"
61#include "log.h"
62#include "loop-util.h"
63#include "main-func.h"
64#include "mount-util.h"
65#include "pager.h"
66#include "parse-argument.h"
67#include "parse-util.h"
68#include "path-util.h"
69#include "pretty-print.h"
70#include "runtime-scope.h"
71#include "string-table.h"
72#include "string-util.h"
73#include "strv.h"
74#include "time-util.h"
75#include "unit-def.h"
76#include "unit-name.h"
77#include "verbs.h"
78
79DotMode arg_dot = DEP_ALL;
80CapabilityMode arg_capability = CAPABILITY_LITERAL;
81char **arg_dot_from_patterns = NULL, **arg_dot_to_patterns = NULL;
82usec_t arg_fuzz = 0;
83PagerFlags arg_pager_flags = 0;
84CatFlags arg_cat_flags = 0;
85BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
86char *arg_debugger = NULL;
87char **arg_debugger_args = NULL;
88const char *arg_host = NULL;
89RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
90RecursiveErrors arg_recursive_errors = _RECURSIVE_ERRORS_INVALID;
91bool arg_man = true;
92bool arg_generators = false;
93const char *arg_instance = "test_instance";
94double arg_svg_timescale = 1.0;
95bool arg_detailed_svg = false;
96char *arg_root = NULL;
97static char *arg_image = NULL;
98char *arg_security_policy = NULL;
99bool arg_offline = false;
100unsigned arg_threshold = 100;
101unsigned arg_iterations = 1;
102usec_t arg_base_time = USEC_INFINITY;
103char *arg_unit = NULL;
104sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
105bool arg_quiet = false;
106char *arg_profile = NULL;
107bool arg_legend = true;
108bool arg_table = false;
109ImagePolicy *arg_image_policy = NULL;
110char *arg_drm_device_path = NULL;
111
112STATIC_DESTRUCTOR_REGISTER(arg_dot_from_patterns, strv_freep);
113STATIC_DESTRUCTOR_REGISTER(arg_dot_to_patterns, strv_freep);
114STATIC_DESTRUCTOR_REGISTER(arg_debugger, freep);
115STATIC_DESTRUCTOR_REGISTER(arg_debugger_args, strv_freep);
116STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
117STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
118STATIC_DESTRUCTOR_REGISTER(arg_security_policy, freep);
119STATIC_DESTRUCTOR_REGISTER(arg_drm_device_path, freep);
120STATIC_DESTRUCTOR_REGISTER(arg_unit, freep);
121STATIC_DESTRUCTOR_REGISTER(arg_profile, freep);
122STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
123
124int acquire_bus(sd_bus **bus, bool *use_full_bus) {
125 int r;
126
127 if (use_full_bus && *use_full_bus) {
128 r = bus_connect_transport(arg_transport, arg_host, arg_runtime_scope, bus);
129 if (IN_SET(r, 0, -EHOSTDOWN))
130 return r;
131
132 *use_full_bus = false;
133 }
134
135 return bus_connect_transport_systemd(arg_transport, arg_host, arg_runtime_scope, bus);
136}
137
138int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv) {
139 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
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);
155 if (r < 0)
156 return log_error_errno(r, "Failed to get unit property %s: %s", property, bus_error_message(&error, r));
157
158 return 0;
159}
160
161void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timespan) {
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);
171}
172
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
196static int help(int argc, char *argv[], void *userdata) {
197 _cleanup_free_ char *link = NULL, *dot_link = NULL;
198 int r;
199
200 pager_open(arg_pager_flags);
201
202 r = terminal_urlify_man("systemd-analyze", "1", &link);
203 if (r < 0)
204 return log_oom();
205
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
211 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
212 "%5$sProfile systemd, show unit dependencies, check unit files.%6$s\n"
213 "\n%3$sBoot Analysis:%4$s\n"
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"
219 "\n%3$sDependency Analysis:%4$s\n"
220 " plot Output SVG graphic showing service\n"
221 " initialization\n"
222 " dot [UNIT...] Output dependency graph in %7$s format\n"
223 " dump [PATTERN...] Output state serialization of service\n"
224 " manager\n"
225 "\n%3$sConfiguration Files and Search Paths:%4$s\n"
226 " cat-config NAME|PATH... Show configuration file and drop-ins\n"
227 " unit-files List files and symlinks for units\n"
228 " unit-paths List load directories for units\n"
229 "\n%3$sEnumerate OS Concepts:%4$s\n"
230 " exit-status [STATUS...] List exit status definitions\n"
231 " capability [CAP...] List capability definitions\n"
232 " syscall-filter [NAME...] List syscalls in seccomp filters\n"
233 " filesystems [NAME...] List known filesystems\n"
234 " architectures [NAME...] List known architectures\n"
235 " smbios11 List strings passed via SMBIOS Type #11\n"
236 " chid List local CHIDs\n"
237 " transient-settings TYPE... List transient settings for unit TYPE\n"
238 "\n%3$sExpression Evaluation:%4$s\n"
239 " condition CONDITION... Evaluate conditions and asserts\n"
240 " compare-versions VERSION1 [OP] VERSION2\n"
241 " Compare two version strings\n"
242 " image-policy POLICY... Analyze image policy string\n"
243 "\n%3$sClock & Time:%4$s\n"
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"
248 "\n%3$sUnit & Service Analysis:%4$s\n"
249 " verify FILE... Check unit files for correctness\n"
250 " security [UNIT...] Analyze security of unit\n"
251 " fdstore SERVICE... Show file descriptor store contents of service\n"
252 " malloc [D-BUS SERVICE...] Dump malloc stats of a D-Bus service\n"
253 " unit-gdb SERVICE Attach a debugger to the given running service\n"
254 " unit-shell SERVICE [Command]\n"
255 " Run command on the namespace of the service\n"
256 "\n%3$sExecutable Analysis:%4$s\n"
257 " inspect-elf FILE... Parse and print ELF package metadata\n"
258 " dlopen-metadata FILE Parse and print ELF dlopen metadata\n"
259 "\n%3$sTPM Operations:%4$s\n"
260 " has-tpm2 Report whether TPM2 support is available\n"
261 " pcrs [PCR...] Show TPM2 PCRs and their names\n"
262 " nvpcrs [NVPCR...] Show additional TPM2 PCRs stored in NV indexes\n"
263 " srk [>FILE] Write TPM2 SRK (to FILE)\n"
264 "\n%3$sOptions:%4$s\n"
265 " --recursive-errors=MODE Control which units are verified\n"
266 " --offline=BOOL Perform a security review on unit file(s)\n"
267 " --threshold=N Exit with a non-zero status when overall\n"
268 " exposure level is over threshold value\n"
269 " --security-policy=PATH Use custom JSON security policy instead\n"
270 " of built-in one\n"
271 " --json=pretty|short|off Generate JSON output of the security\n"
272 " analysis table, or plot's raw time data\n"
273 " --no-pager Do not pipe output into a pager\n"
274 " --no-legend Disable column headers and hints in plot\n"
275 " with either --table or --json=\n"
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"
290 " --instance=NAME Specify fallback instance name for template units\n"
291 " --iterations=N Show the specified number of iterations\n"
292 " --base-time=TIMESTAMP Calculate calendar times relative to\n"
293 " specified time\n"
294 " --profile=name|PATH Include the specified profile in the\n"
295 " security review of the unit(s)\n"
296 " --unit=UNIT Evaluate conditions and asserts of unit\n"
297 " --table Output plot's raw time data as a table\n"
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"
301 " -h --help Show this help\n"
302 " --version Show package version\n"
303 " -q --quiet Do not emit hints\n"
304 " --tldr Skip comments and empty lines\n"
305 " --root=PATH Operate on an alternate filesystem root\n"
306 " --image=PATH Operate on disk image as filesystem root\n"
307 " --image-policy=POLICY Specify disk image dissection policy\n"
308 " -m --mask Parse parameter as numeric capability mask\n"
309 " --drm-device=PATH Use this DRM device sysfs path to get EDID\n"
310 " --debugger=DEBUGGER Use the given debugger\n"
311 " -A --debugger-arguments=ARGS\n"
312 " Pass the given arguments to the debugger\n"
313
314 "\nSee the %2$s for details.\n",
315 program_invocation_short_name,
316 link,
317 ansi_underline(),
318 ansi_normal(),
319 ansi_highlight(),
320 ansi_normal(),
321 dot_link);
322
323 /* When updating this list, including descriptions, apply changes to
324 * shell-completion/bash/systemd-analyze and shell-completion/zsh/_systemd-analyze too. */
325
326 return 0;
327}
328
329static int parse_argv(int argc, char *argv[]) {
330 enum {
331 ARG_VERSION = 0x100,
332 ARG_ORDER,
333 ARG_REQUIRE,
334 ARG_ROOT,
335 ARG_IMAGE,
336 ARG_IMAGE_POLICY,
337 ARG_SYSTEM,
338 ARG_USER,
339 ARG_GLOBAL,
340 ARG_DOT_FROM_PATTERN,
341 ARG_DOT_TO_PATTERN,
342 ARG_FUZZ,
343 ARG_NO_PAGER,
344 ARG_MAN,
345 ARG_GENERATORS,
346 ARG_INSTANCE,
347 ARG_ITERATIONS,
348 ARG_BASE_TIME,
349 ARG_RECURSIVE_ERRORS,
350 ARG_OFFLINE,
351 ARG_THRESHOLD,
352 ARG_SECURITY_POLICY,
353 ARG_JSON,
354 ARG_PROFILE,
355 ARG_TABLE,
356 ARG_NO_LEGEND,
357 ARG_TLDR,
358 ARG_SCALE_FACTOR_SVG,
359 ARG_DETAILED_SVG,
360 ARG_DRM_DEVICE_PATH,
361 ARG_DEBUGGER,
362 };
363
364 static const struct option options[] = {
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' },
403 {}
404 };
405
406 bool reorder = false;
407 int r, c, unit_shell = -1;
408
409 assert(argc >= 0);
410 assert(argv);
411
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 (;;) {
417 static const char option_string[] = "-hqH:M:U:mA:";
418
419 c = getopt_long(argc, argv, option_string + reorder, options, NULL);
420 if (c < 0)
421 break;
422
423 switch (c) {
424
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) {
442 optind--; /* don't process this argument, go one step back */
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
468 case 'h':
469 return help(0, NULL, NULL);
470
471 case ARG_VERSION:
472 return version();
473
474 case 'q':
475 arg_quiet = true;
476 break;
477
478 case ARG_RECURSIVE_ERRORS:
479 if (streq(optarg, "help"))
480 return DUMP_STRING_TABLE(recursive_errors, RecursiveErrors, _RECURSIVE_ERRORS_MAX);
481
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
489 case ARG_ROOT:
490 r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root);
491 if (r < 0)
492 return r;
493 break;
494
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
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
507 case ARG_SYSTEM:
508 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
509 break;
510
511 case ARG_USER:
512 arg_runtime_scope = RUNTIME_SCOPE_USER;
513 break;
514
515 case ARG_GLOBAL:
516 arg_runtime_scope = RUNTIME_SCOPE_GLOBAL;
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
527 case ARG_DOT_FROM_PATTERN:
528 if (strv_extend(&arg_dot_from_patterns, optarg) < 0)
529 return log_oom();
530
531 break;
532
533 case ARG_DOT_TO_PATTERN:
534 if (strv_extend(&arg_dot_to_patterns, optarg) < 0)
535 return log_oom();
536
537 break;
538
539 case ARG_FUZZ:
540 r = parse_sec(optarg, &arg_fuzz);
541 if (r < 0)
542 return r;
543 break;
544
545 case ARG_NO_PAGER:
546 arg_pager_flags |= PAGER_DISABLE;
547 break;
548
549 case 'H':
550 arg_transport = BUS_TRANSPORT_REMOTE;
551 arg_host = optarg;
552 break;
553
554 case 'M':
555 r = parse_machine_argument(optarg, &arg_host, &arg_transport);
556 if (r < 0)
557 return r;
558 break;
559
560 case ARG_MAN:
561 r = parse_boolean_argument("--man", optarg, &arg_man);
562 if (r < 0)
563 return r;
564 break;
565
566 case ARG_GENERATORS:
567 r = parse_boolean_argument("--generators", optarg, &arg_generators);
568 if (r < 0)
569 return r;
570 break;
571
572 case ARG_INSTANCE:
573 arg_instance = optarg;
574 break;
575
576 case ARG_OFFLINE:
577 r = parse_boolean_argument("--offline", optarg, &arg_offline);
578 if (r < 0)
579 return r;
580 break;
581
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
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
595 case ARG_JSON:
596 r = parse_json_argument(optarg, &arg_json_format_flags);
597 if (r <= 0)
598 return r;
599 break;
600
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);
605 break;
606
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);
611 break;
612
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
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 }
641
642 case ARG_TABLE:
643 arg_table = true;
644 break;
645
646 case ARG_NO_LEGEND:
647 arg_legend = false;
648 break;
649
650 case ARG_TLDR:
651 arg_cat_flags = CAT_TLDR;
652 break;
653
654 case 'm':
655 arg_capability = CAPABILITY_MASK;
656 break;
657
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
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
672 case ARG_DEBUGGER:
673 r = free_and_strdup_warn(&arg_debugger, optarg);
674 if (r < 0)
675 return r;
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
687 case '?':
688 return -EINVAL;
689
690 default:
691 assert_not_reached();
692 }
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 }
709
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
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
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
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
726 if (arg_runtime_scope == RUNTIME_SCOPE_GLOBAL && !streq_ptr(argv[optind], "unit-paths"))
727 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
728 "Option --global only makes sense with verb unit-paths.");
729
730 if (streq_ptr(argv[optind], "cat-config") && arg_runtime_scope == RUNTIME_SCOPE_USER)
731 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
732 "Option --user is not supported for cat-config right now.");
733
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
738 if ((arg_root || arg_image) && (!STRPTR_IN_SET(argv[optind], "cat-config", "verify", "condition", "inspect-elf", "unit-gdb")) &&
739 (!(streq_ptr(argv[optind], "security") && arg_offline)))
740 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
741 "Options --root= and --image= are only supported for cat-config, verify, condition, unit-gdb, and security when used with --offline= right now.");
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.");
746
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
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
759 if (arg_table && sd_json_format_enabled(arg_json_format_flags))
760 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--table and --json= are mutually exclusive.");
761
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
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
768 return 1; /* work to do */
769}
770
771static int run(int argc, char *argv[]) {
772 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
773 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
774
775 static const Verb verbs[] = {
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 },
782 /* ↓ The following seven verbs are deprecated, from here … ↓ */
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 },
790 /* ↑ … until here ↑ */
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 },
794 { "unit-gdb", 2, VERB_ANY, 0, verb_unit_gdb },
795 { "unit-paths", 1, 1, 0, verb_unit_paths },
796 { "unit-shell", 2, VERB_ANY, 0, verb_unit_shell },
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 },
809 { "dlopen-metadata", 2, 2, 0, verb_dlopen_metadata },
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 },
815 { "nvpcrs", VERB_ANY, VERB_ANY, 0, verb_nvpcrs },
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 },
821 {}
822 };
823
824 int r;
825
826 setlocale(LC_ALL, "");
827 setlocale(LC_NUMERIC, "C"); /* we want to format/parse floats in C style */
828
829 log_setup();
830
831 r = parse_argv(argc, argv);
832 if (r <= 0)
833 return r;
834
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,
841 arg_image_policy,
842 DISSECT_IMAGE_GENERIC_ROOT |
843 DISSECT_IMAGE_RELAX_VAR_CHECK |
844 DISSECT_IMAGE_READ_ONLY |
845 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
846 &mounted_dir,
847 /* ret_dir_fd= */ NULL,
848 &loop_device);
849 if (r < 0)
850 return r;
851
852 arg_root = strdup(mounted_dir);
853 if (!arg_root)
854 return log_oom();
855 }
856
857 return dispatch_verb(argc, argv, verbs, NULL);
858}
859
860DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);