]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | /*** | |
3 | Copyright © 2013 Simon Peeters | |
4 | ***/ | |
5 | ||
6 | #include <getopt.h> | |
7 | #include <inttypes.h> | |
8 | #include <stdio.h> | |
9 | #include <stdlib.h> | |
10 | #include <unistd.h> | |
11 | ||
12 | #include "sd-bus.h" | |
13 | ||
14 | #include "alloc-util.h" | |
15 | #include "analyze.h" | |
16 | #include "analyze-architectures.h" | |
17 | #include "analyze-blame.h" | |
18 | #include "analyze-calendar.h" | |
19 | #include "analyze-capability.h" | |
20 | #include "analyze-cat-config.h" | |
21 | #include "analyze-compare-versions.h" | |
22 | #include "analyze-condition.h" | |
23 | #include "analyze-critical-chain.h" | |
24 | #include "analyze-dot.h" | |
25 | #include "analyze-dump.h" | |
26 | #include "analyze-exit-status.h" | |
27 | #include "analyze-fdstore.h" | |
28 | #include "analyze-filesystems.h" | |
29 | #include "analyze-image-policy.h" | |
30 | #include "analyze-inspect-elf.h" | |
31 | #include "analyze-log-control.h" | |
32 | #include "analyze-malloc.h" | |
33 | #include "analyze-pcrs.h" | |
34 | #include "analyze-plot.h" | |
35 | #include "analyze-security.h" | |
36 | #include "analyze-service-watchdogs.h" | |
37 | #include "analyze-srk.h" | |
38 | #include "analyze-syscall-filter.h" | |
39 | #include "analyze-time.h" | |
40 | #include "analyze-time-data.h" | |
41 | #include "analyze-timespan.h" | |
42 | #include "analyze-timestamp.h" | |
43 | #include "analyze-unit-files.h" | |
44 | #include "analyze-unit-paths.h" | |
45 | #include "analyze-verify.h" | |
46 | #include "build.h" | |
47 | #include "bus-error.h" | |
48 | #include "bus-locator.h" | |
49 | #include "bus-map-properties.h" | |
50 | #include "bus-unit-util.h" | |
51 | #include "calendarspec.h" | |
52 | #include "cap-list.h" | |
53 | #include "capability-util.h" | |
54 | #include "conf-files.h" | |
55 | #include "copy.h" | |
56 | #include "constants.h" | |
57 | #include "exit-status.h" | |
58 | #include "extract-word.h" | |
59 | #include "fd-util.h" | |
60 | #include "fileio.h" | |
61 | #include "filesystems.h" | |
62 | #include "format-table.h" | |
63 | #include "glob-util.h" | |
64 | #include "hashmap.h" | |
65 | #include "locale-util.h" | |
66 | #include "log.h" | |
67 | #include "main-func.h" | |
68 | #include "mount-util.h" | |
69 | #include "nulstr-util.h" | |
70 | #include "pager.h" | |
71 | #include "parse-argument.h" | |
72 | #include "parse-util.h" | |
73 | #include "path-util.h" | |
74 | #include "pretty-print.h" | |
75 | #include "rm-rf.h" | |
76 | #if HAVE_SECCOMP | |
77 | # include "seccomp-util.h" | |
78 | #endif | |
79 | #include "sort-util.h" | |
80 | #include "special.h" | |
81 | #include "stat-util.h" | |
82 | #include "string-table.h" | |
83 | #include "strv.h" | |
84 | #include "strxcpyx.h" | |
85 | #include "terminal-util.h" | |
86 | #include "time-util.h" | |
87 | #include "tmpfile-util.h" | |
88 | #include "unit-name.h" | |
89 | #include "verb-log-control.h" | |
90 | #include "verbs.h" | |
91 | ||
92 | DotMode arg_dot = DEP_ALL; | |
93 | char **arg_dot_from_patterns = NULL, **arg_dot_to_patterns = NULL; | |
94 | usec_t arg_fuzz = 0; | |
95 | PagerFlags arg_pager_flags = 0; | |
96 | CatFlags arg_cat_flags = 0; | |
97 | BusTransport arg_transport = BUS_TRANSPORT_LOCAL; | |
98 | const char *arg_host = NULL; | |
99 | RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; | |
100 | RecursiveErrors arg_recursive_errors = _RECURSIVE_ERRORS_INVALID; | |
101 | bool arg_man = true; | |
102 | bool arg_generators = false; | |
103 | char *arg_root = NULL; | |
104 | static char *arg_image = NULL; | |
105 | char *arg_security_policy = NULL; | |
106 | bool arg_offline = false; | |
107 | unsigned arg_threshold = 100; | |
108 | unsigned arg_iterations = 1; | |
109 | usec_t arg_base_time = USEC_INFINITY; | |
110 | char *arg_unit = NULL; | |
111 | JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; | |
112 | bool arg_quiet = false; | |
113 | char *arg_profile = NULL; | |
114 | bool arg_legend = true; | |
115 | bool arg_table = false; | |
116 | ImagePolicy *arg_image_policy = NULL; | |
117 | ||
118 | STATIC_DESTRUCTOR_REGISTER(arg_dot_from_patterns, strv_freep); | |
119 | STATIC_DESTRUCTOR_REGISTER(arg_dot_to_patterns, strv_freep); | |
120 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); | |
121 | STATIC_DESTRUCTOR_REGISTER(arg_image, freep); | |
122 | STATIC_DESTRUCTOR_REGISTER(arg_security_policy, freep); | |
123 | STATIC_DESTRUCTOR_REGISTER(arg_unit, freep); | |
124 | STATIC_DESTRUCTOR_REGISTER(arg_profile, freep); | |
125 | STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep); | |
126 | ||
127 | int acquire_bus(sd_bus **bus, bool *use_full_bus) { | |
128 | int r; | |
129 | ||
130 | if (use_full_bus && *use_full_bus) { | |
131 | r = bus_connect_transport(arg_transport, arg_host, arg_runtime_scope, bus); | |
132 | if (IN_SET(r, 0, -EHOSTDOWN)) | |
133 | return r; | |
134 | ||
135 | *use_full_bus = false; | |
136 | } | |
137 | ||
138 | return bus_connect_transport_systemd(arg_transport, arg_host, arg_runtime_scope, bus); | |
139 | } | |
140 | ||
141 | int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv) { | |
142 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
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); | |
158 | if (r < 0) | |
159 | return log_error_errno(r, "Failed to get unit property %s: %s", property, bus_error_message(&error, r)); | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
164 | void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timespan) { | |
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); | |
174 | } | |
175 | ||
176 | int 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 | ||
193 | static int help(int argc, char *argv[], void *userdata) { | |
194 | _cleanup_free_ char *link = NULL, *dot_link = NULL; | |
195 | int r; | |
196 | ||
197 | pager_open(arg_pager_flags); | |
198 | ||
199 | r = terminal_urlify_man("systemd-analyze", "1", &link); | |
200 | if (r < 0) | |
201 | return log_oom(); | |
202 | ||
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 | ||
208 | printf("%s [OPTIONS...] COMMAND ...\n\n" | |
209 | "%sProfile systemd, show unit dependencies, check unit files.%s\n" | |
210 | "\nCommands:\n" | |
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" | |
219 | " dump [PATTERN...] Output state serialization of service\n" | |
220 | " manager\n" | |
221 | " cat-config NAME|PATH... 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" | |
226 | " syscall-filter [NAME...] List syscalls in seccomp filters\n" | |
227 | " filesystems [NAME...] List known filesystems\n" | |
228 | " architectures [NAME...] List known architectures\n" | |
229 | " condition CONDITION... Evaluate conditions and asserts\n" | |
230 | " compare-versions VERSION1 [OP] VERSION2\n" | |
231 | " Compare two version strings\n" | |
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" | |
238 | " inspect-elf FILE... Parse and print ELF package metadata\n" | |
239 | " malloc [D-BUS SERVICE...] Dump malloc stats of a D-Bus service\n" | |
240 | " fdstore SERVICE... Show file descriptor store contents of service\n" | |
241 | " image-policy POLICY... Analyze image policy string\n" | |
242 | " pcrs [PCR...] Show TPM2 PCRs and their names\n" | |
243 | " srk [>FILE] Write TPM2 SRK (to FILE)\n" | |
244 | "\nOptions:\n" | |
245 | " --recursive-errors=MODE Control which units are verified\n" | |
246 | " --offline=BOOL Perform a security review on unit file(s)\n" | |
247 | " --threshold=N Exit with a non-zero status when overall\n" | |
248 | " exposure level is over threshold value\n" | |
249 | " --security-policy=PATH Use custom JSON security policy instead\n" | |
250 | " of built-in one\n" | |
251 | " --json=pretty|short|off Generate JSON output of the security\n" | |
252 | " analysis table, or plot's raw time data\n" | |
253 | " --no-pager Do not pipe output into a pager\n" | |
254 | " --no-legend Disable column headers and hints in plot\n" | |
255 | " with either --table or --json=\n" | |
256 | " --system Operate on system systemd instance\n" | |
257 | " --user Operate on user systemd instance\n" | |
258 | " --global Operate on global user configuration\n" | |
259 | " -H --host=[USER@]HOST Operate on remote host\n" | |
260 | " -M --machine=CONTAINER Operate on local container\n" | |
261 | " --order Show only order in the graph\n" | |
262 | " --require Show only requirement in the graph\n" | |
263 | " --from-pattern=GLOB Show only origins in the graph\n" | |
264 | " --to-pattern=GLOB Show only destinations in the graph\n" | |
265 | " --fuzz=SECONDS Also print services which finished SECONDS\n" | |
266 | " earlier than the latest in the branch\n" | |
267 | " --man[=BOOL] Do [not] check for existence of man pages\n" | |
268 | " --generators[=BOOL] Do [not] run unit generators\n" | |
269 | " (requires privileges)\n" | |
270 | " --iterations=N Show the specified number of iterations\n" | |
271 | " --base-time=TIMESTAMP Calculate calendar times relative to\n" | |
272 | " specified time\n" | |
273 | " --profile=name|PATH Include the specified profile in the\n" | |
274 | " security review of the unit(s)\n" | |
275 | " --unit=UNIT Evaluate conditions and asserts of unit\n" | |
276 | " --table Output plot's raw time data as a table\n" | |
277 | " -h --help Show this help\n" | |
278 | " --version Show package version\n" | |
279 | " -q --quiet Do not emit hints\n" | |
280 | " --tldr Skip comments and empty lines\n" | |
281 | " --root=PATH Operate on an alternate filesystem root\n" | |
282 | " --image=PATH Operate on disk image as filesystem root\n" | |
283 | " --image-policy=POLICY Specify disk image dissection policy\n" | |
284 | "\nSee the %s for details.\n", | |
285 | program_invocation_short_name, | |
286 | ansi_highlight(), | |
287 | ansi_normal(), | |
288 | dot_link, | |
289 | link); | |
290 | ||
291 | /* When updating this list, including descriptions, apply changes to | |
292 | * shell-completion/bash/systemd-analyze and shell-completion/zsh/_systemd-analyze too. */ | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | static int parse_argv(int argc, char *argv[]) { | |
298 | enum { | |
299 | ARG_VERSION = 0x100, | |
300 | ARG_ORDER, | |
301 | ARG_REQUIRE, | |
302 | ARG_ROOT, | |
303 | ARG_IMAGE, | |
304 | ARG_IMAGE_POLICY, | |
305 | ARG_SYSTEM, | |
306 | ARG_USER, | |
307 | ARG_GLOBAL, | |
308 | ARG_DOT_FROM_PATTERN, | |
309 | ARG_DOT_TO_PATTERN, | |
310 | ARG_FUZZ, | |
311 | ARG_NO_PAGER, | |
312 | ARG_MAN, | |
313 | ARG_GENERATORS, | |
314 | ARG_ITERATIONS, | |
315 | ARG_BASE_TIME, | |
316 | ARG_RECURSIVE_ERRORS, | |
317 | ARG_OFFLINE, | |
318 | ARG_THRESHOLD, | |
319 | ARG_SECURITY_POLICY, | |
320 | ARG_JSON, | |
321 | ARG_PROFILE, | |
322 | ARG_TABLE, | |
323 | ARG_NO_LEGEND, | |
324 | ARG_TLDR, | |
325 | }; | |
326 | ||
327 | static const struct option options[] = { | |
328 | { "help", no_argument, NULL, 'h' }, | |
329 | { "version", no_argument, NULL, ARG_VERSION }, | |
330 | { "quiet", no_argument, NULL, 'q' }, | |
331 | { "order", no_argument, NULL, ARG_ORDER }, | |
332 | { "require", no_argument, NULL, ARG_REQUIRE }, | |
333 | { "root", required_argument, NULL, ARG_ROOT }, | |
334 | { "image", required_argument, NULL, ARG_IMAGE }, | |
335 | { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY }, | |
336 | { "recursive-errors", required_argument, NULL, ARG_RECURSIVE_ERRORS }, | |
337 | { "offline", required_argument, NULL, ARG_OFFLINE }, | |
338 | { "threshold", required_argument, NULL, ARG_THRESHOLD }, | |
339 | { "security-policy", required_argument, NULL, ARG_SECURITY_POLICY }, | |
340 | { "system", no_argument, NULL, ARG_SYSTEM }, | |
341 | { "user", no_argument, NULL, ARG_USER }, | |
342 | { "global", no_argument, NULL, ARG_GLOBAL }, | |
343 | { "from-pattern", required_argument, NULL, ARG_DOT_FROM_PATTERN }, | |
344 | { "to-pattern", required_argument, NULL, ARG_DOT_TO_PATTERN }, | |
345 | { "fuzz", required_argument, NULL, ARG_FUZZ }, | |
346 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
347 | { "man", optional_argument, NULL, ARG_MAN }, | |
348 | { "generators", optional_argument, NULL, ARG_GENERATORS }, | |
349 | { "host", required_argument, NULL, 'H' }, | |
350 | { "machine", required_argument, NULL, 'M' }, | |
351 | { "iterations", required_argument, NULL, ARG_ITERATIONS }, | |
352 | { "base-time", required_argument, NULL, ARG_BASE_TIME }, | |
353 | { "unit", required_argument, NULL, 'U' }, | |
354 | { "json", required_argument, NULL, ARG_JSON }, | |
355 | { "profile", required_argument, NULL, ARG_PROFILE }, | |
356 | { "table", optional_argument, NULL, ARG_TABLE }, | |
357 | { "no-legend", optional_argument, NULL, ARG_NO_LEGEND }, | |
358 | { "tldr", no_argument, NULL, ARG_TLDR }, | |
359 | {} | |
360 | }; | |
361 | ||
362 | int r, c; | |
363 | ||
364 | assert(argc >= 0); | |
365 | assert(argv); | |
366 | ||
367 | while ((c = getopt_long(argc, argv, "hH:M:U:q", options, NULL)) >= 0) | |
368 | switch (c) { | |
369 | ||
370 | case 'h': | |
371 | return help(0, NULL, NULL); | |
372 | ||
373 | case ARG_VERSION: | |
374 | return version(); | |
375 | ||
376 | case 'q': | |
377 | arg_quiet = true; | |
378 | break; | |
379 | ||
380 | case ARG_RECURSIVE_ERRORS: | |
381 | if (streq(optarg, "help")) { | |
382 | DUMP_STRING_TABLE(recursive_errors, RecursiveErrors, _RECURSIVE_ERRORS_MAX); | |
383 | return 0; | |
384 | } | |
385 | r = recursive_errors_from_string(optarg); | |
386 | if (r < 0) | |
387 | return log_error_errno(r, "Unknown mode passed to --recursive-errors='%s'.", optarg); | |
388 | ||
389 | arg_recursive_errors = r; | |
390 | break; | |
391 | ||
392 | case ARG_ROOT: | |
393 | r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root); | |
394 | if (r < 0) | |
395 | return r; | |
396 | break; | |
397 | ||
398 | case ARG_IMAGE: | |
399 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image); | |
400 | if (r < 0) | |
401 | return r; | |
402 | break; | |
403 | ||
404 | case ARG_IMAGE_POLICY: | |
405 | r = parse_image_policy_argument(optarg, &arg_image_policy); | |
406 | if (r < 0) | |
407 | return r; | |
408 | break; | |
409 | ||
410 | case ARG_SYSTEM: | |
411 | arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; | |
412 | break; | |
413 | ||
414 | case ARG_USER: | |
415 | arg_runtime_scope = RUNTIME_SCOPE_USER; | |
416 | break; | |
417 | ||
418 | case ARG_GLOBAL: | |
419 | arg_runtime_scope = RUNTIME_SCOPE_GLOBAL; | |
420 | break; | |
421 | ||
422 | case ARG_ORDER: | |
423 | arg_dot = DEP_ORDER; | |
424 | break; | |
425 | ||
426 | case ARG_REQUIRE: | |
427 | arg_dot = DEP_REQUIRE; | |
428 | break; | |
429 | ||
430 | case ARG_DOT_FROM_PATTERN: | |
431 | if (strv_extend(&arg_dot_from_patterns, optarg) < 0) | |
432 | return log_oom(); | |
433 | ||
434 | break; | |
435 | ||
436 | case ARG_DOT_TO_PATTERN: | |
437 | if (strv_extend(&arg_dot_to_patterns, optarg) < 0) | |
438 | return log_oom(); | |
439 | ||
440 | break; | |
441 | ||
442 | case ARG_FUZZ: | |
443 | r = parse_sec(optarg, &arg_fuzz); | |
444 | if (r < 0) | |
445 | return r; | |
446 | break; | |
447 | ||
448 | case ARG_NO_PAGER: | |
449 | arg_pager_flags |= PAGER_DISABLE; | |
450 | break; | |
451 | ||
452 | case 'H': | |
453 | arg_transport = BUS_TRANSPORT_REMOTE; | |
454 | arg_host = optarg; | |
455 | break; | |
456 | ||
457 | case 'M': | |
458 | arg_transport = BUS_TRANSPORT_MACHINE; | |
459 | arg_host = optarg; | |
460 | break; | |
461 | ||
462 | case ARG_MAN: | |
463 | r = parse_boolean_argument("--man", optarg, &arg_man); | |
464 | if (r < 0) | |
465 | return r; | |
466 | break; | |
467 | ||
468 | case ARG_GENERATORS: | |
469 | r = parse_boolean_argument("--generators", optarg, &arg_generators); | |
470 | if (r < 0) | |
471 | return r; | |
472 | break; | |
473 | ||
474 | case ARG_OFFLINE: | |
475 | r = parse_boolean_argument("--offline", optarg, &arg_offline); | |
476 | if (r < 0) | |
477 | return r; | |
478 | break; | |
479 | ||
480 | case ARG_THRESHOLD: | |
481 | r = safe_atou(optarg, &arg_threshold); | |
482 | if (r < 0 || arg_threshold > 100) | |
483 | return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL), "Failed to parse threshold: %s", optarg); | |
484 | ||
485 | break; | |
486 | ||
487 | case ARG_SECURITY_POLICY: | |
488 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_security_policy); | |
489 | if (r < 0) | |
490 | return r; | |
491 | break; | |
492 | ||
493 | case ARG_JSON: | |
494 | r = parse_json_argument(optarg, &arg_json_format_flags); | |
495 | if (r <= 0) | |
496 | return r; | |
497 | break; | |
498 | ||
499 | case ARG_ITERATIONS: | |
500 | r = safe_atou(optarg, &arg_iterations); | |
501 | if (r < 0) | |
502 | return log_error_errno(r, "Failed to parse iterations: %s", optarg); | |
503 | break; | |
504 | ||
505 | case ARG_BASE_TIME: | |
506 | r = parse_timestamp(optarg, &arg_base_time); | |
507 | if (r < 0) | |
508 | return log_error_errno(r, "Failed to parse --base-time= parameter: %s", optarg); | |
509 | break; | |
510 | ||
511 | case ARG_PROFILE: | |
512 | if (isempty(optarg)) | |
513 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Profile file name is empty"); | |
514 | ||
515 | if (is_path(optarg)) { | |
516 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_profile); | |
517 | if (r < 0) | |
518 | return r; | |
519 | if (!endswith(arg_profile, ".conf")) | |
520 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Profile file name must end with .conf: %s", arg_profile); | |
521 | } else { | |
522 | r = free_and_strdup(&arg_profile, optarg); | |
523 | if (r < 0) | |
524 | return log_oom(); | |
525 | } | |
526 | ||
527 | break; | |
528 | ||
529 | case 'U': { | |
530 | _cleanup_free_ char *mangled = NULL; | |
531 | ||
532 | r = unit_name_mangle(optarg, UNIT_NAME_MANGLE_WARN, &mangled); | |
533 | if (r < 0) | |
534 | return log_error_errno(r, "Failed to mangle unit name %s: %m", optarg); | |
535 | ||
536 | free_and_replace(arg_unit, mangled); | |
537 | break; | |
538 | } | |
539 | ||
540 | case ARG_TABLE: | |
541 | arg_table = true; | |
542 | break; | |
543 | ||
544 | case ARG_NO_LEGEND: | |
545 | arg_legend = false; | |
546 | break; | |
547 | ||
548 | case ARG_TLDR: | |
549 | arg_cat_flags = CAT_TLDR; | |
550 | break; | |
551 | ||
552 | case '?': | |
553 | return -EINVAL; | |
554 | ||
555 | default: | |
556 | assert_not_reached(); | |
557 | } | |
558 | ||
559 | if (arg_offline && !streq_ptr(argv[optind], "security")) | |
560 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
561 | "Option --offline= is only supported for security right now."); | |
562 | ||
563 | if (arg_offline && optind >= argc - 1) | |
564 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
565 | "Option --offline= requires one or more units to perform a security review."); | |
566 | ||
567 | if (arg_json_format_flags != JSON_FORMAT_OFF && !STRPTR_IN_SET(argv[optind], "security", "inspect-elf", "plot", "fdstore", "pcrs", "architectures", "capability", "exit-status")) | |
568 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
569 | "Option --json= is only supported for security, inspect-elf, plot, fdstore, pcrs, architectures, capability, exit-status right now."); | |
570 | ||
571 | if (arg_threshold != 100 && !streq_ptr(argv[optind], "security")) | |
572 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
573 | "Option --threshold= is only supported for security right now."); | |
574 | ||
575 | if (arg_runtime_scope == RUNTIME_SCOPE_GLOBAL && !streq_ptr(argv[optind], "unit-paths")) | |
576 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
577 | "Option --global only makes sense with verb unit-paths."); | |
578 | ||
579 | if (streq_ptr(argv[optind], "cat-config") && arg_runtime_scope == RUNTIME_SCOPE_USER) | |
580 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
581 | "Option --user is not supported for cat-config right now."); | |
582 | ||
583 | if (arg_security_policy && !streq_ptr(argv[optind], "security")) | |
584 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
585 | "Option --security-policy= is only supported for security."); | |
586 | ||
587 | if ((arg_root || arg_image) && (!STRPTR_IN_SET(argv[optind], "cat-config", "verify", "condition")) && | |
588 | (!(streq_ptr(argv[optind], "security") && arg_offline))) | |
589 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
590 | "Options --root= and --image= are only supported for cat-config, verify, condition and security when used with --offline= right now."); | |
591 | ||
592 | /* Having both an image and a root is not supported by the code */ | |
593 | if (arg_root && arg_image) | |
594 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported."); | |
595 | ||
596 | if (arg_unit && !streq_ptr(argv[optind], "condition")) | |
597 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --unit= is only supported for condition"); | |
598 | ||
599 | if (streq_ptr(argv[optind], "condition") && !arg_unit && optind >= argc - 1) | |
600 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too few arguments for condition"); | |
601 | ||
602 | if (streq_ptr(argv[optind], "condition") && arg_unit && optind < argc - 1) | |
603 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No conditions can be passed if --unit= is used."); | |
604 | ||
605 | if ((!arg_legend && !STRPTR_IN_SET(argv[optind], "plot", "architectures")) || | |
606 | (streq_ptr(argv[optind], "plot") && !arg_legend && !arg_table && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))) | |
607 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --no-legend is only supported for plot with either --table or --json=."); | |
608 | ||
609 | if (arg_table && !streq_ptr(argv[optind], "plot")) | |
610 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --table is only supported for plot right now."); | |
611 | ||
612 | if (arg_table && !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) | |
613 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--table and --json= are mutually exclusive."); | |
614 | ||
615 | return 1; /* work to do */ | |
616 | } | |
617 | ||
618 | static int run(int argc, char *argv[]) { | |
619 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; | |
620 | _cleanup_(umount_and_freep) char *mounted_dir = NULL; | |
621 | ||
622 | static const Verb verbs[] = { | |
623 | { "help", VERB_ANY, VERB_ANY, 0, help }, | |
624 | { "time", VERB_ANY, 1, VERB_DEFAULT, verb_time }, | |
625 | { "blame", VERB_ANY, 1, 0, verb_blame }, | |
626 | { "critical-chain", VERB_ANY, VERB_ANY, 0, verb_critical_chain }, | |
627 | { "plot", VERB_ANY, 1, 0, verb_plot }, | |
628 | { "dot", VERB_ANY, VERB_ANY, 0, verb_dot }, | |
629 | /* ↓ The following seven verbs are deprecated, from here … ↓ */ | |
630 | { "log-level", VERB_ANY, 2, 0, verb_log_control }, | |
631 | { "log-target", VERB_ANY, 2, 0, verb_log_control }, | |
632 | { "set-log-level", 2, 2, 0, verb_log_control }, | |
633 | { "get-log-level", VERB_ANY, 1, 0, verb_log_control }, | |
634 | { "set-log-target", 2, 2, 0, verb_log_control }, | |
635 | { "get-log-target", VERB_ANY, 1, 0, verb_log_control }, | |
636 | { "service-watchdogs", VERB_ANY, 2, 0, verb_service_watchdogs }, | |
637 | /* ↑ … until here ↑ */ | |
638 | { "dump", VERB_ANY, VERB_ANY, 0, verb_dump }, | |
639 | { "cat-config", 2, VERB_ANY, 0, verb_cat_config }, | |
640 | { "unit-files", VERB_ANY, VERB_ANY, 0, verb_unit_files }, | |
641 | { "unit-paths", 1, 1, 0, verb_unit_paths }, | |
642 | { "exit-status", VERB_ANY, VERB_ANY, 0, verb_exit_status }, | |
643 | { "syscall-filter", VERB_ANY, VERB_ANY, 0, verb_syscall_filters }, | |
644 | { "capability", VERB_ANY, VERB_ANY, 0, verb_capabilities }, | |
645 | { "filesystems", VERB_ANY, VERB_ANY, 0, verb_filesystems }, | |
646 | { "condition", VERB_ANY, VERB_ANY, 0, verb_condition }, | |
647 | { "compare-versions", 3, 4, 0, verb_compare_versions }, | |
648 | { "verify", 2, VERB_ANY, 0, verb_verify }, | |
649 | { "calendar", 2, VERB_ANY, 0, verb_calendar }, | |
650 | { "timestamp", 2, VERB_ANY, 0, verb_timestamp }, | |
651 | { "timespan", 2, VERB_ANY, 0, verb_timespan }, | |
652 | { "security", VERB_ANY, VERB_ANY, 0, verb_security }, | |
653 | { "inspect-elf", 2, VERB_ANY, 0, verb_elf_inspection }, | |
654 | { "malloc", VERB_ANY, VERB_ANY, 0, verb_malloc }, | |
655 | { "fdstore", 2, VERB_ANY, 0, verb_fdstore }, | |
656 | { "image-policy", 2, 2, 0, verb_image_policy }, | |
657 | { "pcrs", VERB_ANY, VERB_ANY, 0, verb_pcrs }, | |
658 | { "srk", VERB_ANY, 1, 0, verb_srk }, | |
659 | { "architectures", VERB_ANY, VERB_ANY, 0, verb_architectures }, | |
660 | {} | |
661 | }; | |
662 | ||
663 | int r; | |
664 | ||
665 | setlocale(LC_ALL, ""); | |
666 | setlocale(LC_NUMERIC, "C"); /* we want to format/parse floats in C style */ | |
667 | ||
668 | log_setup(); | |
669 | ||
670 | r = parse_argv(argc, argv); | |
671 | if (r <= 0) | |
672 | return r; | |
673 | ||
674 | /* Open up and mount the image */ | |
675 | if (arg_image) { | |
676 | assert(!arg_root); | |
677 | ||
678 | r = mount_image_privately_interactively( | |
679 | arg_image, | |
680 | arg_image_policy, | |
681 | DISSECT_IMAGE_GENERIC_ROOT | | |
682 | DISSECT_IMAGE_RELAX_VAR_CHECK | | |
683 | DISSECT_IMAGE_READ_ONLY | | |
684 | DISSECT_IMAGE_ALLOW_USERSPACE_VERITY, | |
685 | &mounted_dir, | |
686 | /* ret_dir_fd= */ NULL, | |
687 | &loop_device); | |
688 | if (r < 0) | |
689 | return r; | |
690 | ||
691 | arg_root = strdup(mounted_dir); | |
692 | if (!arg_root) | |
693 | return log_oom(); | |
694 | } | |
695 | ||
696 | return dispatch_verb(argc, argv, verbs, NULL); | |
697 | } | |
698 | ||
699 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |