]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/import-tar.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / import / import-tar.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/fs.h>
4
5 #include "sd-daemon.h"
6 #include "sd-event.h"
7
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
10 #include "copy.h"
11 #include "fd-util.h"
12 #include "fileio.h"
13 #include "fs-util.h"
14 #include "hostname-util.h"
15 #include "import-common.h"
16 #include "import-compress.h"
17 #include "import-tar.h"
18 #include "install-file.h"
19 #include "io-util.h"
20 #include "machine-pool.h"
21 #include "mkdir-label.h"
22 #include "path-util.h"
23 #include "process-util.h"
24 #include "qcow2-util.h"
25 #include "ratelimit.h"
26 #include "rm-rf.h"
27 #include "string-util.h"
28 #include "tmpfile-util.h"
29
30 struct TarImport {
31 sd_event *event;
32
33 char *image_root;
34
35 TarImportFinished on_finished;
36 void *userdata;
37
38 char *local;
39 ImportFlags flags;
40
41 char *temp_path;
42 char *final_path;
43
44 int input_fd;
45 int tar_fd;
46
47 ImportCompress compress;
48
49 sd_event_source *input_event_source;
50
51 uint8_t buffer[16*1024];
52 size_t buffer_size;
53
54 uint64_t written_compressed;
55 uint64_t written_uncompressed;
56
57 struct stat input_stat;
58
59 pid_t tar_pid;
60
61 unsigned last_percent;
62 RateLimit progress_ratelimit;
63 };
64
65 TarImport* tar_import_unref(TarImport *i) {
66 if (!i)
67 return NULL;
68
69 sd_event_source_unref(i->input_event_source);
70
71 if (i->tar_pid > 1)
72 sigkill_wait(i->tar_pid);
73
74 rm_rf_subvolume_and_free(i->temp_path);
75
76 import_compress_free(&i->compress);
77
78 sd_event_unref(i->event);
79
80 safe_close(i->tar_fd);
81
82 free(i->final_path);
83 free(i->image_root);
84 free(i->local);
85 return mfree(i);
86 }
87
88 int tar_import_new(
89 TarImport **ret,
90 sd_event *event,
91 const char *image_root,
92 TarImportFinished on_finished,
93 void *userdata) {
94
95 _cleanup_(tar_import_unrefp) TarImport *i = NULL;
96 _cleanup_free_ char *root = NULL;
97 int r;
98
99 assert(ret);
100
101 root = strdup(image_root ?: "/var/lib/machines");
102 if (!root)
103 return -ENOMEM;
104
105 i = new(TarImport, 1);
106 if (!i)
107 return -ENOMEM;
108
109 *i = (TarImport) {
110 .input_fd = -EBADF,
111 .tar_fd = -EBADF,
112 .on_finished = on_finished,
113 .userdata = userdata,
114 .last_percent = UINT_MAX,
115 .image_root = TAKE_PTR(root),
116 .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 },
117 };
118
119 if (event)
120 i->event = sd_event_ref(event);
121 else {
122 r = sd_event_default(&i->event);
123 if (r < 0)
124 return r;
125 }
126
127 *ret = TAKE_PTR(i);
128
129 return 0;
130 }
131
132 static void tar_import_report_progress(TarImport *i) {
133 unsigned percent;
134 assert(i);
135
136 /* We have no size information, unless the source is a regular file */
137 if (!S_ISREG(i->input_stat.st_mode))
138 return;
139
140 if (i->written_compressed >= (uint64_t) i->input_stat.st_size)
141 percent = 100;
142 else
143 percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->input_stat.st_size);
144
145 if (percent == i->last_percent)
146 return;
147
148 if (!ratelimit_below(&i->progress_ratelimit))
149 return;
150
151 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
152 log_info("Imported %u%%.", percent);
153
154 i->last_percent = percent;
155 }
156
157 static int tar_import_finish(TarImport *i) {
158 const char *d;
159 int r;
160
161 assert(i);
162 assert(i->tar_fd >= 0);
163
164 i->tar_fd = safe_close(i->tar_fd);
165
166 if (i->tar_pid > 0) {
167 r = wait_for_terminate_and_check("tar", TAKE_PID(i->tar_pid), WAIT_LOG);
168 if (r < 0)
169 return r;
170 if (r != EXIT_SUCCESS)
171 return -EPROTO;
172 }
173
174 assert_se(d = i->temp_path ?: i->local);
175
176 r = import_mangle_os_tree(d);
177 if (r < 0)
178 return r;
179
180 r = install_file(
181 AT_FDCWD, d,
182 AT_FDCWD, i->final_path,
183 (i->flags & IMPORT_FORCE ? INSTALL_REPLACE : 0) |
184 (i->flags & IMPORT_READ_ONLY ? INSTALL_READ_ONLY : 0) |
185 (i->flags & IMPORT_SYNC ? INSTALL_SYNCFS : 0));
186 if (r < 0)
187 return log_error_errno(r, "Failed to move '%s' into place: %m", i->final_path ?: i->local);
188
189 i->temp_path = mfree(i->temp_path);
190
191 return 0;
192 }
193
194 static int tar_import_fork_tar(TarImport *i) {
195 const char *d, *root;
196 int r;
197
198 assert(i);
199 assert(i->local);
200 assert(!i->final_path);
201 assert(!i->temp_path);
202 assert(i->tar_fd < 0);
203
204 if (i->flags & IMPORT_DIRECT) {
205 d = i->local;
206 root = NULL;
207 } else {
208 i->final_path = path_join(i->image_root, i->local);
209 if (!i->final_path)
210 return log_oom();
211
212 r = tempfn_random(i->final_path, NULL, &i->temp_path);
213 if (r < 0)
214 return log_oom();
215
216 d = i->temp_path;
217 root = i->image_root;
218 }
219
220 assert(d);
221
222 (void) mkdir_parents_label(d, 0700);
223
224 if (FLAGS_SET(i->flags, IMPORT_DIRECT|IMPORT_FORCE))
225 (void) rm_rf(d, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
226
227 if (i->flags & IMPORT_BTRFS_SUBVOL)
228 r = btrfs_subvol_make_fallback(d, 0755);
229 else
230 r = RET_NERRNO(mkdir(d, 0755));
231 if (r == -EEXIST && (i->flags & IMPORT_DIRECT)) /* EEXIST is OK if in direct mode, but not otherwise,
232 * because in that case our temporary path collided */
233 r = 0;
234 if (r < 0)
235 return log_error_errno(r, "Failed to create directory/subvolume %s: %m", d);
236 if (r > 0 && (i->flags & IMPORT_BTRFS_QUOTA)) { /* actually btrfs subvol */
237 if (!(i->flags & IMPORT_DIRECT))
238 (void) import_assign_pool_quota_and_warn(root);
239 (void) import_assign_pool_quota_and_warn(d);
240 }
241
242 i->tar_fd = import_fork_tar_x(d, &i->tar_pid);
243 if (i->tar_fd < 0)
244 return i->tar_fd;
245
246 return 0;
247 }
248
249 static int tar_import_write(const void *p, size_t sz, void *userdata) {
250 TarImport *i = userdata;
251 int r;
252
253 r = loop_write(i->tar_fd, p, sz, false);
254 if (r < 0)
255 return r;
256
257 i->written_uncompressed += sz;
258
259 return 0;
260 }
261
262 static int tar_import_process(TarImport *i) {
263 ssize_t l;
264 int r;
265
266 assert(i);
267 assert(i->buffer_size < sizeof(i->buffer));
268
269 l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
270 if (l < 0) {
271 if (errno == EAGAIN)
272 return 0;
273
274 r = log_error_errno(errno, "Failed to read input file: %m");
275 goto finish;
276 }
277
278 i->buffer_size += l;
279
280 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
281
282 if (l == 0) { /* EOF */
283 log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed.");
284 import_uncompress_force_off(&i->compress);
285 } else {
286 r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
287 if (r < 0) {
288 log_error_errno(r, "Failed to detect file compression: %m");
289 goto finish;
290 }
291 if (r == 0) /* Need more data */
292 return 0;
293 }
294
295 r = tar_import_fork_tar(i);
296 if (r < 0)
297 goto finish;
298 }
299
300 r = import_uncompress(&i->compress, i->buffer, i->buffer_size, tar_import_write, i);
301 if (r < 0) {
302 log_error_errno(r, "Failed to decode and write: %m");
303 goto finish;
304 }
305
306 i->written_compressed += i->buffer_size;
307 i->buffer_size = 0;
308
309 if (l == 0) { /* EOF */
310 r = tar_import_finish(i);
311 goto finish;
312 }
313
314 tar_import_report_progress(i);
315
316 return 0;
317
318 finish:
319 if (i->on_finished)
320 i->on_finished(i, r, i->userdata);
321 else
322 sd_event_exit(i->event, r);
323
324 return 0;
325 }
326
327 static int tar_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
328 TarImport *i = userdata;
329
330 return tar_import_process(i);
331 }
332
333 static int tar_import_on_defer(sd_event_source *s, void *userdata) {
334 TarImport *i = userdata;
335
336 return tar_import_process(i);
337 }
338
339 int tar_import_start(TarImport *i, int fd, const char *local, ImportFlags flags) {
340 int r;
341
342 assert(i);
343 assert(fd >= 0);
344 assert(local);
345 assert(!(flags & ~IMPORT_FLAGS_MASK_TAR));
346
347 if (!import_validate_local(local, flags))
348 return -EINVAL;
349
350 if (i->input_fd >= 0)
351 return -EBUSY;
352
353 r = fd_nonblock(fd, true);
354 if (r < 0)
355 return r;
356
357 r = free_and_strdup(&i->local, local);
358 if (r < 0)
359 return r;
360
361 i->flags = flags;
362
363 if (fstat(fd, &i->input_stat) < 0)
364 return -errno;
365
366 r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, tar_import_on_input, i);
367 if (r == -EPERM) {
368 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
369 r = sd_event_add_defer(i->event, &i->input_event_source, tar_import_on_defer, i);
370 if (r < 0)
371 return r;
372
373 r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON);
374 }
375 if (r < 0)
376 return r;
377
378 i->input_fd = fd;
379 return 0;
380 }