]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/analyze/analyze.c
Merge pull request #32540 from DaanDeMeyer/mkosi
[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>
3f1c1287 7#include <inttypes.h>
3f6fd1ba
LP
8#include <stdio.h>
9#include <stdlib.h>
ca78ad1d 10#include <unistd.h>
2265fbf7 11
048ecf5b 12#include "sd-bus.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"
5f43c97c 21#include "analyze-compare-versions.h"
edfea9fe 22#include "analyze-condition.h"
ef215fa7 23#include "analyze-critical-chain.h"
cccd2af6 24#include "analyze-dot.h"
25eb70af 25#include "analyze-dump.h"
73cb64c4 26#include "analyze-exit-status.h"
5f43c97c 27#include "analyze-fdstore.h"
08e36480 28#include "analyze-filesystems.h"
fb8cc599 29#include "analyze-image-policy.h"
2acfc252 30#include "analyze-inspect-elf.h"
faaa6ea0 31#include "analyze-log-control.h"
f50535af 32#include "analyze-malloc.h"
f70c90f5 33#include "analyze-pcrs.h"
ba474dad 34#include "analyze-plot.h"
ec16f3b6 35#include "analyze-security.h"
2b04e72c 36#include "analyze-service-watchdogs.h"
d30693f3 37#include "analyze-srk.h"
389638d3 38#include "analyze-syscall-filter.h"
f24f8e37 39#include "analyze-time.h"
113dd9cb 40#include "analyze-time-data.h"
30bddc06 41#include "analyze-timespan.h"
503ccaaa 42#include "analyze-timestamp.h"
0c2d69df 43#include "analyze-unit-files.h"
36258fbe 44#include "analyze-unit-paths.h"
3f6fd1ba 45#include "analyze-verify.h"
d6b4d1c7 46#include "build.h"
048ecf5b 47#include "bus-error.h"
9b71e4ab 48#include "bus-locator.h"
807542be 49#include "bus-map-properties.h"
20b16441 50#include "bus-unit-util.h"
6d86f4bd 51#include "calendarspec.h"
b2af819b
LP
52#include "cap-list.h"
53#include "capability-util.h"
854a42fb 54#include "conf-files.h"
c0a1bfac 55#include "copy.h"
28db6fbf 56#include "constants.h"
76ed04d9 57#include "exit-status.h"
da845dab 58#include "extract-word.h"
c0a1bfac 59#include "fd-util.h"
cdf6258c 60#include "fileio.h"
b41711cd 61#include "filesystems.h"
d8bfdbe1 62#include "format-table.h"
7d50b32a 63#include "glob-util.h"
bb150966 64#include "hashmap.h"
8752c575 65#include "locale-util.h"
3f6fd1ba 66#include "log.h"
d665c7b2 67#include "main-func.h"
e5ea5c3a 68#include "mount-util.h"
d8b4d14d 69#include "nulstr-util.h"
9ea9d4cf 70#include "pager.h"
599c7c54 71#include "parse-argument.h"
6bedfcbb 72#include "parse-util.h"
854a42fb 73#include "path-util.h"
294bf0c3 74#include "pretty-print.h"
da845dab 75#include "rm-rf.h"
349cc4a5 76#if HAVE_SECCOMP
294bf0c3 77# include "seccomp-util.h"
0f734bdc 78#endif
760877e9 79#include "sort-util.h"
3f6fd1ba 80#include "special.h"
b41711cd 81#include "stat-util.h"
3cc3dc77 82#include "string-table.h"
3f6fd1ba
LP
83#include "strv.h"
84#include "strxcpyx.h"
288a74cc 85#include "terminal-util.h"
760877e9 86#include "time-util.h"
da845dab 87#include "tmpfile-util.h"
3f6fd1ba 88#include "unit-name.h"
a87b151a 89#include "verb-log-control.h"
a6bcef29 90#include "verbs.h"
2265fbf7 91
cccd2af6
LP
92DotMode arg_dot = DEP_ALL;
93char **arg_dot_from_patterns = NULL, **arg_dot_to_patterns = NULL;
ef215fa7 94usec_t arg_fuzz = 0;
08e36480 95PagerFlags arg_pager_flags = 0;
063c8382 96CatFlags arg_cat_flags = 0;
25eb70af 97BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
ba474dad 98const char *arg_host = NULL;
4870133b 99RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
cae7c282 100RecursiveErrors arg_recursive_errors = _RECURSIVE_ERRORS_INVALID;
57a22a3f
LP
101bool arg_man = true;
102bool arg_generators = false;
c649b343 103char *arg_root = NULL;
e5ea5c3a 104static char *arg_image = NULL;
57a22a3f
LP
105char *arg_security_policy = NULL;
106bool arg_offline = false;
107unsigned arg_threshold = 100;
5229b03c
LP
108unsigned arg_iterations = 1;
109usec_t arg_base_time = USEC_INFINITY;
e82116e5 110char *arg_unit = NULL;
57a22a3f 111JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
08e36480 112bool arg_quiet = false;
57a22a3f 113char *arg_profile = NULL;
ff46b2f9
JZ
114bool arg_legend = true;
115bool arg_table = false;
84be0c71 116ImagePolicy *arg_image_policy = NULL;
bb150966 117
d665c7b2
YW
118STATIC_DESTRUCTOR_REGISTER(arg_dot_from_patterns, strv_freep);
119STATIC_DESTRUCTOR_REGISTER(arg_dot_to_patterns, strv_freep);
782671bc 120STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
e5ea5c3a 121STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
ecfd082b 122STATIC_DESTRUCTOR_REGISTER(arg_security_policy, freep);
8de7929d 123STATIC_DESTRUCTOR_REGISTER(arg_unit, freep);
04469211 124STATIC_DESTRUCTOR_REGISTER(arg_profile, freep);
84be0c71 125STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
d665c7b2 126
25eb70af 127int acquire_bus(sd_bus **bus, bool *use_full_bus) {
fb507898 128 int r;
5c69b31c 129
f7e29336 130 if (use_full_bus && *use_full_bus) {
4870133b 131 r = bus_connect_transport(arg_transport, arg_host, arg_runtime_scope, bus);
fb507898
YW
132 if (IN_SET(r, 0, -EHOSTDOWN))
133 return r;
5c69b31c
GJ
134
135 *use_full_bus = false;
136 }
137
4870133b 138 return bus_connect_transport_systemd(arg_transport, arg_host, arg_runtime_scope, bus);
bf0e0a4d
ZJS
139}
140
cccd2af6 141int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv) {
4afd3348 142 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
988b9df2
LP
143 int r;
144
145 assert(bus);
146 assert(path);
147 assert(property);
148 assert(strv);
149
150 r = sd_bus_get_property_strv(
151 bus,
152 "org.freedesktop.systemd1",
153 path,
154 "org.freedesktop.systemd1.Unit",
155 property,
156 &error,
157 strv);
4ae25393 158 if (r < 0)
0ed3da7c 159 return log_error_errno(r, "Failed to get unit property %s: %s", property, bus_error_message(&error, r));
988b9df2
LP
160
161 return 0;
162}
163
30bddc06 164void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timespan) {
c269607f
ZJS
165 if (calendar && calendar_spec_from_string(p, NULL) >= 0)
166 log_notice("Hint: this expression is a valid calendar specification. "
167 "Use 'systemd-analyze calendar \"%s\"' instead?", p);
168 if (timestamp && parse_timestamp(p, NULL) >= 0)
169 log_notice("Hint: this expression is a valid timestamp. "
170 "Use 'systemd-analyze timestamp \"%s\"' instead?", p);
171 if (timespan && parse_time(p, NULL, USEC_PER_SEC) >= 0)
172 log_notice("Hint: this expression is a valid timespan. "
173 "Use 'systemd-analyze timespan \"%s\"' instead?", p);
6d86f4bd
LP
174}
175
f50535af
LB
176int dump_fd_reply(sd_bus_message *message) {
177 int fd, r;
178
179 assert(message);
180
181 r = sd_bus_message_read(message, "h", &fd);
182 if (r < 0)
183 return bus_log_parse_error(r);
184
185 fflush(stdout);
186 r = copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0);
187 if (r < 0)
188 return r;
189
190 return 1; /* Success */
191}
192
a6bcef29 193static int help(int argc, char *argv[], void *userdata) {
49139a5d 194 _cleanup_free_ char *link = NULL, *dot_link = NULL;
37ec0fdd 195 int r;
9ea9d4cf 196
384c2c32 197 pager_open(arg_pager_flags);
9ea9d4cf 198
37ec0fdd
LP
199 r = terminal_urlify_man("systemd-analyze", "1", &link);
200 if (r < 0)
201 return log_oom();
202
49139a5d
LP
203 /* Not using terminal_urlify_man() for this, since we don't want the "man page" text suffix in this case. */
204 r = terminal_urlify("man:dot(1)", "dot(1)", &dot_link);
205 if (r < 0)
206 return log_oom();
207
353b2baa
LP
208 printf("%s [OPTIONS...] COMMAND ...\n\n"
209 "%sProfile systemd, show unit dependencies, check unit files.%s\n"
20a51f6a 210 "\nCommands:\n"
3cc3dc77
MG
211 " [time] Print time required to boot the machine\n"
212 " blame Print list of running units ordered by\n"
213 " time to init\n"
214 " critical-chain [UNIT...] Print a tree of the time critical chain\n"
215 " of units\n"
216 " plot Output SVG graphic showing service\n"
217 " initialization\n"
218 " dot [UNIT...] Output dependency graph in %s format\n"
d1d8786c 219 " dump [PATTERN...] Output state serialization of service\n"
3cc3dc77 220 " manager\n"
ca029693 221 " cat-config NAME|PATH... Show configuration file and drop-ins\n"
3cc3dc77
MG
222 " unit-files List files and symlinks for units\n"
223 " unit-paths List load directories for units\n"
224 " exit-status [STATUS...] List exit status definitions\n"
225 " capability [CAP...] List capability definitions\n"
52117f5a
ZJS
226 " syscall-filter [NAME...] List syscalls in seccomp filters\n"
227 " filesystems [NAME...] List known filesystems\n"
fb8cc599 228 " architectures [NAME...] List known architectures\n"
3cc3dc77 229 " condition CONDITION... Evaluate conditions and asserts\n"
bc012a3e
ZJS
230 " compare-versions VERSION1 [OP] VERSION2\n"
231 " Compare two version strings\n"
3cc3dc77
MG
232 " verify FILE... Check unit files for correctness\n"
233 " calendar SPEC... Validate repetitive calendar time\n"
234 " events\n"
235 " timestamp TIMESTAMP... Validate a timestamp\n"
236 " timespan SPAN... Validate a time span\n"
237 " security [UNIT...] Analyze security of unit\n"
917e6554 238 " inspect-elf FILE... Parse and print ELF package metadata\n"
f50535af 239 " malloc [D-BUS SERVICE...] Dump malloc stats of a D-Bus service\n"
5f43c97c 240 " fdstore SERVICE... Show file descriptor store contents of service\n"
ca029693 241 " image-policy POLICY... Analyze image policy string\n"
f70c90f5 242 " pcrs [PCR...] Show TPM2 PCRs and their names\n"
0615abef 243 " srk [>FILE] Write TPM2 SRK (to FILE)\n"
353b2baa 244 "\nOptions:\n"
3cc3dc77 245 " --recursive-errors=MODE Control which units are verified\n"
bb43d853 246 " --offline=BOOL Perform a security review on unit file(s)\n"
dfbda879
MG
247 " --threshold=N Exit with a non-zero status when overall\n"
248 " exposure level is over threshold value\n"
ecfd082b
MG
249 " --security-policy=PATH Use custom JSON security policy instead\n"
250 " of built-in one\n"
4b4a8ef7 251 " --json=pretty|short|off Generate JSON output of the security\n"
ff46b2f9 252 " analysis table, or plot's raw time data\n"
3cc3dc77 253 " --no-pager Do not pipe output into a pager\n"
ff46b2f9
JZ
254 " --no-legend Disable column headers and hints in plot\n"
255 " with either --table or --json=\n"
3cc3dc77
MG
256 " --system Operate on system systemd instance\n"
257 " --user Operate on user systemd instance\n"
258 " --global Operate on global user configuration\n"
259 " -H --host=[USER@]HOST Operate on remote host\n"
260 " -M --machine=CONTAINER Operate on local container\n"
261 " --order Show only order in the graph\n"
262 " --require Show only requirement in the graph\n"
263 " --from-pattern=GLOB Show only origins in the graph\n"
264 " --to-pattern=GLOB Show only destinations in the graph\n"
265 " --fuzz=SECONDS Also print services which finished SECONDS\n"
266 " earlier than the latest in the branch\n"
267 " --man[=BOOL] Do [not] check for existence of man pages\n"
268 " --generators[=BOOL] Do [not] run unit generators\n"
269 " (requires privileges)\n"
270 " --iterations=N Show the specified number of iterations\n"
271 " --base-time=TIMESTAMP Calculate calendar times relative to\n"
272 " specified time\n"
04469211
LB
273 " --profile=name|PATH Include the specified profile in the\n"
274 " security review of the unit(s)\n"
5e163285 275 " --unit=UNIT Evaluate conditions and asserts of unit\n"
ff46b2f9 276 " --table Output plot's raw time data as a table\n"
52117f5a
ZJS
277 " -h --help Show this help\n"
278 " --version Show package version\n"
279 " -q --quiet Do not emit hints\n"
063c8382 280 " --tldr Skip comments and empty lines\n"
9f5b68d6
LP
281 " --root=PATH Operate on an alternate filesystem root\n"
282 " --image=PATH Operate on disk image as filesystem root\n"
84be0c71 283 " --image-policy=POLICY Specify disk image dissection policy\n"
bc556335
DDM
284 "\nSee the %s for details.\n",
285 program_invocation_short_name,
286 ansi_highlight(),
287 ansi_normal(),
288 dot_link,
289 link);
96de7c04 290
1ace223c
SJ
291 /* When updating this list, including descriptions, apply changes to
292 * shell-completion/bash/systemd-analyze and shell-completion/zsh/_systemd-analyze too. */
a6bcef29
LP
293
294 return 0;
2265fbf7
SP
295}
296
9ea9d4cf 297static int parse_argv(int argc, char *argv[]) {
2265fbf7
SP
298 enum {
299 ARG_VERSION = 0x100,
1700761b
SP
300 ARG_ORDER,
301 ARG_REQUIRE,
46d8646a 302 ARG_ROOT,
e5ea5c3a 303 ARG_IMAGE,
06e78680 304 ARG_IMAGE_POLICY,
e55933db 305 ARG_SYSTEM,
28b35ef2
ZJS
306 ARG_USER,
307 ARG_GLOBAL,
e55933db 308 ARG_DOT_FROM_PATTERN,
bb150966 309 ARG_DOT_TO_PATTERN,
9ea9d4cf 310 ARG_FUZZ,
1d3bc017 311 ARG_NO_PAGER,
dad29dff 312 ARG_MAN,
641c0fd1 313 ARG_GENERATORS,
f2ccf832 314 ARG_ITERATIONS,
985c1880 315 ARG_BASE_TIME,
3cc3dc77 316 ARG_RECURSIVE_ERRORS,
bb43d853 317 ARG_OFFLINE,
dfbda879 318 ARG_THRESHOLD,
ecfd082b 319 ARG_SECURITY_POLICY,
4b4a8ef7 320 ARG_JSON,
04469211 321 ARG_PROFILE,
ff46b2f9
JZ
322 ARG_TABLE,
323 ARG_NO_LEGEND,
063c8382 324 ARG_TLDR,
2265fbf7
SP
325 };
326
327 static const struct option options[] = {
3cc3dc77
MG
328 { "help", no_argument, NULL, 'h' },
329 { "version", no_argument, NULL, ARG_VERSION },
52117f5a 330 { "quiet", no_argument, NULL, 'q' },
3cc3dc77
MG
331 { "order", no_argument, NULL, ARG_ORDER },
332 { "require", no_argument, NULL, ARG_REQUIRE },
333 { "root", required_argument, NULL, ARG_ROOT },
334 { "image", required_argument, NULL, ARG_IMAGE },
06e78680 335 { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
3cc3dc77 336 { "recursive-errors", required_argument, NULL, ARG_RECURSIVE_ERRORS },
bb43d853 337 { "offline", required_argument, NULL, ARG_OFFLINE },
dfbda879 338 { "threshold", required_argument, NULL, ARG_THRESHOLD },
ecfd082b 339 { "security-policy", required_argument, NULL, ARG_SECURITY_POLICY },
3cc3dc77
MG
340 { "system", no_argument, NULL, ARG_SYSTEM },
341 { "user", no_argument, NULL, ARG_USER },
342 { "global", no_argument, NULL, ARG_GLOBAL },
343 { "from-pattern", required_argument, NULL, ARG_DOT_FROM_PATTERN },
344 { "to-pattern", required_argument, NULL, ARG_DOT_TO_PATTERN },
345 { "fuzz", required_argument, NULL, ARG_FUZZ },
346 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
347 { "man", optional_argument, NULL, ARG_MAN },
348 { "generators", optional_argument, NULL, ARG_GENERATORS },
349 { "host", required_argument, NULL, 'H' },
350 { "machine", required_argument, NULL, 'M' },
351 { "iterations", required_argument, NULL, ARG_ITERATIONS },
352 { "base-time", required_argument, NULL, ARG_BASE_TIME },
8de7929d 353 { "unit", required_argument, NULL, 'U' },
4b4a8ef7 354 { "json", required_argument, NULL, ARG_JSON },
04469211 355 { "profile", required_argument, NULL, ARG_PROFILE },
ff46b2f9
JZ
356 { "table", optional_argument, NULL, ARG_TABLE },
357 { "no-legend", optional_argument, NULL, ARG_NO_LEGEND },
063c8382 358 { "tldr", no_argument, NULL, ARG_TLDR },
eb9da376 359 {}
2265fbf7
SP
360 };
361
eb9da376
LP
362 int r, c;
363
2265fbf7
SP
364 assert(argc >= 0);
365 assert(argv);
366
7c0e0bbb 367 while ((c = getopt_long(argc, argv, "hH:M:U:q", options, NULL)) >= 0)
eb9da376 368 switch (c) {
c170f3a4
LP
369
370 case 'h':
a6bcef29 371 return help(0, NULL, NULL);
c170f3a4 372
52117f5a
ZJS
373 case ARG_VERSION:
374 return version();
375
376 case 'q':
377 arg_quiet = true;
378 break;
379
3cc3dc77
MG
380 case ARG_RECURSIVE_ERRORS:
381 if (streq(optarg, "help")) {
382 DUMP_STRING_TABLE(recursive_errors, RecursiveErrors, _RECURSIVE_ERRORS_MAX);
383 return 0;
384 }
385 r = recursive_errors_from_string(optarg);
386 if (r < 0)
387 return log_error_errno(r, "Unknown mode passed to --recursive-errors='%s'.", optarg);
388
389 arg_recursive_errors = r;
390 break;
391
46d8646a 392 case ARG_ROOT:
782671bc
MG
393 r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root);
394 if (r < 0)
395 return r;
46d8646a
ZJS
396 break;
397
e5ea5c3a
MG
398 case ARG_IMAGE:
399 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
400 if (r < 0)
401 return r;
402 break;
403
06e78680
YW
404 case ARG_IMAGE_POLICY:
405 r = parse_image_policy_argument(optarg, &arg_image_policy);
406 if (r < 0)
407 return r;
408 break;
409
28b35ef2 410 case ARG_SYSTEM:
4870133b 411 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
28b35ef2
ZJS
412 break;
413
c170f3a4 414 case ARG_USER:
4870133b 415 arg_runtime_scope = RUNTIME_SCOPE_USER;
c170f3a4
LP
416 break;
417
28b35ef2 418 case ARG_GLOBAL:
4870133b 419 arg_runtime_scope = RUNTIME_SCOPE_GLOBAL;
c170f3a4
LP
420 break;
421
422 case ARG_ORDER:
423 arg_dot = DEP_ORDER;
424 break;
425
426 case ARG_REQUIRE:
427 arg_dot = DEP_REQUIRE;
428 break;
429
e55933db 430 case ARG_DOT_FROM_PATTERN:
903a0b07
LP
431 if (strv_extend(&arg_dot_from_patterns, optarg) < 0)
432 return log_oom();
433
e55933db
ŁS
434 break;
435
436 case ARG_DOT_TO_PATTERN:
903a0b07
LP
437 if (strv_extend(&arg_dot_to_patterns, optarg) < 0)
438 return log_oom();
439
e55933db
ŁS
440 break;
441
bb150966
HH
442 case ARG_FUZZ:
443 r = parse_sec(optarg, &arg_fuzz);
444 if (r < 0)
445 return r;
446 break;
447
9ea9d4cf 448 case ARG_NO_PAGER:
0221d68a 449 arg_pager_flags |= PAGER_DISABLE;
9ea9d4cf
LP
450 break;
451
3cd26e7c
LP
452 case 'H':
453 arg_transport = BUS_TRANSPORT_REMOTE;
454 arg_host = optarg;
455 break;
456
457 case 'M':
de33fc62 458 arg_transport = BUS_TRANSPORT_MACHINE;
3cd26e7c
LP
459 arg_host = optarg;
460 break;
461
dad29dff 462 case ARG_MAN:
599c7c54
ZJS
463 r = parse_boolean_argument("--man", optarg, &arg_man);
464 if (r < 0)
465 return r;
1d3bc017
ZJS
466 break;
467
641c0fd1 468 case ARG_GENERATORS:
599c7c54
ZJS
469 r = parse_boolean_argument("--generators", optarg, &arg_generators);
470 if (r < 0)
471 return r;
641c0fd1
ZJS
472 break;
473
bb43d853
MG
474 case ARG_OFFLINE:
475 r = parse_boolean_argument("--offline", optarg, &arg_offline);
476 if (r < 0)
477 return r;
478 break;
479
dfbda879
MG
480 case ARG_THRESHOLD:
481 r = safe_atou(optarg, &arg_threshold);
482 if (r < 0 || arg_threshold > 100)
483 return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL), "Failed to parse threshold: %s", optarg);
484
485 break;
486
ecfd082b
MG
487 case ARG_SECURITY_POLICY:
488 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_security_policy);
489 if (r < 0)
490 return r;
491 break;
492
4b4a8ef7
MG
493 case ARG_JSON:
494 r = parse_json_argument(optarg, &arg_json_format_flags);
495 if (r <= 0)
496 return r;
497 break;
498
f2ccf832
LP
499 case ARG_ITERATIONS:
500 r = safe_atou(optarg, &arg_iterations);
501 if (r < 0)
502 return log_error_errno(r, "Failed to parse iterations: %s", optarg);
f2ccf832
LP
503 break;
504
985c1880
LP
505 case ARG_BASE_TIME:
506 r = parse_timestamp(optarg, &arg_base_time);
507 if (r < 0)
508 return log_error_errno(r, "Failed to parse --base-time= parameter: %s", optarg);
985c1880
LP
509 break;
510
04469211
LB
511 case ARG_PROFILE:
512 if (isempty(optarg))
513 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Profile file name is empty");
514
515 if (is_path(optarg)) {
516 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_profile);
517 if (r < 0)
518 return r;
519 if (!endswith(arg_profile, ".conf"))
520 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Profile file name must end with .conf: %s", arg_profile);
521 } else {
522 r = free_and_strdup(&arg_profile, optarg);
523 if (r < 0)
524 return log_oom();
525 }
526
527 break;
528
8de7929d
DDM
529 case 'U': {
530 _cleanup_free_ char *mangled = NULL;
531
532 r = unit_name_mangle(optarg, UNIT_NAME_MANGLE_WARN, &mangled);
533 if (r < 0)
534 return log_error_errno(r, "Failed to mangle unit name %s: %m", optarg);
535
536 free_and_replace(arg_unit, mangled);
537 break;
538 }
ff46b2f9
JZ
539
540 case ARG_TABLE:
541 arg_table = true;
542 break;
543
544 case ARG_NO_LEGEND:
545 arg_legend = false;
546 break;
547
063c8382
ZJS
548 case ARG_TLDR:
549 arg_cat_flags = CAT_TLDR;
550 break;
551
c170f3a4
LP
552 case '?':
553 return -EINVAL;
554
555 default:
04499a70 556 assert_not_reached();
2265fbf7 557 }
eb9da376 558
bb43d853
MG
559 if (arg_offline && !streq_ptr(argv[optind], "security"))
560 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
561 "Option --offline= is only supported for security right now.");
562
5e163285
AAF
563 if (arg_offline && optind >= argc - 1)
564 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
565 "Option --offline= requires one or more units to perform a security review.");
566
bf4ffc43 567 if (arg_json_format_flags != JSON_FORMAT_OFF && !STRPTR_IN_SET(argv[optind], "security", "inspect-elf", "plot", "fdstore", "pcrs", "architectures", "capability", "exit-status"))
4b4a8ef7 568 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
bf4ffc43 569 "Option --json= is only supported for security, inspect-elf, plot, fdstore, pcrs, architectures, capability, exit-status right now.");
4b4a8ef7 570
dfbda879
MG
571 if (arg_threshold != 100 && !streq_ptr(argv[optind], "security"))
572 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
573 "Option --threshold= is only supported for security right now.");
574
ac970536 575 if (arg_runtime_scope == RUNTIME_SCOPE_GLOBAL && !streq_ptr(argv[optind], "unit-paths"))
baaa35ad 576 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
ac970536 577 "Option --global only makes sense with verb unit-paths.");
31a5924e 578
4870133b 579 if (streq_ptr(argv[optind], "cat-config") && arg_runtime_scope == RUNTIME_SCOPE_USER)
f1d9d36a
ZJS
580 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
581 "Option --user is not supported for cat-config right now.");
582
ecfd082b
MG
583 if (arg_security_policy && !streq_ptr(argv[optind], "security"))
584 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
585 "Option --security-policy= is only supported for security.");
586
8de7929d 587 if ((arg_root || arg_image) && (!STRPTR_IN_SET(argv[optind], "cat-config", "verify", "condition")) &&
bb43d853 588 (!(streq_ptr(argv[optind], "security") && arg_offline)))
baaa35ad 589 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
8de7929d 590 "Options --root= and --image= are only supported for cat-config, verify, condition and security when used with --offline= right now.");
e5ea5c3a
MG
591
592 /* Having both an image and a root is not supported by the code */
593 if (arg_root && arg_image)
594 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
46d8646a 595
8de7929d
DDM
596 if (arg_unit && !streq_ptr(argv[optind], "condition"))
597 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --unit= is only supported for condition");
598
599 if (streq_ptr(argv[optind], "condition") && !arg_unit && optind >= argc - 1)
600 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too few arguments for condition");
601
602 if (streq_ptr(argv[optind], "condition") && arg_unit && optind < argc - 1)
603 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No conditions can be passed if --unit= is used.");
604
5df839e9 605 if ((!arg_legend && !STRPTR_IN_SET(argv[optind], "plot", "architectures")) ||
ff46b2f9
JZ
606 (streq_ptr(argv[optind], "plot") && !arg_legend && !arg_table && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)))
607 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --no-legend is only supported for plot with either --table or --json=.");
608
609 if (arg_table && !streq_ptr(argv[optind], "plot"))
610 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --table is only supported for plot right now.");
611
612 if (arg_table && !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
613 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--table and --json= are mutually exclusive.");
614
1d3bc017 615 return 1; /* work to do */
2265fbf7
SP
616}
617
d665c7b2 618static int run(int argc, char *argv[]) {
e5ea5c3a 619 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
a4b3e942 620 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
a6bcef29
LP
621
622 static const Verb verbs[] = {
889d695d 623 { "help", VERB_ANY, VERB_ANY, 0, help },
ef38bedb
LP
624 { "time", VERB_ANY, 1, VERB_DEFAULT, verb_time },
625 { "blame", VERB_ANY, 1, 0, verb_blame },
626 { "critical-chain", VERB_ANY, VERB_ANY, 0, verb_critical_chain },
627 { "plot", VERB_ANY, 1, 0, verb_plot },
628 { "dot", VERB_ANY, VERB_ANY, 0, verb_dot },
ab86ccba 629 /* ↓ The following seven verbs are deprecated, from here … ↓ */
a87b151a
DDM
630 { "log-level", VERB_ANY, 2, 0, verb_log_control },
631 { "log-target", VERB_ANY, 2, 0, verb_log_control },
632 { "set-log-level", 2, 2, 0, verb_log_control },
633 { "get-log-level", VERB_ANY, 1, 0, verb_log_control },
634 { "set-log-target", 2, 2, 0, verb_log_control },
635 { "get-log-target", VERB_ANY, 1, 0, verb_log_control },
ef38bedb 636 { "service-watchdogs", VERB_ANY, 2, 0, verb_service_watchdogs },
ab86ccba 637 /* ↑ … until here ↑ */
d1d8786c 638 { "dump", VERB_ANY, VERB_ANY, 0, verb_dump },
ef38bedb
LP
639 { "cat-config", 2, VERB_ANY, 0, verb_cat_config },
640 { "unit-files", VERB_ANY, VERB_ANY, 0, verb_unit_files },
641 { "unit-paths", 1, 1, 0, verb_unit_paths },
642 { "exit-status", VERB_ANY, VERB_ANY, 0, verb_exit_status },
643 { "syscall-filter", VERB_ANY, VERB_ANY, 0, verb_syscall_filters },
644 { "capability", VERB_ANY, VERB_ANY, 0, verb_capabilities },
645 { "filesystems", VERB_ANY, VERB_ANY, 0, verb_filesystems },
646 { "condition", VERB_ANY, VERB_ANY, 0, verb_condition },
bc012a3e 647 { "compare-versions", 3, 4, 0, verb_compare_versions },
ef38bedb
LP
648 { "verify", 2, VERB_ANY, 0, verb_verify },
649 { "calendar", 2, VERB_ANY, 0, verb_calendar },
650 { "timestamp", 2, VERB_ANY, 0, verb_timestamp },
651 { "timespan", 2, VERB_ANY, 0, verb_timespan },
652 { "security", VERB_ANY, VERB_ANY, 0, verb_security },
653 { "inspect-elf", 2, VERB_ANY, 0, verb_elf_inspection },
f50535af 654 { "malloc", VERB_ANY, VERB_ANY, 0, verb_malloc },
5f43c97c 655 { "fdstore", 2, VERB_ANY, 0, verb_fdstore },
a62e12da 656 { "image-policy", 2, 2, 0, verb_image_policy },
f70c90f5 657 { "pcrs", VERB_ANY, VERB_ANY, 0, verb_pcrs },
d30693f3 658 { "srk", VERB_ANY, 1, 0, verb_srk },
fb8cc599 659 { "architectures", VERB_ANY, VERB_ANY, 0, verb_architectures },
a6bcef29
LP
660 {}
661 };
662
5220a6f3 663 int r;
2265fbf7
SP
664
665 setlocale(LC_ALL, "");
c170f3a4 666 setlocale(LC_NUMERIC, "C"); /* we want to format/parse floats in C style */
a6bcef29 667
d2acb93d 668 log_setup();
2265fbf7
SP
669
670 r = parse_argv(argc, argv);
9ea9d4cf 671 if (r <= 0)
d665c7b2 672 return r;
c170f3a4 673
e5ea5c3a
MG
674 /* Open up and mount the image */
675 if (arg_image) {
676 assert(!arg_root);
677
678 r = mount_image_privately_interactively(
679 arg_image,
84be0c71 680 arg_image_policy,
e5ea5c3a
MG
681 DISSECT_IMAGE_GENERIC_ROOT |
682 DISSECT_IMAGE_RELAX_VAR_CHECK |
f4a63ce2
LP
683 DISSECT_IMAGE_READ_ONLY |
684 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
a4b3e942 685 &mounted_dir,
a133d2c3 686 /* ret_dir_fd= */ NULL,
e330f97a 687 &loop_device);
e5ea5c3a
MG
688 if (r < 0)
689 return r;
690
a4b3e942 691 arg_root = strdup(mounted_dir);
e5ea5c3a
MG
692 if (!arg_root)
693 return log_oom();
694 }
695
d665c7b2 696 return dispatch_verb(argc, argv, verbs, NULL);
2265fbf7 697}
d665c7b2 698
fddad5f4 699DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);