Debian Codegraph @

src/shared/verbs.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "env-util.h"
4 #include "format-table.h"
5 #include "log.h"
6 #include "string-util.h"
7 #include "strv.h"
8 #include "terminal-util.h"
9 #include "verbs.h"
10 #include "virt.h"
11
12 /* Wraps running_in_chroot() which is used in various places, but also adds an environment variable check
13 * so external processes can reliably force this on. */
14 bool running_in_chroot_or_offline(void) {
15 int r;
16
17 /* Added to support use cases like rpm-ostree, where from %post scripts we only want to execute "preset",
18 * but not "start"/"restart" for example.
19 *
20 * See docs/ENVIRONMENT.md for docs.
21 */
22 r = getenv_bool("SYSTEMD_OFFLINE");
23 if (r >= 0)
24 return r > 0;
25 if (r != -ENXIO)
26 log_debug_errno(r, "Failed to parse $SYSTEMD_OFFLINE, ignoring: %m");
27
28 /* We've had this condition check for a long time which basically checks for legacy chroot case like Fedora's
29 * "mock", which is used for package builds. We don't want to try to start systemd services there, since
30 * without --new-chroot we don't even have systemd running, and even if we did, adding a concept of background
31 * daemons to builds would be an enormous change, requiring considering things like how the journal output is
32 * handled, etc. And there's really not a use case today for a build talking to a service.
33 *
34 * Note this call itself also looks for a different variable SYSTEMD_IGNORE_CHROOT=1.
35 */
36 r = running_in_chroot();
37 if (r < 0)
38 log_debug_errno(r, "Failed to check if we're running in chroot, assuming not: %m");
39 return r > 0;
40 }
41
42 bool should_bypass(const char *env_prefix) {
43 char *env;
44 int r;
45
46 assert(env_prefix);
47
48 env = strjoina(env_prefix, "_BYPASS");
49
50 r = getenv_bool(env);
51 if (r < 0 && r != -ENXIO)
52 log_debug_errno(r, "Failed to parse $%s, assuming no: %m", env);
53 if (r <= 0)
54 return false;
55
56 log_debug("$%s is enabled, skipping execution.", env);
57 return true;
58 }
59
60 static bool verb_is_metadata(const Verb *verb) {
61 /* A metadata entry that is not a real verb, like the group marker */
62 return FLAGS_SET(ASSERT_PTR(verb)->flags, VERB_GROUP_MARKER);
63 }
64
65 const Verb* verbs_find_verb(const char *name, const Verb verbs[], const Verb verbs_end[]) {
66 assert(verbs);
67 assert(verbs_end > verbs);
68 assert((uintptr_t) verbs % sizeof(void*) == 0);
69 assert(verbs[0].verb);
70
71 for (const Verb *verb = verbs; verb < verbs_end; verb++) {
72 if (verb_is_metadata(verb))
73 continue;
74
75 if (name ? streq(name, verb->verb) : FLAGS_SET(verb->flags, VERB_DEFAULT))
76 return verb;
77 }
78
79 /* At the end of the list? */
80 return NULL;
81 }
82
83 int _dispatch_verb(char **args, const Verb verbs[], const Verb verbs_end[], void *userdata) {
84 int r;
85
86 assert(verbs);
87 assert(verbs_end > verbs);
88 assert((uintptr_t) verbs % sizeof(void*) == 0);
89 assert(verbs[0].verb);
90
91 const char *name = args ? args[0] : NULL;
92 size_t left = strv_length(args);
93
94 const Verb *verb = verbs_find_verb(name, verbs, verbs_end);
95 if (!verb) {
96 _cleanup_strv_free_ char **verb_strv = NULL;
97
98 for (verb = verbs; verb < verbs_end; verb++) {
99 if (verb_is_metadata(verb))
100 continue;
101
102 r = strv_extend(&verb_strv, verb->verb);
103 if (r < 0)
104 return log_oom();
105 }
106 assert(!strv_isempty(verb_strv)); /* At least one verb should be defined… */
107
108 if (name) {
109 /* Be more helpful to the user, and give a hint what the user might have wanted to type. */
110 const char *found = strv_find_closest(verb_strv, name);
111 if (found)
112 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
113 "Unknown command verb '%s', did you mean '%s'?", name, found);
114
115 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command verb '%s'.", name);
116 }
117
118 if (strv_length(verb_strv) >= 2) {
119 _cleanup_free_ char *joined = strv_join(verb_strv, ", ");
120 if (!joined)
121 return log_oom();
122
123 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
124 "Command verb required (one of %s).", joined);
125 }
126
127 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Command verb '%s' required.", verb_strv[0]);
128 }
129
130 if (!name)
131 left = 1;
132
133 if (verb->min_args != VERB_ANY && left < verb->min_args)
134 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too few arguments.");
135
136 if (verb->max_args != VERB_ANY && left > verb->max_args)
137 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many arguments.");
138
139 if ((verb->flags & VERB_ONLINE_ONLY) && running_in_chroot_or_offline()) {
140 log_info("Running in chroot, ignoring command '%s'", name ?: verb->verb);
141 return 0;
142 }
143
144 if (!name)
145 return verb->dispatch(1, STRV_MAKE(verb->verb), verb->data, userdata);
146
147 assert(left < INT_MAX); /* args are derived from argc+argv, so their size must fit in an int. */
148 return verb->dispatch(left, args, verb->data, userdata);
149 }
150
151 #define VERB_SYNOPSIS_WIDTH_SANE 25
152
153 static const char* find_point_to_break(const char *s, size_t max_width) {
154 /* Locate the first space, preferably after max_width, or the last space otherwise.
155 * Return the part after the space. */
156
157 if (strlen(s) <= max_width)
158 return NULL;
159
160 const char *p = strchr(s + max_width, ' ') ?: strrchr(s, ' ');
161 return p ? p + 1 : NULL;
162 }
163
164 static int verb_add_help_one(Table *table, const Verb *verb) {
165 assert(table);
166 assert(verb);
167
168 bool is_default = FLAGS_SET(verb->flags, VERB_DEFAULT);
169 int r;
170
171 /* We indent the option string by two spaces. We could set the minimum cell width and
172 * right-align for a similar result, but that'd be more work. This is only used for
173 * display. */
174 _cleanup_free_ char *s = strjoin(" ",
175 is_default ? "[" : "",
176 verb->verb,
177 verb->argspec ? " " : "",
178 strempty(verb->argspec),
179 is_default ? "]" : "");
180 if (!s)
181 return log_oom();
182
183 const char *ss = NULL;
184 if (columns() < VERB_SYNOPSIS_WIDTH_SANE * 4) {
185 /* If the synopsis is very wide, try to split it up. But do this only if the terminal
186 * is not very wide. If it _is_ wide, the broken up synopsis would look silly. */
187 const char *p = find_point_to_break(s, VERB_SYNOPSIS_WIDTH_SANE), *p2 = NULL;
188 if (p) {
189 const char *s1 = strndupa_safe(s, p - s), *s2 = NULL;
190
191 p2 = find_point_to_break(p, VERB_SYNOPSIS_WIDTH_SANE - 4); /* we indent by two spaces more */
192 if (p2)
193 s2 = strndupa_safe(p, p2 - p);
194
195 if (s2)
196 ss = strjoina(s1, "\n ", s2, "\n ", p2);
197 else
198 ss = strjoina(s1, "\n ", p);
199 }
200 }
201
202 r = table_add_cell(table, NULL, TABLE_STRING, ss ?: s);
203 if (r < 0)
204 return table_log_add_error(r);
205
206 _cleanup_strv_free_ char **t = strv_split(verb->help, /* separators= */ NULL);
207 if (!t)
208 return log_oom();
209
210 r = table_add_many(table, TABLE_STRV_WRAPPED, t);
211 if (r < 0)
212 return table_log_add_error(r);
213
214 return 0;
215 }
216
217 int _verbs_get_help_table(
218 const Verb verbs[],
219 const Verb verbs_end[],
220 const char *group,
221 Table **ret) {
222 int r;
223
224 assert(verbs);
225 assert(verbs_end > verbs);
226 assert((uintptr_t) verbs % sizeof(void*) == 0);
227 assert(ret);
228
229 _cleanup_(table_unrefp) Table *table = table_new("verb", "help");
230 if (!table)
231 return log_oom();
232
233 bool in_group = group == NULL; /* Are we currently in the section on the array that forms
234 * group <group>? The first part is the default group, so
235 * if the group was not specified, we are in. */
236
237 for (const Verb *verb = verbs; verb < verbs_end; verb++) {
238 assert(verb->verb);
239
240 bool group_marker = FLAGS_SET(verb->flags, VERB_GROUP_MARKER);
241 if (!in_group) {
242 in_group = group_marker && streq(group, verb->verb);
243 continue;
244 }
245 if (group_marker)
246 break; /* End of group */
247
248 if (!verb->help)
249 /* No help string — we do not show the verb */
250 continue;
251
252 r = verb_add_help_one(table, verb);
253 if (r < 0)
254 return r;
255 }
256
257 table_set_header(table, false);
258 *ret = TAKE_PTR(table);
259 return 0;
260 }