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