Debian Codegraph @

src/shared/service-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "build.h"
6 #include "bus-object.h"
7 #include "format-table.h"
8 #include "help-util.h"
9 #include "log.h"
10 #include "options.h"
11 #include "runtime-scope.h"
12 #include "service-util.h"
13
14 static int help(const char *service,
15 const char *description,
16 bool with_bus_introspect,
17 bool with_runtime_scope) {
18
19 static const char* const groups[] = {
20 NULL,
21 "Bus introspection",
22 "Runtime scope",
23 };
24
25 bool conds[ELEMENTSOF(groups)] = { true, with_bus_introspect, with_runtime_scope };
26 Table* tables[ELEMENTSOF(groups)] = {};
27 CLEANUP_ELEMENTS(tables, table_unref_array_clear);
28 int r;
29
30 for (size_t i = 0; i < ELEMENTSOF(groups); i++)
31 if (conds[i]) {
32 r = option_parser_get_help_table_group(groups[i], &tables[i]);
33 if (r < 0)
34 return r;
35 }
36
37 (void) table_sync_column_widths(0, tables[0], tables[1] ?: tables[2], tables[1] ? tables[2] : NULL);
38
39 help_cmdline("[OPTIONS...]");
40 help_abstract(description);
41
42 help_section("Options");
43 for (size_t i = 0; i < ELEMENTSOF(groups); i++)
44 if (conds[i]) {
45 r = table_print_or_warn(tables[i]);
46 if (r < 0)
47 return r;
48 }
49
50 help_man_page_reference(service, "8");
51 return 0; /* No further action */
52 }
53
54 int service_parse_argv(
55 const char *service,
56 const char *description,
57 const BusObjectImplementation* const* bus_objects,
58 RuntimeScope *runtime_scope,
59 int argc, char *argv[]) {
60
61 assert(argc >= 0);
62 assert(argv);
63
64 OptionParser opts = { argc, argv };
65
66 FOREACH_OPTION_OR_RETURN(c, &opts)
67 switch (c) {
68
69 OPTION_COMMON_HELP:
70 return help(service,
71 description,
72 /* with_bus_introspect= */ bus_objects,
73 /* with_runtime_scope= */ runtime_scope);
74
75 OPTION_COMMON_VERSION:
76 return version();
77
78 OPTION_GROUP("Bus introspection"): {}
79
80 OPTION_LONG("bus-introspect", "PATH", "Write D-Bus XML introspection data"):
81 /* The option is defined in the shared option table, but it's not supported in this binary,
82 * so we pretend it doesn't exist. */
83 if (!bus_objects)
84 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
85 "This service does not support the --bus-introspect= option.");
86
87 return bus_introspect_implementations(stdout, opts.arg, bus_objects);
88
89 OPTION_GROUP("Runtime scope"): {}
90
91 OPTION_LONG_DATA("system", NULL, /* data= */ RUNTIME_SCOPE_SYSTEM,
92 "Start service in system mode"): {}
93 OPTION_LONG_DATA("user", NULL, /* data= */ RUNTIME_SCOPE_USER,
94 "Start service in user mode"):
95 if (!runtime_scope)
96 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
97 "This service does not support the --system/--user options.");
98
99 *runtime_scope = opts.opt->data;
100 break;
101 }
102
103 if (option_parser_get_n_args(&opts) > 0)
104 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
105
106 return 1; /* Further action */
107 }