| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-bus.h" |
| 7 |
#include "sd-daemon.h" |
| 8 |
#include "sd-varlink.h" |
| 9 |
|
| 10 |
#include "alloc-util.h" |
| 11 |
#include "build-path.h" |
| 12 |
#include "bus-common-errors.h" |
| 13 |
#include "bus-get-properties.h" |
| 14 |
#include "bus-log-control-api.h" |
| 15 |
#include "bus-object.h" |
| 16 |
#include "bus-polkit.h" |
| 17 |
#include "bus-util.h" |
| 18 |
#include "common-signal.h" |
| 19 |
#include "constants.h" |
| 20 |
#include "daemon-util.h" |
| 21 |
#include "discover-image.h" |
| 22 |
#include "env-util.h" |
| 23 |
#include "event-util.h" |
| 24 |
#include "fd-util.h" |
| 25 |
#include "float.h" |
| 26 |
#include "hashmap.h" |
| 27 |
#include "import-common.h" |
| 28 |
#include "import-util.h" |
| 29 |
#include "json-util.h" |
| 30 |
#include "main-func.h" |
| 31 |
#include "notify-recv.h" |
| 32 |
#include "oci-util.h" |
| 33 |
#include "os-util.h" |
| 34 |
#include "parse-util.h" |
| 35 |
#include "path-lookup.h" |
| 36 |
#include "percent-util.h" |
| 37 |
#include "pidref.h" |
| 38 |
#include "process-util.h" |
| 39 |
#include "runtime-scope.h" |
| 40 |
#include "service-util.h" |
| 41 |
#include "set.h" |
| 42 |
#include "signal-util.h" |
| 43 |
#include "stat-util.h" |
| 44 |
#include "string-table.h" |
| 45 |
#include "strv.h" |
| 46 |
#include "syslog-util.h" |
| 47 |
#include "varlink-io.systemd.Import.h" |
| 48 |
#include "varlink-io.systemd.service.h" |
| 49 |
#include "varlink-util.h" |
| 50 |
#include "web-util.h" |
| 51 |
|
| 52 |
typedef struct Manager Manager; |
| 53 |
|
| 54 |
typedef enum TransferType { |
| 55 |
TRANSFER_IMPORT_TAR, |
| 56 |
TRANSFER_IMPORT_RAW, |
| 57 |
TRANSFER_IMPORT_FS, |
| 58 |
TRANSFER_EXPORT_TAR, |
| 59 |
TRANSFER_EXPORT_RAW, |
| 60 |
TRANSFER_PULL_TAR, |
| 61 |
TRANSFER_PULL_RAW, |
| 62 |
TRANSFER_PULL_OCI, |
| 63 |
_TRANSFER_TYPE_MAX, |
| 64 |
_TRANSFER_TYPE_INVALID = -EINVAL, |
| 65 |
} TransferType; |
| 66 |
|
| 67 |
typedef struct Transfer { |
| 68 |
Manager *manager; |
| 69 |
|
| 70 |
uint32_t id; |
| 71 |
char *object_path; |
| 72 |
|
| 73 |
TransferType type; |
| 74 |
ImportVerify verify; |
| 75 |
|
| 76 |
char *remote; |
| 77 |
char *local; |
| 78 |
char *image_root; |
| 79 |
ImageClass class; |
| 80 |
ImportFlags flags; |
| 81 |
char *format; |
| 82 |
|
| 83 |
PidRef pidref; |
| 84 |
|
| 85 |
int log_fd; |
| 86 |
|
| 87 |
char log_message[LINE_MAX]; |
| 88 |
size_t log_message_size; |
| 89 |
|
| 90 |
sd_event_source *pid_event_source; |
| 91 |
sd_event_source *log_event_source; |
| 92 |
|
| 93 |
unsigned n_canceled; |
| 94 |
unsigned progress_percent; |
| 95 |
unsigned progress_percent_sent; |
| 96 |
|
| 97 |
int stdin_fd; |
| 98 |
int stdout_fd; |
| 99 |
|
| 100 |
Set *varlink_subscribed; |
| 101 |
} Transfer; |
| 102 |
|
| 103 |
typedef struct Manager { |
| 104 |
sd_event *event; |
| 105 |
sd_bus *api_bus; |
| 106 |
sd_bus *system_bus; |
| 107 |
sd_varlink_server *varlink_server; |
| 108 |
|
| 109 |
uint32_t current_transfer_id; |
| 110 |
Hashmap *transfers; |
| 111 |
|
| 112 |
Hashmap *polkit_registry; |
| 113 |
|
| 114 |
char *notify_socket_path; |
| 115 |
|
| 116 |
bool use_btrfs_subvol; |
| 117 |
bool use_btrfs_quota; |
| 118 |
|
| 119 |
RuntimeScope runtime_scope; |
| 120 |
} Manager; |
| 121 |
|
| 122 |
#define TRANSFERS_MAX 64 |
| 123 |
|
| 124 |
static const char* const transfer_type_table[_TRANSFER_TYPE_MAX] = { |
| 125 |
[TRANSFER_IMPORT_TAR] = "import-tar", |
| 126 |
[TRANSFER_IMPORT_RAW] = "import-raw", |
| 127 |
[TRANSFER_IMPORT_FS] = "import-fs", |
| 128 |
[TRANSFER_EXPORT_TAR] = "export-tar", |
| 129 |
[TRANSFER_EXPORT_RAW] = "export-raw", |
| 130 |
[TRANSFER_PULL_TAR] = "pull-tar", |
| 131 |
[TRANSFER_PULL_RAW] = "pull-raw", |
| 132 |
[TRANSFER_PULL_OCI] = "pull-oci", |
| 133 |
}; |
| 134 |
|
| 135 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(transfer_type, TransferType); |
| 136 |
|
| 137 |
static Transfer *transfer_unref(Transfer *t) { |
| 138 |
if (!t) |
| 139 |
return NULL; |
| 140 |
|
| 141 |
if (t->manager) |
| 142 |
hashmap_remove(t->manager->transfers, UINT32_TO_PTR(t->id)); |
| 143 |
|
| 144 |
sd_event_source_unref(t->pid_event_source); |
| 145 |
sd_event_source_unref(t->log_event_source); |
| 146 |
|
| 147 |
free(t->remote); |
| 148 |
free(t->local); |
| 149 |
free(t->format); |
| 150 |
free(t->image_root); |
| 151 |
free(t->object_path); |
| 152 |
|
| 153 |
pidref_done_sigkill_wait(&t->pidref); |
| 154 |
|
| 155 |
safe_close(t->log_fd); |
| 156 |
safe_close(t->stdin_fd); |
| 157 |
safe_close(t->stdout_fd); |
| 158 |
|
| 159 |
set_free(t->varlink_subscribed); |
| 160 |
|
| 161 |
return mfree(t); |
| 162 |
} |
| 163 |
|
| 164 |
DEFINE_TRIVIAL_CLEANUP_FUNC(Transfer*, transfer_unref); |
| 165 |
|
| 166 |
static int transfer_new(Manager *m, Transfer **ret) { |
| 167 |
_cleanup_(transfer_unrefp) Transfer *t = NULL; |
| 168 |
uint32_t id; |
| 169 |
int r; |
| 170 |
|
| 171 |
assert(m); |
| 172 |
assert(ret); |
| 173 |
|
| 174 |
if (hashmap_size(m->transfers) >= TRANSFERS_MAX) |
| 175 |
return -E2BIG; |
| 176 |
|
| 177 |
t = new(Transfer, 1); |
| 178 |
if (!t) |
| 179 |
return -ENOMEM; |
| 180 |
|
| 181 |
*t = (Transfer) { |
| 182 |
.type = _TRANSFER_TYPE_INVALID, |
| 183 |
.log_fd = -EBADF, |
| 184 |
.stdin_fd = -EBADF, |
| 185 |
.stdout_fd = -EBADF, |
| 186 |
.verify = _IMPORT_VERIFY_INVALID, |
| 187 |
.progress_percent = UINT_MAX, |
| 188 |
.progress_percent_sent = UINT_MAX, |
| 189 |
}; |
| 190 |
|
| 191 |
id = m->current_transfer_id + 1; |
| 192 |
|
| 193 |
if (asprintf(&t->object_path, "/org/freedesktop/import1/transfer/_%" PRIu32, id) < 0) |
| 194 |
return -ENOMEM; |
| 195 |
|
| 196 |
r = hashmap_ensure_put(&m->transfers, &trivial_hash_ops, UINT32_TO_PTR(id), t); |
| 197 |
if (r < 0) |
| 198 |
return r; |
| 199 |
|
| 200 |
m->current_transfer_id = id; |
| 201 |
|
| 202 |
t->manager = m; |
| 203 |
t->id = id; |
| 204 |
|
| 205 |
*ret = TAKE_PTR(t); |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
static double transfer_percent_as_double(Transfer *t) { |
| 211 |
assert(t); |
| 212 |
|
| 213 |
if (t->progress_percent == UINT_MAX) |
| 214 |
return -DBL_MAX; |
| 215 |
|
| 216 |
return (double) t->progress_percent / 100.0; |
| 217 |
} |
| 218 |
|
| 219 |
static void transfer_send_log_line(Transfer *t, const char *line) { |
| 220 |
int r, priority = LOG_INFO; |
| 221 |
|
| 222 |
assert(t); |
| 223 |
assert(line); |
| 224 |
|
| 225 |
syslog_parse_priority(&line, &priority, true); |
| 226 |
|
| 227 |
log_full(priority, "(transfer%" PRIu32 ") %s", t->id, line); |
| 228 |
|
| 229 |
r = sd_bus_emit_signal( |
| 230 |
t->manager->api_bus, |
| 231 |
t->object_path, |
| 232 |
"org.freedesktop.import1.Transfer", |
| 233 |
"LogMessage", |
| 234 |
"us", |
| 235 |
priority, |
| 236 |
line); |
| 237 |
if (r < 0) |
| 238 |
log_warning_errno(r, "Cannot emit log message bus signal, ignoring: %m"); |
| 239 |
|
| 240 |
r = varlink_many_notifybo( |
| 241 |
t->varlink_subscribed, |
| 242 |
SD_JSON_BUILD_PAIR("log", |
| 243 |
SD_JSON_BUILD_OBJECT( |
| 244 |
SD_JSON_BUILD_PAIR_UNSIGNED("priority", priority), |
| 245 |
SD_JSON_BUILD_PAIR_STRING("message", line)))); |
| 246 |
if (r < 0) |
| 247 |
log_warning_errno(r, "Cannot emit log message varlink message, ignoring: %m"); |
| 248 |
} |
| 249 |
|
| 250 |
static void transfer_send_progress_update(Transfer *t) { |
| 251 |
int r; |
| 252 |
|
| 253 |
assert(t); |
| 254 |
|
| 255 |
if (t->progress_percent_sent == t->progress_percent) |
| 256 |
return; |
| 257 |
|
| 258 |
double progress = transfer_percent_as_double(t); |
| 259 |
|
| 260 |
r = sd_bus_emit_signal( |
| 261 |
t->manager->api_bus, |
| 262 |
t->object_path, |
| 263 |
"org.freedesktop.import1.Transfer", |
| 264 |
"ProgressUpdate", |
| 265 |
"d", |
| 266 |
progress); |
| 267 |
if (r < 0) |
| 268 |
log_warning_errno(r, "Cannot emit progress update bus signal, ignoring: %m"); |
| 269 |
|
| 270 |
r = varlink_many_notifybo( |
| 271 |
t->varlink_subscribed, |
| 272 |
SD_JSON_BUILD_PAIR_REAL("progress", progress)); |
| 273 |
if (r < 0) |
| 274 |
log_warning_errno(r, "Cannot emit progress update varlink message, ignoring: %m"); |
| 275 |
|
| 276 |
t->progress_percent_sent = t->progress_percent; |
| 277 |
} |
| 278 |
|
| 279 |
static void transfer_send_logs(Transfer *t, bool flush) { |
| 280 |
assert(t); |
| 281 |
|
| 282 |
/* Try to send out all log messages, if we can. But if we |
| 283 |
* can't we remove the messages from the buffer, but don't |
| 284 |
* fail */ |
| 285 |
|
| 286 |
while (t->log_message_size > 0) { |
| 287 |
_cleanup_free_ char *n = NULL; |
| 288 |
char *e; |
| 289 |
|
| 290 |
if (t->log_message_size >= sizeof(t->log_message)) |
| 291 |
e = t->log_message + sizeof(t->log_message); |
| 292 |
else { |
| 293 |
char *a, *b; |
| 294 |
|
| 295 |
a = memchr(t->log_message, 0, t->log_message_size); |
| 296 |
b = memchr(t->log_message, '\n', t->log_message_size); |
| 297 |
|
| 298 |
if (a && b) |
| 299 |
e = a < b ? a : b; |
| 300 |
else if (a) |
| 301 |
e = a; |
| 302 |
else |
| 303 |
e = b; |
| 304 |
} |
| 305 |
|
| 306 |
if (!e) { |
| 307 |
if (!flush) |
| 308 |
return; |
| 309 |
|
| 310 |
e = t->log_message + t->log_message_size; |
| 311 |
} |
| 312 |
|
| 313 |
n = strndup(t->log_message, e - t->log_message); |
| 314 |
|
| 315 |
/* Skip over NUL and newlines */ |
| 316 |
while (e < t->log_message + t->log_message_size && IN_SET(*e, 0, '\n')) |
| 317 |
e++; |
| 318 |
|
| 319 |
memmove(t->log_message, e, t->log_message + sizeof(t->log_message) - e); |
| 320 |
t->log_message_size -= e - t->log_message; |
| 321 |
|
| 322 |
if (!n) { |
| 323 |
log_oom(); |
| 324 |
continue; |
| 325 |
} |
| 326 |
|
| 327 |
if (isempty(n)) |
| 328 |
continue; |
| 329 |
|
| 330 |
transfer_send_log_line(t, n); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
static int transfer_finalize(Transfer *t, bool success) { |
| 335 |
int r; |
| 336 |
|
| 337 |
assert(t); |
| 338 |
|
| 339 |
transfer_send_logs(t, true); |
| 340 |
|
| 341 |
r = sd_bus_emit_signal( |
| 342 |
t->manager->api_bus, |
| 343 |
"/org/freedesktop/import1", |
| 344 |
"org.freedesktop.import1.Manager", |
| 345 |
"TransferRemoved", |
| 346 |
"uos", |
| 347 |
t->id, |
| 348 |
t->object_path, |
| 349 |
success ? "done" : |
| 350 |
t->n_canceled > 0 ? "canceled" : "failed"); |
| 351 |
if (r < 0) |
| 352 |
log_error_errno(r, "Cannot emit message: %m"); |
| 353 |
|
| 354 |
if (success) |
| 355 |
r = varlink_many_reply(t->varlink_subscribed, NULL); |
| 356 |
else if (t->n_canceled > 0) |
| 357 |
r = varlink_many_error(t->varlink_subscribed, "io.systemd.Import.TransferCancelled", NULL); |
| 358 |
else |
| 359 |
r = varlink_many_error(t->varlink_subscribed, "io.systemd.Import.TransferFailed", NULL); |
| 360 |
if (r < 0) |
| 361 |
log_warning_errno(r, "Cannot emit varlink reply, ignoring: %m"); |
| 362 |
|
| 363 |
transfer_unref(t); |
| 364 |
return 0; |
| 365 |
} |
| 366 |
|
| 367 |
static int transfer_cancel(Transfer *t) { |
| 368 |
int r; |
| 369 |
|
| 370 |
assert(t); |
| 371 |
|
| 372 |
r = pidref_kill_and_sigcont(&t->pidref, t->n_canceled < 3 ? SIGTERM : SIGKILL); |
| 373 |
if (r < 0) |
| 374 |
return r; |
| 375 |
|
| 376 |
t->n_canceled++; |
| 377 |
return 0; |
| 378 |
} |
| 379 |
|
| 380 |
static int transfer_on_pid(sd_event_source *s, const siginfo_t *si, void *userdata) { |
| 381 |
Transfer *t = ASSERT_PTR(userdata); |
| 382 |
bool success = false; |
| 383 |
|
| 384 |
assert(s); |
| 385 |
|
| 386 |
if (si->si_code == CLD_EXITED) { |
| 387 |
if (si->si_status != 0) |
| 388 |
log_error("Transfer process failed with exit code %i.", si->si_status); |
| 389 |
else { |
| 390 |
log_debug("Transfer process succeeded."); |
| 391 |
success = true; |
| 392 |
} |
| 393 |
|
| 394 |
} else if (IN_SET(si->si_code, CLD_KILLED, CLD_DUMPED)) |
| 395 |
log_error("Transfer process terminated by signal %s.", signal_to_string(si->si_status)); |
| 396 |
else |
| 397 |
log_error("Transfer process failed due to unknown reason."); |
| 398 |
|
| 399 |
pidref_done(&t->pidref); |
| 400 |
|
| 401 |
return transfer_finalize(t, success); |
| 402 |
} |
| 403 |
|
| 404 |
static int transfer_on_log(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 405 |
Transfer *t = ASSERT_PTR(userdata); |
| 406 |
ssize_t l; |
| 407 |
|
| 408 |
assert(s); |
| 409 |
|
| 410 |
l = read(fd, t->log_message + t->log_message_size, sizeof(t->log_message) - t->log_message_size); |
| 411 |
if (l < 0) |
| 412 |
log_error_errno(errno, "Failed to read log message: %m"); |
| 413 |
if (l <= 0) { |
| 414 |
/* EOF/read error. We just close the pipe here, and |
| 415 |
* close the watch, waiting for the child to exit, |
| 416 |
* before we do anything else. */ |
| 417 |
t->log_event_source = sd_event_source_unref(t->log_event_source); |
| 418 |
return 0; |
| 419 |
} |
| 420 |
|
| 421 |
/* Silence static analyzers, l is bounded by read() count: sizeof - log_message_size */ |
| 422 |
assert((size_t) l <= sizeof(t->log_message) - t->log_message_size); |
| 423 |
t->log_message_size += l; |
| 424 |
|
| 425 |
transfer_send_logs(t, false); |
| 426 |
|
| 427 |
return 0; |
| 428 |
} |
| 429 |
|
| 430 |
static int transfer_start(Transfer *t) { |
| 431 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 432 |
int r; |
| 433 |
|
| 434 |
assert(t); |
| 435 |
assert(!pidref_is_set(&t->pidref)); |
| 436 |
|
| 437 |
if (pipe2(pipefd, O_CLOEXEC) < 0) |
| 438 |
return -errno; |
| 439 |
|
| 440 |
r = pidref_safe_fork_full( |
| 441 |
"(sd-transfer)", |
| 442 |
(int[]) { t->stdin_fd, t->stdout_fd < 0 ? pipefd[1] : t->stdout_fd, pipefd[1] }, |
| 443 |
/* except_fds= */ NULL, /* n_except_fds= */ 0, |
| 444 |
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_REOPEN_LOG, |
| 445 |
&t->pidref); |
| 446 |
if (r < 0) |
| 447 |
return r; |
| 448 |
if (r == 0) { |
| 449 |
const char *cmd[] = { |
| 450 |
NULL, /* systemd-import, systemd-import-fs, systemd-export or systemd-pull */ |
| 451 |
NULL, /* tar, raw */ |
| 452 |
NULL, /* --system or --user */ |
| 453 |
NULL, /* --verify= */ |
| 454 |
NULL, /* verify argument */ |
| 455 |
NULL, /* --class= */ |
| 456 |
NULL, /* class argument */ |
| 457 |
NULL, /* --keep-download= */ |
| 458 |
NULL, /* maybe --force */ |
| 459 |
NULL, /* maybe --read-only */ |
| 460 |
NULL, /* if so: the actual URL */ |
| 461 |
NULL, /* maybe --format= */ |
| 462 |
NULL, /* if so: the actual format */ |
| 463 |
NULL, /* maybe --image-root= */ |
| 464 |
NULL, /* if so: the image root path */ |
| 465 |
NULL, /* remote */ |
| 466 |
NULL, /* local */ |
| 467 |
NULL |
| 468 |
}; |
| 469 |
size_t k = 0; |
| 470 |
|
| 471 |
/* Child */ |
| 472 |
|
| 473 |
if (setenv("SYSTEMD_LOG_TARGET", "console-prefixed", 1) < 0 || |
| 474 |
setenv("NOTIFY_SOCKET", t->manager->notify_socket_path, 1) < 0) { |
| 475 |
log_error_errno(errno, "setenv() failed: %m"); |
| 476 |
_exit(EXIT_FAILURE); |
| 477 |
} |
| 478 |
|
| 479 |
r = setenv_systemd_log_level(); |
| 480 |
if (r < 0) |
| 481 |
log_warning_errno(r, "Failed to update $SYSTEMD_LOG_LEVEL, ignoring: %m"); |
| 482 |
|
| 483 |
r = setenv_systemd_exec_pid(true); |
| 484 |
if (r < 0) |
| 485 |
log_warning_errno(r, "Failed to update $SYSTEMD_EXEC_PID, ignoring: %m"); |
| 486 |
|
| 487 |
switch (t->type) { |
| 488 |
|
| 489 |
case TRANSFER_IMPORT_TAR: |
| 490 |
case TRANSFER_IMPORT_RAW: |
| 491 |
cmd[k++] = SYSTEMD_IMPORT_PATH; |
| 492 |
break; |
| 493 |
|
| 494 |
case TRANSFER_IMPORT_FS: |
| 495 |
cmd[k++] = SYSTEMD_IMPORT_FS_PATH; |
| 496 |
break; |
| 497 |
|
| 498 |
case TRANSFER_EXPORT_TAR: |
| 499 |
case TRANSFER_EXPORT_RAW: |
| 500 |
cmd[k++] = SYSTEMD_EXPORT_PATH; |
| 501 |
break; |
| 502 |
|
| 503 |
case TRANSFER_PULL_TAR: |
| 504 |
case TRANSFER_PULL_RAW: |
| 505 |
case TRANSFER_PULL_OCI: |
| 506 |
cmd[k++] = SYSTEMD_PULL_PATH; |
| 507 |
break; |
| 508 |
|
| 509 |
default: |
| 510 |
assert_not_reached(); |
| 511 |
} |
| 512 |
|
| 513 |
switch (t->type) { |
| 514 |
|
| 515 |
case TRANSFER_IMPORT_TAR: |
| 516 |
case TRANSFER_EXPORT_TAR: |
| 517 |
case TRANSFER_PULL_TAR: |
| 518 |
cmd[k++] = "tar"; |
| 519 |
break; |
| 520 |
|
| 521 |
case TRANSFER_IMPORT_RAW: |
| 522 |
case TRANSFER_EXPORT_RAW: |
| 523 |
case TRANSFER_PULL_RAW: |
| 524 |
cmd[k++] = "raw"; |
| 525 |
break; |
| 526 |
|
| 527 |
case TRANSFER_PULL_OCI: |
| 528 |
cmd[k++] = "oci"; |
| 529 |
break; |
| 530 |
|
| 531 |
case TRANSFER_IMPORT_FS: |
| 532 |
cmd[k++] = "run"; |
| 533 |
break; |
| 534 |
|
| 535 |
default: |
| 536 |
; |
| 537 |
} |
| 538 |
|
| 539 |
cmd[k++] = runtime_scope_cmdline_option_to_string(t->manager->runtime_scope); |
| 540 |
|
| 541 |
if (t->verify != _IMPORT_VERIFY_INVALID) { |
| 542 |
cmd[k++] = "--verify"; |
| 543 |
cmd[k++] = import_verify_to_string(t->verify); |
| 544 |
} |
| 545 |
|
| 546 |
if (t->class != IMAGE_MACHINE) { |
| 547 |
cmd[k++] = "--class"; |
| 548 |
cmd[k++] = image_class_to_string(t->class); |
| 549 |
} |
| 550 |
|
| 551 |
if (IN_SET(t->type, TRANSFER_PULL_TAR, TRANSFER_PULL_RAW)) |
| 552 |
cmd[k++] = FLAGS_SET(t->flags, IMPORT_PULL_KEEP_DOWNLOAD) ? |
| 553 |
"--keep-download=yes" : "--keep-download=no"; |
| 554 |
|
| 555 |
if (FLAGS_SET(t->flags, IMPORT_FORCE)) |
| 556 |
cmd[k++] = "--force"; |
| 557 |
if (FLAGS_SET(t->flags, IMPORT_READ_ONLY)) |
| 558 |
cmd[k++] = "--read-only"; |
| 559 |
|
| 560 |
if (t->format) { |
| 561 |
cmd[k++] = "--format"; |
| 562 |
cmd[k++] = t->format; |
| 563 |
} |
| 564 |
|
| 565 |
if (t->image_root) { |
| 566 |
cmd[k++] = "--image-root"; |
| 567 |
cmd[k++] = t->image_root; |
| 568 |
} |
| 569 |
|
| 570 |
if (!IN_SET(t->type, TRANSFER_EXPORT_TAR, TRANSFER_EXPORT_RAW)) { |
| 571 |
if (t->remote) |
| 572 |
cmd[k++] = t->remote; |
| 573 |
else |
| 574 |
cmd[k++] = "-"; |
| 575 |
} |
| 576 |
|
| 577 |
if (t->local) |
| 578 |
cmd[k++] = t->local; |
| 579 |
cmd[k] = NULL; |
| 580 |
|
| 581 |
assert(k < ELEMENTSOF(cmd)); |
| 582 |
|
| 583 |
if (DEBUG_LOGGING) { |
| 584 |
_cleanup_free_ char *joined = strv_join((char**) cmd, " "); |
| 585 |
log_debug("Calling: %s", strnull(joined)); |
| 586 |
} |
| 587 |
|
| 588 |
r = invoke_callout_binary(cmd[0], (char * const *) cmd); |
| 589 |
log_error_errno(r, "Failed to execute %s tool: %m", cmd[0]); |
| 590 |
_exit(EXIT_FAILURE); |
| 591 |
} |
| 592 |
|
| 593 |
pipefd[1] = safe_close(pipefd[1]); |
| 594 |
t->log_fd = TAKE_FD(pipefd[0]); |
| 595 |
|
| 596 |
t->stdin_fd = safe_close(t->stdin_fd); |
| 597 |
|
| 598 |
r = event_add_child_pidref( |
| 599 |
t->manager->event, |
| 600 |
&t->pid_event_source, |
| 601 |
&t->pidref, |
| 602 |
WEXITED, |
| 603 |
transfer_on_pid, |
| 604 |
t); |
| 605 |
if (r < 0) |
| 606 |
return r; |
| 607 |
|
| 608 |
r = sd_event_add_io(t->manager->event, &t->log_event_source, |
| 609 |
t->log_fd, EPOLLIN, transfer_on_log, t); |
| 610 |
if (r < 0) |
| 611 |
return r; |
| 612 |
|
| 613 |
/* Make sure always process logging before child exit */ |
| 614 |
r = sd_event_source_set_priority(t->log_event_source, SD_EVENT_PRIORITY_NORMAL -5); |
| 615 |
if (r < 0) |
| 616 |
return r; |
| 617 |
|
| 618 |
r = sd_bus_emit_signal( |
| 619 |
t->manager->api_bus, |
| 620 |
"/org/freedesktop/import1", |
| 621 |
"org.freedesktop.import1.Manager", |
| 622 |
"TransferNew", |
| 623 |
"uo", |
| 624 |
t->id, |
| 625 |
t->object_path); |
| 626 |
if (r < 0) |
| 627 |
return r; |
| 628 |
|
| 629 |
return 0; |
| 630 |
} |
| 631 |
|
| 632 |
static Manager *manager_unref(Manager *m) { |
| 633 |
Transfer *t; |
| 634 |
|
| 635 |
if (!m) |
| 636 |
return NULL; |
| 637 |
|
| 638 |
free(m->notify_socket_path); |
| 639 |
|
| 640 |
while ((t = hashmap_first(m->transfers))) |
| 641 |
transfer_unref(t); |
| 642 |
|
| 643 |
hashmap_free(m->transfers); |
| 644 |
|
| 645 |
hashmap_free(m->polkit_registry); |
| 646 |
|
| 647 |
m->api_bus = sd_bus_flush_close_unref(m->api_bus); |
| 648 |
m->system_bus = sd_bus_flush_close_unref(m->system_bus); |
| 649 |
m->varlink_server = sd_varlink_server_unref(m->varlink_server); |
| 650 |
|
| 651 |
sd_event_unref(m->event); |
| 652 |
|
| 653 |
return mfree(m); |
| 654 |
} |
| 655 |
|
| 656 |
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref); |
| 657 |
|
| 658 |
static int manager_on_notify(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 659 |
Manager *m = ASSERT_PTR(userdata); |
| 660 |
int r; |
| 661 |
|
| 662 |
_cleanup_free_ char *buf = NULL; |
| 663 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 664 |
r = notify_recv(fd, &buf, /* ret_ucred= */ NULL, &pidref); |
| 665 |
if (r == -EAGAIN) |
| 666 |
return 0; |
| 667 |
if (r < 0) |
| 668 |
return r; |
| 669 |
|
| 670 |
Transfer *t; |
| 671 |
HASHMAP_FOREACH(t, m->transfers) |
| 672 |
if (pidref_equal(&pidref, &t->pidref)) |
| 673 |
break; |
| 674 |
if (!t) { |
| 675 |
log_warning("Got notification datagram from unexpected peer, ignoring."); |
| 676 |
return 0; |
| 677 |
} |
| 678 |
|
| 679 |
char *p = find_line_startswith(buf, "X_IMPORT_PROGRESS="); |
| 680 |
if (!p) |
| 681 |
return 0; |
| 682 |
|
| 683 |
truncate_nl(p); |
| 684 |
|
| 685 |
r = parse_percent(p); |
| 686 |
if (r < 0) { |
| 687 |
log_warning("Got invalid percent value '%s', ignoring.", p); |
| 688 |
return 0; |
| 689 |
} |
| 690 |
|
| 691 |
t->progress_percent = (unsigned) r; |
| 692 |
|
| 693 |
log_debug("Got percentage from client: %u%%", t->progress_percent); |
| 694 |
|
| 695 |
transfer_send_progress_update(t); |
| 696 |
return 0; |
| 697 |
} |
| 698 |
|
| 699 |
static int manager_new(RuntimeScope scope, Manager **ret) { |
| 700 |
_cleanup_(manager_unrefp) Manager *m = NULL; |
| 701 |
int r; |
| 702 |
|
| 703 |
assert(ret); |
| 704 |
|
| 705 |
m = new(Manager, 1); |
| 706 |
if (!m) |
| 707 |
return -ENOMEM; |
| 708 |
|
| 709 |
*m = (Manager) { |
| 710 |
.use_btrfs_subvol = true, |
| 711 |
.use_btrfs_quota = true, |
| 712 |
.runtime_scope = scope, |
| 713 |
}; |
| 714 |
|
| 715 |
r = sd_event_default(&m->event); |
| 716 |
if (r < 0) |
| 717 |
return r; |
| 718 |
|
| 719 |
r = sd_event_set_signal_exit(m->event, true); |
| 720 |
if (r < 0) |
| 721 |
return r; |
| 722 |
|
| 723 |
r = sd_event_add_signal(m->event, NULL, (SIGRTMIN+18)|SD_EVENT_SIGNAL_PROCMASK, sigrtmin18_handler, NULL); |
| 724 |
if (r < 0) |
| 725 |
return r; |
| 726 |
|
| 727 |
r = sd_event_add_memory_pressure(m->event, NULL, NULL, NULL); |
| 728 |
if (r < 0) |
| 729 |
log_debug_errno(r, "Failed to allocate memory pressure event source, ignoring: %m"); |
| 730 |
|
| 731 |
r = sd_event_set_watchdog(m->event, true); |
| 732 |
if (r < 0) |
| 733 |
log_debug_errno(r, "Failed to enable watchdog logic, ignoring: %m"); |
| 734 |
|
| 735 |
r = notify_socket_prepare( |
| 736 |
m->event, |
| 737 |
SD_EVENT_PRIORITY_NORMAL - 1, /* Make this processed before child exit. */ |
| 738 |
manager_on_notify, |
| 739 |
m, |
| 740 |
&m->notify_socket_path); |
| 741 |
if (r < 0) |
| 742 |
return r; |
| 743 |
|
| 744 |
*ret = TAKE_PTR(m); |
| 745 |
|
| 746 |
return 0; |
| 747 |
} |
| 748 |
|
| 749 |
static Transfer *manager_find(Manager *m, TransferType type, const char *remote) { |
| 750 |
Transfer *t; |
| 751 |
|
| 752 |
assert(m); |
| 753 |
assert(type >= 0); |
| 754 |
assert(type < _TRANSFER_TYPE_MAX); |
| 755 |
|
| 756 |
HASHMAP_FOREACH(t, m->transfers) |
| 757 |
if (t->type == type && streq_ptr(t->remote, remote)) |
| 758 |
return t; |
| 759 |
|
| 760 |
return NULL; |
| 761 |
} |
| 762 |
|
| 763 |
static int method_import_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_error *error) { |
| 764 |
_cleanup_(transfer_unrefp) Transfer *t = NULL; |
| 765 |
ImageClass class = _IMAGE_CLASS_INVALID; |
| 766 |
Manager *m = ASSERT_PTR(userdata); |
| 767 |
const char *local; |
| 768 |
TransferType type; |
| 769 |
struct stat st; |
| 770 |
uint64_t flags; |
| 771 |
int fd, r; |
| 772 |
|
| 773 |
assert(msg); |
| 774 |
|
| 775 |
if (m->runtime_scope != RUNTIME_SCOPE_USER) { |
| 776 |
r = bus_verify_polkit_async( |
| 777 |
msg, |
| 778 |
"org.freedesktop.import1.import", |
| 779 |
/* details= */ NULL, |
| 780 |
&m->polkit_registry, |
| 781 |
error); |
| 782 |
if (r < 0) |
| 783 |
return r; |
| 784 |
if (r == 0) |
| 785 |
return 1; /* Will call us back */ |
| 786 |
} |
| 787 |
|
| 788 |
if (endswith(sd_bus_message_get_member(msg), "Ex")) { |
| 789 |
const char *sclass; |
| 790 |
|
| 791 |
r = sd_bus_message_read(msg, "hsst", &fd, &local, &sclass, &flags); |
| 792 |
if (r < 0) |
| 793 |
return r; |
| 794 |
|
| 795 |
class = image_class_from_string(sclass); |
| 796 |
if (class < 0) |
| 797 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 798 |
"Image class '%s' not known", sclass); |
| 799 |
|
| 800 |
if (flags & ~(IMPORT_READ_ONLY|IMPORT_FORCE)) |
| 801 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 802 |
"Flags 0x%" PRIx64 " invalid", flags); |
| 803 |
} else { |
| 804 |
int force, read_only; |
| 805 |
|
| 806 |
r = sd_bus_message_read(msg, "hsbb", &fd, &local, &force, &read_only); |
| 807 |
if (r < 0) |
| 808 |
return r; |
| 809 |
|
| 810 |
class = IMAGE_MACHINE; |
| 811 |
|
| 812 |
flags = 0; |
| 813 |
SET_FLAG(flags, IMPORT_FORCE, force); |
| 814 |
SET_FLAG(flags, IMPORT_READ_ONLY, read_only); |
| 815 |
} |
| 816 |
|
| 817 |
r = fd_verify_safe_flags(fd); |
| 818 |
if (r < 0) |
| 819 |
return r; |
| 820 |
|
| 821 |
if (fstat(fd, &st) < 0) |
| 822 |
return -errno; |
| 823 |
|
| 824 |
if (!S_ISREG(st.st_mode) && !S_ISFIFO(st.st_mode)) |
| 825 |
return -EINVAL; |
| 826 |
|
| 827 |
if (!image_name_is_valid(local)) |
| 828 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 829 |
"Local image name %s is invalid", local); |
| 830 |
|
| 831 |
if (class == IMAGE_MACHINE) { |
| 832 |
r = image_setup_pool(m->runtime_scope, class, m->use_btrfs_subvol, m->use_btrfs_quota); |
| 833 |
if (r < 0) |
| 834 |
return sd_bus_error_set_errnof(error, r, "Failed to set up machine pool: %m"); |
| 835 |
} |
| 836 |
|
| 837 |
type = startswith(sd_bus_message_get_member(msg), "ImportTar") ? |
| 838 |
TRANSFER_IMPORT_TAR : TRANSFER_IMPORT_RAW; |
| 839 |
|
| 840 |
r = transfer_new(m, &t); |
| 841 |
if (r < 0) |
| 842 |
return r; |
| 843 |
|
| 844 |
t->type = type; |
| 845 |
t->class = class; |
| 846 |
t->flags = flags; |
| 847 |
|
| 848 |
t->local = strdup(local); |
| 849 |
if (!t->local) |
| 850 |
return -ENOMEM; |
| 851 |
|
| 852 |
t->stdin_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
| 853 |
if (t->stdin_fd < 0) |
| 854 |
return -errno; |
| 855 |
|
| 856 |
r = transfer_start(t); |
| 857 |
if (r < 0) |
| 858 |
return r; |
| 859 |
|
| 860 |
r = sd_bus_reply_method_return(msg, "uo", t->id, t->object_path); |
| 861 |
if (r < 0) |
| 862 |
return r; |
| 863 |
|
| 864 |
TAKE_PTR(t); |
| 865 |
return 1; |
| 866 |
} |
| 867 |
|
| 868 |
static int method_import_fs(sd_bus_message *msg, void *userdata, sd_bus_error *error) { |
| 869 |
_cleanup_(transfer_unrefp) Transfer *t = NULL; |
| 870 |
ImageClass class = _IMAGE_CLASS_INVALID; |
| 871 |
Manager *m = ASSERT_PTR(userdata); |
| 872 |
const char *local; |
| 873 |
uint64_t flags; |
| 874 |
int fd, r; |
| 875 |
|
| 876 |
assert(msg); |
| 877 |
|
| 878 |
if (m->runtime_scope != RUNTIME_SCOPE_USER) { |
| 879 |
r = bus_verify_polkit_async( |
| 880 |
msg, |
| 881 |
"org.freedesktop.import1.import", |
| 882 |
/* details= */ NULL, |
| 883 |
&m->polkit_registry, |
| 884 |
error); |
| 885 |
if (r < 0) |
| 886 |
return r; |
| 887 |
if (r == 0) |
| 888 |
return 1; /* Will call us back */ |
| 889 |
} |
| 890 |
|
| 891 |
if (endswith(sd_bus_message_get_member(msg), "Ex")) { |
| 892 |
const char *sclass; |
| 893 |
|
| 894 |
r = sd_bus_message_read(msg, "hsst", &fd, &local, &sclass, &flags); |
| 895 |
if (r < 0) |
| 896 |
return r; |
| 897 |
|
| 898 |
class = image_class_from_string(sclass); |
| 899 |
if (class < 0) |
| 900 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 901 |
"Image class '%s' not known", sclass); |
| 902 |
|
| 903 |
if (flags & ~(IMPORT_READ_ONLY|IMPORT_FORCE)) |
| 904 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 905 |
"Flags 0x%" PRIx64 " invalid", flags); |
| 906 |
} else { |
| 907 |
int force, read_only; |
| 908 |
|
| 909 |
r = sd_bus_message_read(msg, "hsbb", &fd, &local, &force, &read_only); |
| 910 |
if (r < 0) |
| 911 |
return r; |
| 912 |
|
| 913 |
class = IMAGE_MACHINE; |
| 914 |
|
| 915 |
flags = 0; |
| 916 |
SET_FLAG(flags, IMPORT_FORCE, force); |
| 917 |
SET_FLAG(flags, IMPORT_READ_ONLY, read_only); |
| 918 |
} |
| 919 |
|
| 920 |
r = fd_verify_safe_flags_full(fd, O_DIRECTORY); |
| 921 |
if (r < 0) |
| 922 |
return r; |
| 923 |
|
| 924 |
r = fd_verify_directory(fd); |
| 925 |
if (r < 0) |
| 926 |
return r; |
| 927 |
|
| 928 |
if (!image_name_is_valid(local)) |
| 929 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 930 |
"Local image name %s is invalid", local); |
| 931 |
|
| 932 |
if (class == IMAGE_MACHINE) { |
| 933 |
r = image_setup_pool(m->runtime_scope, class, m->use_btrfs_subvol, m->use_btrfs_quota); |
| 934 |
if (r < 0) |
| 935 |
return sd_bus_error_set_errnof(error, r, "Failed to set up machine pool: %m"); |
| 936 |
} |
| 937 |
|
| 938 |
r = transfer_new(m, &t); |
| 939 |
if (r < 0) |
| 940 |
return r; |
| 941 |
|
| 942 |
t->type = TRANSFER_IMPORT_FS; |
| 943 |
t->class = class; |
| 944 |
t->flags = flags; |
| 945 |
|
| 946 |
t->local = strdup(local); |
| 947 |
if (!t->local) |
| 948 |
return -ENOMEM; |
| 949 |
|
| 950 |
t->stdin_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
| 951 |
if (t->stdin_fd < 0) |
| 952 |
return -errno; |
| 953 |
|
| 954 |
r = transfer_start(t); |
| 955 |
if (r < 0) |
| 956 |
return r; |
| 957 |
|
| 958 |
r = sd_bus_reply_method_return(msg, "uo", t->id, t->object_path); |
| 959 |
if (r < 0) |
| 960 |
return r; |
| 961 |
|
| 962 |
TAKE_PTR(t); |
| 963 |
return 1; |
| 964 |
} |
| 965 |
|
| 966 |
static int method_export_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_error *error) { |
| 967 |
_cleanup_(transfer_unrefp) Transfer *t = NULL; |
| 968 |
ImageClass class = _IMAGE_CLASS_INVALID; |
| 969 |
Manager *m = ASSERT_PTR(userdata); |
| 970 |
const char *local, *format; |
| 971 |
TransferType type; |
| 972 |
uint64_t flags; |
| 973 |
struct stat st; |
| 974 |
int fd, r; |
| 975 |
|
| 976 |
assert(msg); |
| 977 |
|
| 978 |
if (m->runtime_scope != RUNTIME_SCOPE_USER) { |
| 979 |
r = bus_verify_polkit_async( |
| 980 |
msg, |
| 981 |
"org.freedesktop.import1.export", |
| 982 |
/* details= */ NULL, |
| 983 |
&m->polkit_registry, |
| 984 |
error); |
| 985 |
if (r < 0) |
| 986 |
return r; |
| 987 |
if (r == 0) |
| 988 |
return 1; /* Will call us back */ |
| 989 |
} |
| 990 |
|
| 991 |
if (endswith(sd_bus_message_get_member(msg), "Ex")) { |
| 992 |
const char *sclass; |
| 993 |
|
| 994 |
r = sd_bus_message_read(msg, "sshst", &local, &sclass, &fd, &format, &flags); |
| 995 |
if (r < 0) |
| 996 |
return r; |
| 997 |
|
| 998 |
class = image_class_from_string(sclass); |
| 999 |
if (class < 0) |
| 1000 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1001 |
"Image class '%s' not known", sclass); |
| 1002 |
|
| 1003 |
if (flags != 0) |
| 1004 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1005 |
"Flags 0x%" PRIx64 " invalid", flags); |
| 1006 |
} else { |
| 1007 |
r = sd_bus_message_read(msg, "shs", &local, &fd, &format); |
| 1008 |
if (r < 0) |
| 1009 |
return r; |
| 1010 |
|
| 1011 |
class = IMAGE_MACHINE; |
| 1012 |
flags = 0; |
| 1013 |
} |
| 1014 |
|
| 1015 |
if (!image_name_is_valid(local)) |
| 1016 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1017 |
"Local image name %s is invalid", local); |
| 1018 |
|
| 1019 |
r = fd_verify_safe_flags(fd); |
| 1020 |
if (r < 0) |
| 1021 |
return r; |
| 1022 |
|
| 1023 |
if (fstat(fd, &st) < 0) |
| 1024 |
return -errno; |
| 1025 |
|
| 1026 |
if (!S_ISREG(st.st_mode) && !S_ISFIFO(st.st_mode)) |
| 1027 |
return -EINVAL; |
| 1028 |
|
| 1029 |
type = startswith(sd_bus_message_get_member(msg), "ExportTar") ? |
| 1030 |
TRANSFER_EXPORT_TAR : TRANSFER_EXPORT_RAW; |
| 1031 |
|
| 1032 |
r = transfer_new(m, &t); |
| 1033 |
if (r < 0) |
| 1034 |
return r; |
| 1035 |
|
| 1036 |
t->type = type; |
| 1037 |
t->class = class; |
| 1038 |
t->flags = flags; |
| 1039 |
|
| 1040 |
if (!isempty(format)) { |
| 1041 |
t->format = strdup(format); |
| 1042 |
if (!t->format) |
| 1043 |
return -ENOMEM; |
| 1044 |
} |
| 1045 |
|
| 1046 |
t->local = strdup(local); |
| 1047 |
if (!t->local) |
| 1048 |
return -ENOMEM; |
| 1049 |
|
| 1050 |
t->stdout_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
| 1051 |
if (t->stdout_fd < 0) |
| 1052 |
return -errno; |
| 1053 |
|
| 1054 |
r = transfer_start(t); |
| 1055 |
if (r < 0) |
| 1056 |
return r; |
| 1057 |
|
| 1058 |
r = sd_bus_reply_method_return(msg, "uo", t->id, t->object_path); |
| 1059 |
if (r < 0) |
| 1060 |
return r; |
| 1061 |
|
| 1062 |
TAKE_PTR(t); |
| 1063 |
return 1; |
| 1064 |
} |
| 1065 |
|
| 1066 |
static int method_pull_tar_or_raw_or_oci(sd_bus_message *msg, void *userdata, sd_bus_error *error) { |
| 1067 |
_cleanup_(transfer_unrefp) Transfer *t = NULL; |
| 1068 |
ImageClass class = _IMAGE_CLASS_INVALID; |
| 1069 |
const char *remote, *local, *verify; |
| 1070 |
Manager *m = ASSERT_PTR(userdata); |
| 1071 |
TransferType type; |
| 1072 |
uint64_t flags; |
| 1073 |
ImportVerify v; |
| 1074 |
int r; |
| 1075 |
|
| 1076 |
assert(msg); |
| 1077 |
|
| 1078 |
if (m->runtime_scope != RUNTIME_SCOPE_USER) { |
| 1079 |
r = bus_verify_polkit_async( |
| 1080 |
msg, |
| 1081 |
"org.freedesktop.import1.pull", |
| 1082 |
/* details= */ NULL, |
| 1083 |
&m->polkit_registry, |
| 1084 |
error); |
| 1085 |
if (r < 0) |
| 1086 |
return r; |
| 1087 |
if (r == 0) |
| 1088 |
return 1; /* Will call us back */ |
| 1089 |
} |
| 1090 |
|
| 1091 |
if (streq(sd_bus_message_get_member(msg), "PullOci")) { |
| 1092 |
const char *sclass; |
| 1093 |
|
| 1094 |
r = sd_bus_message_read(msg, "ssst", &remote, &local, &sclass, &flags); |
| 1095 |
if (r < 0) |
| 1096 |
return r; |
| 1097 |
|
| 1098 |
class = image_class_from_string(sclass); |
| 1099 |
if (class < 0) |
| 1100 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1101 |
"Image class '%s' not known", sclass); |
| 1102 |
|
| 1103 |
if (flags & ~(IMPORT_FORCE|IMPORT_READ_ONLY)) |
| 1104 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1105 |
"Flags 0x%" PRIx64 " invalid", flags); |
| 1106 |
|
| 1107 |
verify = NULL; |
| 1108 |
} else if (endswith(sd_bus_message_get_member(msg), "Ex")) { |
| 1109 |
const char *sclass; |
| 1110 |
|
| 1111 |
r = sd_bus_message_read(msg, "sssst", &remote, &local, &sclass, &verify, &flags); |
| 1112 |
if (r < 0) |
| 1113 |
return r; |
| 1114 |
|
| 1115 |
class = image_class_from_string(sclass); |
| 1116 |
if (class < 0) |
| 1117 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1118 |
"Image class '%s' not known", sclass); |
| 1119 |
|
| 1120 |
if (flags & ~(IMPORT_FORCE|IMPORT_READ_ONLY|IMPORT_PULL_KEEP_DOWNLOAD)) |
| 1121 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1122 |
"Flags 0x%" PRIx64 " invalid", flags); |
| 1123 |
} else { |
| 1124 |
int force; |
| 1125 |
|
| 1126 |
r = sd_bus_message_read(msg, "sssb", &remote, &local, &verify, &force); |
| 1127 |
if (r < 0) |
| 1128 |
return r; |
| 1129 |
|
| 1130 |
class = IMAGE_MACHINE; |
| 1131 |
|
| 1132 |
flags = 0; |
| 1133 |
SET_FLAG(flags, IMPORT_FORCE, force); |
| 1134 |
} |
| 1135 |
|
| 1136 |
type = startswith(sd_bus_message_get_member(msg), "PullTar") ? TRANSFER_PULL_TAR : |
| 1137 |
startswith(sd_bus_message_get_member(msg), "PullRaw") ? TRANSFER_PULL_RAW : |
| 1138 |
streq(sd_bus_message_get_member(msg), "PullOci") ? TRANSFER_PULL_OCI : _TRANSFER_TYPE_INVALID; |
| 1139 |
assert(type >= 0); |
| 1140 |
|
| 1141 |
if (type == TRANSFER_PULL_OCI) { |
| 1142 |
r = oci_ref_valid(remote); |
| 1143 |
if (r < 0) |
| 1144 |
return r; |
| 1145 |
if (r == 0) |
| 1146 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1147 |
"Reference '%s' is invalid", remote); |
| 1148 |
} else if (!http_url_is_valid(remote) && !file_url_is_valid(remote)) |
| 1149 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1150 |
"URL '%s' is invalid", remote); |
| 1151 |
|
| 1152 |
if (isempty(local)) |
| 1153 |
local = NULL; |
| 1154 |
else if (!image_name_is_valid(local)) |
| 1155 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1156 |
"Local image name %s is invalid", local); |
| 1157 |
|
| 1158 |
if (type == TRANSFER_PULL_OCI) |
| 1159 |
v = _IMPORT_VERIFY_INVALID; |
| 1160 |
else if (isempty(verify)) |
| 1161 |
v = IMPORT_VERIFY_SIGNATURE; |
| 1162 |
else { |
| 1163 |
v = import_verify_from_string(verify); |
| 1164 |
if (v < 0) |
| 1165 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1166 |
"Unknown verification mode %s", verify); |
| 1167 |
} |
| 1168 |
|
| 1169 |
if (class == IMAGE_MACHINE) { |
| 1170 |
r = image_setup_pool(m->runtime_scope, class, m->use_btrfs_subvol, m->use_btrfs_quota); |
| 1171 |
if (r < 0) |
| 1172 |
return sd_bus_error_set_errnof(error, r, "Failed to set up machine pool: %m"); |
| 1173 |
} |
| 1174 |
|
| 1175 |
if (manager_find(m, type, remote)) |
| 1176 |
return sd_bus_error_setf(error, BUS_ERROR_TRANSFER_IN_PROGRESS, |
| 1177 |
"Transfer for %s already in progress.", remote); |
| 1178 |
|
| 1179 |
r = transfer_new(m, &t); |
| 1180 |
if (r < 0) |
| 1181 |
return r; |
| 1182 |
|
| 1183 |
t->type = type; |
| 1184 |
t->verify = v; |
| 1185 |
t->flags = flags; |
| 1186 |
t->class = class; |
| 1187 |
|
| 1188 |
t->remote = strdup(remote); |
| 1189 |
if (!t->remote) |
| 1190 |
return -ENOMEM; |
| 1191 |
|
| 1192 |
if (local) { |
| 1193 |
t->local = strdup(local); |
| 1194 |
if (!t->local) |
| 1195 |
return -ENOMEM; |
| 1196 |
} |
| 1197 |
|
| 1198 |
r = transfer_start(t); |
| 1199 |
if (r < 0) |
| 1200 |
return r; |
| 1201 |
|
| 1202 |
r = sd_bus_reply_method_return(msg, "uo", t->id, t->object_path); |
| 1203 |
if (r < 0) |
| 1204 |
return r; |
| 1205 |
|
| 1206 |
TAKE_PTR(t); |
| 1207 |
return 1; |
| 1208 |
} |
| 1209 |
|
| 1210 |
static int method_list_transfers(sd_bus_message *msg, void *userdata, sd_bus_error *error) { |
| 1211 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 1212 |
Manager *m = ASSERT_PTR(userdata); |
| 1213 |
ImageClass class = _IMAGE_CLASS_INVALID; |
| 1214 |
Transfer *t; |
| 1215 |
int r; |
| 1216 |
|
| 1217 |
assert(msg); |
| 1218 |
|
| 1219 |
bool ex = endswith(sd_bus_message_get_member(msg), "Ex"); |
| 1220 |
if (ex) { |
| 1221 |
const char *sclass; |
| 1222 |
uint64_t flags; |
| 1223 |
|
| 1224 |
r = sd_bus_message_read(msg, "st", &sclass, &flags); |
| 1225 |
if (r < 0) |
| 1226 |
return bus_log_parse_error(r); |
| 1227 |
|
| 1228 |
if (!isempty(sclass)) { |
| 1229 |
class = image_class_from_string(sclass); |
| 1230 |
if (class < 0) |
| 1231 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1232 |
"Image class '%s' not known", sclass); |
| 1233 |
} |
| 1234 |
|
| 1235 |
if (flags != 0) |
| 1236 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1237 |
"Flags 0x%" PRIx64 " invalid", flags); |
| 1238 |
} |
| 1239 |
|
| 1240 |
r = sd_bus_message_new_method_return(msg, &reply); |
| 1241 |
if (r < 0) |
| 1242 |
return r; |
| 1243 |
|
| 1244 |
if (ex) |
| 1245 |
r = sd_bus_message_open_container(reply, 'a', "(ussssdo)"); |
| 1246 |
else |
| 1247 |
r = sd_bus_message_open_container(reply, 'a', "(usssdo)"); |
| 1248 |
if (r < 0) |
| 1249 |
return r; |
| 1250 |
|
| 1251 |
HASHMAP_FOREACH(t, m->transfers) { |
| 1252 |
|
| 1253 |
if (class >= 0 && class != t->class) |
| 1254 |
continue; |
| 1255 |
|
| 1256 |
if (ex) |
| 1257 |
r = sd_bus_message_append( |
| 1258 |
reply, |
| 1259 |
"(ussssdo)", |
| 1260 |
t->id, |
| 1261 |
transfer_type_to_string(t->type), |
| 1262 |
t->remote, |
| 1263 |
t->local, |
| 1264 |
image_class_to_string(t->class), |
| 1265 |
transfer_percent_as_double(t), |
| 1266 |
t->object_path); |
| 1267 |
else |
| 1268 |
r = sd_bus_message_append( |
| 1269 |
reply, |
| 1270 |
"(usssdo)", |
| 1271 |
t->id, |
| 1272 |
transfer_type_to_string(t->type), |
| 1273 |
t->remote, |
| 1274 |
t->local, |
| 1275 |
transfer_percent_as_double(t), |
| 1276 |
t->object_path); |
| 1277 |
if (r < 0) |
| 1278 |
return r; |
| 1279 |
} |
| 1280 |
|
| 1281 |
r = sd_bus_message_close_container(reply); |
| 1282 |
if (r < 0) |
| 1283 |
return r; |
| 1284 |
|
| 1285 |
return sd_bus_message_send(reply); |
| 1286 |
} |
| 1287 |
|
| 1288 |
static int method_cancel(sd_bus_message *msg, void *userdata, sd_bus_error *error) { |
| 1289 |
Transfer *t = ASSERT_PTR(userdata); |
| 1290 |
int r; |
| 1291 |
|
| 1292 |
assert(msg); |
| 1293 |
|
| 1294 |
r = bus_verify_polkit_async( |
| 1295 |
msg, |
| 1296 |
"org.freedesktop.import1.pull", |
| 1297 |
/* details= */ NULL, |
| 1298 |
&t->manager->polkit_registry, |
| 1299 |
error); |
| 1300 |
if (r < 0) |
| 1301 |
return r; |
| 1302 |
if (r == 0) |
| 1303 |
return 1; /* Will call us back */ |
| 1304 |
|
| 1305 |
r = transfer_cancel(t); |
| 1306 |
if (r < 0) |
| 1307 |
return r; |
| 1308 |
|
| 1309 |
return sd_bus_reply_method_return(msg, NULL); |
| 1310 |
} |
| 1311 |
|
| 1312 |
static int method_cancel_transfer(sd_bus_message *msg, void *userdata, sd_bus_error *error) { |
| 1313 |
Manager *m = ASSERT_PTR(userdata); |
| 1314 |
Transfer *t; |
| 1315 |
uint32_t id; |
| 1316 |
int r; |
| 1317 |
|
| 1318 |
assert(msg); |
| 1319 |
|
| 1320 |
r = bus_verify_polkit_async( |
| 1321 |
msg, |
| 1322 |
"org.freedesktop.import1.cancel", |
| 1323 |
/* details= */ NULL, |
| 1324 |
&m->polkit_registry, |
| 1325 |
error); |
| 1326 |
if (r < 0) |
| 1327 |
return r; |
| 1328 |
if (r == 0) |
| 1329 |
return 1; /* Will call us back */ |
| 1330 |
|
| 1331 |
r = sd_bus_message_read(msg, "u", &id); |
| 1332 |
if (r < 0) |
| 1333 |
return r; |
| 1334 |
if (id <= 0) |
| 1335 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid transfer id"); |
| 1336 |
|
| 1337 |
t = hashmap_get(m->transfers, UINT32_TO_PTR(id)); |
| 1338 |
if (!t) |
| 1339 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_TRANSFER, "No transfer by id %" PRIu32, id); |
| 1340 |
|
| 1341 |
r = transfer_cancel(t); |
| 1342 |
if (r < 0) |
| 1343 |
return r; |
| 1344 |
|
| 1345 |
return sd_bus_reply_method_return(msg, NULL); |
| 1346 |
} |
| 1347 |
|
| 1348 |
static int method_list_images(sd_bus_message *msg, void *userdata, sd_bus_error *error) { |
| 1349 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 1350 |
ImageClass class = _IMAGE_CLASS_INVALID; |
| 1351 |
Manager *m = ASSERT_PTR(userdata); |
| 1352 |
int r; |
| 1353 |
|
| 1354 |
assert(msg); |
| 1355 |
|
| 1356 |
const char *sclass; |
| 1357 |
uint64_t flags; |
| 1358 |
|
| 1359 |
r = sd_bus_message_read(msg, "st", &sclass, &flags); |
| 1360 |
if (r < 0) |
| 1361 |
return r; |
| 1362 |
|
| 1363 |
if (!isempty(sclass)) { |
| 1364 |
class = image_class_from_string(sclass); |
| 1365 |
if (class < 0) |
| 1366 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1367 |
"Image class '%s' not known", sclass); |
| 1368 |
} |
| 1369 |
|
| 1370 |
if (flags != 0) |
| 1371 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1372 |
"Flags 0x%" PRIx64 " invalid", flags); |
| 1373 |
|
| 1374 |
r = sd_bus_message_new_method_return(msg, &reply); |
| 1375 |
if (r < 0) |
| 1376 |
return r; |
| 1377 |
|
| 1378 |
r = sd_bus_message_open_container(reply, 'a', "(ssssbtttttt)"); |
| 1379 |
if (r < 0) |
| 1380 |
return r; |
| 1381 |
|
| 1382 |
for (ImageClass c = class < 0 ? 0 : class; |
| 1383 |
class < 0 ? (c < _IMAGE_CLASS_MAX) : (c == class); |
| 1384 |
c++) { |
| 1385 |
|
| 1386 |
_cleanup_hashmap_free_ Hashmap *images = NULL; |
| 1387 |
|
| 1388 |
r = image_discover(m->runtime_scope, c, /* root= */ NULL, &images); |
| 1389 |
if (r < 0) { |
| 1390 |
if (class >= 0) |
| 1391 |
return r; |
| 1392 |
|
| 1393 |
log_warning_errno(r, "Failed to discover images of type %s: %m", image_class_to_string(c)); |
| 1394 |
continue; |
| 1395 |
} |
| 1396 |
|
| 1397 |
Image *i; |
| 1398 |
HASHMAP_FOREACH(i, images) { |
| 1399 |
r = sd_bus_message_append( |
| 1400 |
reply, |
| 1401 |
"(ssssbtttttt)", |
| 1402 |
image_class_to_string(i->class), |
| 1403 |
i->name, |
| 1404 |
image_type_to_string(i->type), |
| 1405 |
i->path, |
| 1406 |
image_is_read_only(i), |
| 1407 |
i->crtime, |
| 1408 |
i->mtime, |
| 1409 |
i->usage, |
| 1410 |
i->usage_exclusive, |
| 1411 |
i->limit, |
| 1412 |
i->limit_exclusive); |
| 1413 |
if (r < 0) |
| 1414 |
return r; |
| 1415 |
} |
| 1416 |
} |
| 1417 |
|
| 1418 |
r = sd_bus_message_close_container(reply); |
| 1419 |
if (r < 0) |
| 1420 |
return r; |
| 1421 |
|
| 1422 |
return sd_bus_message_send(reply); |
| 1423 |
} |
| 1424 |
|
| 1425 |
static int property_get_progress( |
| 1426 |
sd_bus *bus, |
| 1427 |
const char *path, |
| 1428 |
const char *interface, |
| 1429 |
const char *property, |
| 1430 |
sd_bus_message *reply, |
| 1431 |
void *userdata, |
| 1432 |
sd_bus_error *error) { |
| 1433 |
|
| 1434 |
Transfer *t = ASSERT_PTR(userdata); |
| 1435 |
|
| 1436 |
assert(bus); |
| 1437 |
assert(reply); |
| 1438 |
|
| 1439 |
return sd_bus_message_append(reply, "d", transfer_percent_as_double(t)); |
| 1440 |
} |
| 1441 |
|
| 1442 |
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, transfer_type, TransferType); |
| 1443 |
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_verify, import_verify, ImportVerify); |
| 1444 |
|
| 1445 |
static int transfer_object_find( |
| 1446 |
sd_bus *bus, |
| 1447 |
const char *path, |
| 1448 |
const char *interface, |
| 1449 |
void *userdata, |
| 1450 |
void **found, |
| 1451 |
sd_bus_error *error) { |
| 1452 |
|
| 1453 |
Manager *m = ASSERT_PTR(userdata); |
| 1454 |
Transfer *t; |
| 1455 |
const char *p; |
| 1456 |
uint32_t id; |
| 1457 |
int r; |
| 1458 |
|
| 1459 |
assert(bus); |
| 1460 |
assert(path); |
| 1461 |
assert(interface); |
| 1462 |
assert(found); |
| 1463 |
|
| 1464 |
p = startswith(path, "/org/freedesktop/import1/transfer/_"); |
| 1465 |
if (!p) |
| 1466 |
return 0; |
| 1467 |
|
| 1468 |
r = safe_atou32(p, &id); |
| 1469 |
if (r < 0 || id == 0) |
| 1470 |
return 0; |
| 1471 |
|
| 1472 |
t = hashmap_get(m->transfers, UINT32_TO_PTR(id)); |
| 1473 |
if (!t) |
| 1474 |
return 0; |
| 1475 |
|
| 1476 |
*found = t; |
| 1477 |
return 1; |
| 1478 |
} |
| 1479 |
|
| 1480 |
static int transfer_node_enumerator( |
| 1481 |
sd_bus *bus, |
| 1482 |
const char *path, |
| 1483 |
void *userdata, |
| 1484 |
char ***nodes, |
| 1485 |
sd_bus_error *error) { |
| 1486 |
|
| 1487 |
_cleanup_strv_free_ char **l = NULL; |
| 1488 |
Manager *m = userdata; |
| 1489 |
Transfer *t; |
| 1490 |
unsigned k = 0; |
| 1491 |
|
| 1492 |
l = new0(char*, hashmap_size(m->transfers) + 1); |
| 1493 |
if (!l) |
| 1494 |
return -ENOMEM; |
| 1495 |
|
| 1496 |
HASHMAP_FOREACH(t, m->transfers) { |
| 1497 |
|
| 1498 |
l[k] = strdup(t->object_path); |
| 1499 |
if (!l[k]) |
| 1500 |
return -ENOMEM; |
| 1501 |
|
| 1502 |
k++; |
| 1503 |
} |
| 1504 |
|
| 1505 |
*nodes = TAKE_PTR(l); |
| 1506 |
|
| 1507 |
return 1; |
| 1508 |
} |
| 1509 |
|
| 1510 |
static const sd_bus_vtable transfer_vtable[] = { |
| 1511 |
SD_BUS_VTABLE_START(0), |
| 1512 |
|
| 1513 |
SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Transfer, id), SD_BUS_VTABLE_PROPERTY_CONST), |
| 1514 |
SD_BUS_PROPERTY("Local", "s", NULL, offsetof(Transfer, local), SD_BUS_VTABLE_PROPERTY_CONST), |
| 1515 |
SD_BUS_PROPERTY("Remote", "s", NULL, offsetof(Transfer, remote), SD_BUS_VTABLE_PROPERTY_CONST), |
| 1516 |
SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Transfer, type), SD_BUS_VTABLE_PROPERTY_CONST), |
| 1517 |
SD_BUS_PROPERTY("Verify", "s", property_get_verify, offsetof(Transfer, verify), SD_BUS_VTABLE_PROPERTY_CONST), |
| 1518 |
SD_BUS_PROPERTY("Progress", "d", property_get_progress, 0, 0), |
| 1519 |
|
| 1520 |
SD_BUS_METHOD("Cancel", NULL, NULL, method_cancel, SD_BUS_VTABLE_UNPRIVILEGED), |
| 1521 |
|
| 1522 |
SD_BUS_SIGNAL_WITH_NAMES("LogMessage", |
| 1523 |
"us", |
| 1524 |
SD_BUS_PARAM(priority) |
| 1525 |
SD_BUS_PARAM(line), |
| 1526 |
0), |
| 1527 |
SD_BUS_SIGNAL_WITH_NAMES("ProgressUpdate", |
| 1528 |
"d", |
| 1529 |
SD_BUS_PARAM(progress), |
| 1530 |
0), |
| 1531 |
|
| 1532 |
SD_BUS_VTABLE_END, |
| 1533 |
}; |
| 1534 |
|
| 1535 |
static const BusObjectImplementation transfer_object = { |
| 1536 |
"/org/freedesktop/import1/transfer", |
| 1537 |
"org.freedesktop.import1.Transfer", |
| 1538 |
.fallback_vtables = BUS_FALLBACK_VTABLES({transfer_vtable, transfer_object_find}), |
| 1539 |
.node_enumerator = transfer_node_enumerator, |
| 1540 |
}; |
| 1541 |
|
| 1542 |
static const sd_bus_vtable manager_vtable[] = { |
| 1543 |
SD_BUS_VTABLE_START(0), |
| 1544 |
|
| 1545 |
SD_BUS_METHOD_WITH_NAMES("ImportTar", |
| 1546 |
"hsbb", |
| 1547 |
SD_BUS_PARAM(fd) |
| 1548 |
SD_BUS_PARAM(local_name) |
| 1549 |
SD_BUS_PARAM(force) |
| 1550 |
SD_BUS_PARAM(read_only), |
| 1551 |
"uo", |
| 1552 |
SD_BUS_PARAM(transfer_id) |
| 1553 |
SD_BUS_PARAM(transfer_path), |
| 1554 |
method_import_tar_or_raw, |
| 1555 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1556 |
SD_BUS_METHOD_WITH_NAMES("ImportTarEx", |
| 1557 |
"hsst", |
| 1558 |
SD_BUS_PARAM(fd) |
| 1559 |
SD_BUS_PARAM(local_name) |
| 1560 |
SD_BUS_PARAM(class) |
| 1561 |
SD_BUS_PARAM(flags), |
| 1562 |
"uo", |
| 1563 |
SD_BUS_PARAM(transfer_id) |
| 1564 |
SD_BUS_PARAM(transfer_path), |
| 1565 |
method_import_tar_or_raw, |
| 1566 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1567 |
SD_BUS_METHOD_WITH_NAMES("ImportRaw", |
| 1568 |
"hsbb", |
| 1569 |
SD_BUS_PARAM(fd) |
| 1570 |
SD_BUS_PARAM(local_name) |
| 1571 |
SD_BUS_PARAM(force) |
| 1572 |
SD_BUS_PARAM(read_only), |
| 1573 |
"uo", |
| 1574 |
SD_BUS_PARAM(transfer_id) |
| 1575 |
SD_BUS_PARAM(transfer_path), |
| 1576 |
method_import_tar_or_raw, |
| 1577 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1578 |
SD_BUS_METHOD_WITH_NAMES("ImportRawEx", |
| 1579 |
"hsst", |
| 1580 |
SD_BUS_PARAM(fd) |
| 1581 |
SD_BUS_PARAM(local_name) |
| 1582 |
SD_BUS_PARAM(class) |
| 1583 |
SD_BUS_PARAM(flags), |
| 1584 |
"uo", |
| 1585 |
SD_BUS_PARAM(transfer_id) |
| 1586 |
SD_BUS_PARAM(transfer_path), |
| 1587 |
method_import_tar_or_raw, |
| 1588 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1589 |
SD_BUS_METHOD_WITH_NAMES("ImportFileSystem", |
| 1590 |
"hsbb", |
| 1591 |
SD_BUS_PARAM(fd) |
| 1592 |
SD_BUS_PARAM(local_name) |
| 1593 |
SD_BUS_PARAM(force) |
| 1594 |
SD_BUS_PARAM(read_only), |
| 1595 |
"uo", |
| 1596 |
SD_BUS_PARAM(transfer_id) |
| 1597 |
SD_BUS_PARAM(transfer_path), |
| 1598 |
method_import_fs, |
| 1599 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1600 |
SD_BUS_METHOD_WITH_NAMES("ImportFileSystemEx", |
| 1601 |
"hsst", |
| 1602 |
SD_BUS_PARAM(fd) |
| 1603 |
SD_BUS_PARAM(local_name) |
| 1604 |
SD_BUS_PARAM(class) |
| 1605 |
SD_BUS_PARAM(flags), |
| 1606 |
"uo", |
| 1607 |
SD_BUS_PARAM(transfer_id) |
| 1608 |
SD_BUS_PARAM(transfer_path), |
| 1609 |
method_import_fs, |
| 1610 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1611 |
SD_BUS_METHOD_WITH_NAMES("ExportTar", |
| 1612 |
"shs", |
| 1613 |
SD_BUS_PARAM(local_name) |
| 1614 |
SD_BUS_PARAM(fd) |
| 1615 |
SD_BUS_PARAM(format), |
| 1616 |
"uo", |
| 1617 |
SD_BUS_PARAM(transfer_id) |
| 1618 |
SD_BUS_PARAM(transfer_path), |
| 1619 |
method_export_tar_or_raw, |
| 1620 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1621 |
SD_BUS_METHOD_WITH_NAMES("ExportTarEx", |
| 1622 |
"sshst", |
| 1623 |
SD_BUS_PARAM(local_name) |
| 1624 |
SD_BUS_PARAM(class) |
| 1625 |
SD_BUS_PARAM(fd) |
| 1626 |
SD_BUS_PARAM(format) |
| 1627 |
SD_BUS_PARAM(flags), |
| 1628 |
"uo", |
| 1629 |
SD_BUS_PARAM(transfer_id) |
| 1630 |
SD_BUS_PARAM(transfer_path), |
| 1631 |
method_export_tar_or_raw, |
| 1632 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1633 |
SD_BUS_METHOD_WITH_NAMES("ExportRaw", |
| 1634 |
"shs", |
| 1635 |
SD_BUS_PARAM(local_name) |
| 1636 |
SD_BUS_PARAM(fd) |
| 1637 |
SD_BUS_PARAM(format), |
| 1638 |
"uo", |
| 1639 |
SD_BUS_PARAM(transfer_id) |
| 1640 |
SD_BUS_PARAM(transfer_path), |
| 1641 |
method_export_tar_or_raw, |
| 1642 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1643 |
SD_BUS_METHOD_WITH_NAMES("ExportRawEx", |
| 1644 |
"sshst", |
| 1645 |
SD_BUS_PARAM(local_name) |
| 1646 |
SD_BUS_PARAM(class) |
| 1647 |
SD_BUS_PARAM(fd) |
| 1648 |
SD_BUS_PARAM(format) |
| 1649 |
SD_BUS_PARAM(flags), |
| 1650 |
"uo", |
| 1651 |
SD_BUS_PARAM(transfer_id) |
| 1652 |
SD_BUS_PARAM(transfer_path), |
| 1653 |
method_export_tar_or_raw, |
| 1654 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1655 |
SD_BUS_METHOD_WITH_ARGS("PullTar", |
| 1656 |
SD_BUS_ARGS("s", url, |
| 1657 |
"s", local_name, |
| 1658 |
"s", verify_mode, |
| 1659 |
"b", force), |
| 1660 |
SD_BUS_RESULT("u", transfer_id, |
| 1661 |
"o", transfer_path), |
| 1662 |
method_pull_tar_or_raw_or_oci, |
| 1663 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1664 |
SD_BUS_METHOD_WITH_ARGS("PullTarEx", |
| 1665 |
SD_BUS_ARGS("s", url, |
| 1666 |
"s", local_name, |
| 1667 |
"s", class, |
| 1668 |
"s", verify_mode, |
| 1669 |
"t", flags), |
| 1670 |
SD_BUS_RESULT("u", transfer_id, |
| 1671 |
"o", transfer_path), |
| 1672 |
method_pull_tar_or_raw_or_oci, |
| 1673 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1674 |
SD_BUS_METHOD_WITH_ARGS("PullRaw", |
| 1675 |
SD_BUS_ARGS("s", url, |
| 1676 |
"s", local_name, |
| 1677 |
"s", verify_mode, |
| 1678 |
"b", force), |
| 1679 |
SD_BUS_RESULT("u", transfer_id, |
| 1680 |
"o", transfer_path), |
| 1681 |
method_pull_tar_or_raw_or_oci, |
| 1682 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1683 |
SD_BUS_METHOD_WITH_ARGS("PullRawEx", |
| 1684 |
SD_BUS_ARGS("s", url, |
| 1685 |
"s", local_name, |
| 1686 |
"s", class, |
| 1687 |
"s", verify_mode, |
| 1688 |
"t", flags), |
| 1689 |
SD_BUS_RESULT("u", transfer_id, |
| 1690 |
"o", transfer_path), |
| 1691 |
method_pull_tar_or_raw_or_oci, |
| 1692 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1693 |
SD_BUS_METHOD_WITH_ARGS("PullOci", |
| 1694 |
SD_BUS_ARGS("s", ref, |
| 1695 |
"s", local_name, |
| 1696 |
"s", class, |
| 1697 |
"t", flags), |
| 1698 |
SD_BUS_RESULT("u", transfer_id, |
| 1699 |
"o", transfer_path), |
| 1700 |
method_pull_tar_or_raw_or_oci, |
| 1701 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1702 |
SD_BUS_METHOD_WITH_NAMES("ListTransfers", |
| 1703 |
NULL,, |
| 1704 |
"a(usssdo)", |
| 1705 |
SD_BUS_PARAM(transfers), |
| 1706 |
method_list_transfers, |
| 1707 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1708 |
SD_BUS_METHOD_WITH_NAMES("ListTransfersEx", |
| 1709 |
"st", |
| 1710 |
SD_BUS_PARAM(class) |
| 1711 |
SD_BUS_PARAM(flags), |
| 1712 |
"a(ussssdo)", |
| 1713 |
SD_BUS_PARAM(transfers), |
| 1714 |
method_list_transfers, |
| 1715 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1716 |
SD_BUS_METHOD_WITH_NAMES("CancelTransfer", |
| 1717 |
"u", |
| 1718 |
SD_BUS_PARAM(transfer_id), |
| 1719 |
NULL,, |
| 1720 |
method_cancel_transfer, |
| 1721 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1722 |
SD_BUS_METHOD_WITH_NAMES("ListImages", |
| 1723 |
"st", |
| 1724 |
SD_BUS_PARAM(class) |
| 1725 |
SD_BUS_PARAM(flags), |
| 1726 |
"a(ssssbtttttt)", |
| 1727 |
SD_BUS_PARAM(images), |
| 1728 |
method_list_images, |
| 1729 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1730 |
|
| 1731 |
SD_BUS_SIGNAL_WITH_NAMES("TransferNew", |
| 1732 |
"uo", |
| 1733 |
SD_BUS_PARAM(transfer_id) |
| 1734 |
SD_BUS_PARAM(transfer_path), |
| 1735 |
0), |
| 1736 |
SD_BUS_SIGNAL_WITH_NAMES("TransferRemoved", |
| 1737 |
"uos", |
| 1738 |
SD_BUS_PARAM(transfer_id) |
| 1739 |
SD_BUS_PARAM(transfer_path) |
| 1740 |
SD_BUS_PARAM(result), |
| 1741 |
0), |
| 1742 |
|
| 1743 |
SD_BUS_VTABLE_END, |
| 1744 |
}; |
| 1745 |
|
| 1746 |
static const BusObjectImplementation manager_object = { |
| 1747 |
"/org/freedesktop/import1", |
| 1748 |
"org.freedesktop.import1.Manager", |
| 1749 |
.vtables = BUS_VTABLES(manager_vtable), |
| 1750 |
.children = BUS_IMPLEMENTATIONS(&transfer_object), |
| 1751 |
}; |
| 1752 |
|
| 1753 |
static int manager_connect_bus(Manager *m) { |
| 1754 |
int r; |
| 1755 |
|
| 1756 |
assert(m); |
| 1757 |
assert(m->event); |
| 1758 |
assert(!m->system_bus); |
| 1759 |
assert(!m->api_bus); |
| 1760 |
|
| 1761 |
r = bus_open_system_watch_bind(&m->system_bus); |
| 1762 |
if (r < 0) |
| 1763 |
return log_error_errno(r, "Failed to get system bus connection: %m"); |
| 1764 |
|
| 1765 |
r = sd_bus_attach_event(m->system_bus, m->event, 0); |
| 1766 |
if (r < 0) |
| 1767 |
return log_error_errno(r, "Failed to attach system bus to event loop: %m"); |
| 1768 |
|
| 1769 |
if (m->runtime_scope == RUNTIME_SCOPE_SYSTEM) |
| 1770 |
m->api_bus = sd_bus_ref(m->system_bus); |
| 1771 |
else { |
| 1772 |
assert(m->runtime_scope == RUNTIME_SCOPE_USER); |
| 1773 |
|
| 1774 |
r = sd_bus_default_user(&m->api_bus); |
| 1775 |
if (r < 0) |
| 1776 |
return log_error_errno(r, "Failed to get user bus connection: %m"); |
| 1777 |
|
| 1778 |
r = sd_bus_attach_event(m->api_bus, m->event, 0); |
| 1779 |
if (r < 0) |
| 1780 |
return log_error_errno(r, "Failed to attach user bus to event loop: %m"); |
| 1781 |
} |
| 1782 |
|
| 1783 |
r = bus_add_implementation(m->api_bus, &manager_object, m); |
| 1784 |
if (r < 0) |
| 1785 |
return r; |
| 1786 |
|
| 1787 |
r = bus_log_control_api_register(m->api_bus); |
| 1788 |
if (r < 0) |
| 1789 |
return r; |
| 1790 |
|
| 1791 |
r = sd_bus_request_name_async(m->api_bus, NULL, "org.freedesktop.import1", 0, NULL, NULL); |
| 1792 |
if (r < 0) |
| 1793 |
return log_error_errno(r, "Failed to request name: %m"); |
| 1794 |
|
| 1795 |
return 0; |
| 1796 |
} |
| 1797 |
|
| 1798 |
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_image_class, ImageClass, image_class_from_string); |
| 1799 |
|
| 1800 |
static int make_transfer_json(Transfer *t, sd_json_variant **ret) { |
| 1801 |
int r; |
| 1802 |
|
| 1803 |
assert(t); |
| 1804 |
|
| 1805 |
r = sd_json_buildo(ret, |
| 1806 |
SD_JSON_BUILD_PAIR_UNSIGNED("id", t->id), |
| 1807 |
JSON_BUILD_PAIR_ENUM("type", transfer_type_to_string(t->type)), |
| 1808 |
SD_JSON_BUILD_PAIR_STRING("remote", t->remote), |
| 1809 |
SD_JSON_BUILD_PAIR_STRING("local", t->local), |
| 1810 |
JSON_BUILD_PAIR_ENUM("class", image_class_to_string(t->class)), |
| 1811 |
SD_JSON_BUILD_PAIR_REAL("percent", transfer_percent_as_double(t))); |
| 1812 |
if (r < 0) |
| 1813 |
return log_error_errno(r, "Failed to build transfer JSON data: %m"); |
| 1814 |
|
| 1815 |
return 0; |
| 1816 |
} |
| 1817 |
|
| 1818 |
static int vl_method_list_transfers(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 1819 |
|
| 1820 |
struct p { |
| 1821 |
ImageClass class; |
| 1822 |
} p = { |
| 1823 |
.class = _IMAGE_CLASS_INVALID, |
| 1824 |
}; |
| 1825 |
|
| 1826 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 1827 |
{ "class", SD_JSON_VARIANT_STRING, json_dispatch_image_class, offsetof(struct p, class), 0 }, |
| 1828 |
{}, |
| 1829 |
}; |
| 1830 |
|
| 1831 |
Manager *m = ASSERT_PTR(userdata); |
| 1832 |
int r; |
| 1833 |
|
| 1834 |
assert(link); |
| 1835 |
assert(parameters); |
| 1836 |
assert(FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)); |
| 1837 |
|
| 1838 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 1839 |
if (r != 0) |
| 1840 |
return r; |
| 1841 |
|
| 1842 |
r = sd_varlink_set_sentinel(link, "io.systemd.Import.NoTransfers"); |
| 1843 |
if (r < 0) |
| 1844 |
return r; |
| 1845 |
|
| 1846 |
Transfer *t; |
| 1847 |
HASHMAP_FOREACH(t, m->transfers) { |
| 1848 |
if (p.class >= 0 && p.class != t->class) |
| 1849 |
continue; |
| 1850 |
|
| 1851 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 1852 |
r = make_transfer_json(t, &v); |
| 1853 |
if (r < 0) |
| 1854 |
return r; |
| 1855 |
|
| 1856 |
r = sd_varlink_reply(link, v); |
| 1857 |
if (r < 0) |
| 1858 |
return r; |
| 1859 |
} |
| 1860 |
|
| 1861 |
return 0; |
| 1862 |
} |
| 1863 |
|
| 1864 |
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_import_verify, ImportVerify, import_verify_from_string); |
| 1865 |
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_import_type, ImportType, import_type_from_string); |
| 1866 |
|
| 1867 |
static int vl_method_pull(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 1868 |
|
| 1869 |
struct p { |
| 1870 |
const char *remote, *local; |
| 1871 |
ImageClass class; |
| 1872 |
ImportType type; |
| 1873 |
ImportVerify verify; |
| 1874 |
bool force; |
| 1875 |
bool read_only; |
| 1876 |
bool keep_download; |
| 1877 |
const char *image_root; |
| 1878 |
} p = { |
| 1879 |
.class = _IMAGE_CLASS_INVALID, |
| 1880 |
.verify = IMPORT_VERIFY_SIGNATURE, |
| 1881 |
}; |
| 1882 |
|
| 1883 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 1884 |
{ "remote", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct p, remote), SD_JSON_MANDATORY }, |
| 1885 |
{ "local", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct p, local), 0 }, |
| 1886 |
{ "class", SD_JSON_VARIANT_STRING, json_dispatch_image_class, offsetof(struct p, class), SD_JSON_MANDATORY }, |
| 1887 |
{ "type", SD_JSON_VARIANT_STRING, json_dispatch_import_type, offsetof(struct p, type), SD_JSON_MANDATORY }, |
| 1888 |
{ "verify", SD_JSON_VARIANT_STRING, json_dispatch_import_verify, offsetof(struct p, verify), SD_JSON_STRICT }, |
| 1889 |
{ "force", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct p, force), 0 }, |
| 1890 |
{ "readOnly", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct p, read_only), 0 }, |
| 1891 |
{ "keepDownload", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct p, keep_download), 0 }, |
| 1892 |
{ "imageRoot", SD_JSON_VARIANT_STRING, json_dispatch_const_path, offsetof(struct p, image_root), SD_JSON_STRICT }, |
| 1893 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 1894 |
{}, |
| 1895 |
}; |
| 1896 |
|
| 1897 |
Manager *m = ASSERT_PTR(userdata); |
| 1898 |
int r; |
| 1899 |
|
| 1900 |
assert(link); |
| 1901 |
assert(parameters); |
| 1902 |
|
| 1903 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 1904 |
if (r != 0) |
| 1905 |
return r; |
| 1906 |
|
| 1907 |
if (p.type == IMPORT_OCI) { |
| 1908 |
r = oci_ref_valid(p.remote); |
| 1909 |
if (r < 0) |
| 1910 |
return r; |
| 1911 |
if (r == 0) |
| 1912 |
return sd_varlink_error_invalid_parameter_name(link, "remote"); |
| 1913 |
} else if (!http_url_is_valid(p.remote) && !file_url_is_valid(p.remote)) |
| 1914 |
return sd_varlink_error_invalid_parameter_name(link, "remote"); |
| 1915 |
|
| 1916 |
if (p.local && !image_name_is_valid(p.local)) |
| 1917 |
return sd_varlink_error_invalid_parameter_name(link, "local"); |
| 1918 |
|
| 1919 |
uint64_t transfer_flags = (p.force * IMPORT_FORCE) | (p.read_only * IMPORT_READ_ONLY) | (p.keep_download * IMPORT_PULL_KEEP_DOWNLOAD); |
| 1920 |
|
| 1921 |
TransferType tt = |
| 1922 |
p.type == IMPORT_TAR ? TRANSFER_PULL_TAR : |
| 1923 |
p.type == IMPORT_RAW ? TRANSFER_PULL_RAW : |
| 1924 |
p.type == IMPORT_OCI ? TRANSFER_PULL_OCI : _TRANSFER_TYPE_INVALID; |
| 1925 |
assert(tt >= 0); |
| 1926 |
|
| 1927 |
if (manager_find(m, tt, p.remote)) |
| 1928 |
return sd_varlink_errorbo(link, "io.systemd.Import.AlreadyInProgress", SD_JSON_BUILD_PAIR_STRING("remote", p.remote)); |
| 1929 |
|
| 1930 |
if (m->runtime_scope != RUNTIME_SCOPE_USER) { |
| 1931 |
r = varlink_verify_polkit_async( |
| 1932 |
link, |
| 1933 |
m->system_bus, |
| 1934 |
"org.freedesktop.import1.pull", |
| 1935 |
(const char**) STRV_MAKE( |
| 1936 |
"remote", p.remote, |
| 1937 |
"local", p.local, |
| 1938 |
"class", image_class_to_string(p.class), |
| 1939 |
"type", import_type_to_string(p.type), |
| 1940 |
"verify", import_verify_to_string(p.verify)), |
| 1941 |
&m->polkit_registry); |
| 1942 |
if (r <= 0) |
| 1943 |
return r; |
| 1944 |
} |
| 1945 |
|
| 1946 |
_cleanup_(transfer_unrefp) Transfer *t = NULL; |
| 1947 |
|
| 1948 |
r = transfer_new(m, &t); |
| 1949 |
if (r < 0) |
| 1950 |
return r; |
| 1951 |
|
| 1952 |
t->type = tt; |
| 1953 |
t->verify = p.verify; |
| 1954 |
t->flags = transfer_flags; |
| 1955 |
t->class = p.class; |
| 1956 |
|
| 1957 |
t->remote = strdup(p.remote); |
| 1958 |
if (!t->remote) |
| 1959 |
return -ENOMEM; |
| 1960 |
|
| 1961 |
if (p.local) { |
| 1962 |
t->local = strdup(p.local); |
| 1963 |
if (!t->local) |
| 1964 |
return -ENOMEM; |
| 1965 |
} |
| 1966 |
|
| 1967 |
if (p.image_root) { |
| 1968 |
t->image_root = strdup(p.image_root); |
| 1969 |
if (!t->image_root) |
| 1970 |
return -ENOMEM; |
| 1971 |
} |
| 1972 |
|
| 1973 |
r = transfer_start(t); |
| 1974 |
if (r < 0) |
| 1975 |
return r; |
| 1976 |
|
| 1977 |
/* If more was not set, just return the download id, and be done with it */ |
| 1978 |
if (!FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)) |
| 1979 |
return sd_varlink_replybo(link, SD_JSON_BUILD_PAIR_UNSIGNED("id", t->id)); |
| 1980 |
|
| 1981 |
/* Otherwise add this connection to the set of subscriptions, return the id, but keep the thing running */ |
| 1982 |
r = set_ensure_put(&t->varlink_subscribed, &varlink_hash_ops, link); |
| 1983 |
if (r < 0) |
| 1984 |
return r; |
| 1985 |
|
| 1986 |
sd_varlink_ref(link); |
| 1987 |
|
| 1988 |
r = sd_varlink_notifybo(link, SD_JSON_BUILD_PAIR_UNSIGNED("id", t->id)); |
| 1989 |
if (r < 0) |
| 1990 |
return r; |
| 1991 |
|
| 1992 |
TAKE_PTR(t); |
| 1993 |
return 0; |
| 1994 |
} |
| 1995 |
|
| 1996 |
static int manager_connect_varlink(Manager *m) { |
| 1997 |
int r; |
| 1998 |
|
| 1999 |
assert(m); |
| 2000 |
assert(m->event); |
| 2001 |
assert(!m->varlink_server); |
| 2002 |
|
| 2003 |
r = varlink_server_new( |
| 2004 |
&m->varlink_server, |
| 2005 |
(m->runtime_scope != RUNTIME_SCOPE_USER ? SD_VARLINK_SERVER_ACCOUNT_UID : 0)| |
| 2006 |
SD_VARLINK_SERVER_INHERIT_USERDATA, |
| 2007 |
m); |
| 2008 |
if (r < 0) |
| 2009 |
return log_error_errno(r, "Failed to allocate varlink server object: %m"); |
| 2010 |
|
| 2011 |
r = sd_varlink_server_add_interface_many( |
| 2012 |
m->varlink_server, |
| 2013 |
&vl_interface_io_systemd_Import, |
| 2014 |
&vl_interface_io_systemd_service); |
| 2015 |
if (r < 0) |
| 2016 |
return log_error_errno(r, "Failed to add Import interface to varlink server: %m"); |
| 2017 |
|
| 2018 |
r = sd_varlink_server_bind_method_many( |
| 2019 |
m->varlink_server, |
| 2020 |
"io.systemd.Import.ListTransfers", vl_method_list_transfers, |
| 2021 |
"io.systemd.Import.Pull", vl_method_pull, |
| 2022 |
"io.systemd.service.Ping", varlink_method_ping, |
| 2023 |
"io.systemd.service.SetLogLevel", varlink_method_set_log_level, |
| 2024 |
"io.systemd.service.GetEnvironment", varlink_method_get_environment); |
| 2025 |
if (r < 0) |
| 2026 |
return log_error_errno(r, "Failed to bind Varlink method calls: %m"); |
| 2027 |
|
| 2028 |
r = sd_varlink_server_attach_event(m->varlink_server, m->event, SD_EVENT_PRIORITY_NORMAL); |
| 2029 |
if (r < 0) |
| 2030 |
return log_error_errno(r, "Failed to attach Varlink server to event loop: %m"); |
| 2031 |
|
| 2032 |
r = sd_varlink_server_listen_auto(m->varlink_server); |
| 2033 |
if (r < 0) |
| 2034 |
return log_error_errno(r, "Failed to bind to passed Varlink sockets: %m"); |
| 2035 |
if (r == 0) { |
| 2036 |
_cleanup_free_ char *socket_path = NULL; |
| 2037 |
r = runtime_directory_generic(m->runtime_scope, "systemd/io.systemd.Import", &socket_path); |
| 2038 |
if (r < 0) |
| 2039 |
return log_error_errno(r, "Failed to determine socket path: %m"); |
| 2040 |
|
| 2041 |
r = sd_varlink_server_listen_address(m->varlink_server, socket_path, runtime_scope_to_socket_mode(m->runtime_scope) | SD_VARLINK_SERVER_MODE_MKDIR_0755); |
| 2042 |
if (r < 0) |
| 2043 |
return log_error_errno(r, "Failed to bind to Varlink socket: %m"); |
| 2044 |
} |
| 2045 |
|
| 2046 |
return 0; |
| 2047 |
} |
| 2048 |
|
| 2049 |
static bool manager_check_idle(void *userdata) { |
| 2050 |
Manager *m = ASSERT_PTR(userdata); |
| 2051 |
|
| 2052 |
return hashmap_isempty(m->transfers) && |
| 2053 |
hashmap_isempty(m->polkit_registry) && |
| 2054 |
sd_varlink_server_current_connections(m->varlink_server) == 0; |
| 2055 |
} |
| 2056 |
|
| 2057 |
static void manager_parse_env(Manager *m) { |
| 2058 |
int r; |
| 2059 |
|
| 2060 |
assert(m); |
| 2061 |
|
| 2062 |
/* Same as src/import/{import,pull}.c: |
| 2063 |
* Let's make these relatively low-level settings also controllable via env vars. User can then set |
| 2064 |
* them for systemd-importd.service if they like to tweak behaviour */ |
| 2065 |
|
| 2066 |
r = getenv_bool("SYSTEMD_IMPORT_BTRFS_SUBVOL"); |
| 2067 |
if (r >= 0) |
| 2068 |
m->use_btrfs_subvol = r; |
| 2069 |
else if (r != -ENXIO) |
| 2070 |
log_warning_errno(r, "Failed to parse $SYSTEMD_IMPORT_BTRFS_SUBVOL: %m"); |
| 2071 |
|
| 2072 |
r = getenv_bool("SYSTEMD_IMPORT_BTRFS_QUOTA"); |
| 2073 |
if (r >= 0) |
| 2074 |
m->use_btrfs_quota = r; |
| 2075 |
else if (r != -ENXIO) |
| 2076 |
log_warning_errno(r, "Failed to parse $SYSTEMD_IMPORT_BTRFS_QUOTA: %m"); |
| 2077 |
} |
| 2078 |
|
| 2079 |
static int run(int argc, char *argv[]) { |
| 2080 |
_cleanup_(manager_unrefp) Manager *m = NULL; |
| 2081 |
RuntimeScope scope = RUNTIME_SCOPE_SYSTEM; |
| 2082 |
int r; |
| 2083 |
|
| 2084 |
log_setup(); |
| 2085 |
|
| 2086 |
r = service_parse_argv("systemd-importd.service", |
| 2087 |
"VM and container image import and export service.", |
| 2088 |
BUS_IMPLEMENTATIONS(&manager_object, |
| 2089 |
&log_control_object), |
| 2090 |
&scope, |
| 2091 |
argc, argv); |
| 2092 |
if (r <= 0) |
| 2093 |
return r; |
| 2094 |
|
| 2095 |
umask(0022); |
| 2096 |
|
| 2097 |
r = manager_new(scope, &m); |
| 2098 |
if (r < 0) |
| 2099 |
return log_error_errno(r, "Failed to allocate manager object: %m"); |
| 2100 |
|
| 2101 |
manager_parse_env(m); |
| 2102 |
|
| 2103 |
r = manager_connect_bus(m); |
| 2104 |
if (r < 0) |
| 2105 |
return r; |
| 2106 |
|
| 2107 |
r = manager_connect_varlink(m); |
| 2108 |
if (r < 0) |
| 2109 |
return r; |
| 2110 |
|
| 2111 |
r = sd_notify(false, NOTIFY_READY_MESSAGE); |
| 2112 |
if (r < 0) |
| 2113 |
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m"); |
| 2114 |
|
| 2115 |
r = bus_event_loop_with_idle( |
| 2116 |
m->event, |
| 2117 |
m->api_bus, |
| 2118 |
"org.freedesktop.import1", |
| 2119 |
DEFAULT_EXIT_USEC, |
| 2120 |
manager_check_idle, |
| 2121 |
m); |
| 2122 |
if (r < 0) |
| 2123 |
return log_error_errno(r, "Failed to run event loop: %m"); |
| 2124 |
|
| 2125 |
return 0; |
| 2126 |
} |
| 2127 |
|
| 2128 |
DEFINE_MAIN_FUNCTION(run); |