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