| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/stat.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-daemon.h" |
| 7 |
#include "sd-event.h" |
| 8 |
#include "sd-varlink.h" |
| 9 |
|
| 10 |
#include "alloc-util.h" |
| 11 |
#include "btrfs-util.h" |
| 12 |
#include "compress.h" |
| 13 |
#include "dissect-image.h" |
| 14 |
#include "errno-util.h" |
| 15 |
#include "fd-util.h" |
| 16 |
#include "format-util.h" |
| 17 |
#include "import-common.h" |
| 18 |
#include "import-tar.h" |
| 19 |
#include "import-util.h" |
| 20 |
#include "install-file.h" |
| 21 |
#include "io-util.h" |
| 22 |
#include "log.h" |
| 23 |
#include "mkdir.h" |
| 24 |
#include "path-util.h" |
| 25 |
#include "pidref.h" |
| 26 |
#include "pretty-print.h" |
| 27 |
#include "process-util.h" |
| 28 |
#include "ratelimit.h" |
| 29 |
#include "rm-rf.h" |
| 30 |
#include "string-util.h" |
| 31 |
#include "terminal-util.h" |
| 32 |
#include "time-util.h" |
| 33 |
#include "tmpfile-util.h" |
| 34 |
|
| 35 |
typedef struct TarImport { |
| 36 |
sd_event *event; |
| 37 |
|
| 38 |
char *image_root; |
| 39 |
|
| 40 |
TarImportFinished on_finished; |
| 41 |
void *userdata; |
| 42 |
|
| 43 |
char *local; |
| 44 |
ImportFlags flags; |
| 45 |
|
| 46 |
char *temp_path; |
| 47 |
char *final_path; |
| 48 |
|
| 49 |
int input_fd; |
| 50 |
int tar_fd; |
| 51 |
int tree_fd; |
| 52 |
int userns_fd; |
| 53 |
|
| 54 |
Compressor *compress; |
| 55 |
|
| 56 |
sd_event_source *input_event_source; |
| 57 |
|
| 58 |
uint8_t buffer[COMPRESS_PIPE_BUFFER_SIZE]; |
| 59 |
size_t buffer_size; |
| 60 |
|
| 61 |
uint64_t written_compressed; |
| 62 |
uint64_t written_uncompressed; |
| 63 |
|
| 64 |
struct stat input_stat; |
| 65 |
|
| 66 |
PidRef tar_pid; |
| 67 |
|
| 68 |
unsigned last_percent; |
| 69 |
RateLimit progress_ratelimit; |
| 70 |
} TarImport; |
| 71 |
|
| 72 |
TarImport* tar_import_unref(TarImport *i) { |
| 73 |
if (!i) |
| 74 |
return NULL; |
| 75 |
|
| 76 |
sd_event_source_unref(i->input_event_source); |
| 77 |
|
| 78 |
pidref_done_sigkill_wait(&i->tar_pid); |
| 79 |
|
| 80 |
if (i->temp_path) { |
| 81 |
import_remove_tree(i->temp_path, &i->userns_fd, i->flags); |
| 82 |
free(i->temp_path); |
| 83 |
} |
| 84 |
|
| 85 |
i->compress = compressor_free(i->compress); |
| 86 |
|
| 87 |
sd_event_unref(i->event); |
| 88 |
|
| 89 |
safe_close(i->tar_fd); |
| 90 |
safe_close(i->tree_fd); |
| 91 |
safe_close(i->userns_fd); |
| 92 |
|
| 93 |
free(i->final_path); |
| 94 |
free(i->image_root); |
| 95 |
free(i->local); |
| 96 |
return mfree(i); |
| 97 |
} |
| 98 |
|
| 99 |
int tar_import_new( |
| 100 |
TarImport **ret, |
| 101 |
sd_event *event, |
| 102 |
const char *image_root, |
| 103 |
TarImportFinished on_finished, |
| 104 |
void *userdata) { |
| 105 |
|
| 106 |
_cleanup_(tar_import_unrefp) TarImport *i = NULL; |
| 107 |
_cleanup_free_ char *root = NULL; |
| 108 |
int r; |
| 109 |
|
| 110 |
assert(ret); |
| 111 |
assert(image_root); |
| 112 |
|
| 113 |
root = strdup(image_root); |
| 114 |
if (!root) |
| 115 |
return -ENOMEM; |
| 116 |
|
| 117 |
i = new(TarImport, 1); |
| 118 |
if (!i) |
| 119 |
return -ENOMEM; |
| 120 |
|
| 121 |
*i = (TarImport) { |
| 122 |
.input_fd = -EBADF, |
| 123 |
.tar_fd = -EBADF, |
| 124 |
.tree_fd = -EBADF, |
| 125 |
.userns_fd = -EBADF, |
| 126 |
.on_finished = on_finished, |
| 127 |
.userdata = userdata, |
| 128 |
.last_percent = UINT_MAX, |
| 129 |
.image_root = TAKE_PTR(root), |
| 130 |
.progress_ratelimit = { 100 * USEC_PER_MSEC, 1 }, |
| 131 |
.tar_pid = PIDREF_NULL, |
| 132 |
}; |
| 133 |
|
| 134 |
if (event) |
| 135 |
i->event = sd_event_ref(event); |
| 136 |
else { |
| 137 |
r = sd_event_default(&i->event); |
| 138 |
if (r < 0) |
| 139 |
return r; |
| 140 |
} |
| 141 |
|
| 142 |
*ret = TAKE_PTR(i); |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
static void tar_import_report_progress(TarImport *i) { |
| 148 |
unsigned percent; |
| 149 |
assert(i); |
| 150 |
|
| 151 |
/* We have no size information, unless the source is a regular file */ |
| 152 |
if (!S_ISREG(i->input_stat.st_mode)) |
| 153 |
return; |
| 154 |
|
| 155 |
if (i->written_compressed >= (uint64_t) i->input_stat.st_size) |
| 156 |
percent = 100; |
| 157 |
else |
| 158 |
percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->input_stat.st_size); |
| 159 |
|
| 160 |
if (percent == i->last_percent) |
| 161 |
return; |
| 162 |
|
| 163 |
if (!ratelimit_below(&i->progress_ratelimit)) |
| 164 |
return; |
| 165 |
|
| 166 |
sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent); |
| 167 |
|
| 168 |
if (isatty_safe(STDERR_FILENO)) |
| 169 |
(void) draw_progress_barf( |
| 170 |
percent, |
| 171 |
"%s %s/%s", |
| 172 |
glyph(GLYPH_ARROW_RIGHT), |
| 173 |
FORMAT_BYTES(i->written_compressed), |
| 174 |
FORMAT_BYTES(i->input_stat.st_size)); |
| 175 |
else |
| 176 |
log_info("Imported %u%%.", percent); |
| 177 |
|
| 178 |
i->last_percent = percent; |
| 179 |
} |
| 180 |
|
| 181 |
static int tar_import_finish(TarImport *i) { |
| 182 |
const char *d; |
| 183 |
int r; |
| 184 |
|
| 185 |
assert(i); |
| 186 |
assert(i->tar_fd >= 0); |
| 187 |
assert(i->tree_fd >= 0); |
| 188 |
|
| 189 |
i->tar_fd = safe_close(i->tar_fd); |
| 190 |
|
| 191 |
if (pidref_is_set(&i->tar_pid)) { |
| 192 |
r = pidref_wait_for_terminate_and_check("tar", &i->tar_pid, WAIT_LOG); |
| 193 |
if (r < 0) |
| 194 |
return r; |
| 195 |
|
| 196 |
pidref_done(&i->tar_pid); |
| 197 |
|
| 198 |
if (r != EXIT_SUCCESS) |
| 199 |
return -EPROTO; |
| 200 |
} |
| 201 |
|
| 202 |
assert_se(d = i->temp_path ?: i->local); |
| 203 |
|
| 204 |
r = import_mangle_os_tree_fd(i->tree_fd, i->userns_fd, i->flags); |
| 205 |
if (r < 0) |
| 206 |
return r; |
| 207 |
|
| 208 |
r = install_file( |
| 209 |
AT_FDCWD, d, |
| 210 |
AT_FDCWD, i->final_path, |
| 211 |
(i->flags & IMPORT_FORCE ? INSTALL_REPLACE : 0) | |
| 212 |
(i->flags & IMPORT_READ_ONLY ? INSTALL_READ_ONLY|INSTALL_GRACEFUL : 0) | |
| 213 |
(i->flags & IMPORT_SYNC ? INSTALL_SYNCFS|INSTALL_GRACEFUL : 0)); |
| 214 |
if (r < 0) |
| 215 |
return log_error_errno(r, "Failed to move '%s' into place: %m", i->final_path ?: i->local); |
| 216 |
|
| 217 |
i->temp_path = mfree(i->temp_path); |
| 218 |
|
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
static int tar_import_fork_tar(TarImport *i) { |
| 223 |
const char *d, *root; |
| 224 |
int r; |
| 225 |
|
| 226 |
assert(i); |
| 227 |
assert(i->local); |
| 228 |
assert(!i->final_path); |
| 229 |
assert(!i->temp_path); |
| 230 |
assert(i->tar_fd < 0); |
| 231 |
assert(i->tree_fd < 0); |
| 232 |
|
| 233 |
if (i->flags & IMPORT_DIRECT) { |
| 234 |
d = i->local; |
| 235 |
root = NULL; |
| 236 |
} else { |
| 237 |
i->final_path = path_join(i->image_root, i->local); |
| 238 |
if (!i->final_path) |
| 239 |
return log_oom(); |
| 240 |
|
| 241 |
r = tempfn_random(i->final_path, NULL, &i->temp_path); |
| 242 |
if (r < 0) |
| 243 |
return log_oom(); |
| 244 |
|
| 245 |
d = i->temp_path; |
| 246 |
root = i->image_root; |
| 247 |
} |
| 248 |
|
| 249 |
assert(d); |
| 250 |
|
| 251 |
(void) mkdir_parents_label(d, 0700); |
| 252 |
|
| 253 |
if (FLAGS_SET(i->flags, IMPORT_DIRECT|IMPORT_FORCE)) |
| 254 |
(void) rm_rf(d, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME); |
| 255 |
|
| 256 |
if (FLAGS_SET(i->flags, IMPORT_FOREIGN_UID)) { |
| 257 |
r = import_make_foreign_userns(&i->userns_fd); |
| 258 |
if (r < 0) |
| 259 |
return r; |
| 260 |
|
| 261 |
_cleanup_(sd_varlink_unrefp) sd_varlink *mountfsd_link = NULL; |
| 262 |
r = mountfsd_connect(&mountfsd_link); |
| 263 |
if (r < 0) |
| 264 |
return log_error_errno(r, "Failed to connect to mountfsd: %m"); |
| 265 |
|
| 266 |
_cleanup_close_ int directory_fd = -EBADF; |
| 267 |
r = mountfsd_make_directory( |
| 268 |
mountfsd_link, |
| 269 |
d, |
| 270 |
MODE_INVALID, |
| 271 |
/* flags= */ 0, |
| 272 |
&directory_fd); |
| 273 |
if (r < 0) |
| 274 |
return log_error_errno(r, "Failed to make directory via mountfsd: %m"); |
| 275 |
|
| 276 |
r = mountfsd_mount_directory_fd( |
| 277 |
mountfsd_link, |
| 278 |
directory_fd, |
| 279 |
i->userns_fd, |
| 280 |
DISSECT_IMAGE_FOREIGN_UID, |
| 281 |
&i->tree_fd); |
| 282 |
if (r < 0) |
| 283 |
return log_error_errno(r, "Failed mount directory via mountfsd: %m"); |
| 284 |
} else { |
| 285 |
if (i->flags & IMPORT_BTRFS_SUBVOL) |
| 286 |
r = btrfs_subvol_make_fallback(AT_FDCWD, d, 0755); |
| 287 |
else |
| 288 |
r = RET_NERRNO(mkdir(d, 0755)); |
| 289 |
if (r == -EEXIST && (i->flags & IMPORT_DIRECT)) /* EEXIST is OK if in direct mode, but not otherwise, |
| 290 |
* because in that case our temporary path collided */ |
| 291 |
r = 0; |
| 292 |
if (r < 0) |
| 293 |
return log_error_errno(r, "Failed to create directory/subvolume %s: %m", d); |
| 294 |
if (r > 0 && (i->flags & IMPORT_BTRFS_QUOTA)) { /* actually btrfs subvol */ |
| 295 |
if (!(i->flags & IMPORT_DIRECT)) |
| 296 |
(void) import_assign_pool_quota_and_warn(root); |
| 297 |
(void) import_assign_pool_quota_and_warn(d); |
| 298 |
} |
| 299 |
|
| 300 |
i->tree_fd = open(d, O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW); |
| 301 |
if (i->tree_fd < 0) |
| 302 |
return log_error_errno(errno, "Failed to open '%s': %m", d); |
| 303 |
} |
| 304 |
|
| 305 |
i->tar_fd = import_fork_tar_x(i->tree_fd, i->userns_fd, &i->tar_pid); |
| 306 |
if (i->tar_fd < 0) |
| 307 |
return i->tar_fd; |
| 308 |
|
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
static int tar_import_write(const void *p, size_t sz, void *userdata) { |
| 313 |
TarImport *i = userdata; |
| 314 |
int r; |
| 315 |
|
| 316 |
r = loop_write(i->tar_fd, p, sz); |
| 317 |
if (r < 0) |
| 318 |
return r; |
| 319 |
|
| 320 |
i->written_uncompressed += sz; |
| 321 |
|
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
static int tar_import_process(TarImport *i) { |
| 326 |
ssize_t l; |
| 327 |
int r; |
| 328 |
|
| 329 |
assert(i); |
| 330 |
assert(i->buffer_size < sizeof(i->buffer)); |
| 331 |
|
| 332 |
l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size); |
| 333 |
if (l < 0) { |
| 334 |
if (errno == EAGAIN) |
| 335 |
return 0; |
| 336 |
|
| 337 |
r = log_error_errno(errno, "Failed to read input file: %m"); |
| 338 |
goto finish; |
| 339 |
} |
| 340 |
|
| 341 |
if ((size_t) l > sizeof(i->buffer) - i->buffer_size) { |
| 342 |
r = log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Read input file exceeded maximum size."); |
| 343 |
goto finish; |
| 344 |
} |
| 345 |
|
| 346 |
i->buffer_size += l; |
| 347 |
|
| 348 |
if (!i->compress) { |
| 349 |
|
| 350 |
if (l == 0) { /* EOF */ |
| 351 |
log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed."); |
| 352 |
decompressor_force_off(&i->compress); |
| 353 |
} else { |
| 354 |
r = decompressor_detect(&i->compress, i->buffer, i->buffer_size); |
| 355 |
if (r < 0) { |
| 356 |
log_error_errno(r, "Failed to detect file compression: %m"); |
| 357 |
goto finish; |
| 358 |
} |
| 359 |
if (r == 0) /* Need more data */ |
| 360 |
return 0; |
| 361 |
} |
| 362 |
|
| 363 |
r = tar_import_fork_tar(i); |
| 364 |
if (r < 0) |
| 365 |
goto finish; |
| 366 |
} |
| 367 |
|
| 368 |
r = decompressor_push(i->compress, i->buffer, i->buffer_size, tar_import_write, i); |
| 369 |
if (r < 0) { |
| 370 |
log_error_errno(r, "Failed to decode and write: %m"); |
| 371 |
|
| 372 |
/* Try to check the actual exit code from the child process, to make debugging easier */ |
| 373 |
if (r == -EPIPE && pidref_is_set(&i->tar_pid)) { |
| 374 |
int q = pidref_wait_for_terminate_and_check("tar", &i->tar_pid, WAIT_LOG); |
| 375 |
pidref_done(&i->tar_pid); |
| 376 |
if (q < 0) |
| 377 |
r = q; |
| 378 |
else if (q != EXIT_SUCCESS) |
| 379 |
r = -EPROTO; |
| 380 |
} |
| 381 |
|
| 382 |
goto finish; |
| 383 |
} |
| 384 |
|
| 385 |
i->written_compressed += i->buffer_size; |
| 386 |
i->buffer_size = 0; |
| 387 |
|
| 388 |
if (l == 0) { /* EOF */ |
| 389 |
r = tar_import_finish(i); |
| 390 |
goto finish; |
| 391 |
} |
| 392 |
|
| 393 |
tar_import_report_progress(i); |
| 394 |
|
| 395 |
return 0; |
| 396 |
|
| 397 |
finish: |
| 398 |
if (r >= 0 && isatty_safe(STDERR_FILENO)) |
| 399 |
clear_progress_bar(/* prefix= */ NULL); |
| 400 |
|
| 401 |
if (i->on_finished) |
| 402 |
i->on_finished(i, r, i->userdata); |
| 403 |
else |
| 404 |
sd_event_exit(i->event, r); |
| 405 |
|
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
static int tar_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 410 |
TarImport *i = userdata; |
| 411 |
|
| 412 |
return tar_import_process(i); |
| 413 |
} |
| 414 |
|
| 415 |
static int tar_import_on_defer(sd_event_source *s, void *userdata) { |
| 416 |
TarImport *i = userdata; |
| 417 |
|
| 418 |
return tar_import_process(i); |
| 419 |
} |
| 420 |
|
| 421 |
int tar_import_start(TarImport *i, int fd, const char *local, ImportFlags flags) { |
| 422 |
int r; |
| 423 |
|
| 424 |
assert(i); |
| 425 |
assert(fd >= 0); |
| 426 |
assert(local); |
| 427 |
assert(!(flags & ~IMPORT_FLAGS_MASK_TAR)); |
| 428 |
|
| 429 |
if (!import_validate_local(local, flags)) |
| 430 |
return -EINVAL; |
| 431 |
|
| 432 |
if (i->input_fd >= 0) |
| 433 |
return -EBUSY; |
| 434 |
|
| 435 |
r = fd_nonblock(fd, true); |
| 436 |
if (r < 0) |
| 437 |
return r; |
| 438 |
|
| 439 |
r = free_and_strdup(&i->local, local); |
| 440 |
if (r < 0) |
| 441 |
return r; |
| 442 |
|
| 443 |
i->flags = flags; |
| 444 |
|
| 445 |
if (fstat(fd, &i->input_stat) < 0) |
| 446 |
return -errno; |
| 447 |
|
| 448 |
r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, tar_import_on_input, i); |
| 449 |
if (r == -EPERM) { |
| 450 |
/* This fd does not support epoll, for example because it is a regular file. Busy read in that case */ |
| 451 |
r = sd_event_add_defer(i->event, &i->input_event_source, tar_import_on_defer, i); |
| 452 |
if (r < 0) |
| 453 |
return r; |
| 454 |
|
| 455 |
r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON); |
| 456 |
} |
| 457 |
if (r < 0) |
| 458 |
return r; |
| 459 |
|
| 460 |
i->input_fd = fd; |
| 461 |
return 0; |
| 462 |
} |