| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-json.h" |
| 6 |
#include "sd-varlink.h" |
| 7 |
|
| 8 |
#include "bus-polkit.h" |
| 9 |
#include "discover-image.h" |
| 10 |
#include "errno-util.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "fileio.h" |
| 13 |
#include "image.h" |
| 14 |
#include "image-varlink.h" |
| 15 |
#include "io-util.h" |
| 16 |
#include "json-util.h" |
| 17 |
#include "machined.h" |
| 18 |
#include "operation.h" |
| 19 |
#include "process-util.h" |
| 20 |
|
| 21 |
typedef struct ImageUpdateParameters { |
| 22 |
const char *name; |
| 23 |
const char *new_name; |
| 24 |
int read_only; |
| 25 |
uint64_t limit; |
| 26 |
} ImageUpdateParameters; |
| 27 |
|
| 28 |
#define IMAGE_UPDATE_PARAMETERS_NULL \ |
| 29 |
(ImageUpdateParameters) { \ |
| 30 |
.read_only = -1, \ |
| 31 |
.limit = UINT64_MAX, \ |
| 32 |
} |
| 33 |
|
| 34 |
int vl_method_update_image(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 35 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 36 |
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(ImageUpdateParameters, name), SD_JSON_MANDATORY }, |
| 37 |
{ "newName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(ImageUpdateParameters, new_name), 0 }, |
| 38 |
{ "readOnly", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(ImageUpdateParameters, read_only), 0 }, |
| 39 |
{ "limit", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(ImageUpdateParameters, limit), 0 }, |
| 40 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 41 |
{} |
| 42 |
}; |
| 43 |
|
| 44 |
Manager *manager = ASSERT_PTR(userdata); |
| 45 |
ImageUpdateParameters p = IMAGE_UPDATE_PARAMETERS_NULL; |
| 46 |
Image *image; |
| 47 |
int r, ret = 0; |
| 48 |
|
| 49 |
assert(link); |
| 50 |
assert(parameters); |
| 51 |
|
| 52 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 53 |
if (r != 0) |
| 54 |
return r; |
| 55 |
|
| 56 |
if (!image_name_is_valid(p.name)) |
| 57 |
return sd_varlink_error_invalid_parameter_name(link, "name"); |
| 58 |
|
| 59 |
if (p.new_name && !image_name_is_valid(p.new_name)) |
| 60 |
return sd_varlink_error_invalid_parameter_name(link, "newName"); |
| 61 |
|
| 62 |
r = manager_acquire_image(manager, p.name, &image); |
| 63 |
if (r == -ENOENT) |
| 64 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_IMAGE_NO_SUCH_IMAGE, NULL); |
| 65 |
if (r < 0) |
| 66 |
return r; |
| 67 |
|
| 68 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 69 |
r = varlink_verify_polkit_async( |
| 70 |
link, |
| 71 |
manager->system_bus, |
| 72 |
"org.freedesktop.machine1.manage-images", |
| 73 |
(const char**) STRV_MAKE("image", image->name, |
| 74 |
"verb", "update"), |
| 75 |
&manager->polkit_registry); |
| 76 |
if (r <= 0) |
| 77 |
return r; |
| 78 |
} |
| 79 |
|
| 80 |
if (p.new_name) { |
| 81 |
r = rename_image_and_update_cache(manager, image, p.new_name); |
| 82 |
if (r < 0) |
| 83 |
return log_debug_errno(r, "Failed to rename image: %m"); |
| 84 |
} |
| 85 |
|
| 86 |
if (p.read_only >= 0) { |
| 87 |
r = image_read_only(image, p.read_only, manager->runtime_scope); |
| 88 |
if (r < 0) |
| 89 |
RET_GATHER(ret, log_debug_errno(r, "Failed to toggle image read only, ignoring: %m")); |
| 90 |
} |
| 91 |
|
| 92 |
if (p.limit != UINT64_MAX) { |
| 93 |
r = image_set_limit(image, p.limit); |
| 94 |
if (r < 0) |
| 95 |
RET_GATHER(ret, log_debug_errno(r, "Failed to set image limit, ignoring: %m")); |
| 96 |
} |
| 97 |
|
| 98 |
/* We intentionally swallowed errors from image_read_only() and image_set_limit(). Here we return first one to the user if any */ |
| 99 |
if (ret < 0) |
| 100 |
return ret; |
| 101 |
|
| 102 |
return sd_varlink_reply(link, NULL); |
| 103 |
} |
| 104 |
|
| 105 |
int vl_method_clone_image(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 106 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 107 |
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(ImageUpdateParameters, name), SD_JSON_MANDATORY }, |
| 108 |
{ "newName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(ImageUpdateParameters, new_name), SD_JSON_MANDATORY }, |
| 109 |
{ "readOnly", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(ImageUpdateParameters, read_only), 0 }, |
| 110 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 111 |
{} |
| 112 |
}; |
| 113 |
|
| 114 |
Manager *manager = ASSERT_PTR(userdata); |
| 115 |
_cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR; |
| 116 |
ImageUpdateParameters p = IMAGE_UPDATE_PARAMETERS_NULL; |
| 117 |
Image *image; |
| 118 |
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL; |
| 119 |
int r; |
| 120 |
|
| 121 |
assert(link); |
| 122 |
assert(parameters); |
| 123 |
|
| 124 |
if (manager->n_operations >= OPERATIONS_MAX) |
| 125 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_IMAGE_TOO_MANY_OPERATIONS, NULL); |
| 126 |
|
| 127 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 128 |
if (r != 0) |
| 129 |
return r; |
| 130 |
|
| 131 |
if (!image_name_is_valid(p.name)) |
| 132 |
return sd_varlink_error_invalid_parameter_name(link, "name"); |
| 133 |
|
| 134 |
if (!image_name_is_valid(p.new_name)) |
| 135 |
return sd_varlink_error_invalid_parameter_name(link, "newName"); |
| 136 |
|
| 137 |
r = manager_acquire_image(manager, p.name, &image); |
| 138 |
if (r == -ENOENT) |
| 139 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_IMAGE_NO_SUCH_IMAGE, NULL); |
| 140 |
if (r < 0) |
| 141 |
return r; |
| 142 |
|
| 143 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 144 |
r = varlink_verify_polkit_async( |
| 145 |
link, |
| 146 |
manager->system_bus, |
| 147 |
"org.freedesktop.machine1.manage-images", |
| 148 |
(const char**) STRV_MAKE("image", image->name, |
| 149 |
"verb", "clone", |
| 150 |
"new_name", p.new_name), |
| 151 |
&manager->polkit_registry); |
| 152 |
if (r <= 0) |
| 153 |
return r; |
| 154 |
} |
| 155 |
|
| 156 |
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) |
| 157 |
return log_debug_errno(errno, "Failed to open pipe: %m"); |
| 158 |
|
| 159 |
r = pidref_safe_fork("(sd-imgclone)", FORK_RESET_SIGNALS, &child); |
| 160 |
if (r < 0) |
| 161 |
return log_debug_errno(r, "Failed to fork: %m"); |
| 162 |
if (r == 0) { |
| 163 |
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); |
| 164 |
r = image_clone(image, p.new_name, p.read_only > 0, manager->runtime_scope); |
| 165 |
report_errno_and_exit(errno_pipe_fd[1], r); |
| 166 |
} |
| 167 |
|
| 168 |
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); |
| 169 |
|
| 170 |
r = operation_new_with_varlink_reply(manager, /* machine= */ NULL, &child, link, errno_pipe_fd[0], /* ret= */ NULL); |
| 171 |
if (r < 0) |
| 172 |
return r; |
| 173 |
|
| 174 |
TAKE_PIDREF(child); |
| 175 |
TAKE_FD(errno_pipe_fd[0]); |
| 176 |
return 1; |
| 177 |
} |
| 178 |
|
| 179 |
int vl_method_remove_image(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 180 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 181 |
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, 0, SD_JSON_MANDATORY }, |
| 182 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 183 |
{} |
| 184 |
}; |
| 185 |
|
| 186 |
Manager *manager = ASSERT_PTR(userdata); |
| 187 |
_cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR; |
| 188 |
const char *image_name; |
| 189 |
Image *image; |
| 190 |
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL; |
| 191 |
int r; |
| 192 |
|
| 193 |
assert(link); |
| 194 |
assert(parameters); |
| 195 |
|
| 196 |
if (manager->n_operations >= OPERATIONS_MAX) |
| 197 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_IMAGE_TOO_MANY_OPERATIONS, NULL); |
| 198 |
|
| 199 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &image_name); |
| 200 |
if (r != 0) |
| 201 |
return r; |
| 202 |
|
| 203 |
if (!image_name_is_valid(image_name)) |
| 204 |
return sd_varlink_error_invalid_parameter_name(link, "name"); |
| 205 |
|
| 206 |
r = manager_acquire_image(manager, image_name, &image); |
| 207 |
if (r == -ENOENT) |
| 208 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_IMAGE_NO_SUCH_IMAGE, NULL); |
| 209 |
if (r < 0) |
| 210 |
return r; |
| 211 |
|
| 212 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 213 |
r = varlink_verify_polkit_async( |
| 214 |
link, |
| 215 |
manager->system_bus, |
| 216 |
"org.freedesktop.machine1.manage-images", |
| 217 |
(const char**) STRV_MAKE("image", image->name, |
| 218 |
"verb", "remove"), |
| 219 |
&manager->polkit_registry); |
| 220 |
if (r <= 0) |
| 221 |
return r; |
| 222 |
} |
| 223 |
|
| 224 |
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) |
| 225 |
return log_debug_errno(errno, "Failed to open pipe: %m"); |
| 226 |
|
| 227 |
r = pidref_safe_fork("(sd-imgrm)", FORK_RESET_SIGNALS, &child); |
| 228 |
if (r < 0) |
| 229 |
return log_debug_errno(r, "Failed to fork: %m"); |
| 230 |
if (r == 0) { |
| 231 |
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); |
| 232 |
r = image_remove(image, manager->runtime_scope); |
| 233 |
report_errno_and_exit(errno_pipe_fd[1], r); |
| 234 |
} |
| 235 |
|
| 236 |
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); |
| 237 |
|
| 238 |
r = operation_new_with_varlink_reply(manager, /* machine= */ NULL, &child, link, errno_pipe_fd[0], /* ret= */ NULL); |
| 239 |
if (r < 0) |
| 240 |
return r; |
| 241 |
|
| 242 |
TAKE_PIDREF(child); |
| 243 |
TAKE_FD(errno_pipe_fd[0]); |
| 244 |
return 1; |
| 245 |
} |
| 246 |
|
| 247 |
int vl_method_set_pool_limit(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 248 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 249 |
{ "limit", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, 0, SD_JSON_MANDATORY }, |
| 250 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 251 |
{} |
| 252 |
}; |
| 253 |
|
| 254 |
Manager *manager = ASSERT_PTR(userdata); |
| 255 |
uint64_t limit; |
| 256 |
int r; |
| 257 |
|
| 258 |
assert(link); |
| 259 |
assert(parameters); |
| 260 |
|
| 261 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &limit); |
| 262 |
if (r != 0) |
| 263 |
return r; |
| 264 |
|
| 265 |
if (!FILE_SIZE_VALID_OR_INFINITY(limit)) |
| 266 |
return sd_varlink_error_invalid_parameter_name(link, "limit"); |
| 267 |
|
| 268 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 269 |
r = varlink_verify_polkit_async( |
| 270 |
link, |
| 271 |
manager->system_bus, |
| 272 |
"org.freedesktop.machine1.manage-images", |
| 273 |
(const char**) STRV_MAKE("verb", "set_pool_limit"), |
| 274 |
&manager->polkit_registry); |
| 275 |
if (r <= 0) |
| 276 |
return r; |
| 277 |
} |
| 278 |
|
| 279 |
/* Set up the machine directory if necessary */ |
| 280 |
r = image_setup_pool( |
| 281 |
manager->runtime_scope, |
| 282 |
IMAGE_MACHINE, |
| 283 |
/* use_btrfs_subvol= */ true, |
| 284 |
/* use_btrfs_quota= */ true); |
| 285 |
if (r < 0) |
| 286 |
return r; |
| 287 |
|
| 288 |
r = image_set_pool_limit(manager->runtime_scope, IMAGE_MACHINE, limit); |
| 289 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 290 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_IMAGE_NOT_SUPPORTED, NULL); |
| 291 |
if (r < 0) |
| 292 |
return r; |
| 293 |
|
| 294 |
return sd_varlink_reply(link, NULL); |
| 295 |
} |
| 296 |
|
| 297 |
static int clean_pool_list_one_image(sd_varlink *link, const char *name, uint64_t usage_exclusive, bool more) { |
| 298 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 299 |
int r; |
| 300 |
|
| 301 |
assert(link); |
| 302 |
assert(name); |
| 303 |
|
| 304 |
r = sd_json_buildo( |
| 305 |
&v, |
| 306 |
SD_JSON_BUILD_PAIR_STRING("name", name), |
| 307 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("usageExclusive", usage_exclusive, UINT64_MAX)); |
| 308 |
if (r < 0) |
| 309 |
return r; |
| 310 |
|
| 311 |
if (more) |
| 312 |
return sd_varlink_notify(link, v); |
| 313 |
|
| 314 |
return sd_varlink_reply(link, v); |
| 315 |
} |
| 316 |
|
| 317 |
static int clean_pool_done_internal(Operation *operation, FILE *file, int child_error) { |
| 318 |
int r; |
| 319 |
|
| 320 |
assert(operation); |
| 321 |
assert(operation->link); |
| 322 |
|
| 323 |
r = clean_pool_read_first_entry(file, child_error, /* error= */ NULL); |
| 324 |
if (r < 0) |
| 325 |
return log_debug_errno(r, "Failed to read first entry from tmp file: %m"); |
| 326 |
|
| 327 |
/* On success the resulting temporary file will contain a list of image names that were removed followed by |
| 328 |
* their size on disk. Let's read that and turn it into a bus message. */ |
| 329 |
_cleanup_free_ char *previous_name = NULL; |
| 330 |
uint64_t previous_usage; |
| 331 |
for (;;) { |
| 332 |
_cleanup_free_ char *name = NULL; |
| 333 |
uint64_t usage; |
| 334 |
r = clean_pool_read_next_entry(file, &name, &usage); |
| 335 |
if (r < 0) |
| 336 |
return log_debug_errno(r, "Failed to read next entry from tmp file: %m"); |
| 337 |
if (r == 0) |
| 338 |
break; |
| 339 |
|
| 340 |
if (previous_name) { |
| 341 |
r = clean_pool_list_one_image(operation->link, previous_name, previous_usage, /* more= */ true); |
| 342 |
if (r < 0) |
| 343 |
return r; |
| 344 |
/* freeing memory to avoid memleak at the following assignment */ |
| 345 |
previous_name = mfree(previous_name); |
| 346 |
} |
| 347 |
|
| 348 |
previous_name = TAKE_PTR(name); |
| 349 |
previous_usage = usage; |
| 350 |
} |
| 351 |
|
| 352 |
if (previous_name) |
| 353 |
return clean_pool_list_one_image(operation->link, previous_name, previous_usage, /* more= */ false); |
| 354 |
|
| 355 |
return sd_varlink_error(operation->link, "io.systemd.MachineImage.NoSuchImage", NULL); |
| 356 |
} |
| 357 |
|
| 358 |
static int clean_pool_done(Operation *operation, int child_error, sd_bus_error *error) { |
| 359 |
_cleanup_fclose_ FILE *file = NULL; |
| 360 |
int r; |
| 361 |
|
| 362 |
assert(operation); |
| 363 |
assert(operation->link); |
| 364 |
assert(operation->extra_fd >= 0); |
| 365 |
|
| 366 |
file = take_fdopen(&operation->extra_fd, "r"); |
| 367 |
if (!file) |
| 368 |
return log_debug_errno(errno, "Failed to take opened tmp file's fd: %m"); |
| 369 |
|
| 370 |
r = clean_pool_done_internal(operation, file, child_error); |
| 371 |
if (r < 0) { |
| 372 |
r = sd_varlink_error_errno(operation->link, r); |
| 373 |
if (r < 0) |
| 374 |
log_debug_errno(r, "Failed to reply to varlink request, ignoring: %m"); |
| 375 |
} |
| 376 |
|
| 377 |
return 0; |
| 378 |
} |
| 379 |
|
| 380 |
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_image_clean_pool_mode, ImageCleanPoolMode, image_clean_pool_mode_from_string); |
| 381 |
|
| 382 |
int vl_method_clean_pool(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 383 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 384 |
{ "mode", SD_JSON_VARIANT_STRING, json_dispatch_image_clean_pool_mode, 0, SD_JSON_MANDATORY }, |
| 385 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 386 |
{} |
| 387 |
}; |
| 388 |
|
| 389 |
Manager *manager = ASSERT_PTR(userdata); |
| 390 |
ImageCleanPoolMode mode; |
| 391 |
int r; |
| 392 |
|
| 393 |
assert(link); |
| 394 |
assert(parameters); |
| 395 |
assert(FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)); |
| 396 |
|
| 397 |
if (manager->n_operations >= OPERATIONS_MAX) |
| 398 |
return sd_varlink_error(link, "io.systemd.MachineImage.TooManyOperations", NULL); |
| 399 |
|
| 400 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &mode); |
| 401 |
if (r != 0) |
| 402 |
return r; |
| 403 |
|
| 404 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 405 |
r = varlink_verify_polkit_async( |
| 406 |
link, |
| 407 |
manager->system_bus, |
| 408 |
"org.freedesktop.machine1.manage-images", |
| 409 |
(const char**) STRV_MAKE("mode", image_clean_pool_mode_to_string(mode), |
| 410 |
"verb", "clean_pool"), |
| 411 |
&manager->polkit_registry); |
| 412 |
if (r <= 0) |
| 413 |
return r; |
| 414 |
} |
| 415 |
|
| 416 |
Operation *op; |
| 417 |
r = image_clean_pool_operation(manager, mode, &op); |
| 418 |
if (r < 0) |
| 419 |
return log_debug_errno(r, "Failed to clean pool of images: %m"); |
| 420 |
|
| 421 |
operation_attach_varlink_reply(op, link); |
| 422 |
op->done = clean_pool_done; |
| 423 |
return 1; |
| 424 |
} |