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