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