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