diff options
author | Ivan Kruglov <mail@ikruglov.com> | 2024-10-21 13:53:38 +0200 |
---|---|---|
committer | Ivan Kruglov <mail@ikruglov.com> | 2024-11-01 15:30:34 +0100 |
commit | 38a0cf4172f472b24b2001e11412251664de33bb (patch) | |
tree | b3893204da337a453743eca04c93be3af6d4cae5 /src/machine | |
parent | machine: use report_errno_and_exit() in dbus code (diff) | |
download | systemd-38a0cf4172f472b24b2001e11412251664de33bb.tar.xz systemd-38a0cf4172f472b24b2001e11412251664de33bb.zip |
machine: introduce io.systemd.MachineImage.Clone method
Diffstat (limited to 'src/machine')
-rw-r--r-- | src/machine/image-varlink.c | 85 | ||||
-rw-r--r-- | src/machine/image-varlink.h | 1 | ||||
-rw-r--r-- | src/machine/machined-varlink.c | 3 |
3 files changed, 88 insertions, 1 deletions
diff --git a/src/machine/image-varlink.c b/src/machine/image-varlink.c index 2909e2a750..f2f745e655 100644 --- a/src/machine/image-varlink.c +++ b/src/machine/image-varlink.c @@ -4,10 +4,22 @@ #include "sd-varlink.h" #include "bus-polkit.h" +#include "fd-util.h" #include "image-varlink.h" #include "machine.h" #include "string-util.h" +typedef struct ImageUpdateParameters { + const char *name; + const char *new_name; + int read_only; +} ImageUpdateParameters; + +#define IMAGE_UPDATE_PARAMETERS_NULL \ + (ImageUpdateParameters) { \ + .read_only = -1, \ + } + int vl_method_update_image(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { struct params { const char *image_name; @@ -86,3 +98,76 @@ int vl_method_update_image(sd_varlink *link, sd_json_variant *parameters, sd_var return sd_varlink_reply(link, NULL); } + +int vl_method_clone_image(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { + static const sd_json_dispatch_field dispatch_table[] = { + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(ImageUpdateParameters, name), SD_JSON_MANDATORY }, + { "newName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(ImageUpdateParameters, new_name), SD_JSON_MANDATORY }, + { "readOnly", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(ImageUpdateParameters, read_only), 0 }, + VARLINK_DISPATCH_POLKIT_FIELD, + {} + }; + + Manager *manager = ASSERT_PTR(userdata); + _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR; + ImageUpdateParameters p = IMAGE_UPDATE_PARAMETERS_NULL; + Image *image; + pid_t child; + int r; + + assert(link); + assert(parameters); + + if (manager->n_operations >= OPERATIONS_MAX) + return sd_varlink_error(link, "io.systemd.MachineImage.TooManyOperations", NULL); + + r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); + if (r != 0) + return r; + + if (!image_name_is_valid(p.name)) + return sd_varlink_error_invalid_parameter_name(link, "name"); + + if (!image_name_is_valid(p.new_name)) + return sd_varlink_error_invalid_parameter_name(link, "newName"); + + r = manager_acquire_image(manager, p.name, &image); + if (r == -ENOENT) + return sd_varlink_error(link, "io.systemd.MachineImage.NoSuchImage", NULL); + if (r < 0) + return r; + + r = varlink_verify_polkit_async( + link, + manager->bus, + "org.freedesktop.machine1.manage-images", + (const char**) STRV_MAKE("image", image->name, + "verb", "clone", + "new_name", p.new_name), + &manager->polkit_registry); + if (r <= 0) + return r; + + if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) + return log_debug_errno(errno, "Failed to open pipe: %m"); + + r = safe_fork("(sd-imgclone)", FORK_RESET_SIGNALS, &child); + if (r < 0) + return log_debug_errno(r, "Failed to fork: %m"); + if (r == 0) { + errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); + r = image_clone(image, p.new_name, p.read_only > 0); + report_errno_and_exit(errno_pipe_fd[1], r); + } + + errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); + + r = operation_new_with_varlink_reply(manager, /* machine= */ NULL, child, link, errno_pipe_fd[0], /* ret= */ NULL); + if (r < 0) { + sigkill_wait(child); + return r; + } + + TAKE_FD(errno_pipe_fd[0]); + return 1; +} diff --git a/src/machine/image-varlink.h b/src/machine/image-varlink.h index 72028239de..e5532fa887 100644 --- a/src/machine/image-varlink.h +++ b/src/machine/image-varlink.h @@ -4,3 +4,4 @@ #include "sd-varlink.h" int vl_method_update_image(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata); +int vl_method_clone_image(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata); diff --git a/src/machine/machined-varlink.c b/src/machine/machined-varlink.c index 151d06e5f4..691248e674 100644 --- a/src/machine/machined-varlink.c +++ b/src/machine/machined-varlink.c @@ -774,7 +774,8 @@ static int manager_varlink_init_machine(Manager *m) { "io.systemd.Machine.Terminate", vl_method_terminate, "io.systemd.Machine.Kill", vl_method_kill, "io.systemd.MachineImage.List", vl_method_list_images, - "io.systemd.MachineImage.Update", vl_method_update_image); + "io.systemd.MachineImage.Update", vl_method_update_image, + "io.systemd.MachineImage.Clone", vl_method_clone_image); if (r < 0) return log_error_errno(r, "Failed to register varlink methods: %m"); |