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