Debian Codegraph @

src/systemctl/systemctl-edit.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "alloc-util.h"
6 #include "bus-util.h"
7 #include "edit-util.h"
8 #include "hashmap.h"
9 #include "label-util.h"
10 #include "pager.h"
11 #include "path-lookup.h"
12 #include "path-util.h"
13 #include "pretty-print.h"
14 #include "string-util.h"
15 #include "strv.h"
16 #include "systemctl.h"
17 #include "systemctl-daemon-reload.h"
18 #include "systemctl-edit.h"
19 #include "systemctl-util.h"
20 #include "terminal-util.h"
21 #include "unit-name.h"
22
23 int verb_cat(int argc, char *argv[], uintptr_t _data, void *userdata) {
24 _cleanup_hashmap_free_ Hashmap *cached_id_map = NULL, *cached_name_map = NULL;
25 _cleanup_(lookup_paths_done) LookupPaths lp = {};
26 _cleanup_strv_free_ char **names = NULL;
27 sd_bus *bus = NULL;
28 bool first = true;
29 int r, rc = 0;
30
31 /* Include all units by default — i.e. continue as if the --all option was used */
32 if (strv_isempty(arg_states))
33 arg_all = true;
34
35 if (arg_transport != BUS_TRANSPORT_LOCAL)
36 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot remotely cat units.");
37
38 r = lookup_paths_init_or_warn(&lp, arg_runtime_scope, 0, arg_root);
39 if (r < 0)
40 return r;
41
42 if (install_client_side() == INSTALL_CLIENT_SIDE_NO) {
43 r = acquire_bus(BUS_MANAGER, &bus);
44 if (r < 0)
45 return r;
46
47 r = expand_unit_names(bus, strv_skip(argv, 1), NULL, &names, NULL);
48 if (r < 0)
49 return log_error_errno(r, "Failed to expand names: %m");
50
51 r = maybe_extend_with_unit_dependencies(bus, &names);
52 if (r < 0)
53 return r;
54 } else {
55 /* In client-side mode (--global, --root, etc.), just mangle names without bus interaction */
56 r = mangle_names("to cat", strv_skip(argv, 1), &names);
57 if (r < 0)
58 return r;
59 }
60
61 pager_open(arg_pager_flags);
62
63 STRV_FOREACH(name, names) {
64 _cleanup_free_ char *fragment_path = NULL;
65 _cleanup_strv_free_ char **dropin_paths = NULL;
66
67 r = unit_find_paths(bus, *name, &lp, /* force_client_side= */ !bus, &cached_id_map, &cached_name_map, &fragment_path, &dropin_paths);
68 if (r == -ERFKILL) {
69 printf("%s# Unit %s is masked%s.\n",
70 ansi_highlight_magenta(),
71 *name,
72 ansi_normal());
73 continue;
74 }
75 if (r == -EKEYREJECTED) {
76 printf("%s# Unit %s could not be loaded.%s\n",
77 ansi_highlight_magenta(),
78 *name,
79 ansi_normal());
80 continue;
81 }
82 if (r < 0)
83 return r;
84 if (r == 0) {
85 /* Skip units which have no on-disk counterpart, but propagate the error to the
86 * user (if --force is set, eat the error, just like unit_find_paths()) */
87 if (!arg_force)
88 rc = -ENOENT;
89 continue;
90 }
91
92 if (first)
93 first = false;
94 else
95 puts("");
96
97 if (bus && need_daemon_reload(bus, *name) > 0) /* ignore errors (<0), this is informational output */
98 fprintf(stderr,
99 "%s# Warning: %s changed on disk, the version systemd has loaded is outdated.\n"
100 "%s# This output shows the current version of the unit's original fragment and drop-in files.\n"
101 "%s# If fragments or drop-ins were added or removed, they are not properly reflected in this output.\n"
102 "%s# Run 'systemctl%s daemon-reload' to reload units.%s\n",
103 ansi_highlight_red(),
104 *name,
105 ansi_highlight_red(),
106 ansi_highlight_red(),
107 ansi_highlight_red(),
108 arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? "" : " --user",
109 ansi_normal());
110
111 r = cat_files(fragment_path, dropin_paths, /* flags= */ CAT_FORMAT_HAS_SECTIONS);
112 if (r < 0)
113 return r;
114 }
115
116 return rc;
117 }
118
119 static int get_file_to_edit(
120 const LookupPaths *lp,
121 const char *name,
122 char **ret_path) {
123
124 _cleanup_free_ char *path = NULL;
125
126 assert(lp);
127 assert(name);
128 assert(ret_path);
129
130 path = path_join(lp->persistent_config, name);
131 if (!path)
132 return log_oom();
133
134 if (arg_runtime) {
135 _cleanup_free_ char *run = NULL;
136
137 run = path_join(lp->runtime_config, name);
138 if (!run)
139 return log_oom();
140
141 if (access(path, F_OK) >= 0)
142 return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
143 "Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.",
144 run, path);
145
146 *ret_path = TAKE_PTR(run);
147 } else
148 *ret_path = TAKE_PTR(path);
149
150 return 0;
151 }
152
153 static int unit_file_create_new(
154 EditFileContext *context,
155 const LookupPaths *lp,
156 const char *unit_name,
157 const char *suffix,
158 char * const *original_unit_paths) {
159
160 _cleanup_free_ char *unit = NULL, *new_path = NULL;
161 int r;
162
163 assert(context);
164 assert(lp);
165 assert(unit_name);
166
167 unit = strjoin(unit_name, suffix);
168 if (!unit)
169 return log_oom();
170
171 r = get_file_to_edit(lp, unit, &new_path);
172 if (r < 0)
173 return r;
174
175 return edit_files_add(context, new_path, NULL, original_unit_paths);
176 }
177
178 static int unit_file_create_copy(
179 EditFileContext *context,
180 const LookupPaths *lp,
181 const char *unit_name,
182 const char *fragment_path) {
183
184 _cleanup_free_ char *new_path = NULL;
185 int r;
186
187 assert(context);
188 assert(lp);
189 assert(fragment_path);
190 assert(unit_name);
191
192 r = get_file_to_edit(lp, unit_name, &new_path);
193 if (r < 0)
194 return r;
195
196 if (!path_equal(fragment_path, new_path) && access(new_path, F_OK) >= 0) {
197 char response;
198
199 r = ask_char(&response, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", new_path, fragment_path);
200 if (r < 0)
201 return r;
202
203 if (response != 'y')
204 return log_warning_errno(SYNTHETIC_ERRNO(EKEYREJECTED), "%s skipped.", unit_name);
205 }
206
207 return edit_files_add(context, new_path, fragment_path, NULL);
208 }
209
210 static int find_paths_to_edit(
211 sd_bus *bus,
212 EditFileContext *context,
213 char **names) {
214
215 _cleanup_hashmap_free_ Hashmap *cached_id_map = NULL, *cached_name_map = NULL;
216 _cleanup_(lookup_paths_done) LookupPaths lp = {};
217 _cleanup_free_ char *drop_in_alloc = NULL, *suffix = NULL;
218 const char *drop_in;
219 int r;
220
221 assert(context);
222 assert(names);
223
224 if (isempty(arg_drop_in))
225 drop_in = "override.conf";
226 else if (!endswith(arg_drop_in, ".conf")) {
227 drop_in_alloc = strjoin(arg_drop_in, ".conf");
228 if (!drop_in_alloc)
229 return log_oom();
230
231 drop_in = drop_in_alloc;
232 } else
233 drop_in = arg_drop_in;
234
235 if (!filename_is_valid(drop_in))
236 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid drop-in file name '%s'.", drop_in);
237
238 suffix = strjoin(".d/", drop_in);
239 if (!suffix)
240 return log_oom();
241
242 r = lookup_paths_init(&lp, arg_runtime_scope, 0, arg_root);
243 if (r < 0)
244 return r;
245
246 STRV_FOREACH(name, names) {
247 _cleanup_free_ char *path = NULL;
248 _cleanup_strv_free_ char **unit_paths = NULL;
249
250 r = unit_find_paths(bus, *name, &lp, /* force_client_side= */ !bus, &cached_id_map, &cached_name_map, &path, &unit_paths);
251 if (r == -EKEYREJECTED && bus) {
252 /* If loading of the unit failed server side complete, then the server won't tell us
253 * the unit file path. In that case, find the file client side. */
254
255 log_debug_errno(r, "Unit '%s' was not loaded correctly, retrying client-side.", *name);
256 r = unit_find_paths(bus, *name, &lp, /* force_client_side= */ true, &cached_id_map, &cached_name_map, &path, &unit_paths);
257 }
258 if (r == -ERFKILL)
259 return log_error_errno(r, "Unit '%s' masked, cannot edit.", *name);
260 if (r < 0)
261 return r; /* Already logged by unit_find_paths() */
262
263 if (!path) {
264 if (!arg_force)
265 return log_info_errno(SYNTHETIC_ERRNO(ENOENT),
266 "Run 'systemctl edit%s --force --full %s' to create a new unit.",
267 arg_runtime_scope == RUNTIME_SCOPE_GLOBAL ? " --global" :
268 arg_runtime_scope == RUNTIME_SCOPE_USER ? " --user" : "",
269 *name);
270
271 /* Create a new unit from scratch */
272 r = unit_file_create_new(
273 context,
274 &lp,
275 *name,
276 arg_full ? NULL : suffix,
277 NULL);
278 } else {
279 _cleanup_free_ char *unit_name = NULL;
280
281 r = path_extract_filename(path, &unit_name);
282 if (r < 0)
283 return log_error_errno(r, "Failed to extract unit name from path '%s': %m", path);
284
285 /* We follow unit aliases, but we need to propagate the instance */
286 if (unit_name_is_valid(*name, UNIT_NAME_INSTANCE) &&
287 unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) {
288 _cleanup_free_ char *instance = NULL, *tmp_name = NULL;
289
290 r = unit_name_to_instance(*name, &instance);
291 if (r < 0)
292 return r;
293
294 r = unit_name_replace_instance(unit_name, instance, &tmp_name);
295 if (r < 0)
296 return r;
297
298 free_and_replace(unit_name, tmp_name);
299 }
300
301 if (arg_full)
302 r = unit_file_create_copy(
303 context,
304 &lp,
305 unit_name,
306 path);
307 else {
308 r = strv_prepend(&unit_paths, path);
309 if (r < 0)
310 return log_oom();
311
312 r = unit_file_create_new(
313 context,
314 &lp,
315 unit_name,
316 suffix,
317 unit_paths);
318 }
319 }
320 if (r < 0)
321 return r;
322 }
323
324 return 0;
325 }
326
327 int verb_edit(int argc, char *argv[], uintptr_t _data, void *userdata) {
328 _cleanup_(edit_file_context_done) EditFileContext context = {
329 .marker_start = DROPIN_MARKER_START,
330 .marker_end = DROPIN_MARKER_END,
331 .remove_parent = !arg_full,
332 .overwrite_with_origin = true,
333 .read_from_stdin = arg_stdin,
334 };
335 _cleanup_strv_free_ char **names = NULL;
336 sd_bus *bus = NULL;
337 int r;
338
339 if (!on_tty() && !arg_stdin)
340 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units interactively if not on a tty.");
341
342 if (arg_transport != BUS_TRANSPORT_LOCAL)
343 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units remotely.");
344
345 r = mac_init();
346 if (r < 0)
347 return r;
348
349 if (install_client_side() == INSTALL_CLIENT_SIDE_NO) {
350 r = acquire_bus(BUS_MANAGER, &bus);
351 if (r < 0)
352 return r;
353
354 r = expand_unit_names(bus, strv_skip(argv, 1), NULL, &names, NULL);
355 if (r < 0)
356 return log_error_errno(r, "Failed to expand names: %m");
357 } else {
358 /* In client-side mode (--global, --root, etc.), just mangle names without bus interaction */
359 r = mangle_names("to edit", strv_skip(argv, 1), &names);
360 if (r < 0)
361 return r;
362 }
363 if (strv_isempty(names))
364 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No units matched the specified patterns.");
365
366 if (arg_stdin && arg_full && strv_length(names) != 1)
367 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
368 "With 'edit --stdin --full', exactly one unit for editing must be specified.");
369
370 r = find_paths_to_edit(bus, &context, names);
371 if (r < 0)
372 return r;
373
374 r = do_edit_files_and_install(&context);
375 if (r < 0)
376 return r;
377
378 if (!arg_no_reload &&
379 install_client_side() == INSTALL_CLIENT_SIDE_NO) {
380 r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
381 if (r < 0)
382 return r;
383 }
384
385 return 0;
386 }