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