Debian Codegraph @

src/analyze/analyze.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2013 Simon Peeters
4 ***/
5
6 #include <locale.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include "sd-bus.h"
11 #include "sd-json.h"
12
13 #include "alloc-util.h"
14 #include "analyze.h"
15 #include "analyze-architectures.h"
16 #include "analyze-blame.h"
17 #include "analyze-calendar.h"
18 #include "analyze-capability.h"
19 #include "analyze-cat-config.h"
20 #include "analyze-chid.h"
21 #include "analyze-compare-versions.h"
22 #include "analyze-condition.h"
23 #include "analyze-critical-chain.h"
24 #include "analyze-dlopen-metadata.h"
25 #include "analyze-dot.h"
26 #include "analyze-dump.h"
27 #include "analyze-exit-status.h"
28 #include "analyze-fdstore.h"
29 #include "analyze-filesystems.h"
30 #include "analyze-has-tpm2.h"
31 #include "analyze-image-policy.h"
32 #include "analyze-inspect-elf.h"
33 #include "analyze-log-control.h"
34 #include "analyze-malloc.h"
35 #include "analyze-nvpcrs.h"
36 #include "analyze-pcrs.h"
37 #include "analyze-plot.h"
38 #include "analyze-security.h"
39 #include "analyze-service-watchdogs.h"
40 #include "analyze-smbios11.h"
41 #include "analyze-srk.h"
42 #include "analyze-syscall-filter.h"
43 #include "analyze-time.h"
44 #include "analyze-timespan.h"
45 #include "analyze-timestamp.h"
46 #include "analyze-unit-files.h"
47 #include "analyze-unit-gdb.h"
48 #include "analyze-unit-paths.h"
49 #include "analyze-unit-shell.h"
50 #include "analyze-verify.h"
51 #include "analyze-verify-util.h"
52 #include "build.h"
53 #include "bus-error.h"
54 #include "bus-unit-util.h"
55 #include "bus-util.h"
56 #include "calendarspec.h"
57 #include "dissect-image.h"
58 #include "extract-word.h"
59 #include "format-table.h"
60 #include "help-util.h"
61 #include "image-policy.h"
62 #include "log.h"
63 #include "loop-util.h"
64 #include "main-func.h"
65 #include "mount-util.h"
66 #include "options.h"
67 #include "pager.h"
68 #include "parse-argument.h"
69 #include "parse-util.h"
70 #include "path-util.h"
71 #include "pretty-print.h"
72 #include "runtime-scope.h"
73 #include "string-table.h"
74 #include "string-util.h"
75 #include "strv.h"
76 #include "time-util.h"
77 #include "unit-def.h"
78 #include "unit-name.h"
79 #include "verbs.h"
80
81 DotMode arg_dot = DEP_ALL;
82 CapabilityMode arg_capability = CAPABILITY_LITERAL;
83 char **arg_dot_from_patterns = NULL, **arg_dot_to_patterns = NULL;
84 usec_t arg_fuzz = 0;
85 PagerFlags arg_pager_flags = 0;
86 CatFlags arg_cat_flags = 0;
87 BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
88 char *arg_debugger = NULL;
89 char **arg_debugger_args = NULL;
90 const char *arg_host = NULL;
91 RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
92 RecursiveErrors arg_recursive_errors = _RECURSIVE_ERRORS_INVALID;
93 bool arg_man = true;
94 bool arg_generators = false;
95 const char *arg_instance = "test_instance";
96 double arg_svg_timescale = 1.0;
97 bool arg_detailed_svg = false;
98 char *arg_root = NULL;
99 static char *arg_image = NULL;
100 char *arg_security_policy = NULL;
101 bool arg_offline = false;
102 unsigned arg_threshold = 100;
103 unsigned arg_iterations = 1;
104 usec_t arg_base_time = USEC_INFINITY;
105 char *arg_unit = NULL;
106 sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
107 bool arg_quiet = false;
108 char *arg_profile = NULL;
109 bool arg_legend = true;
110 bool arg_table = false;
111 ImagePolicy *arg_image_policy = NULL;
112 char *arg_drm_device_path = NULL;
113
114 STATIC_DESTRUCTOR_REGISTER(arg_dot_from_patterns, strv_freep);
115 STATIC_DESTRUCTOR_REGISTER(arg_dot_to_patterns, strv_freep);
116 STATIC_DESTRUCTOR_REGISTER(arg_debugger, freep);
117 STATIC_DESTRUCTOR_REGISTER(arg_debugger_args, strv_freep);
118 STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
119 STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
120 STATIC_DESTRUCTOR_REGISTER(arg_security_policy, freep);
121 STATIC_DESTRUCTOR_REGISTER(arg_drm_device_path, freep);
122 STATIC_DESTRUCTOR_REGISTER(arg_unit, freep);
123 STATIC_DESTRUCTOR_REGISTER(arg_profile, freep);
124 STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
125
126 int acquire_bus(sd_bus **bus, bool *use_full_bus) {
127 int r;
128
129 POINTER_MAY_BE_NULL(use_full_bus);
130
131 if (use_full_bus && *use_full_bus) {
132 r = bus_connect_transport(arg_transport, arg_host, arg_runtime_scope, bus);
133 if (IN_SET(r, 0, -EHOSTDOWN))
134 return r;
135
136 *use_full_bus = false;
137 }
138
139 return bus_connect_transport_systemd(arg_transport, arg_host, arg_runtime_scope, bus);
140 }
141
142 int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv) {
143 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
144 int r;
145
146 assert(bus);
147 assert(path);
148 assert(property);
149 assert(strv);
150
151 r = sd_bus_get_property_strv(
152 bus,
153 "org.freedesktop.systemd1",
154 path,
155 "org.freedesktop.systemd1.Unit",
156 property,
157 &error,
158 strv);
159 if (r < 0)
160 return log_error_errno(r, "Failed to get unit property %s: %s", property, bus_error_message(&error, r));
161
162 return 0;
163 }
164
165 void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timespan) {
166 if (calendar && calendar_spec_from_string(p, NULL) >= 0)
167 log_notice("Hint: this expression is a valid calendar specification. "
168 "Use 'systemd-analyze calendar \"%s\"' instead?", p);
169 if (timestamp && parse_timestamp(p, NULL) >= 0)
170 log_notice("Hint: this expression is a valid timestamp. "
171 "Use 'systemd-analyze timestamp \"%s\"' instead?", p);
172 if (timespan && parse_time(p, NULL, USEC_PER_SEC) >= 0)
173 log_notice("Hint: this expression is a valid timespan. "
174 "Use 'systemd-analyze timespan \"%s\"' instead?", p);
175 }
176
177 static int verb_transient_settings(int argc, char *argv[], uintptr_t _data, void *userdata) {
178 assert(argc >= 2);
179
180 pager_open(arg_pager_flags);
181
182 bool first = true;
183 STRV_FOREACH(arg, strv_skip(argv, 1)) {
184 UnitType t;
185
186 t = unit_type_from_string(*arg);
187 if (t < 0)
188 return log_error_errno(t, "Invalid unit type '%s'.", *arg);
189
190 if (!first)
191 puts("");
192
193 bus_dump_transient_settings(t);
194 first = false;
195 }
196
197 return 0;
198 }
199
200 static int help(void) {
201 static const char *const vgroups[] = {
202 "Boot Analysis",
203 "Dependency Analysis",
204 "Configuration Files and Search Paths",
205 "Enumerate OS Concepts",
206 "Expression Evaluation",
207 "Clock & Time",
208 "Unit & Service Analysis",
209 "Executable Analysis",
210 "TPM Operations",
211 };
212
213 Table *vtables[ELEMENTSOF(vgroups)] = {};
214 CLEANUP_ELEMENTS(vtables, table_unref_array_clear);
215 _cleanup_(table_unrefp) Table *options = NULL;
216 int r;
217
218 pager_open(arg_pager_flags);
219
220 for (size_t i = 0; i < ELEMENTSOF(vgroups); i++) {
221 r = verbs_get_help_table_group(vgroups[i], &vtables[i]);
222 if (r < 0)
223 return r;
224 }
225
226 r = option_parser_get_help_table(&options);
227 if (r < 0)
228 return r;
229
230 assert_cc(ELEMENTSOF(vtables) == 9);
231 (void) table_sync_column_widths(0, options, vtables[0], vtables[1], vtables[2],
232 vtables[3], vtables[4], vtables[5], vtables[6],
233 vtables[7], vtables[8]);
234
235 help_cmdline("[OPTIONS...] COMMAND ...");
236 help_abstract("Profile systemd, show unit dependencies, check unit files.");
237
238 for (size_t i = 0; i < ELEMENTSOF(vgroups); i++) {
239 help_section(vgroups[i]);
240 r = table_print_or_warn(vtables[i]);
241 if (r < 0)
242 return r;
243 }
244
245 help_section("Options");
246 r = table_print_or_warn(options);
247 if (r < 0)
248 return r;
249
250 help_man_page_reference("systemd-analyze", "1");
251
252 return 0;
253 }
254
255 VERB_COMMON_HELP_HIDDEN(help);
256
257 /* When updating this list, including descriptions, apply changes to
258 * shell-completion/bash/systemd-analyze and shell-completion/zsh/_systemd-analyze too. */
259
260 VERB_GROUP("Boot Analysis");
261 VERB_SCOPE(, verb_time, "time", NULL, VERB_ANY, 1, VERB_DEFAULT,
262 "Print time required to boot the machine");
263 VERB_SCOPE(, verb_blame, "blame", NULL, VERB_ANY, 1, 0,
264 "Print list of running units ordered by time to init");
265 VERB_SCOPE(, verb_critical_chain, "critical-chain", "[UNIT...]", VERB_ANY, VERB_ANY, 0,
266 "Print a tree of the time critical chain of units");
267
268 VERB_GROUP("Dependency Analysis");
269 VERB_SCOPE(, verb_plot, "plot", NULL, VERB_ANY, 1, 0,
270 "Output SVG graphic showing service initialization");
271 VERB_SCOPE(, verb_dot, "dot", "[UNIT...]", VERB_ANY, VERB_ANY, 0,
272 "Output dependency graph in dot(1) format");
273 VERB_SCOPE(, verb_dump, "dump", "[PATTERN...]", VERB_ANY, VERB_ANY, 0,
274 "Output state serialization of service manager");
275
276 VERB_GROUP("Configuration Files and Search Paths");
277 VERB_SCOPE(, verb_cat_config, "cat-config", "NAME|PATH...", 2, VERB_ANY, 0,
278 "Show configuration file and drop-ins");
279 VERB_SCOPE(, verb_unit_files, "unit-files", NULL, VERB_ANY, VERB_ANY, 0,
280 "List files and symlinks for units");
281 VERB_SCOPE(, verb_unit_paths, "unit-paths", NULL, 1, 1, 0,
282 "List load directories for units");
283
284 VERB_GROUP("Enumerate OS Concepts");
285 VERB_SCOPE(, verb_exit_status, "exit-status", "[STATUS...]", VERB_ANY, VERB_ANY, 0,
286 "List exit status definitions");
287 VERB_SCOPE(, verb_capabilities, "capability", "[CAP...]", VERB_ANY, VERB_ANY, 0,
288 "List capability definitions");
289 VERB_SCOPE(, verb_syscall_filters, "syscall-filter", "[NAME...]", VERB_ANY, VERB_ANY, 0,
290 "List syscalls in seccomp filters");
291 VERB_SCOPE(, verb_filesystems, "filesystems", "[NAME...]", VERB_ANY, VERB_ANY, 0,
292 "List known filesystems");
293 VERB_SCOPE(, verb_architectures, "architectures", "[NAME...]", VERB_ANY, VERB_ANY, 0,
294 "List known architectures");
295 VERB_SCOPE(, verb_smbios11, "smbios11", NULL, VERB_ANY, 1, 0,
296 "List strings passed via SMBIOS Type #11");
297 VERB_SCOPE(, verb_chid, "chid", NULL, VERB_ANY, VERB_ANY, 0,
298 "List local CHIDs");
299 VERB(verb_transient_settings, "transient-settings", "TYPE...", 2, VERB_ANY, 0,
300 "List transient settings for unit TYPE");
301
302 VERB_GROUP("Expression Evaluation");
303 VERB_SCOPE(, verb_condition, "condition", "CONDITION...", VERB_ANY, VERB_ANY, 0,
304 "Evaluate conditions and asserts");
305 VERB_SCOPE(, verb_compare_versions, "compare-versions", "V1 [OP] V2", 3, 4, 0,
306 "Compare two version strings");
307 VERB_SCOPE(, verb_image_policy, "image-policy", "POLICY...", 2, 2, 0,
308 "Analyze image policy string");
309
310 VERB_GROUP("Clock & Time");
311 VERB_SCOPE(, verb_calendar, "calendar", "SPEC...", 2, VERB_ANY, 0,
312 "Validate repetitive calendar time events");
313 VERB_SCOPE(, verb_timestamp, "timestamp", "TIMESTAMP...", 2, VERB_ANY, 0,
314 "Validate a timestamp");
315 VERB_SCOPE(, verb_timespan, "timespan", "SPAN...", 2, VERB_ANY, 0,
316 "Validate a time span");
317
318 VERB_GROUP("Unit & Service Analysis");
319 VERB_SCOPE(, verb_verify, "verify", "FILE...", 2, VERB_ANY, 0,
320 "Check unit files for correctness");
321 VERB_SCOPE(, verb_security, "security", "[UNIT...]", VERB_ANY, VERB_ANY, 0,
322 "Analyze security of unit");
323 VERB_SCOPE(, verb_fdstore, "fdstore", "SERVICE...", 2, VERB_ANY, 0,
324 "Show file descriptor store contents of service");
325 VERB_SCOPE(, verb_malloc, "malloc", "[D-BUS SERVICE...]", VERB_ANY, VERB_ANY, 0,
326 "Dump malloc stats of a D-Bus service");
327 VERB_SCOPE(, verb_unit_gdb, "unit-gdb", "SERVICE", 2, VERB_ANY, 0,
328 "Attach a debugger to the given running service");
329 VERB_SCOPE(, verb_unit_shell, "unit-shell", "SERVICE [COMMAND ...]", 2, VERB_ANY, 0,
330 "Run command on the namespace of the service");
331
332 VERB_GROUP("Executable Analysis");
333 VERB_SCOPE(, verb_elf_inspection, "inspect-elf", "FILE...", 2, VERB_ANY, 0,
334 "Parse and print ELF package metadata");
335 VERB_SCOPE(, verb_dlopen_metadata, "dlopen-metadata", "FILE", 2, 2, 0,
336 "Parse and print ELF dlopen metadata");
337
338 VERB_GROUP("TPM Operations");
339 VERB_SCOPE(, verb_has_tpm2, "has-tpm2", NULL, VERB_ANY, 1, 0,
340 "Report whether TPM2 support is available");
341 VERB_SCOPE(, verb_identify_tpm2, "identify-tpm2", NULL, VERB_ANY, 1, 0,
342 "Show TPM2 vendor information");
343 VERB_SCOPE(, verb_pcrs, "pcrs", "[PCR...]", VERB_ANY, VERB_ANY, 0,
344 "Show TPM2 PCRs and their names");
345 VERB_SCOPE(, verb_nvpcrs, "nvpcrs", "[NVPCR...]", VERB_ANY, VERB_ANY, 0,
346 "Show additional TPM2 PCRs stored in NV indexes");
347 VERB_SCOPE(, verb_srk, "srk", "[>FILE]", VERB_ANY, 1, 0,
348 "Write TPM2 SRK (to FILE)");
349
350 /* The following are deprecated and not shown in --help. */
351 VERB_SCOPE(, verb_log_control, "log-level", NULL, VERB_ANY, 2, 0, /* help= */ NULL);
352 VERB_SCOPE(, verb_log_control, "log-target", NULL, VERB_ANY, 2, 0, /* help= */ NULL);
353 VERB_SCOPE(, verb_log_control, "set-log-level", NULL, 2, 2, 0, /* help= */ NULL);
354 VERB_SCOPE(, verb_log_control, "get-log-level", NULL, VERB_ANY, 1, 0, /* help= */ NULL);
355 VERB_SCOPE(, verb_log_control, "set-log-target", NULL, 2, 2, 0, /* help= */ NULL);
356 VERB_SCOPE(, verb_log_control, "get-log-target", NULL, VERB_ANY, 1, 0, /* help= */ NULL);
357 VERB_SCOPE(, verb_service_watchdogs, "service-watchdogs", NULL, VERB_ANY, 2, 0, /* help= */ NULL);
358
359 static int parse_argv(int argc, char *argv[], char ***ret_args) {
360 int r;
361
362 assert(argc >= 0);
363 assert(argv);
364 assert(ret_args);
365
366 /* For "unit-shell" the switches specified after the service name are part of the commandline
367 * to execute and are not processed by us. For other verbs, we consume all options as usual.
368 * To make this work, start with mode==OPTION_PARSER_RETURN_POSITIONAL_ARGS and switch to
369 * either OPTION_PARSER_STOP_AT_FIRST_NONOPTION or OPTION_PARSER_NORMAL after we've seen
370 * the verb. */
371 OptionParser opts = { argc, argv, OPTION_PARSER_RETURN_POSITIONAL_ARGS };
372 const char *verb = NULL;
373
374 FOREACH_OPTION_OR_RETURN(c, &opts)
375 switch (c) {
376
377 OPTION_POSITIONAL:
378 verb = opts.arg;
379
380 assert(opts.mode == OPTION_PARSER_RETURN_POSITIONAL_ARGS);
381 if (streq(verb, "unit-shell"))
382 opts.mode = OPTION_PARSER_STOP_AT_FIRST_NONOPTION;
383 else
384 opts.mode = OPTION_PARSER_NORMAL;
385 break;
386
387 OPTION_COMMON_HELP:
388 return help();
389
390 OPTION_COMMON_VERSION:
391 return version();
392
393 OPTION('q', "quiet", NULL, "Do not emit hints"):
394 arg_quiet = true;
395 break;
396
397 OPTION_LONG("recursive-errors", "MODE", "Control which units are verified"):
398 if (streq(opts.arg, "help"))
399 return DUMP_STRING_TABLE(recursive_errors, RecursiveErrors, _RECURSIVE_ERRORS_MAX);
400
401 r = recursive_errors_from_string(opts.arg);
402 if (r < 0)
403 return log_error_errno(r, "Unknown mode passed to --recursive-errors='%s'.", opts.arg);
404
405 arg_recursive_errors = r;
406 break;
407
408 OPTION_LONG("root", "PATH", "Operate on an alternate filesystem root"):
409 r = parse_path_argument(opts.arg, /* suppress_root= */ true, &arg_root);
410 if (r < 0)
411 return r;
412 break;
413
414 OPTION_LONG("image", "PATH", "Operate on disk image as filesystem root"):
415 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_image);
416 if (r < 0)
417 return r;
418 break;
419
420 OPTION_LONG("image-policy", "POLICY", "Specify disk image dissection policy"):
421 r = parse_image_policy_argument(opts.arg, &arg_image_policy);
422 if (r < 0)
423 return r;
424 break;
425
426 OPTION_LONG("system", NULL, "Operate on system systemd instance"):
427 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
428 break;
429
430 OPTION_LONG("user", NULL, "Operate on user systemd instance"):
431 arg_runtime_scope = RUNTIME_SCOPE_USER;
432 break;
433
434 OPTION_LONG("global", NULL, "Operate on global user configuration"):
435 arg_runtime_scope = RUNTIME_SCOPE_GLOBAL;
436 break;
437
438 OPTION_LONG("order", NULL, "Show only order in the graph"):
439 arg_dot = DEP_ORDER;
440 break;
441
442 OPTION_LONG("require", NULL, "Show only requirement in the graph"):
443 arg_dot = DEP_REQUIRE;
444 break;
445
446 OPTION_LONG("from-pattern", "GLOB", "Show only origins in the graph"):
447 if (strv_extend(&arg_dot_from_patterns, opts.arg) < 0)
448 return log_oom();
449 break;
450
451 OPTION_LONG("to-pattern", "GLOB", "Show only destinations in the graph"):
452 if (strv_extend(&arg_dot_to_patterns, opts.arg) < 0)
453 return log_oom();
454 break;
455
456 OPTION_LONG("fuzz", "SECONDS",
457 "Also print services which finished SECONDS earlier than the latest in the branch"):
458 r = parse_sec(opts.arg, &arg_fuzz);
459 if (r < 0)
460 return r;
461 break;
462
463 OPTION_COMMON_NO_PAGER:
464 arg_pager_flags |= PAGER_DISABLE;
465 break;
466
467 OPTION_COMMON_HOST:
468 arg_transport = BUS_TRANSPORT_REMOTE;
469 arg_host = opts.arg;
470 break;
471
472 OPTION_COMMON_MACHINE:
473 r = parse_machine_argument(opts.arg, &arg_host, &arg_transport);
474 if (r < 0)
475 return r;
476 break;
477
478 OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "man", "BOOL", "Whether to check for existence of man pages"):
479 r = parse_boolean_argument("--man", opts.arg, &arg_man);
480 if (r < 0)
481 return r;
482 break;
483
484 OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "generators", "BOOL",
485 "Whether to run unit generators (which requires privileges)"):
486 r = parse_boolean_argument("--generators", opts.arg, &arg_generators);
487 if (r < 0)
488 return r;
489 break;
490
491 OPTION_LONG("instance", "NAME", "Specify fallback instance name for template units"):
492 arg_instance = opts.arg;
493 break;
494
495 OPTION_LONG("offline", "BOOL", "Perform a security review on unit files"):
496 r = parse_boolean_argument("--offline", opts.arg, &arg_offline);
497 if (r < 0)
498 return r;
499 break;
500
501 OPTION_LONG("threshold", "N",
502 "Exit with a non-zero status when overall exposure level is over threshold value"):
503 r = safe_atou(opts.arg, &arg_threshold);
504 if (r < 0 || arg_threshold > 100)
505 return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL),
506 "Failed to parse threshold: %s", opts.arg);
507 break;
508
509 OPTION_LONG("security-policy", "PATH",
510 "Use custom JSON security policy instead of built-in one"):
511 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_security_policy);
512 if (r < 0)
513 return r;
514 break;
515
516 OPTION_COMMON_JSON:
517 r = parse_json_argument(opts.arg, &arg_json_format_flags);
518 if (r <= 0)
519 return r;
520 break;
521
522 OPTION_LONG("iterations", "N", "Show the specified number of iterations"):
523 r = safe_atou(opts.arg, &arg_iterations);
524 if (r < 0)
525 return log_error_errno(r, "Failed to parse iterations: %s", opts.arg);
526 break;
527
528 OPTION_LONG("base-time", "TIMESTAMP",
529 "Calculate calendar times relative to specified time"):
530 r = parse_timestamp(opts.arg, &arg_base_time);
531 if (r < 0)
532 return log_error_errno(r, "Failed to parse --base-time= parameter: %s", opts.arg);
533 break;
534
535 OPTION_LONG("profile", "name|PATH",
536 "Include the specified profile in the security review of the units"):
537 if (isempty(opts.arg))
538 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Profile file name is empty");
539
540 if (is_path(opts.arg)) {
541 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_profile);
542 if (r < 0)
543 return r;
544 if (!endswith(arg_profile, ".conf"))
545 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
546 "Profile file name must end with .conf: %s", arg_profile);
547 } else {
548 r = free_and_strdup(&arg_profile, opts.arg);
549 if (r < 0)
550 return log_oom();
551 }
552 break;
553
554 OPTION('U', "unit", "UNIT", "Evaluate conditions and asserts of unit"): {
555 _cleanup_free_ char *mangled = NULL;
556
557 r = unit_name_mangle(opts.arg, UNIT_NAME_MANGLE_WARN, &mangled);
558 if (r < 0)
559 return log_error_errno(r, "Failed to mangle unit name %s: %m", opts.arg);
560
561 free_and_replace(arg_unit, mangled);
562 break;
563 }
564
565 OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "table", NULL,
566 "Output plot's raw time data as a table"):
567 arg_table = true;
568 break;
569
570 OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "no-legend", NULL,
571 "Disable column headers and hints in plot with either --table or --json="):
572 arg_legend = false;
573 break;
574
575 OPTION_LONG("tldr", NULL, "Skip comments and empty lines"):
576 arg_cat_flags = CAT_TLDR;
577 break;
578
579 OPTION('m', "mask", NULL, "Parse parameter as numeric capability mask"):
580 arg_capability = CAPABILITY_MASK;
581 break;
582
583 OPTION_LONG("scale-svg", "FACTOR", "Stretch x-axis of plot by FACTOR (default: 1.0)"):
584 arg_svg_timescale = strtod(opts.arg, NULL);
585 break;
586
587 OPTION_LONG("detailed", NULL,
588 "Add more details to SVG plot, e.g. show activation timestamps"):
589 arg_detailed_svg = true;
590 break;
591
592 OPTION_LONG("drm-device", "PATH", "Use this DRM device sysfs path to get EDID"):
593 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_drm_device_path);
594 if (r < 0)
595 return r;
596 break;
597
598 OPTION_LONG("debugger", "DEBUGGER", "Use the given debugger"):
599 r = free_and_strdup_warn(&arg_debugger, opts.arg);
600 if (r < 0)
601 return r;
602 break;
603
604 OPTION('A', "debugger-arguments", "ARGS", "Pass the given arguments to the debugger"): {
605 _cleanup_strv_free_ char **l = NULL;
606 r = strv_split_full(&l, opts.arg, WHITESPACE, EXTRACT_UNQUOTE);
607 if (r < 0)
608 return log_error_errno(r, "Failed to parse debugger arguments '%s': %m", opts.arg);
609 strv_free_and_replace(arg_debugger_args, l);
610 break;
611 }
612 }
613
614 _cleanup_strv_free_ char **args = strv_copy(option_parser_get_args(&opts)); /* args is [arg1, arg2, …] */
615 if (!args || strv_prepend(&args, verb) < 0) /* args is now [arg0, arg1, arg2, …] */
616 return log_oom();
617
618 if (arg_offline && !streq_ptr(verb, "security"))
619 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
620 "Option --offline= is only supported for security right now.");
621
622 if (arg_offline && strv_length(args) < 2)
623 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
624 "Option --offline= requires one or more units to perform a security review.");
625
626 if (arg_json_format_flags != SD_JSON_FORMAT_OFF &&
627 !STRPTR_IN_SET(verb, "security", "inspect-elf", "dlopen-metadata", "plot", "fdstore", "pcrs", "nvpcrs", "architectures", "capability", "exit-status"))
628 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
629 "Option --json= is only supported for security, inspect-elf, dlopen-metadata, plot, fdstore, pcrs, nvpcrs, architectures, capability, exit-status right now.");
630
631 if (arg_threshold != 100 && !streq_ptr(verb, "security"))
632 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
633 "Option --threshold= is only supported for security right now.");
634
635 if (arg_runtime_scope == RUNTIME_SCOPE_GLOBAL && !streq_ptr(verb, "unit-paths"))
636 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
637 "Option --global only makes sense with verb unit-paths.");
638
639 if (streq_ptr(verb, "cat-config") && arg_runtime_scope == RUNTIME_SCOPE_USER)
640 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
641 "Option --user is not supported for cat-config right now.");
642
643 if (arg_security_policy && !streq_ptr(verb, "security"))
644 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
645 "Option --security-policy= is only supported for security.");
646
647 if ((arg_root || arg_image) &&
648 !STRPTR_IN_SET(verb, "cat-config", "verify", "condition", "inspect-elf", "unit-gdb") &&
649 (!(streq_ptr(verb, "security") && arg_offline)))
650 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
651 "Options --root= and --image= are only supported for cat-config, verify, condition, unit-gdb, and security when used with --offline= right now.");
652
653 /* Having both an image and a root is not supported by the code */
654 if (arg_root && arg_image)
655 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
656
657 if (arg_unit && !streq_ptr(verb, "condition"))
658 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --unit= is only supported for condition");
659
660 if (streq_ptr(verb, "condition") && !arg_unit && strv_length(args) < 2)
661 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too few arguments for condition");
662
663 if (streq_ptr(verb, "condition") && arg_unit && strv_length(args) > 1)
664 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No conditions can be passed if --unit= is used.");
665
666 if (arg_table && !streq_ptr(verb, "plot"))
667 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --table is only supported for plot right now.");
668
669 if (arg_table && sd_json_format_enabled(arg_json_format_flags))
670 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--table and --json= are mutually exclusive.");
671
672 if (arg_capability != CAPABILITY_LITERAL && !streq_ptr(verb, "capability"))
673 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --mask is only supported for capability.");
674
675 if (arg_drm_device_path && !streq_ptr(verb, "chid"))
676 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --drm-device is only supported for chid right now.");
677
678 *ret_args = TAKE_PTR(args);
679 return 1; /* work to do */
680 }
681
682 static int run(int argc, char *argv[]) {
683 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
684 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
685 _cleanup_strv_free_ char **args = NULL;
686 int r;
687
688 setlocale(LC_ALL, "");
689 setlocale(LC_NUMERIC, "C"); /* we want to format/parse floats in C style */
690
691 log_setup();
692
693 r = parse_argv(argc, argv, &args);
694 if (r <= 0)
695 return r;
696
697 /* Open up and mount the image */
698 if (arg_image) {
699 assert(!arg_root);
700
701 r = mount_image_privately_interactively(
702 arg_image,
703 arg_image_policy,
704 DISSECT_IMAGE_GENERIC_ROOT |
705 DISSECT_IMAGE_RELAX_VAR_CHECK |
706 DISSECT_IMAGE_READ_ONLY |
707 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
708 &mounted_dir,
709 /* ret_dir_fd= */ NULL,
710 &loop_device);
711 if (r < 0)
712 return r;
713
714 arg_root = strdup(mounted_dir);
715 if (!arg_root)
716 return log_oom();
717 }
718
719 return dispatch_verb(args, NULL);
720 }
721
722 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);