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