| 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 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "btrfs-util.h" |
| 11 |
#include "dissect-image.h" |
| 12 |
#include "export-tar.h" |
| 13 |
#include "fd-util.h" |
| 14 |
#include "format-util.h" |
| 15 |
#include "import-common.h" |
| 16 |
#include "log.h" |
| 17 |
#include "pidref.h" |
| 18 |
#include "pretty-print.h" |
| 19 |
#include "process-util.h" |
| 20 |
#include "ratelimit.h" |
| 21 |
#include "string-util.h" |
| 22 |
#include "terminal-util.h" |
| 23 |
#include "time-util.h" |
| 24 |
#include "tmpfile-util.h" |
| 25 |
|
| 26 |
typedef struct TarExport { |
| 27 |
sd_event *event; |
| 28 |
|
| 29 |
TarExportFinished on_finished; |
| 30 |
void *userdata; |
| 31 |
|
| 32 |
ImportFlags flags; |
| 33 |
|
| 34 |
char *path; |
| 35 |
char *temp_path; |
| 36 |
|
| 37 |
int output_fd; /* compressed tar file in the fs */ |
| 38 |
int tar_fd; /* uncompressed tar stream coming from child doing the libarchive loop */ |
| 39 |
int tree_fd; /* directory fd of the tree to set up */ |
| 40 |
int userns_fd; |
| 41 |
|
| 42 |
Compressor *compress; |
| 43 |
|
| 44 |
sd_event_source *output_event_source; |
| 45 |
|
| 46 |
void *buffer; |
| 47 |
size_t buffer_size; |
| 48 |
size_t buffer_allocated; |
| 49 |
|
| 50 |
uint64_t written_compressed; |
| 51 |
uint64_t written_uncompressed; |
| 52 |
|
| 53 |
PidRef tar_pid; |
| 54 |
|
| 55 |
struct stat st; |
| 56 |
uint64_t quota_referenced; |
| 57 |
|
| 58 |
unsigned last_percent; |
| 59 |
RateLimit progress_ratelimit; |
| 60 |
|
| 61 |
bool eof; |
| 62 |
} TarExport; |
| 63 |
|
| 64 |
TarExport *tar_export_unref(TarExport *e) { |
| 65 |
if (!e) |
| 66 |
return NULL; |
| 67 |
|
| 68 |
sd_event_source_unref(e->output_event_source); |
| 69 |
|
| 70 |
pidref_done_sigkill_wait(&e->tar_pid); |
| 71 |
|
| 72 |
if (e->temp_path) { |
| 73 |
(void) btrfs_subvol_remove(e->temp_path, BTRFS_REMOVE_QUOTA); |
| 74 |
free(e->temp_path); |
| 75 |
} |
| 76 |
|
| 77 |
e->compress = compressor_free(e->compress); |
| 78 |
|
| 79 |
sd_event_unref(e->event); |
| 80 |
|
| 81 |
safe_close(e->tar_fd); |
| 82 |
|
| 83 |
free(e->buffer); |
| 84 |
free(e->path); |
| 85 |
return mfree(e); |
| 86 |
} |
| 87 |
|
| 88 |
int tar_export_new( |
| 89 |
TarExport **ret, |
| 90 |
sd_event *event, |
| 91 |
TarExportFinished on_finished, |
| 92 |
void *userdata) { |
| 93 |
|
| 94 |
_cleanup_(tar_export_unrefp) TarExport *e = NULL; |
| 95 |
int r; |
| 96 |
|
| 97 |
assert(ret); |
| 98 |
|
| 99 |
e = new(TarExport, 1); |
| 100 |
if (!e) |
| 101 |
return -ENOMEM; |
| 102 |
|
| 103 |
*e = (TarExport) { |
| 104 |
.output_fd = -EBADF, |
| 105 |
.tar_fd = -EBADF, |
| 106 |
.tree_fd = -EBADF, |
| 107 |
.userns_fd = -EBADF, |
| 108 |
.on_finished = on_finished, |
| 109 |
.userdata = userdata, |
| 110 |
.quota_referenced = UINT64_MAX, |
| 111 |
.last_percent = UINT_MAX, |
| 112 |
.progress_ratelimit = { 100 * USEC_PER_MSEC, 1 }, |
| 113 |
.tar_pid = PIDREF_NULL, |
| 114 |
}; |
| 115 |
|
| 116 |
if (event) |
| 117 |
e->event = sd_event_ref(event); |
| 118 |
else { |
| 119 |
r = sd_event_default(&e->event); |
| 120 |
if (r < 0) |
| 121 |
return r; |
| 122 |
} |
| 123 |
|
| 124 |
*ret = TAKE_PTR(e); |
| 125 |
|
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
static void tar_export_report_progress(TarExport *e) { |
| 130 |
unsigned percent; |
| 131 |
assert(e); |
| 132 |
|
| 133 |
/* Do we have any quota info? If not, we don't know anything about the progress */ |
| 134 |
if (e->quota_referenced == UINT64_MAX) |
| 135 |
return; |
| 136 |
|
| 137 |
if (e->written_uncompressed >= e->quota_referenced) |
| 138 |
percent = 100; |
| 139 |
else |
| 140 |
percent = (unsigned) ((e->written_uncompressed * UINT64_C(100)) / e->quota_referenced); |
| 141 |
|
| 142 |
if (percent == e->last_percent) |
| 143 |
return; |
| 144 |
|
| 145 |
if (!ratelimit_below(&e->progress_ratelimit)) |
| 146 |
return; |
| 147 |
|
| 148 |
sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent); |
| 149 |
|
| 150 |
if (isatty_safe(STDERR_FILENO)) |
| 151 |
(void) draw_progress_barf( |
| 152 |
percent, |
| 153 |
"%s %s/%s", |
| 154 |
glyph(GLYPH_ARROW_RIGHT), |
| 155 |
FORMAT_BYTES(e->written_uncompressed), |
| 156 |
FORMAT_BYTES(e->quota_referenced)); |
| 157 |
else |
| 158 |
log_info("Exported %u%%.", percent); |
| 159 |
|
| 160 |
e->last_percent = percent; |
| 161 |
} |
| 162 |
|
| 163 |
static int tar_export_finish(TarExport *e) { |
| 164 |
int r; |
| 165 |
|
| 166 |
assert(e); |
| 167 |
assert(e->tar_fd >= 0); |
| 168 |
|
| 169 |
if (pidref_is_set(&e->tar_pid)) { |
| 170 |
r = pidref_wait_for_terminate_and_check("tar", &e->tar_pid, WAIT_LOG); |
| 171 |
if (r < 0) |
| 172 |
return r; |
| 173 |
|
| 174 |
pidref_done(&e->tar_pid); |
| 175 |
|
| 176 |
if (r != EXIT_SUCCESS) |
| 177 |
return -EPROTO; |
| 178 |
} |
| 179 |
|
| 180 |
e->tar_fd = safe_close(e->tar_fd); |
| 181 |
|
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
static int tar_export_process(TarExport *e) { |
| 186 |
ssize_t l; |
| 187 |
int r; |
| 188 |
|
| 189 |
assert(e); |
| 190 |
|
| 191 |
while (e->buffer_size <= 0) { |
| 192 |
uint8_t input[COMPRESS_PIPE_BUFFER_SIZE]; |
| 193 |
|
| 194 |
if (e->eof) { |
| 195 |
r = tar_export_finish(e); |
| 196 |
goto finish; |
| 197 |
} |
| 198 |
|
| 199 |
l = read(e->tar_fd, input, sizeof(input)); |
| 200 |
if (l < 0) { |
| 201 |
r = log_error_errno(errno, "Failed to read tar file: %m"); |
| 202 |
goto finish; |
| 203 |
} |
| 204 |
|
| 205 |
if (l == 0) { |
| 206 |
e->eof = true; |
| 207 |
r = compressor_finish(e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated); |
| 208 |
} else { |
| 209 |
e->written_uncompressed += l; |
| 210 |
r = compressor_start(e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated); |
| 211 |
} |
| 212 |
if (r < 0) { |
| 213 |
r = log_error_errno(r, "Failed to encode: %m"); |
| 214 |
goto finish; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
l = write(e->output_fd, e->buffer, e->buffer_size); |
| 219 |
if (l < 0) { |
| 220 |
if (errno == EAGAIN) |
| 221 |
return 0; |
| 222 |
|
| 223 |
r = log_error_errno(errno, "Failed to write output file: %m"); |
| 224 |
goto finish; |
| 225 |
} |
| 226 |
|
| 227 |
assert((size_t) l <= e->buffer_size); |
| 228 |
memmove(e->buffer, (uint8_t*) e->buffer + l, e->buffer_size - l); |
| 229 |
e->buffer_size -= l; |
| 230 |
e->written_compressed += l; |
| 231 |
|
| 232 |
tar_export_report_progress(e); |
| 233 |
|
| 234 |
return 0; |
| 235 |
|
| 236 |
finish: |
| 237 |
if (r >= 0 && isatty_safe(STDERR_FILENO)) |
| 238 |
clear_progress_bar(/* prefix= */ NULL); |
| 239 |
|
| 240 |
if (e->on_finished) |
| 241 |
e->on_finished(e, r, e->userdata); |
| 242 |
else |
| 243 |
sd_event_exit(e->event, r); |
| 244 |
|
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
static int tar_export_on_output(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 249 |
TarExport *i = userdata; |
| 250 |
|
| 251 |
return tar_export_process(i); |
| 252 |
} |
| 253 |
|
| 254 |
static int tar_export_on_defer(sd_event_source *s, void *userdata) { |
| 255 |
TarExport *i = userdata; |
| 256 |
|
| 257 |
return tar_export_process(i); |
| 258 |
} |
| 259 |
|
| 260 |
int tar_export_start( |
| 261 |
TarExport *e, |
| 262 |
const char *path, |
| 263 |
int fd, |
| 264 |
Compression compress, |
| 265 |
ImportFlags flags) { |
| 266 |
|
| 267 |
_cleanup_close_ int sfd = -EBADF; |
| 268 |
int r; |
| 269 |
|
| 270 |
assert(e); |
| 271 |
assert(path); |
| 272 |
assert(fd >= 0); |
| 273 |
assert(compress >= 0); |
| 274 |
assert(compress < _COMPRESSION_MAX); |
| 275 |
|
| 276 |
if (e->output_fd >= 0) |
| 277 |
return -EBUSY; |
| 278 |
|
| 279 |
sfd = open(path, O_DIRECTORY|O_RDONLY|O_NOCTTY|O_CLOEXEC); |
| 280 |
if (sfd < 0) |
| 281 |
return -errno; |
| 282 |
|
| 283 |
if (fstat(sfd, &e->st) < 0) |
| 284 |
return -errno; |
| 285 |
|
| 286 |
r = fd_nonblock(fd, true); |
| 287 |
if (r < 0) |
| 288 |
return r; |
| 289 |
|
| 290 |
r = free_and_strdup(&e->path, path); |
| 291 |
if (r < 0) |
| 292 |
return r; |
| 293 |
|
| 294 |
e->flags = flags; |
| 295 |
e->quota_referenced = UINT64_MAX; |
| 296 |
|
| 297 |
if (btrfs_might_be_subvol(&e->st)) { |
| 298 |
BtrfsQuotaInfo q; |
| 299 |
|
| 300 |
r = btrfs_subvol_get_subtree_quota_fd(sfd, 0, &q); |
| 301 |
if (r >= 0) |
| 302 |
e->quota_referenced = q.referenced; |
| 303 |
|
| 304 |
e->temp_path = mfree(e->temp_path); |
| 305 |
|
| 306 |
r = tempfn_random(path, NULL, &e->temp_path); |
| 307 |
if (r < 0) |
| 308 |
return r; |
| 309 |
|
| 310 |
/* Let's try to make a snapshot, if we can, so that the export is atomic */ |
| 311 |
r = btrfs_subvol_snapshot_at(sfd, NULL, AT_FDCWD, e->temp_path, BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_RECURSIVE); |
| 312 |
if (r < 0) { |
| 313 |
log_debug_errno(r, "Couldn't create snapshot %s of %s, not exporting atomically: %m", e->temp_path, path); |
| 314 |
e->temp_path = mfree(e->temp_path); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
r = compressor_new(&e->compress, compress); |
| 319 |
if (r < 0) |
| 320 |
return r; |
| 321 |
|
| 322 |
r = sd_event_add_io(e->event, &e->output_event_source, fd, EPOLLOUT, tar_export_on_output, e); |
| 323 |
if (r == -EPERM) { |
| 324 |
r = sd_event_add_defer(e->event, &e->output_event_source, tar_export_on_defer, e); |
| 325 |
if (r < 0) |
| 326 |
return r; |
| 327 |
|
| 328 |
r = sd_event_source_set_enabled(e->output_event_source, SD_EVENT_ON); |
| 329 |
} |
| 330 |
if (r < 0) |
| 331 |
return r; |
| 332 |
|
| 333 |
const char *p = e->temp_path ?: e->path; |
| 334 |
|
| 335 |
if (FLAGS_SET(e->flags, IMPORT_FOREIGN_UID)) { |
| 336 |
r = import_make_foreign_userns(&e->userns_fd); |
| 337 |
if (r < 0) |
| 338 |
return r; |
| 339 |
|
| 340 |
_cleanup_close_ int directory_fd = open(p, O_DIRECTORY|O_CLOEXEC|O_PATH); |
| 341 |
if (directory_fd < 0) |
| 342 |
return log_error_errno(r, "Failed to open '%s': %m", p); |
| 343 |
|
| 344 |
_cleanup_close_ int mapped_fd = -EBADF; |
| 345 |
r = mountfsd_mount_directory_fd( |
| 346 |
/* vl= */ NULL, |
| 347 |
directory_fd, |
| 348 |
e->userns_fd, |
| 349 |
DISSECT_IMAGE_FOREIGN_UID, |
| 350 |
&mapped_fd); |
| 351 |
if (r < 0) |
| 352 |
return log_error_errno(r, "Failed to mount directory via mountfsd: %m"); |
| 353 |
|
| 354 |
/* Drop O_PATH */ |
| 355 |
e->tree_fd = fd_reopen(mapped_fd, O_DIRECTORY|O_CLOEXEC); |
| 356 |
if (e->tree_fd < 0) |
| 357 |
return log_error_errno(errno, "Failed to re-open mapped '%s': %m", p); |
| 358 |
} else { |
| 359 |
e->tree_fd = open(p, O_DIRECTORY|O_CLOEXEC); |
| 360 |
if (e->tree_fd < 0) |
| 361 |
return log_error_errno(errno, "Failed to open '%s': %m", p); |
| 362 |
} |
| 363 |
|
| 364 |
e->tar_fd = import_fork_tar_c(e->tree_fd, e->userns_fd, &e->tar_pid); |
| 365 |
if (e->tar_fd < 0) { |
| 366 |
e->output_event_source = sd_event_source_unref(e->output_event_source); |
| 367 |
return e->tar_fd; |
| 368 |
} |
| 369 |
|
| 370 |
e->output_fd = fd; |
| 371 |
return r; |
| 372 |
} |