Debian Codegraph @

src/machine/image.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "sd-bus.h"
6
7 #include "discover-image.h"
8 #include "errno-util.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "hashmap.h"
12 #include "image.h"
13 #include "io-util.h"
14 #include "log.h"
15 #include "machined.h"
16 #include "operation.h"
17 #include "process-util.h"
18 #include "string-table.h"
19 #include "tmpfile-util.h"
20
21 int clean_pool_read_first_entry(FILE *file, int child_error, sd_bus_error *error) {
22 _cleanup_free_ char *name = NULL;
23 bool success;
24 int r;
25
26 assert(file);
27
28 if (fseek(file, 0, SEEK_SET) < 0)
29 return log_debug_errno(errno, "Failed to seek to the beginning of tmp file: %m");
30
31 /* The resulting temporary file starts with a boolean value that indicates success or not. */
32 errno = 0;
33 size_t n = fread(&success, 1, sizeof(success), file);
34 if (n != sizeof(success)) {
35 log_debug_errno(errno_or_else(EIO), "Received unexpected amount of %zu bytes: %m", n);
36 return child_error < 0 ? child_error : errno_or_else(EIO);
37 }
38
39 if (child_error < 0) {
40 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
41 * set to false followed by the name of the failed image. Let's try to read this and use it for the
42 * error message. If we can't read it, don't mind, and return the naked error. */
43
44 if (success) { /* The resulting temporary file could not be updated, ignore it. */
45 log_debug("Child process exited with an error. We expected a name of the image which caused the failure in the tmp file, but it failed as well.");
46 return child_error;
47 }
48
49 r = read_nul_string(file, LONG_LINE_MAX, &name);
50 if (r <= 0) { /* Same here... */
51 log_debug_errno(r, "Failed to read nul-terminated string from tmp file: %m");
52 return child_error;
53 }
54
55 log_debug_errno(child_error, "Failed to remove image '%s': %m", name);
56 return sd_bus_error_set_errnof(error, child_error, "Failed to remove image '%s': %m", name);
57 }
58
59 if (!success)
60 return log_debug_errno(SYNTHETIC_ERRNO(ESTALE), "Inconsistent state of the temporary file.");
61
62 return 0;
63 }
64
65 int clean_pool_read_next_entry(FILE *file, char **ret_name, uint64_t *ret_usage) {
66 /* Return value:
67 * r < 0: error
68 * r == 0 last record returned
69 * r > 0 more records expected */
70
71 _cleanup_free_ char *name = NULL;
72 uint64_t usage;
73 int r;
74
75 assert(file);
76
77 r = read_nul_string(file, LONG_LINE_MAX, &name);
78 if (r < 0)
79 return log_debug_errno(r, "Failed to read nul-terminated string: %m");
80 if (r == 0) /* reached the end */
81 return 0;
82
83 errno = 0;
84 size_t n = fread(&usage, 1, sizeof(usage), file);
85 if (n != sizeof(usage))
86 return log_debug_errno(errno_or_else(EIO), "Received unexpected amount of %zu bytes: %m", n);
87
88 if (ret_name)
89 *ret_name = TAKE_PTR(name);
90 if (ret_usage)
91 *ret_usage = usage;
92
93 return 1;
94 }
95
96 int image_clean_pool_operation(Manager *manager, ImageCleanPoolMode mode, Operation **ret_operation) {
97 _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
98 _cleanup_close_ int result_fd = -EBADF;
99 _cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL;
100 int r;
101
102 assert(manager);
103 assert(ret_operation);
104
105 if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
106 return log_debug_errno(errno, "Failed to create pipe: %m");
107
108 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
109 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
110 * continuously */
111 result_fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC);
112 if (result_fd < 0)
113 return log_debug_errno(result_fd, "Failed to open tmpfile: %m");
114
115 /* This might be a slow operation, run it asynchronously in a background process */
116 r = pidref_safe_fork("(sd-clean)", FORK_RESET_SIGNALS, &child);
117 if (r < 0)
118 return log_debug_errno(r, "Failed to fork(): %m");
119 if (r == 0) {
120 errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
121
122 _cleanup_hashmap_free_ Hashmap *images = NULL;
123 r = image_discover(manager->runtime_scope, IMAGE_MACHINE, /* root= */ NULL, &images);
124 if (r < 0) {
125 log_debug_errno(r, "Failed to discover images: %m");
126 report_errno_and_exit(errno_pipe_fd[1], r);
127 }
128
129 bool success = true;
130 ssize_t n = loop_write(result_fd, &success, sizeof(success));
131 if (n < 0) {
132 log_debug_errno(n, "Failed to write to tmp file: %m");
133 report_errno_and_exit(errno_pipe_fd[1], n);
134 }
135
136 Image *image;
137 HASHMAP_FOREACH(image, images) {
138 /* We can't remove vendor images (i.e. those in /usr) */
139 if (image_is_vendor(image))
140 continue;
141 if (image_is_host(image))
142 continue;
143 if (mode == IMAGE_CLEAN_POOL_REMOVE_HIDDEN && !image_is_hidden(image))
144 continue;
145
146 r = image_remove(image, manager->runtime_scope);
147 if (r == -EBUSY) {
148 log_debug("Keeping image '%s' because it's currently used.", image->name);
149 continue;
150 }
151 if (r < 0) {
152 log_debug_errno(r, "Failed to remove image '%s': %m", image->name);
153 success = false;
154
155 /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */
156 if (ftruncate(result_fd, 0) < 0 ||
157 lseek(result_fd, 0, SEEK_SET) < 0 ||
158 write(result_fd, &success, sizeof(success)) < 0 ||
159 write(result_fd, image->name, strlen(image->name)+1) < 0)
160 log_debug_errno(errno, "Failed to truncate, rewind, or write to tmp file, ignoring: %m");
161
162 /* Report original error code (not results of rewind/truncate/etc */
163 report_errno_and_exit(errno_pipe_fd[1], r);
164 }
165
166 n = loop_write(result_fd, image->name, strlen(image->name) + 1);
167 if (n < 0) {
168 log_debug_errno(n, "Failed to write image name to tmp file: %m");
169 report_errno_and_exit(errno_pipe_fd[1], n);
170 }
171
172 n = loop_write(result_fd, &image->usage_exclusive, sizeof(image->usage_exclusive));
173 if (n < 0) {
174 log_debug_errno(n, "Failed to write image's usage to tmp file: %m");
175 report_errno_and_exit(errno_pipe_fd[1], n);
176 }
177 }
178
179 result_fd = safe_close(result_fd);
180 _exit(EXIT_SUCCESS);
181 }
182
183 errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
184
185 /* The clean-up might take a while, hence install a watch on the child and return */
186 r = operation_new(manager, /* machine= */ NULL, &child, errno_pipe_fd[0], ret_operation);
187 if (r < 0)
188 return r;
189
190 (*ret_operation)->extra_fd = TAKE_FD(result_fd);
191 TAKE_FD(errno_pipe_fd[0]);
192 TAKE_PIDREF(child);
193 return 0;
194 }
195
196 static const char* const image_clean_pool_mode_table[_IMAGE_CLEAN_POOL_MAX] = {
197 [IMAGE_CLEAN_POOL_REMOVE_ALL] = "all",
198 [IMAGE_CLEAN_POOL_REMOVE_HIDDEN] = "hidden"
199 };
200
201 DEFINE_STRING_TABLE_LOOKUP(image_clean_pool_mode, ImageCleanPoolMode);