]>
Commit | Line | Data |
---|---|---|
61d0578b LP |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
e08f94ac LP |
3 | #include <linux/loop.h> |
4 | ||
61d0578b LP |
5 | #include "bus-common-errors.h" |
6 | #include "bus-error.h" | |
7 | #include "conf-files.h" | |
8 | #include "copy.h" | |
9 | #include "def.h" | |
10 | #include "dirent-util.h" | |
11 | #include "dissect-image.h" | |
12 | #include "fd-util.h" | |
13 | #include "fileio.h" | |
14 | #include "fs-util.h" | |
5cfa33e0 | 15 | #include "install.h" |
61d0578b LP |
16 | #include "io-util.h" |
17 | #include "locale-util.h" | |
18 | #include "loop-util.h" | |
19 | #include "machine-image.h" | |
20 | #include "mkdir.h" | |
d8b4d14d | 21 | #include "nulstr-util.h" |
61d0578b LP |
22 | #include "os-util.h" |
23 | #include "path-lookup.h" | |
24 | #include "portable.h" | |
25 | #include "process-util.h" | |
26 | #include "set.h" | |
27 | #include "signal-util.h" | |
28 | #include "socket-util.h" | |
760877e9 | 29 | #include "sort-util.h" |
61d0578b LP |
30 | #include "string-table.h" |
31 | #include "strv.h" | |
e4de7287 | 32 | #include "tmpfile-util.h" |
61d0578b LP |
33 | #include "user-util.h" |
34 | ||
35 | static const char profile_dirs[] = CONF_PATHS_NULSTR("systemd/portable/profile"); | |
36 | ||
37 | /* Markers used in the first line of our 20-portable.conf unit file drop-in to determine, that a) the unit file was | |
38 | * dropped there by the portable service logic and b) for which image it was dropped there. */ | |
39 | #define PORTABLE_DROPIN_MARKER_BEGIN "# Drop-in created for image '" | |
40 | #define PORTABLE_DROPIN_MARKER_END "', do not edit." | |
41 | ||
42 | static bool prefix_match(const char *unit, const char *prefix) { | |
43 | const char *p; | |
44 | ||
45 | p = startswith(unit, prefix); | |
46 | if (!p) | |
47 | return false; | |
48 | ||
49 | /* Only respect prefixes followed by dash or dot or when there's a complete match */ | |
50 | return IN_SET(*p, '-', '.', '@', 0); | |
51 | } | |
52 | ||
53 | static bool unit_match(const char *unit, char **matches) { | |
54 | const char *dot; | |
55 | char **i; | |
56 | ||
57 | dot = strrchr(unit, '.'); | |
58 | if (!dot) | |
59 | return false; | |
60 | ||
61 | if (!STR_IN_SET(dot, ".service", ".socket", ".target", ".timer", ".path")) | |
62 | return false; | |
63 | ||
64 | /* Empty match expression means: everything */ | |
65 | if (strv_isempty(matches)) | |
66 | return true; | |
67 | ||
68 | /* Otherwise, at least one needs to match */ | |
69 | STRV_FOREACH(i, matches) | |
70 | if (prefix_match(unit, *i)) | |
71 | return true; | |
72 | ||
73 | return false; | |
74 | } | |
75 | ||
76 | static PortableMetadata *portable_metadata_new(const char *name, int fd) { | |
77 | PortableMetadata *m; | |
78 | ||
79 | m = malloc0(offsetof(PortableMetadata, name) + strlen(name) + 1); | |
80 | if (!m) | |
81 | return NULL; | |
82 | ||
83 | strcpy(m->name, name); | |
84 | m->fd = fd; | |
85 | ||
86 | return m; | |
87 | } | |
88 | ||
89 | PortableMetadata *portable_metadata_unref(PortableMetadata *i) { | |
90 | if (!i) | |
91 | return NULL; | |
92 | ||
93 | safe_close(i->fd); | |
94 | free(i->source); | |
95 | ||
96 | return mfree(i); | |
97 | } | |
98 | ||
61d0578b LP |
99 | static int compare_metadata(PortableMetadata *const *x, PortableMetadata *const *y) { |
100 | return strcmp((*x)->name, (*y)->name); | |
101 | } | |
102 | ||
103 | int portable_metadata_hashmap_to_sorted_array(Hashmap *unit_files, PortableMetadata ***ret) { | |
104 | ||
105 | _cleanup_free_ PortableMetadata **sorted = NULL; | |
106 | Iterator iterator; | |
107 | PortableMetadata *item; | |
108 | size_t k = 0; | |
109 | ||
110 | sorted = new(PortableMetadata*, hashmap_size(unit_files)); | |
111 | if (!sorted) | |
112 | return -ENOMEM; | |
113 | ||
114 | HASHMAP_FOREACH(item, unit_files, iterator) | |
115 | sorted[k++] = item; | |
116 | ||
117 | assert(k == hashmap_size(unit_files)); | |
118 | ||
119 | typesafe_qsort(sorted, k, compare_metadata); | |
120 | ||
121 | *ret = TAKE_PTR(sorted); | |
122 | return 0; | |
123 | } | |
124 | ||
125 | static int send_item( | |
126 | int socket_fd, | |
127 | const char *name, | |
128 | int fd) { | |
129 | ||
130 | union { | |
131 | struct cmsghdr cmsghdr; | |
132 | uint8_t buf[CMSG_SPACE(sizeof(int))]; | |
133 | } control = {}; | |
134 | struct iovec iovec; | |
135 | struct msghdr mh = { | |
136 | .msg_control = &control, | |
137 | .msg_controllen = sizeof(control), | |
138 | .msg_iov = &iovec, | |
139 | .msg_iovlen = 1, | |
140 | }; | |
141 | struct cmsghdr *cmsg; | |
142 | _cleanup_close_ int data_fd = -1; | |
143 | ||
144 | assert(socket_fd >= 0); | |
145 | assert(name); | |
146 | assert(fd >= 0); | |
147 | ||
148 | data_fd = fd_duplicate_data_fd(fd); | |
149 | if (data_fd < 0) | |
150 | return data_fd; | |
151 | ||
152 | cmsg = CMSG_FIRSTHDR(&mh); | |
153 | cmsg->cmsg_level = SOL_SOCKET; | |
154 | cmsg->cmsg_type = SCM_RIGHTS; | |
155 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); | |
156 | memcpy(CMSG_DATA(cmsg), &data_fd, sizeof(int)); | |
157 | ||
158 | mh.msg_controllen = CMSG_SPACE(sizeof(int)); | |
159 | iovec = IOVEC_MAKE_STRING(name); | |
160 | ||
161 | if (sendmsg(socket_fd, &mh, MSG_NOSIGNAL) < 0) | |
162 | return -errno; | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
167 | static int recv_item( | |
168 | int socket_fd, | |
169 | char **ret_name, | |
170 | int *ret_fd) { | |
171 | ||
172 | union { | |
173 | struct cmsghdr cmsghdr; | |
174 | uint8_t buf[CMSG_SPACE(sizeof(int))]; | |
175 | } control = {}; | |
176 | char buffer[PATH_MAX+2]; | |
177 | struct iovec iov = IOVEC_INIT(buffer, sizeof(buffer)-1); | |
178 | struct msghdr mh = { | |
179 | .msg_control = &control, | |
180 | .msg_controllen = sizeof(control), | |
181 | .msg_iov = &iov, | |
182 | .msg_iovlen = 1, | |
183 | }; | |
184 | struct cmsghdr *cmsg; | |
185 | _cleanup_close_ int found_fd = -1; | |
186 | char *copy; | |
187 | ssize_t n; | |
188 | ||
189 | assert(socket_fd >= 0); | |
190 | assert(ret_name); | |
191 | assert(ret_fd); | |
192 | ||
a6887cc0 | 193 | n = recvmsg(socket_fd, &mh, MSG_CMSG_CLOEXEC); |
61d0578b LP |
194 | if (n < 0) |
195 | return -errno; | |
196 | ||
197 | CMSG_FOREACH(cmsg, &mh) { | |
198 | if (cmsg->cmsg_level == SOL_SOCKET && | |
199 | cmsg->cmsg_type == SCM_RIGHTS) { | |
200 | ||
201 | if (cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { | |
202 | assert(found_fd < 0); | |
203 | found_fd = *(int*) CMSG_DATA(cmsg); | |
204 | break; | |
205 | } | |
206 | ||
207 | cmsg_close_all(&mh); | |
208 | return -EIO; | |
209 | } | |
210 | } | |
211 | ||
212 | buffer[n] = 0; | |
213 | ||
214 | copy = strdup(buffer); | |
215 | if (!copy) | |
216 | return -ENOMEM; | |
217 | ||
218 | *ret_name = copy; | |
219 | *ret_fd = TAKE_FD(found_fd); | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
67818055 YW |
224 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(portable_metadata_hash_ops, char, string_hash_func, string_compare_func, |
225 | PortableMetadata, portable_metadata_unref); | |
226 | ||
61d0578b LP |
227 | static int extract_now( |
228 | const char *where, | |
229 | char **matches, | |
230 | int socket_fd, | |
231 | PortableMetadata **ret_os_release, | |
232 | Hashmap **ret_unit_files) { | |
233 | ||
67818055 | 234 | _cleanup_hashmap_free_ Hashmap *unit_files = NULL; |
61d0578b LP |
235 | _cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL; |
236 | _cleanup_(lookup_paths_free) LookupPaths paths = {}; | |
237 | _cleanup_close_ int os_release_fd = -1; | |
238 | _cleanup_free_ char *os_release_path = NULL; | |
239 | char **i; | |
240 | int r; | |
241 | ||
242 | /* Extracts the metadata from a directory tree 'where'. Extracts two kinds of information: the /etc/os-release | |
243 | * data, and all unit files matching the specified expression. Note that this function is called in two very | |
244 | * different but also similar contexts. When the tool gets invoked on a directory tree, we'll process it | |
245 | * directly, and in-process, and thus can return the requested data directly, via 'ret_os_release' and | |
246 | * 'ret_unit_files'. However, if the tool is invoked on a raw disk image — which needs to be mounted first — we | |
247 | * are invoked in a child process with private mounts and then need to send the collected data to our | |
248 | * parent. To handle both cases in one call this function also gets a 'socket_fd' parameter, which when >= 0 is | |
249 | * used to send the data to the parent. */ | |
250 | ||
251 | assert(where); | |
252 | ||
253 | /* First, find /etc/os-release and send it upstream (or just save it). */ | |
254 | r = open_os_release(where, &os_release_path, &os_release_fd); | |
255 | if (r < 0) | |
256 | log_debug_errno(r, "Couldn't acquire os-release file, ignoring: %m"); | |
257 | else { | |
258 | if (socket_fd >= 0) { | |
259 | r = send_item(socket_fd, "/etc/os-release", os_release_fd); | |
260 | if (r < 0) | |
261 | return log_debug_errno(r, "Failed to send os-release file: %m"); | |
262 | } | |
263 | ||
264 | if (ret_os_release) { | |
265 | os_release = portable_metadata_new("/etc/os-release", os_release_fd); | |
266 | if (!os_release) | |
267 | return -ENOMEM; | |
268 | ||
269 | os_release_fd = -1; | |
270 | os_release->source = TAKE_PTR(os_release_path); | |
271 | } | |
272 | } | |
273 | ||
274 | /* Then, send unit file data to the parent (or/and add it to the hashmap). For that we use our usual unit | |
275 | * discovery logic. Note that we force looking inside of /lib/systemd/system/ for units too, as we mightbe | |
276 | * compiled for a split-usr system but the image might be a legacy-usr one. */ | |
277 | r = lookup_paths_init(&paths, UNIT_FILE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, where); | |
278 | if (r < 0) | |
279 | return log_debug_errno(r, "Failed to acquire lookup paths: %m"); | |
280 | ||
67818055 | 281 | unit_files = hashmap_new(&portable_metadata_hash_ops); |
61d0578b LP |
282 | if (!unit_files) |
283 | return -ENOMEM; | |
284 | ||
285 | STRV_FOREACH(i, paths.search_path) { | |
286 | _cleanup_free_ char *resolved = NULL; | |
287 | _cleanup_closedir_ DIR *d = NULL; | |
288 | struct dirent *de; | |
289 | ||
290 | r = chase_symlinks_and_opendir(*i, where, 0, &resolved, &d); | |
291 | if (r < 0) { | |
292 | log_debug_errno(r, "Failed to open unit path '%s', ignoring: %m", *i); | |
293 | continue; | |
294 | } | |
295 | ||
296 | FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to read directory: %m")) { | |
297 | _cleanup_(portable_metadata_unrefp) PortableMetadata *m = NULL; | |
298 | _cleanup_close_ int fd = -1; | |
299 | ||
300 | if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY)) | |
301 | continue; | |
302 | ||
303 | if (!unit_match(de->d_name, matches)) | |
304 | continue; | |
305 | ||
306 | /* Filter out duplicates */ | |
307 | if (hashmap_get(unit_files, de->d_name)) | |
308 | continue; | |
309 | ||
310 | dirent_ensure_type(d, de); | |
311 | if (!IN_SET(de->d_type, DT_LNK, DT_REG)) | |
312 | continue; | |
313 | ||
314 | fd = openat(dirfd(d), de->d_name, O_CLOEXEC|O_RDONLY); | |
315 | if (fd < 0) { | |
316 | log_debug_errno(errno, "Failed to open unit file '%s', ignoring: %m", de->d_name); | |
317 | continue; | |
318 | } | |
319 | ||
320 | if (socket_fd >= 0) { | |
321 | r = send_item(socket_fd, de->d_name, fd); | |
322 | if (r < 0) | |
323 | return log_debug_errno(r, "Failed to send unit metadata to parent: %m"); | |
324 | } | |
325 | ||
326 | m = portable_metadata_new(de->d_name, fd); | |
327 | if (!m) | |
328 | return -ENOMEM; | |
329 | fd = -1; | |
330 | ||
657ee2d8 | 331 | m->source = path_join(resolved, de->d_name); |
61d0578b LP |
332 | if (!m->source) |
333 | return -ENOMEM; | |
334 | ||
335 | r = hashmap_put(unit_files, m->name, m); | |
336 | if (r < 0) | |
337 | return log_debug_errno(r, "Failed to add unit to hashmap: %m"); | |
338 | m = NULL; | |
339 | } | |
340 | } | |
341 | ||
342 | if (ret_os_release) | |
343 | *ret_os_release = TAKE_PTR(os_release); | |
344 | if (ret_unit_files) | |
345 | *ret_unit_files = TAKE_PTR(unit_files); | |
346 | ||
347 | return 0; | |
348 | } | |
349 | ||
350 | static int portable_extract_by_path( | |
351 | const char *path, | |
352 | char **matches, | |
353 | PortableMetadata **ret_os_release, | |
354 | Hashmap **ret_unit_files, | |
355 | sd_bus_error *error) { | |
356 | ||
67818055 | 357 | _cleanup_hashmap_free_ Hashmap *unit_files = NULL; |
61d0578b LP |
358 | _cleanup_(portable_metadata_unrefp) PortableMetadata* os_release = NULL; |
359 | _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; | |
360 | int r; | |
361 | ||
362 | assert(path); | |
363 | ||
e08f94ac | 364 | r = loop_device_make_by_path(path, O_RDONLY, LO_FLAGS_PARTSCAN, &d); |
61d0578b LP |
365 | if (r == -EISDIR) { |
366 | /* We can't turn this into a loop-back block device, and this returns EISDIR? Then this is a directory | |
367 | * tree and not a raw device. It's easy then. */ | |
368 | ||
369 | r = extract_now(path, matches, -1, &os_release, &unit_files); | |
370 | if (r < 0) | |
371 | return r; | |
372 | ||
373 | } else if (r < 0) | |
374 | return log_debug_errno(r, "Failed to set up loopback device: %m"); | |
375 | else { | |
376 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; | |
377 | _cleanup_(rmdir_and_freep) char *tmpdir = NULL; | |
378 | _cleanup_(close_pairp) int seq[2] = { -1, -1 }; | |
379 | _cleanup_(sigkill_waitp) pid_t child = 0; | |
380 | ||
381 | /* We now have a loopback block device, let's fork off a child in its own mount namespace, mount it | |
382 | * there, and extract the metadata we need. The metadata is sent from the child back to us. */ | |
383 | ||
384 | BLOCK_SIGNALS(SIGCHLD); | |
385 | ||
386 | r = mkdtemp_malloc("/tmp/inspect-XXXXXX", &tmpdir); | |
387 | if (r < 0) | |
388 | return log_debug_errno(r, "Failed to create temporary directory: %m"); | |
389 | ||
d4dffb85 | 390 | r = dissect_image(d->fd, NULL, 0, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_REQUIRE_ROOT|DISSECT_IMAGE_DISCARD_ON_LOOP|DISSECT_IMAGE_RELAX_VAR_CHECK, &m); |
61d0578b LP |
391 | if (r == -ENOPKG) |
392 | sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Couldn't identify a suitable partition table or file system in '%s'.", path); | |
393 | else if (r == -EADDRNOTAVAIL) | |
394 | sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "No root partition for specified root hash found in '%s'.", path); | |
395 | else if (r == -ENOTUNIQ) | |
396 | sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Multiple suitable root partitions found in image '%s'.", path); | |
397 | else if (r == -ENXIO) | |
398 | sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "No suitable root partition found in image '%s'.", path); | |
399 | else if (r == -EPROTONOSUPPORT) | |
400 | sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Device '%s' is loopback block device with partition scanning turned off, please turn it on.", path); | |
401 | if (r < 0) | |
402 | return r; | |
403 | ||
404 | if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, seq) < 0) | |
405 | return log_debug_errno(errno, "Failed to allocated SOCK_SEQPACKET socket: %m"); | |
406 | ||
407 | r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE|FORK_LOG, &child); | |
408 | if (r < 0) | |
409 | return r; | |
410 | if (r == 0) { | |
411 | seq[0] = safe_close(seq[0]); | |
412 | ||
413 | r = dissected_image_mount(m, tmpdir, UID_INVALID, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_VALIDATE_OS); | |
414 | if (r < 0) { | |
415 | log_debug_errno(r, "Failed to mount dissected image: %m"); | |
416 | goto child_finish; | |
417 | } | |
418 | ||
419 | r = extract_now(tmpdir, matches, seq[1], NULL, NULL); | |
420 | ||
421 | child_finish: | |
422 | _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); | |
423 | } | |
424 | ||
425 | seq[1] = safe_close(seq[1]); | |
426 | ||
67818055 | 427 | unit_files = hashmap_new(&portable_metadata_hash_ops); |
61d0578b LP |
428 | if (!unit_files) |
429 | return -ENOMEM; | |
430 | ||
431 | for (;;) { | |
432 | _cleanup_(portable_metadata_unrefp) PortableMetadata *add = NULL; | |
433 | _cleanup_free_ char *name = NULL; | |
434 | _cleanup_close_ int fd = -1; | |
435 | ||
436 | r = recv_item(seq[0], &name, &fd); | |
437 | if (r < 0) | |
438 | return log_debug_errno(r, "Failed to receive item: %m"); | |
439 | ||
5238e957 | 440 | /* We can't really distinguish a zero-length datagram without any fds from EOF (both are signalled the |
61d0578b LP |
441 | * same way by recvmsg()). Hence, accept either as end notification. */ |
442 | if (isempty(name) && fd < 0) | |
443 | break; | |
444 | ||
445 | if (isempty(name) || fd < 0) { | |
446 | log_debug("Invalid item sent from child."); | |
447 | return -EINVAL; | |
448 | } | |
449 | ||
450 | add = portable_metadata_new(name, fd); | |
451 | if (!add) | |
452 | return -ENOMEM; | |
453 | fd = -1; | |
454 | ||
455 | /* Note that we do not initialize 'add->source' here, as the source path is not usable here as | |
456 | * it refers to a path only valid in the short-living namespaced child process we forked | |
457 | * here. */ | |
458 | ||
459 | if (PORTABLE_METADATA_IS_UNIT(add)) { | |
460 | r = hashmap_put(unit_files, add->name, add); | |
461 | if (r < 0) | |
462 | return log_debug_errno(r, "Failed to add item to unit file list: %m"); | |
463 | ||
464 | add = NULL; | |
465 | ||
466 | } else if (PORTABLE_METADATA_IS_OS_RELEASE(add)) { | |
467 | ||
468 | assert(!os_release); | |
469 | os_release = TAKE_PTR(add); | |
470 | } else | |
471 | assert_not_reached("Unexpected metadata item from child."); | |
472 | } | |
473 | ||
474 | r = wait_for_terminate_and_check("(sd-dissect)", child, 0); | |
475 | if (r < 0) | |
476 | return r; | |
477 | child = 0; | |
478 | } | |
479 | ||
480 | if (!os_release) | |
481 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image '%s' lacks os-release data, refusing.", path); | |
482 | ||
483 | if (hashmap_isempty(unit_files)) | |
484 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Couldn't find any matching unit files in image '%s', refusing.", path); | |
485 | ||
486 | if (ret_unit_files) | |
487 | *ret_unit_files = TAKE_PTR(unit_files); | |
488 | ||
489 | if (ret_os_release) | |
490 | *ret_os_release = TAKE_PTR(os_release); | |
491 | ||
492 | return 0; | |
493 | } | |
494 | ||
495 | int portable_extract( | |
496 | const char *name_or_path, | |
497 | char **matches, | |
498 | PortableMetadata **ret_os_release, | |
499 | Hashmap **ret_unit_files, | |
500 | sd_bus_error *error) { | |
501 | ||
502 | _cleanup_(image_unrefp) Image *image = NULL; | |
503 | int r; | |
504 | ||
505 | assert(name_or_path); | |
506 | ||
507 | r = image_find_harder(IMAGE_PORTABLE, name_or_path, &image); | |
508 | if (r < 0) | |
509 | return r; | |
510 | ||
511 | return portable_extract_by_path(image->path, matches, ret_os_release, ret_unit_files, error); | |
512 | } | |
513 | ||
514 | static int unit_file_is_active( | |
515 | sd_bus *bus, | |
516 | const char *name, | |
517 | sd_bus_error *error) { | |
518 | ||
519 | static const char *const active_states[] = { | |
520 | "activating", | |
521 | "active", | |
522 | "reloading", | |
523 | "deactivating", | |
524 | NULL, | |
525 | }; | |
526 | int r; | |
527 | ||
528 | if (!bus) | |
529 | return false; | |
530 | ||
531 | /* If we are looking at a plain or instance things are easy, we can just query the state */ | |
532 | if (unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) { | |
533 | _cleanup_free_ char *path = NULL, *buf = NULL; | |
534 | ||
535 | path = unit_dbus_path_from_name(name); | |
536 | if (!path) | |
537 | return -ENOMEM; | |
538 | ||
539 | r = sd_bus_get_property_string( | |
540 | bus, | |
541 | "org.freedesktop.systemd1", | |
542 | path, | |
543 | "org.freedesktop.systemd1.Unit", | |
544 | "ActiveState", | |
545 | error, | |
546 | &buf); | |
547 | if (r < 0) | |
548 | return log_debug_errno(r, "Failed to retrieve unit state: %s", bus_error_message(error, r)); | |
549 | ||
550 | return strv_contains((char**) active_states, buf); | |
551 | } | |
552 | ||
553 | /* Otherwise we need to enumerate. But let's build the most restricted query we can */ | |
554 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { | |
555 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
556 | const char *at, *prefix, *joined; | |
557 | ||
558 | r = sd_bus_message_new_method_call( | |
559 | bus, | |
560 | &m, | |
561 | "org.freedesktop.systemd1", | |
562 | "/org/freedesktop/systemd1", | |
563 | "org.freedesktop.systemd1.Manager", | |
564 | "ListUnitsByPatterns"); | |
565 | if (r < 0) | |
566 | return r; | |
567 | ||
568 | r = sd_bus_message_append_strv(m, (char**) active_states); | |
569 | if (r < 0) | |
570 | return r; | |
571 | ||
572 | at = strchr(name, '@'); | |
573 | assert(at); | |
574 | ||
575 | prefix = strndupa(name, at + 1 - name); | |
576 | joined = strjoina(prefix, "*", at + 1); | |
577 | ||
578 | r = sd_bus_message_append_strv(m, STRV_MAKE(joined)); | |
579 | if (r < 0) | |
580 | return r; | |
581 | ||
582 | r = sd_bus_call(bus, m, 0, error, &reply); | |
583 | if (r < 0) | |
584 | return log_debug_errno(r, "Failed to list units: %s", bus_error_message(error, r)); | |
585 | ||
586 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); | |
587 | if (r < 0) | |
588 | return r; | |
589 | ||
590 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_STRUCT, "ssssssouso"); | |
591 | if (r < 0) | |
592 | return r; | |
593 | ||
594 | return r > 0; | |
595 | } | |
596 | ||
597 | return -EINVAL; | |
598 | } | |
599 | ||
600 | static int portable_changes_add( | |
601 | PortableChange **changes, | |
602 | size_t *n_changes, | |
603 | PortableChangeType type, | |
604 | const char *path, | |
605 | const char *source) { | |
606 | ||
607 | _cleanup_free_ char *p = NULL, *s = NULL; | |
608 | PortableChange *c; | |
609 | ||
610 | assert(path); | |
611 | assert(!changes == !n_changes); | |
612 | ||
613 | if (!changes) | |
614 | return 0; | |
615 | ||
616 | c = reallocarray(*changes, *n_changes + 1, sizeof(PortableChange)); | |
617 | if (!c) | |
618 | return -ENOMEM; | |
619 | *changes = c; | |
620 | ||
621 | p = strdup(path); | |
622 | if (!p) | |
623 | return -ENOMEM; | |
624 | ||
858d36c1 | 625 | path_simplify(p, false); |
61d0578b LP |
626 | |
627 | if (source) { | |
628 | s = strdup(source); | |
629 | if (!s) | |
630 | return -ENOMEM; | |
631 | ||
858d36c1 | 632 | path_simplify(s, false); |
61d0578b LP |
633 | } |
634 | ||
635 | c[(*n_changes)++] = (PortableChange) { | |
636 | .type = type, | |
637 | .path = TAKE_PTR(p), | |
638 | .source = TAKE_PTR(s), | |
639 | }; | |
640 | ||
641 | return 0; | |
642 | } | |
643 | ||
644 | static int portable_changes_add_with_prefix( | |
645 | PortableChange **changes, | |
646 | size_t *n_changes, | |
647 | PortableChangeType type, | |
648 | const char *prefix, | |
649 | const char *path, | |
650 | const char *source) { | |
651 | ||
652 | assert(path); | |
653 | assert(!changes == !n_changes); | |
654 | ||
655 | if (!changes) | |
656 | return 0; | |
657 | ||
658 | if (prefix) { | |
270384b2 | 659 | path = prefix_roota(prefix, path); |
61d0578b LP |
660 | |
661 | if (source) | |
270384b2 | 662 | source = prefix_roota(prefix, source); |
61d0578b LP |
663 | } |
664 | ||
665 | return portable_changes_add(changes, n_changes, type, path, source); | |
666 | } | |
667 | ||
668 | void portable_changes_free(PortableChange *changes, size_t n_changes) { | |
669 | size_t i; | |
670 | ||
671 | assert(changes || n_changes == 0); | |
672 | ||
673 | for (i = 0; i < n_changes; i++) { | |
674 | free(changes[i].path); | |
675 | free(changes[i].source); | |
676 | } | |
677 | ||
678 | free(changes); | |
679 | } | |
680 | ||
681 | static int install_chroot_dropin( | |
682 | const char *image_path, | |
683 | ImageType type, | |
684 | const PortableMetadata *m, | |
685 | const char *dropin_dir, | |
686 | char **ret_dropin, | |
687 | PortableChange **changes, | |
688 | size_t *n_changes) { | |
689 | ||
690 | _cleanup_free_ char *text = NULL, *dropin = NULL; | |
691 | int r; | |
692 | ||
693 | assert(image_path); | |
694 | assert(m); | |
695 | assert(dropin_dir); | |
696 | ||
657ee2d8 | 697 | dropin = path_join(dropin_dir, "20-portable.conf"); |
61d0578b LP |
698 | if (!dropin) |
699 | return -ENOMEM; | |
700 | ||
701 | text = strjoin(PORTABLE_DROPIN_MARKER_BEGIN, image_path, PORTABLE_DROPIN_MARKER_END "\n"); | |
702 | if (!text) | |
703 | return -ENOMEM; | |
704 | ||
705 | if (endswith(m->name, ".service")) | |
706 | if (!strextend(&text, | |
707 | "\n" | |
708 | "[Service]\n", | |
709 | IN_SET(type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME) ? "RootDirectory=" : "RootImage=", image_path, "\n" | |
710 | "Environment=PORTABLE=", basename(image_path), "\n" | |
711 | "LogExtraFields=PORTABLE=", basename(image_path), "\n", | |
712 | NULL)) | |
713 | ||
714 | return -ENOMEM; | |
715 | ||
716 | r = write_string_file(dropin, text, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC); | |
717 | if (r < 0) | |
718 | return log_debug_errno(r, "Failed to write '%s': %m", dropin); | |
719 | ||
720 | (void) portable_changes_add(changes, n_changes, PORTABLE_WRITE, dropin, NULL); | |
721 | ||
722 | if (ret_dropin) | |
723 | *ret_dropin = TAKE_PTR(dropin); | |
724 | ||
725 | return 0; | |
726 | } | |
727 | ||
728 | static int find_profile(const char *name, const char *unit, char **ret) { | |
729 | const char *p, *dot; | |
730 | ||
731 | assert(name); | |
732 | assert(ret); | |
733 | ||
734 | assert_se(dot = strrchr(unit, '.')); | |
735 | ||
736 | NULSTR_FOREACH(p, profile_dirs) { | |
737 | _cleanup_free_ char *joined; | |
738 | ||
739 | joined = strjoin(p, "/", name, "/", dot + 1, ".conf"); | |
740 | if (!joined) | |
741 | return -ENOMEM; | |
742 | ||
743 | if (laccess(joined, F_OK) >= 0) { | |
744 | *ret = TAKE_PTR(joined); | |
745 | return 0; | |
746 | } | |
747 | ||
748 | if (errno != ENOENT) | |
749 | return -errno; | |
750 | } | |
751 | ||
752 | return -ENOENT; | |
753 | } | |
754 | ||
755 | static int install_profile_dropin( | |
756 | const char *image_path, | |
757 | const PortableMetadata *m, | |
758 | const char *dropin_dir, | |
759 | const char *profile, | |
760 | PortableFlags flags, | |
761 | char **ret_dropin, | |
762 | PortableChange **changes, | |
763 | size_t *n_changes) { | |
764 | ||
765 | _cleanup_free_ char *dropin = NULL, *from = NULL; | |
766 | int r; | |
767 | ||
768 | assert(image_path); | |
769 | assert(m); | |
770 | assert(dropin_dir); | |
771 | ||
772 | if (!profile) | |
773 | return 0; | |
774 | ||
775 | r = find_profile(profile, m->name, &from); | |
776 | if (r < 0) { | |
99c89da0 | 777 | if (r != -ENOENT) |
61d0578b LP |
778 | return log_debug_errno(errno, "Profile '%s' is not accessible: %m", profile); |
779 | ||
780 | log_debug_errno(errno, "Skipping link to profile '%s', as it does not exist: %m", profile); | |
781 | return 0; | |
782 | } | |
783 | ||
657ee2d8 | 784 | dropin = path_join(dropin_dir, "10-profile.conf"); |
61d0578b LP |
785 | if (!dropin) |
786 | return -ENOMEM; | |
787 | ||
788 | if (flags & PORTABLE_PREFER_COPY) { | |
789 | ||
8a016c74 | 790 | r = copy_file_atomic(from, dropin, 0644, 0, 0, COPY_REFLINK); |
61d0578b | 791 | if (r < 0) |
9a6f746f | 792 | return log_debug_errno(r, "Failed to copy %s %s %s: %m", from, special_glyph(SPECIAL_GLYPH_ARROW), dropin); |
61d0578b LP |
793 | |
794 | (void) portable_changes_add(changes, n_changes, PORTABLE_COPY, dropin, from); | |
795 | ||
796 | } else { | |
797 | ||
798 | if (symlink(from, dropin) < 0) | |
9a6f746f | 799 | return log_debug_errno(errno, "Failed to link %s %s %s: %m", from, special_glyph(SPECIAL_GLYPH_ARROW), dropin); |
61d0578b LP |
800 | |
801 | (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, dropin, from); | |
802 | } | |
803 | ||
804 | if (ret_dropin) | |
805 | *ret_dropin = TAKE_PTR(dropin); | |
806 | ||
807 | return 0; | |
808 | } | |
809 | ||
40a7b232 | 810 | static const char *attached_path(const LookupPaths *paths, PortableFlags flags) { |
61d0578b LP |
811 | const char *where; |
812 | ||
813 | assert(paths); | |
814 | ||
815 | if (flags & PORTABLE_RUNTIME) | |
40a7b232 | 816 | where = paths->runtime_attached; |
61d0578b | 817 | else |
40a7b232 | 818 | where = paths->persistent_attached; |
61d0578b LP |
819 | |
820 | assert(where); | |
821 | return where; | |
822 | } | |
823 | ||
824 | static int attach_unit_file( | |
825 | const LookupPaths *paths, | |
826 | const char *image_path, | |
827 | ImageType type, | |
828 | const PortableMetadata *m, | |
829 | const char *profile, | |
830 | PortableFlags flags, | |
831 | PortableChange **changes, | |
832 | size_t *n_changes) { | |
833 | ||
834 | _cleanup_(unlink_and_freep) char *chroot_dropin = NULL, *profile_dropin = NULL; | |
835 | _cleanup_(rmdir_and_freep) char *dropin_dir = NULL; | |
836 | const char *where, *path; | |
837 | int r; | |
838 | ||
839 | assert(paths); | |
840 | assert(image_path); | |
841 | assert(m); | |
842 | assert(PORTABLE_METADATA_IS_UNIT(m)); | |
843 | ||
40a7b232 | 844 | where = attached_path(paths, flags); |
61d0578b | 845 | |
d09d85a2 LP |
846 | (void) mkdir_parents(where, 0755); |
847 | if (mkdir(where, 0755) < 0) { | |
848 | if (errno != EEXIST) | |
849 | return -errno; | |
850 | } else | |
851 | (void) portable_changes_add(changes, n_changes, PORTABLE_MKDIR, where, NULL); | |
852 | ||
270384b2 | 853 | path = prefix_roota(where, m->name); |
61d0578b LP |
854 | dropin_dir = strjoin(path, ".d"); |
855 | if (!dropin_dir) | |
856 | return -ENOMEM; | |
857 | ||
d09d85a2 LP |
858 | if (mkdir(dropin_dir, 0755) < 0) { |
859 | if (errno != EEXIST) | |
860 | return -errno; | |
861 | } else | |
862 | (void) portable_changes_add(changes, n_changes, PORTABLE_MKDIR, dropin_dir, NULL); | |
61d0578b LP |
863 | |
864 | /* We install the drop-ins first, and the actual unit file last to achieve somewhat atomic behaviour if PID 1 | |
865 | * is reloaded while we are creating things here: as long as only the drop-ins exist the unit doesn't exist at | |
866 | * all for PID 1. */ | |
867 | ||
868 | r = install_chroot_dropin(image_path, type, m, dropin_dir, &chroot_dropin, changes, n_changes); | |
869 | if (r < 0) | |
870 | return r; | |
871 | ||
872 | r = install_profile_dropin(image_path, m, dropin_dir, profile, flags, &profile_dropin, changes, n_changes); | |
873 | if (r < 0) | |
874 | return r; | |
875 | ||
876 | if ((flags & PORTABLE_PREFER_SYMLINK) && m->source) { | |
877 | ||
878 | if (symlink(m->source, path) < 0) | |
879 | return log_debug_errno(errno, "Failed to symlink unit file '%s': %m", path); | |
880 | ||
881 | (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, path, m->source); | |
882 | ||
883 | } else { | |
884 | _cleanup_(unlink_and_freep) char *tmp = NULL; | |
885 | _cleanup_close_ int fd = -1; | |
886 | ||
887 | fd = open_tmpfile_linkable(where, O_WRONLY|O_CLOEXEC, &tmp); | |
888 | if (fd < 0) | |
889 | return log_debug_errno(fd, "Failed to create unit file '%s': %m", path); | |
890 | ||
891 | r = copy_bytes(m->fd, fd, UINT64_MAX, COPY_REFLINK); | |
892 | if (r < 0) | |
893 | return log_debug_errno(r, "Failed to copy unit file '%s': %m", path); | |
894 | ||
895 | if (fchmod(fd, 0644) < 0) | |
896 | return log_debug_errno(errno, "Failed to change unit file access mode for '%s': %m", path); | |
897 | ||
898 | r = link_tmpfile(fd, tmp, path); | |
899 | if (r < 0) | |
900 | return log_debug_errno(r, "Failed to install unit file '%s': %m", path); | |
901 | ||
902 | tmp = mfree(tmp); | |
903 | ||
904 | (void) portable_changes_add(changes, n_changes, PORTABLE_COPY, path, m->source); | |
905 | } | |
906 | ||
907 | /* All is established now, now let's disable any rollbacks */ | |
908 | chroot_dropin = mfree(chroot_dropin); | |
909 | profile_dropin = mfree(profile_dropin); | |
910 | dropin_dir = mfree(dropin_dir); | |
911 | ||
912 | return 0; | |
913 | } | |
914 | ||
915 | static int image_symlink( | |
916 | const char *image_path, | |
917 | PortableFlags flags, | |
918 | char **ret) { | |
919 | ||
920 | const char *fn, *where; | |
921 | char *joined = NULL; | |
922 | ||
923 | assert(image_path); | |
924 | assert(ret); | |
925 | ||
926 | fn = last_path_component(image_path); | |
927 | ||
928 | if (flags & PORTABLE_RUNTIME) | |
929 | where = "/run/portables/"; | |
930 | else | |
931 | where = "/etc/portables/"; | |
932 | ||
933 | joined = strjoin(where, fn); | |
934 | if (!joined) | |
935 | return -ENOMEM; | |
936 | ||
937 | *ret = joined; | |
938 | return 0; | |
939 | } | |
940 | ||
941 | static int install_image_symlink( | |
942 | const char *image_path, | |
943 | PortableFlags flags, | |
944 | PortableChange **changes, | |
945 | size_t *n_changes) { | |
946 | ||
947 | _cleanup_free_ char *sl = NULL; | |
948 | int r; | |
949 | ||
950 | assert(image_path); | |
951 | ||
952 | /* If the image is outside of the image search also link it into it, so that it can be found with short image | |
953 | * names and is listed among the images. */ | |
954 | ||
955 | if (image_in_search_path(IMAGE_PORTABLE, image_path)) | |
956 | return 0; | |
957 | ||
958 | r = image_symlink(image_path, flags, &sl); | |
959 | if (r < 0) | |
960 | return log_debug_errno(r, "Failed to generate image symlink path: %m"); | |
961 | ||
962 | (void) mkdir_parents(sl, 0755); | |
963 | ||
964 | if (symlink(image_path, sl) < 0) | |
9a6f746f | 965 | return log_debug_errno(errno, "Failed to link %s %s %s: %m", image_path, special_glyph(SPECIAL_GLYPH_ARROW), sl); |
61d0578b LP |
966 | |
967 | (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, sl, image_path); | |
968 | return 0; | |
969 | } | |
970 | ||
971 | int portable_attach( | |
972 | sd_bus *bus, | |
973 | const char *name_or_path, | |
974 | char **matches, | |
975 | const char *profile, | |
976 | PortableFlags flags, | |
977 | PortableChange **changes, | |
978 | size_t *n_changes, | |
979 | sd_bus_error *error) { | |
980 | ||
67818055 | 981 | _cleanup_hashmap_free_ Hashmap *unit_files = NULL; |
61d0578b LP |
982 | _cleanup_(lookup_paths_free) LookupPaths paths = {}; |
983 | _cleanup_(image_unrefp) Image *image = NULL; | |
984 | PortableMetadata *item; | |
985 | Iterator iterator; | |
986 | int r; | |
987 | ||
988 | assert(name_or_path); | |
989 | ||
990 | r = image_find_harder(IMAGE_PORTABLE, name_or_path, &image); | |
991 | if (r < 0) | |
992 | return r; | |
993 | ||
994 | r = portable_extract_by_path(image->path, matches, NULL, &unit_files, error); | |
995 | if (r < 0) | |
996 | return r; | |
997 | ||
998 | r = lookup_paths_init(&paths, UNIT_FILE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, NULL); | |
999 | if (r < 0) | |
1000 | return r; | |
1001 | ||
1002 | HASHMAP_FOREACH(item, unit_files, iterator) { | |
1003 | r = unit_file_exists(UNIT_FILE_SYSTEM, &paths, item->name); | |
1004 | if (r < 0) | |
1005 | return sd_bus_error_set_errnof(error, r, "Failed to determine whether unit '%s' exists on the host: %m", item->name); | |
1006 | if (r > 0) | |
1007 | return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' exists on the host already, refusing.", item->name); | |
1008 | ||
1009 | r = unit_file_is_active(bus, item->name, error); | |
1010 | if (r < 0) | |
1011 | return r; | |
1012 | if (r > 0) | |
1013 | return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' is active already, refusing.", item->name); | |
1014 | } | |
1015 | ||
1016 | HASHMAP_FOREACH(item, unit_files, iterator) { | |
1017 | r = attach_unit_file(&paths, image->path, image->type, item, profile, flags, changes, n_changes); | |
1018 | if (r < 0) | |
1019 | return r; | |
1020 | } | |
1021 | ||
1022 | /* We don't care too much for the image symlink, it's just a convenience thing, it's not necessary for proper | |
1023 | * operation otherwise. */ | |
1024 | (void) install_image_symlink(image->path, flags, changes, n_changes); | |
1025 | ||
1026 | return 0; | |
1027 | } | |
1028 | ||
1029 | static bool marker_matches_image(const char *marker, const char *name_or_path) { | |
1030 | const char *a; | |
1031 | ||
1032 | assert(marker); | |
1033 | assert(name_or_path); | |
1034 | ||
1035 | a = last_path_component(marker); | |
1036 | ||
1037 | if (image_name_is_valid(name_or_path)) { | |
1038 | const char *e; | |
1039 | ||
1040 | /* We shall match against an image name. In that case let's compare the last component, and optionally | |
1041 | * allow either a suffix of ".raw" or a series of "/". */ | |
1042 | ||
1043 | e = startswith(a, name_or_path); | |
1044 | if (!e) | |
1045 | return false; | |
1046 | ||
1047 | return | |
1048 | e[strspn(e, "/")] == 0 || | |
1049 | streq(e, ".raw"); | |
1050 | } else { | |
1051 | const char *b; | |
1052 | size_t l; | |
1053 | ||
1054 | /* We shall match against a path. Let's ignore any prefix here though, as often there are many ways to | |
1055 | * reach the same file. However, in this mode, let's validate any file suffix. */ | |
1056 | ||
1057 | l = strcspn(a, "/"); | |
1058 | b = last_path_component(name_or_path); | |
1059 | ||
1060 | if (strcspn(b, "/") != l) | |
1061 | return false; | |
1062 | ||
1063 | return memcmp(a, b, l) == 0; | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | static int test_chroot_dropin( | |
1068 | DIR *d, | |
1069 | const char *where, | |
1070 | const char *fname, | |
1071 | const char *name_or_path, | |
1072 | char **ret_marker) { | |
1073 | ||
1074 | _cleanup_free_ char *line = NULL, *marker = NULL; | |
1075 | _cleanup_fclose_ FILE *f = NULL; | |
1076 | _cleanup_close_ int fd = -1; | |
1077 | const char *p, *e, *k; | |
1078 | int r; | |
1079 | ||
1080 | assert(d); | |
1081 | assert(where); | |
1082 | assert(fname); | |
1083 | ||
1084 | /* We recognize unis created from portable images via the drop-in we created for them */ | |
1085 | ||
1086 | p = strjoina(fname, ".d/20-portable.conf"); | |
1087 | fd = openat(dirfd(d), p, O_RDONLY|O_CLOEXEC); | |
1088 | if (fd < 0) { | |
1089 | if (errno == ENOENT) | |
1090 | return 0; | |
1091 | ||
1092 | return log_debug_errno(errno, "Failed to open %s/%s: %m", where, p); | |
1093 | } | |
1094 | ||
4fa744a3 | 1095 | r = take_fdopen_unlocked(&fd, "r", &f); |
02e23d1a ZJS |
1096 | if (r < 0) |
1097 | return log_debug_errno(r, "Failed to convert file handle: %m"); | |
61d0578b LP |
1098 | |
1099 | r = read_line(f, LONG_LINE_MAX, &line); | |
1100 | if (r < 0) | |
1101 | return log_debug_errno(r, "Failed to read from %s/%s: %m", where, p); | |
1102 | ||
1103 | e = startswith(line, PORTABLE_DROPIN_MARKER_BEGIN); | |
1104 | if (!e) | |
1105 | return 0; | |
1106 | ||
1107 | k = endswith(e, PORTABLE_DROPIN_MARKER_END); | |
1108 | if (!k) | |
1109 | return 0; | |
1110 | ||
1111 | marker = strndup(e, k - e); | |
1112 | if (!marker) | |
1113 | return -ENOMEM; | |
1114 | ||
1115 | if (!name_or_path) | |
1116 | r = true; | |
1117 | else | |
1118 | r = marker_matches_image(marker, name_or_path); | |
1119 | ||
1120 | if (ret_marker) | |
1121 | *ret_marker = TAKE_PTR(marker); | |
1122 | ||
1123 | return r; | |
1124 | } | |
1125 | ||
1126 | int portable_detach( | |
1127 | sd_bus *bus, | |
1128 | const char *name_or_path, | |
1129 | PortableFlags flags, | |
1130 | PortableChange **changes, | |
1131 | size_t *n_changes, | |
1132 | sd_bus_error *error) { | |
1133 | ||
1134 | _cleanup_(lookup_paths_free) LookupPaths paths = {}; | |
1135 | _cleanup_set_free_free_ Set *unit_files = NULL, *markers = NULL; | |
1136 | _cleanup_closedir_ DIR *d = NULL; | |
1137 | const char *where, *item; | |
1138 | Iterator iterator; | |
1139 | struct dirent *de; | |
1140 | int ret = 0; | |
1141 | int r; | |
1142 | ||
1143 | assert(name_or_path); | |
1144 | ||
1145 | r = lookup_paths_init(&paths, UNIT_FILE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, NULL); | |
1146 | if (r < 0) | |
1147 | return r; | |
1148 | ||
40a7b232 | 1149 | where = attached_path(&paths, flags); |
61d0578b LP |
1150 | |
1151 | d = opendir(where); | |
339731db LP |
1152 | if (!d) { |
1153 | if (errno == ENOENT) | |
1154 | goto not_found; | |
1155 | ||
61d0578b | 1156 | return log_debug_errno(errno, "Failed to open '%s' directory: %m", where); |
339731db | 1157 | } |
61d0578b LP |
1158 | |
1159 | unit_files = set_new(&string_hash_ops); | |
1160 | if (!unit_files) | |
1161 | return -ENOMEM; | |
1162 | ||
1163 | markers = set_new(&path_hash_ops); | |
1164 | if (!markers) | |
1165 | return -ENOMEM; | |
1166 | ||
1167 | FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to enumerate '%s' directory: %m", where)) { | |
1168 | _cleanup_free_ char *marker = NULL; | |
1169 | UnitFileState state; | |
1170 | ||
1171 | if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY)) | |
1172 | continue; | |
1173 | ||
1174 | /* Filter out duplicates */ | |
1175 | if (set_get(unit_files, de->d_name)) | |
1176 | continue; | |
1177 | ||
1178 | dirent_ensure_type(d, de); | |
1179 | if (!IN_SET(de->d_type, DT_LNK, DT_REG)) | |
1180 | continue; | |
1181 | ||
1182 | r = test_chroot_dropin(d, where, de->d_name, name_or_path, &marker); | |
1183 | if (r < 0) | |
1184 | return r; | |
1185 | if (r == 0) | |
1186 | continue; | |
1187 | ||
1188 | r = unit_file_lookup_state(UNIT_FILE_SYSTEM, &paths, de->d_name, &state); | |
1189 | if (r < 0) | |
1190 | return log_debug_errno(r, "Failed to determine unit file state of '%s': %m", de->d_name); | |
c3d809ef | 1191 | if (!IN_SET(state, UNIT_FILE_STATIC, UNIT_FILE_DISABLED, UNIT_FILE_LINKED, UNIT_FILE_RUNTIME, UNIT_FILE_LINKED_RUNTIME)) |
61d0578b LP |
1192 | return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' is in state '%s', can't detach.", de->d_name, unit_file_state_to_string(state)); |
1193 | ||
1194 | r = unit_file_is_active(bus, de->d_name, error); | |
1195 | if (r < 0) | |
1196 | return r; | |
1197 | if (r > 0) | |
1198 | return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' is active, can't detach.", de->d_name); | |
1199 | ||
1200 | r = set_put_strdup(unit_files, de->d_name); | |
1201 | if (r < 0) | |
1202 | return log_debug_errno(r, "Failed to add unit name '%s' to set: %m", de->d_name); | |
1203 | ||
1204 | if (path_is_absolute(marker) && | |
1205 | !image_in_search_path(IMAGE_PORTABLE, marker)) { | |
1206 | ||
1207 | r = set_ensure_allocated(&markers, &path_hash_ops); | |
1208 | if (r < 0) | |
1209 | return r; | |
1210 | ||
1211 | r = set_put(markers, marker); | |
1212 | if (r >= 0) | |
1213 | marker = NULL; | |
1214 | else if (r != -EEXIST) | |
1215 | return r; | |
1216 | } | |
1217 | } | |
1218 | ||
339731db LP |
1219 | if (set_isempty(unit_files)) |
1220 | goto not_found; | |
61d0578b LP |
1221 | |
1222 | SET_FOREACH(item, unit_files, iterator) { | |
1223 | _cleanup_free_ char *md = NULL; | |
1224 | const char *suffix; | |
1225 | ||
1226 | if (unlinkat(dirfd(d), item, 0) < 0) { | |
1227 | log_debug_errno(errno, "Can't remove unit file %s/%s: %m", where, item); | |
1228 | ||
1229 | if (errno != ENOENT && ret >= 0) | |
1230 | ret = -errno; | |
1231 | } else | |
1232 | portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, item, NULL); | |
1233 | ||
1234 | FOREACH_STRING(suffix, ".d/10-profile.conf", ".d/20-portable.conf") { | |
1235 | _cleanup_free_ char *dropin = NULL; | |
1236 | ||
1237 | dropin = strjoin(item, suffix); | |
1238 | if (!dropin) | |
1239 | return -ENOMEM; | |
1240 | ||
1241 | if (unlinkat(dirfd(d), dropin, 0) < 0) { | |
1242 | log_debug_errno(errno, "Can't remove drop-in %s/%s: %m", where, dropin); | |
1243 | ||
1244 | if (errno != ENOENT && ret >= 0) | |
1245 | ret = -errno; | |
1246 | } else | |
1247 | portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, dropin, NULL); | |
1248 | } | |
1249 | ||
1250 | md = strjoin(item, ".d"); | |
1251 | if (!md) | |
1252 | return -ENOMEM; | |
1253 | ||
1254 | if (unlinkat(dirfd(d), md, AT_REMOVEDIR) < 0) { | |
1255 | log_debug_errno(errno, "Can't remove drop-in directory %s/%s: %m", where, md); | |
1256 | ||
1257 | if (errno != ENOENT && ret >= 0) | |
1258 | ret = -errno; | |
1259 | } else | |
1260 | portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, md, NULL); | |
1261 | } | |
1262 | ||
1263 | /* Now, also drop any image symlink, for images outside of the sarch path */ | |
1264 | SET_FOREACH(item, markers, iterator) { | |
1265 | _cleanup_free_ char *sl = NULL; | |
1266 | struct stat st; | |
1267 | ||
1268 | r = image_symlink(item, flags, &sl); | |
1269 | if (r < 0) { | |
1270 | log_debug_errno(r, "Failed to determine image symlink for '%s', ignoring: %m", item); | |
1271 | continue; | |
1272 | } | |
1273 | ||
1274 | if (lstat(sl, &st) < 0) { | |
1275 | log_debug_errno(errno, "Failed to stat '%s', ignoring: %m", sl); | |
1276 | continue; | |
1277 | } | |
1278 | ||
1279 | if (!S_ISLNK(st.st_mode)) { | |
1280 | log_debug("Image '%s' is not a symlink, ignoring.", sl); | |
1281 | continue; | |
1282 | } | |
1283 | ||
1284 | if (unlink(sl) < 0) { | |
1285 | log_debug_errno(errno, "Can't remove image symlink '%s': %m", sl); | |
1286 | ||
1287 | if (errno != ENOENT && ret >= 0) | |
1288 | ret = -errno; | |
1289 | } else | |
1290 | portable_changes_add(changes, n_changes, PORTABLE_UNLINK, sl, NULL); | |
1291 | } | |
1292 | ||
d09d85a2 LP |
1293 | /* Try to remove the unit file directory, if we can */ |
1294 | if (rmdir(where) >= 0) | |
1295 | portable_changes_add(changes, n_changes, PORTABLE_UNLINK, where, NULL); | |
1296 | ||
61d0578b | 1297 | return ret; |
339731db LP |
1298 | |
1299 | not_found: | |
1300 | log_debug("No unit files associated with '%s' found. Image not attached?", name_or_path); | |
1301 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "No unit files associated with '%s' found. Image not attached?", name_or_path); | |
61d0578b LP |
1302 | } |
1303 | ||
1304 | static int portable_get_state_internal( | |
1305 | sd_bus *bus, | |
1306 | const char *name_or_path, | |
1307 | PortableFlags flags, | |
1308 | PortableState *ret, | |
1309 | sd_bus_error *error) { | |
1310 | ||
1311 | _cleanup_(lookup_paths_free) LookupPaths paths = {}; | |
1312 | bool found_enabled = false, found_running = false; | |
1313 | _cleanup_set_free_free_ Set *unit_files = NULL; | |
1314 | _cleanup_closedir_ DIR *d = NULL; | |
1315 | const char *where; | |
1316 | struct dirent *de; | |
1317 | int r; | |
1318 | ||
1319 | assert(name_or_path); | |
1320 | assert(ret); | |
1321 | ||
1322 | r = lookup_paths_init(&paths, UNIT_FILE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, NULL); | |
1323 | if (r < 0) | |
1324 | return r; | |
1325 | ||
40a7b232 | 1326 | where = attached_path(&paths, flags); |
61d0578b LP |
1327 | |
1328 | d = opendir(where); | |
339731db LP |
1329 | if (!d) { |
1330 | if (errno == ENOENT) { | |
1331 | /* If the 'attached' directory doesn't exist at all, then we know for sure this image isn't attached. */ | |
1332 | *ret = PORTABLE_DETACHED; | |
1333 | return 0; | |
1334 | } | |
1335 | ||
61d0578b | 1336 | return log_debug_errno(errno, "Failed to open '%s' directory: %m", where); |
339731db | 1337 | } |
61d0578b LP |
1338 | |
1339 | unit_files = set_new(&string_hash_ops); | |
1340 | if (!unit_files) | |
1341 | return -ENOMEM; | |
1342 | ||
1343 | FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to enumerate '%s' directory: %m", where)) { | |
1344 | UnitFileState state; | |
1345 | ||
1346 | if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY)) | |
1347 | continue; | |
1348 | ||
1349 | /* Filter out duplicates */ | |
1350 | if (set_get(unit_files, de->d_name)) | |
1351 | continue; | |
1352 | ||
1353 | dirent_ensure_type(d, de); | |
1354 | if (!IN_SET(de->d_type, DT_LNK, DT_REG)) | |
1355 | continue; | |
1356 | ||
1357 | r = test_chroot_dropin(d, where, de->d_name, name_or_path, NULL); | |
1358 | if (r < 0) | |
1359 | return r; | |
1360 | if (r == 0) | |
1361 | continue; | |
1362 | ||
1363 | r = unit_file_lookup_state(UNIT_FILE_SYSTEM, &paths, de->d_name, &state); | |
1364 | if (r < 0) | |
1365 | return log_debug_errno(r, "Failed to determine unit file state of '%s': %m", de->d_name); | |
1366 | if (!IN_SET(state, UNIT_FILE_STATIC, UNIT_FILE_DISABLED, UNIT_FILE_LINKED, UNIT_FILE_LINKED_RUNTIME)) | |
1367 | found_enabled = true; | |
1368 | ||
1369 | r = unit_file_is_active(bus, de->d_name, error); | |
1370 | if (r < 0) | |
1371 | return r; | |
1372 | if (r > 0) | |
1373 | found_running = true; | |
1374 | ||
1375 | r = set_put_strdup(unit_files, de->d_name); | |
1376 | if (r < 0) | |
1377 | return log_debug_errno(r, "Failed to add unit name '%s' to set: %m", de->d_name); | |
1378 | } | |
1379 | ||
1380 | *ret = found_running ? (!set_isempty(unit_files) && (flags & PORTABLE_RUNTIME) ? PORTABLE_RUNNING_RUNTIME : PORTABLE_RUNNING) : | |
1381 | found_enabled ? (flags & PORTABLE_RUNTIME ? PORTABLE_ENABLED_RUNTIME : PORTABLE_ENABLED) : | |
1382 | !set_isempty(unit_files) ? (flags & PORTABLE_RUNTIME ? PORTABLE_ATTACHED_RUNTIME : PORTABLE_ATTACHED) : PORTABLE_DETACHED; | |
1383 | ||
1384 | return 0; | |
1385 | } | |
1386 | ||
1387 | int portable_get_state( | |
1388 | sd_bus *bus, | |
1389 | const char *name_or_path, | |
1390 | PortableFlags flags, | |
1391 | PortableState *ret, | |
1392 | sd_bus_error *error) { | |
1393 | ||
1394 | PortableState state; | |
1395 | int r; | |
1396 | ||
1397 | assert(name_or_path); | |
1398 | assert(ret); | |
1399 | ||
1400 | /* We look for matching units twice: once in the regular directories, and once in the runtime directories — but | |
1401 | * the latter only if we didn't find anything in the former. */ | |
1402 | ||
1403 | r = portable_get_state_internal(bus, name_or_path, flags & ~PORTABLE_RUNTIME, &state, error); | |
1404 | if (r < 0) | |
1405 | return r; | |
1406 | ||
1407 | if (state == PORTABLE_DETACHED) { | |
1408 | r = portable_get_state_internal(bus, name_or_path, flags | PORTABLE_RUNTIME, &state, error); | |
1409 | if (r < 0) | |
1410 | return r; | |
1411 | } | |
1412 | ||
1413 | *ret = state; | |
1414 | return 0; | |
1415 | } | |
1416 | ||
1417 | int portable_get_profiles(char ***ret) { | |
1418 | assert(ret); | |
1419 | ||
1420 | return conf_files_list_nulstr(ret, NULL, NULL, CONF_FILES_DIRECTORY|CONF_FILES_BASENAME|CONF_FILES_FILTER_MASKED, profile_dirs); | |
1421 | } | |
1422 | ||
1423 | static const char* const portable_change_type_table[_PORTABLE_CHANGE_TYPE_MAX] = { | |
1424 | [PORTABLE_COPY] = "copy", | |
1425 | [PORTABLE_MKDIR] = "mkdir", | |
1426 | [PORTABLE_SYMLINK] = "symlink", | |
1427 | [PORTABLE_UNLINK] = "unlink", | |
1428 | [PORTABLE_WRITE] = "write", | |
1429 | }; | |
1430 | ||
1431 | DEFINE_STRING_TABLE_LOOKUP(portable_change_type, PortableChangeType); | |
1432 | ||
1433 | static const char* const portable_state_table[_PORTABLE_STATE_MAX] = { | |
1434 | [PORTABLE_DETACHED] = "detached", | |
1435 | [PORTABLE_ATTACHED] = "attached", | |
1436 | [PORTABLE_ATTACHED_RUNTIME] = "attached-runtime", | |
1437 | [PORTABLE_ENABLED] = "enabled", | |
1438 | [PORTABLE_ENABLED_RUNTIME] = "enabled-runtime", | |
1439 | [PORTABLE_RUNNING] = "running", | |
1440 | [PORTABLE_RUNNING_RUNTIME] = "running-runtime", | |
1441 | }; | |
1442 | ||
1443 | DEFINE_STRING_TABLE_LOOKUP(portable_state, PortableState); |