]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/analyze/analyze.c
analyze: fix -q option
[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
MG
220 " manager\n"
221 " cat-config Show configuration file and drop-ins\n"
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"
f70c90f5 241 " pcrs [PCR...] Show TPM2 PCRs and their names\n"
d30693f3 242 " srk > FILE Write TPM2 SRK to stdout\n"
353b2baa 243 "\nOptions:\n"
3cc3dc77 244 " --recursive-errors=MODE Control which units are verified\n"
bb43d853 245 " --offline=BOOL Perform a security review on unit file(s)\n"
dfbda879
MG
246 " --threshold=N Exit with a non-zero status when overall\n"
247 " exposure level is over threshold value\n"
ecfd082b
MG
248 " --security-policy=PATH Use custom JSON security policy instead\n"
249 " of built-in one\n"
4b4a8ef7 250 " --json=pretty|short|off Generate JSON output of the security\n"
ff46b2f9 251 " analysis table, or plot's raw time data\n"
3cc3dc77 252 " --no-pager Do not pipe output into a pager\n"
ff46b2f9
JZ
253 " --no-legend Disable column headers and hints in plot\n"
254 " with either --table or --json=\n"
3cc3dc77
MG
255 " --system Operate on system systemd instance\n"
256 " --user Operate on user systemd instance\n"
257 " --global Operate on global user configuration\n"
258 " -H --host=[USER@]HOST Operate on remote host\n"
259 " -M --machine=CONTAINER Operate on local container\n"
260 " --order Show only order in the graph\n"
261 " --require Show only requirement in the graph\n"
262 " --from-pattern=GLOB Show only origins in the graph\n"
263 " --to-pattern=GLOB Show only destinations in the graph\n"
264 " --fuzz=SECONDS Also print services which finished SECONDS\n"
265 " earlier than the latest in the branch\n"
266 " --man[=BOOL] Do [not] check for existence of man pages\n"
267 " --generators[=BOOL] Do [not] run unit generators\n"
268 " (requires privileges)\n"
269 " --iterations=N Show the specified number of iterations\n"
270 " --base-time=TIMESTAMP Calculate calendar times relative to\n"
271 " specified time\n"
04469211
LB
272 " --profile=name|PATH Include the specified profile in the\n"
273 " security review of the unit(s)\n"
ff46b2f9 274 " --table Output plot's raw time data as a table\n"
52117f5a
ZJS
275 " -h --help Show this help\n"
276 " --version Show package version\n"
277 " -q --quiet Do not emit hints\n"
063c8382 278 " --tldr Skip comments and empty lines\n"
9f5b68d6
LP
279 " --root=PATH Operate on an alternate filesystem root\n"
280 " --image=PATH Operate on disk image as filesystem root\n"
84be0c71 281 " --image-policy=POLICY Specify disk image dissection policy\n"
bc556335
DDM
282 "\nSee the %s for details.\n",
283 program_invocation_short_name,
284 ansi_highlight(),
285 ansi_normal(),
286 dot_link,
287 link);
96de7c04 288
1ace223c
SJ
289 /* When updating this list, including descriptions, apply changes to
290 * shell-completion/bash/systemd-analyze and shell-completion/zsh/_systemd-analyze too. */
a6bcef29
LP
291
292 return 0;
2265fbf7
SP
293}
294
9ea9d4cf 295static int parse_argv(int argc, char *argv[]) {
2265fbf7
SP
296 enum {
297 ARG_VERSION = 0x100,
1700761b
SP
298 ARG_ORDER,
299 ARG_REQUIRE,
46d8646a 300 ARG_ROOT,
e5ea5c3a 301 ARG_IMAGE,
06e78680 302 ARG_IMAGE_POLICY,
e55933db 303 ARG_SYSTEM,
28b35ef2
ZJS
304 ARG_USER,
305 ARG_GLOBAL,
e55933db 306 ARG_DOT_FROM_PATTERN,
bb150966 307 ARG_DOT_TO_PATTERN,
9ea9d4cf 308 ARG_FUZZ,
1d3bc017 309 ARG_NO_PAGER,
dad29dff 310 ARG_MAN,
641c0fd1 311 ARG_GENERATORS,
f2ccf832 312 ARG_ITERATIONS,
985c1880 313 ARG_BASE_TIME,
3cc3dc77 314 ARG_RECURSIVE_ERRORS,
bb43d853 315 ARG_OFFLINE,
dfbda879 316 ARG_THRESHOLD,
ecfd082b 317 ARG_SECURITY_POLICY,
4b4a8ef7 318 ARG_JSON,
04469211 319 ARG_PROFILE,
ff46b2f9
JZ
320 ARG_TABLE,
321 ARG_NO_LEGEND,
063c8382 322 ARG_TLDR,
2265fbf7
SP
323 };
324
325 static const struct option options[] = {
3cc3dc77
MG
326 { "help", no_argument, NULL, 'h' },
327 { "version", no_argument, NULL, ARG_VERSION },
52117f5a 328 { "quiet", no_argument, NULL, 'q' },
3cc3dc77
MG
329 { "order", no_argument, NULL, ARG_ORDER },
330 { "require", no_argument, NULL, ARG_REQUIRE },
331 { "root", required_argument, NULL, ARG_ROOT },
332 { "image", required_argument, NULL, ARG_IMAGE },
06e78680 333 { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
3cc3dc77 334 { "recursive-errors", required_argument, NULL, ARG_RECURSIVE_ERRORS },
bb43d853 335 { "offline", required_argument, NULL, ARG_OFFLINE },
dfbda879 336 { "threshold", required_argument, NULL, ARG_THRESHOLD },
ecfd082b 337 { "security-policy", required_argument, NULL, ARG_SECURITY_POLICY },
3cc3dc77
MG
338 { "system", no_argument, NULL, ARG_SYSTEM },
339 { "user", no_argument, NULL, ARG_USER },
340 { "global", no_argument, NULL, ARG_GLOBAL },
341 { "from-pattern", required_argument, NULL, ARG_DOT_FROM_PATTERN },
342 { "to-pattern", required_argument, NULL, ARG_DOT_TO_PATTERN },
343 { "fuzz", required_argument, NULL, ARG_FUZZ },
344 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
345 { "man", optional_argument, NULL, ARG_MAN },
346 { "generators", optional_argument, NULL, ARG_GENERATORS },
347 { "host", required_argument, NULL, 'H' },
348 { "machine", required_argument, NULL, 'M' },
349 { "iterations", required_argument, NULL, ARG_ITERATIONS },
350 { "base-time", required_argument, NULL, ARG_BASE_TIME },
8de7929d 351 { "unit", required_argument, NULL, 'U' },
4b4a8ef7 352 { "json", required_argument, NULL, ARG_JSON },
04469211 353 { "profile", required_argument, NULL, ARG_PROFILE },
ff46b2f9
JZ
354 { "table", optional_argument, NULL, ARG_TABLE },
355 { "no-legend", optional_argument, NULL, ARG_NO_LEGEND },
063c8382 356 { "tldr", no_argument, NULL, ARG_TLDR },
eb9da376 357 {}
2265fbf7
SP
358 };
359
eb9da376
LP
360 int r, c;
361
2265fbf7
SP
362 assert(argc >= 0);
363 assert(argv);
364
7c0e0bbb 365 while ((c = getopt_long(argc, argv, "hH:M:U:q", options, NULL)) >= 0)
eb9da376 366 switch (c) {
c170f3a4
LP
367
368 case 'h':
a6bcef29 369 return help(0, NULL, NULL);
c170f3a4 370
52117f5a
ZJS
371 case ARG_VERSION:
372 return version();
373
374 case 'q':
375 arg_quiet = true;
376 break;
377
3cc3dc77
MG
378 case ARG_RECURSIVE_ERRORS:
379 if (streq(optarg, "help")) {
380 DUMP_STRING_TABLE(recursive_errors, RecursiveErrors, _RECURSIVE_ERRORS_MAX);
381 return 0;
382 }
383 r = recursive_errors_from_string(optarg);
384 if (r < 0)
385 return log_error_errno(r, "Unknown mode passed to --recursive-errors='%s'.", optarg);
386
387 arg_recursive_errors = r;
388 break;
389
46d8646a 390 case ARG_ROOT:
782671bc
MG
391 r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root);
392 if (r < 0)
393 return r;
46d8646a
ZJS
394 break;
395
e5ea5c3a
MG
396 case ARG_IMAGE:
397 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
398 if (r < 0)
399 return r;
400 break;
401
06e78680
YW
402 case ARG_IMAGE_POLICY:
403 r = parse_image_policy_argument(optarg, &arg_image_policy);
404 if (r < 0)
405 return r;
406 break;
407
28b35ef2 408 case ARG_SYSTEM:
4870133b 409 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
28b35ef2
ZJS
410 break;
411
c170f3a4 412 case ARG_USER:
4870133b 413 arg_runtime_scope = RUNTIME_SCOPE_USER;
c170f3a4
LP
414 break;
415
28b35ef2 416 case ARG_GLOBAL:
4870133b 417 arg_runtime_scope = RUNTIME_SCOPE_GLOBAL;
c170f3a4
LP
418 break;
419
420 case ARG_ORDER:
421 arg_dot = DEP_ORDER;
422 break;
423
424 case ARG_REQUIRE:
425 arg_dot = DEP_REQUIRE;
426 break;
427
e55933db 428 case ARG_DOT_FROM_PATTERN:
903a0b07
LP
429 if (strv_extend(&arg_dot_from_patterns, optarg) < 0)
430 return log_oom();
431
e55933db
ŁS
432 break;
433
434 case ARG_DOT_TO_PATTERN:
903a0b07
LP
435 if (strv_extend(&arg_dot_to_patterns, optarg) < 0)
436 return log_oom();
437
e55933db
ŁS
438 break;
439
bb150966
HH
440 case ARG_FUZZ:
441 r = parse_sec(optarg, &arg_fuzz);
442 if (r < 0)
443 return r;
444 break;
445
9ea9d4cf 446 case ARG_NO_PAGER:
0221d68a 447 arg_pager_flags |= PAGER_DISABLE;
9ea9d4cf
LP
448 break;
449
3cd26e7c
LP
450 case 'H':
451 arg_transport = BUS_TRANSPORT_REMOTE;
452 arg_host = optarg;
453 break;
454
455 case 'M':
de33fc62 456 arg_transport = BUS_TRANSPORT_MACHINE;
3cd26e7c
LP
457 arg_host = optarg;
458 break;
459
dad29dff 460 case ARG_MAN:
599c7c54
ZJS
461 r = parse_boolean_argument("--man", optarg, &arg_man);
462 if (r < 0)
463 return r;
1d3bc017
ZJS
464 break;
465
641c0fd1 466 case ARG_GENERATORS:
599c7c54
ZJS
467 r = parse_boolean_argument("--generators", optarg, &arg_generators);
468 if (r < 0)
469 return r;
641c0fd1
ZJS
470 break;
471
bb43d853
MG
472 case ARG_OFFLINE:
473 r = parse_boolean_argument("--offline", optarg, &arg_offline);
474 if (r < 0)
475 return r;
476 break;
477
dfbda879
MG
478 case ARG_THRESHOLD:
479 r = safe_atou(optarg, &arg_threshold);
480 if (r < 0 || arg_threshold > 100)
481 return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL), "Failed to parse threshold: %s", optarg);
482
483 break;
484
ecfd082b
MG
485 case ARG_SECURITY_POLICY:
486 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_security_policy);
487 if (r < 0)
488 return r;
489 break;
490
4b4a8ef7
MG
491 case ARG_JSON:
492 r = parse_json_argument(optarg, &arg_json_format_flags);
493 if (r <= 0)
494 return r;
495 break;
496
f2ccf832
LP
497 case ARG_ITERATIONS:
498 r = safe_atou(optarg, &arg_iterations);
499 if (r < 0)
500 return log_error_errno(r, "Failed to parse iterations: %s", optarg);
f2ccf832
LP
501 break;
502
985c1880
LP
503 case ARG_BASE_TIME:
504 r = parse_timestamp(optarg, &arg_base_time);
505 if (r < 0)
506 return log_error_errno(r, "Failed to parse --base-time= parameter: %s", optarg);
985c1880
LP
507 break;
508
04469211
LB
509 case ARG_PROFILE:
510 if (isempty(optarg))
511 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Profile file name is empty");
512
513 if (is_path(optarg)) {
514 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_profile);
515 if (r < 0)
516 return r;
517 if (!endswith(arg_profile, ".conf"))
518 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Profile file name must end with .conf: %s", arg_profile);
519 } else {
520 r = free_and_strdup(&arg_profile, optarg);
521 if (r < 0)
522 return log_oom();
523 }
524
525 break;
526
8de7929d
DDM
527 case 'U': {
528 _cleanup_free_ char *mangled = NULL;
529
530 r = unit_name_mangle(optarg, UNIT_NAME_MANGLE_WARN, &mangled);
531 if (r < 0)
532 return log_error_errno(r, "Failed to mangle unit name %s: %m", optarg);
533
534 free_and_replace(arg_unit, mangled);
535 break;
536 }
ff46b2f9
JZ
537
538 case ARG_TABLE:
539 arg_table = true;
540 break;
541
542 case ARG_NO_LEGEND:
543 arg_legend = false;
544 break;
545
063c8382
ZJS
546 case ARG_TLDR:
547 arg_cat_flags = CAT_TLDR;
548 break;
549
c170f3a4
LP
550 case '?':
551 return -EINVAL;
552
553 default:
04499a70 554 assert_not_reached();
2265fbf7 555 }
eb9da376 556
bb43d853
MG
557 if (arg_offline && !streq_ptr(argv[optind], "security"))
558 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
559 "Option --offline= is only supported for security right now.");
560
bf4ffc43 561 if (arg_json_format_flags != JSON_FORMAT_OFF && !STRPTR_IN_SET(argv[optind], "security", "inspect-elf", "plot", "fdstore", "pcrs", "architectures", "capability", "exit-status"))
4b4a8ef7 562 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
bf4ffc43 563 "Option --json= is only supported for security, inspect-elf, plot, fdstore, pcrs, architectures, capability, exit-status right now.");
4b4a8ef7 564
dfbda879
MG
565 if (arg_threshold != 100 && !streq_ptr(argv[optind], "security"))
566 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
567 "Option --threshold= is only supported for security right now.");
568
4870133b 569 if (arg_runtime_scope == RUNTIME_SCOPE_GLOBAL &&
baaa35ad
ZJS
570 !STR_IN_SET(argv[optind] ?: "time", "dot", "unit-paths", "verify"))
571 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
572 "Option --global only makes sense with verbs dot, unit-paths, verify.");
31a5924e 573
4870133b 574 if (streq_ptr(argv[optind], "cat-config") && arg_runtime_scope == RUNTIME_SCOPE_USER)
f1d9d36a
ZJS
575 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
576 "Option --user is not supported for cat-config right now.");
577
ecfd082b
MG
578 if (arg_security_policy && !streq_ptr(argv[optind], "security"))
579 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
580 "Option --security-policy= is only supported for security.");
581
8de7929d 582 if ((arg_root || arg_image) && (!STRPTR_IN_SET(argv[optind], "cat-config", "verify", "condition")) &&
bb43d853 583 (!(streq_ptr(argv[optind], "security") && arg_offline)))
baaa35ad 584 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
8de7929d 585 "Options --root= and --image= are only supported for cat-config, verify, condition and security when used with --offline= right now.");
e5ea5c3a
MG
586
587 /* Having both an image and a root is not supported by the code */
588 if (arg_root && arg_image)
589 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
46d8646a 590
8de7929d
DDM
591 if (arg_unit && !streq_ptr(argv[optind], "condition"))
592 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --unit= is only supported for condition");
593
594 if (streq_ptr(argv[optind], "condition") && !arg_unit && optind >= argc - 1)
595 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too few arguments for condition");
596
597 if (streq_ptr(argv[optind], "condition") && arg_unit && optind < argc - 1)
598 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No conditions can be passed if --unit= is used.");
599
5df839e9 600 if ((!arg_legend && !STRPTR_IN_SET(argv[optind], "plot", "architectures")) ||
ff46b2f9
JZ
601 (streq_ptr(argv[optind], "plot") && !arg_legend && !arg_table && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)))
602 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --no-legend is only supported for plot with either --table or --json=.");
603
604 if (arg_table && !streq_ptr(argv[optind], "plot"))
605 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --table is only supported for plot right now.");
606
607 if (arg_table && !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
608 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--table and --json= are mutually exclusive.");
609
1d3bc017 610 return 1; /* work to do */
2265fbf7
SP
611}
612
d665c7b2 613static int run(int argc, char *argv[]) {
e5ea5c3a 614 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
a4b3e942 615 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
a6bcef29
LP
616
617 static const Verb verbs[] = {
889d695d 618 { "help", VERB_ANY, VERB_ANY, 0, help },
ef38bedb
LP
619 { "time", VERB_ANY, 1, VERB_DEFAULT, verb_time },
620 { "blame", VERB_ANY, 1, 0, verb_blame },
621 { "critical-chain", VERB_ANY, VERB_ANY, 0, verb_critical_chain },
622 { "plot", VERB_ANY, 1, 0, verb_plot },
623 { "dot", VERB_ANY, VERB_ANY, 0, verb_dot },
ab86ccba 624 /* ↓ The following seven verbs are deprecated, from here … ↓ */
a87b151a
DDM
625 { "log-level", VERB_ANY, 2, 0, verb_log_control },
626 { "log-target", VERB_ANY, 2, 0, verb_log_control },
627 { "set-log-level", 2, 2, 0, verb_log_control },
628 { "get-log-level", VERB_ANY, 1, 0, verb_log_control },
629 { "set-log-target", 2, 2, 0, verb_log_control },
630 { "get-log-target", VERB_ANY, 1, 0, verb_log_control },
ef38bedb 631 { "service-watchdogs", VERB_ANY, 2, 0, verb_service_watchdogs },
ab86ccba 632 /* ↑ … until here ↑ */
d1d8786c 633 { "dump", VERB_ANY, VERB_ANY, 0, verb_dump },
ef38bedb
LP
634 { "cat-config", 2, VERB_ANY, 0, verb_cat_config },
635 { "unit-files", VERB_ANY, VERB_ANY, 0, verb_unit_files },
636 { "unit-paths", 1, 1, 0, verb_unit_paths },
637 { "exit-status", VERB_ANY, VERB_ANY, 0, verb_exit_status },
638 { "syscall-filter", VERB_ANY, VERB_ANY, 0, verb_syscall_filters },
639 { "capability", VERB_ANY, VERB_ANY, 0, verb_capabilities },
640 { "filesystems", VERB_ANY, VERB_ANY, 0, verb_filesystems },
641 { "condition", VERB_ANY, VERB_ANY, 0, verb_condition },
bc012a3e 642 { "compare-versions", 3, 4, 0, verb_compare_versions },
ef38bedb
LP
643 { "verify", 2, VERB_ANY, 0, verb_verify },
644 { "calendar", 2, VERB_ANY, 0, verb_calendar },
645 { "timestamp", 2, VERB_ANY, 0, verb_timestamp },
646 { "timespan", 2, VERB_ANY, 0, verb_timespan },
647 { "security", VERB_ANY, VERB_ANY, 0, verb_security },
648 { "inspect-elf", 2, VERB_ANY, 0, verb_elf_inspection },
f50535af 649 { "malloc", VERB_ANY, VERB_ANY, 0, verb_malloc },
5f43c97c 650 { "fdstore", 2, VERB_ANY, 0, verb_fdstore },
a62e12da 651 { "image-policy", 2, 2, 0, verb_image_policy },
f70c90f5 652 { "pcrs", VERB_ANY, VERB_ANY, 0, verb_pcrs },
d30693f3 653 { "srk", VERB_ANY, 1, 0, verb_srk },
fb8cc599 654 { "architectures", VERB_ANY, VERB_ANY, 0, verb_architectures },
a6bcef29
LP
655 {}
656 };
657
5220a6f3 658 int r;
2265fbf7
SP
659
660 setlocale(LC_ALL, "");
c170f3a4 661 setlocale(LC_NUMERIC, "C"); /* we want to format/parse floats in C style */
a6bcef29 662
d2acb93d 663 log_setup();
2265fbf7
SP
664
665 r = parse_argv(argc, argv);
9ea9d4cf 666 if (r <= 0)
d665c7b2 667 return r;
c170f3a4 668
e5ea5c3a
MG
669 /* Open up and mount the image */
670 if (arg_image) {
671 assert(!arg_root);
672
673 r = mount_image_privately_interactively(
674 arg_image,
84be0c71 675 arg_image_policy,
e5ea5c3a
MG
676 DISSECT_IMAGE_GENERIC_ROOT |
677 DISSECT_IMAGE_RELAX_VAR_CHECK |
678 DISSECT_IMAGE_READ_ONLY,
a4b3e942 679 &mounted_dir,
a133d2c3 680 /* ret_dir_fd= */ NULL,
e330f97a 681 &loop_device);
e5ea5c3a
MG
682 if (r < 0)
683 return r;
684
a4b3e942 685 arg_root = strdup(mounted_dir);
e5ea5c3a
MG
686 if (!arg_root)
687 return log_oom();
688 }
689
d665c7b2 690 return dispatch_verb(argc, argv, verbs, NULL);
2265fbf7 691}
d665c7b2 692
fddad5f4 693DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);