HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/core/dbus-execute.c

193,499 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 4d86c07a41a43be36f83303ea24907c57169829b

Lines 4001–4331 of 4,331

                                } else
                                        /* When the resource limit is not initialized yet, then assign the value to both fields */
                                        nl = (struct rlimit) {
                                                .rlim_cur = x,
                                                .rlim_max = x,
                                        };
 
                                r = rlimit_format(&nl, &f);
                                if (r < 0)
                                        return r;
 
                                if (c->rlimit[ri])
                                        *c->rlimit[ri] = nl;
                                else {
                                        c->rlimit[ri] = newdup(struct rlimit, &nl, 1);
                                        if (!c->rlimit[ri])
                                                return -ENOMEM;
                                }
 
                                unit_write_settingf(u, flags, name, "%s=%s", name, f);
                        }
 
                        return 1;
                }
 
        } else if (streq(name, "MountImages")) {
                _cleanup_free_ char *format_str = NULL;
                MountImage *mount_images = NULL;
                size_t n_mount_images = 0;
 
                CLEANUP_ARRAY(mount_images, n_mount_images, mount_image_free_array);
 
                r = sd_bus_message_enter_container(message, 'a', "(ssba(ss))");
                if (r < 0)
                        return r;
 
                for (;;) {
                        _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
                        _cleanup_free_ char *source_escaped = NULL, *destination_escaped = NULL;
                        char *source, *destination;
                        int permissive;
 
                        r = sd_bus_message_enter_container(message, 'r', "ssba(ss)");
                        if (r < 0)
                                return r;
 
                        r = sd_bus_message_read(message, "ssb", &source, &destination, &permissive);
                        if (r <= 0)
                                break;
 
                        if (!path_is_absolute(source))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not absolute.", source);
                        if (!path_is_normalized(source))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not normalized.", source);
                        if (!path_is_absolute(destination))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not absolute.", destination);
                        if (!path_is_normalized(destination))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not normalized.", destination);
 
                        /* Need to store them in the unit with the escapes, so that they can be parsed again */
                        source_escaped = shell_escape(source, ":");
                        if (!source_escaped)
                                return -ENOMEM;
                        destination_escaped = shell_escape(destination, ":");
                        if (!destination_escaped)
                                return -ENOMEM;
 
                        r = strextendf_with_separator(&format_str, " ", "%s%s:%s",
                                                      permissive ? "-" : "",
                                                      source_escaped,
                                                      destination_escaped);
                        if (r < 0)
                                return r;
 
                        r = bus_read_mount_options(message, reterr_error, &options, &format_str, ":");
                        if (r < 0)
                                return r;
 
                        r = sd_bus_message_exit_container(message);
                        if (r < 0)
                                return r;
 
                        r = mount_image_add(&mount_images, &n_mount_images,
                                            &(MountImage) {
                                                    .source = source,
                                                    .destination = destination,
                                                    .mount_options = options,
                                                    .ignore_enoent = permissive,
                                                    .type = MOUNT_IMAGE_DISCRETE,
                                            });
                        if (r < 0)
                                return r;
                }
                if (r < 0)
                        return r;
 
                r = sd_bus_message_exit_container(message);
                if (r < 0)
                        return r;
 
                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                        if (n_mount_images == 0) {
                                mount_image_free_array(c->mount_images, c->n_mount_images);
                                c->mount_images = NULL;
                                c->n_mount_images = 0;
 
                                unit_write_settingf(u, flags, name, "%s=", name);
                        } else {
                                if (!c->mount_images) {
                                        c->mount_images = TAKE_PTR(mount_images);
                                        c->n_mount_images = n_mount_images;
                                } else
                                        FOREACH_ARRAY(i, mount_images, n_mount_images) {
                                                r = mount_image_add(&c->mount_images, &c->n_mount_images, i);
                                                if (r < 0)
                                                        return r;
                                        }
 
                                unit_write_settingf(u, flags|UNIT_ESCAPE_C|UNIT_ESCAPE_SPECIFIERS,
                                                    name,
                                                    "%s=%s",
                                                    name,
                                                    format_str);
                        }
                }
 
                return 1;
 
        } else if (streq(name, "ExtensionImages")) {
                _cleanup_free_ char *format_str = NULL;
                MountImage *extension_images = NULL;
                size_t n_extension_images = 0;
 
                CLEANUP_ARRAY(extension_images, n_extension_images, mount_image_free_array);
 
                r = sd_bus_message_enter_container(message, 'a', "(sba(ss))");
                if (r < 0)
                        return r;
 
                for (;;) {
                        _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
                        _cleanup_free_ char *source_escaped = NULL;
                        char *source;
                        int permissive;
 
                        r = sd_bus_message_enter_container(message, 'r', "sba(ss)");
                        if (r < 0)
                                return r;
 
                        r = sd_bus_message_read(message, "sb", &source, &permissive);
                        if (r <= 0)
                                break;
 
                        if (!path_is_absolute(source))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not absolute.", source);
                        if (!path_is_normalized(source))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not normalized.", source);
 
                        /* Need to store them in the unit with the escapes, so that they can be parsed again */
                        source_escaped = shell_escape(source, ":");
                        if (!source_escaped)
                                return -ENOMEM;
 
                        r = strextendf_with_separator(&format_str, " ", "%s%s",
                                                      permissive ? "-" : "", source_escaped);
                        if (r < 0)
                                return r;
 
                        r = bus_read_mount_options(message, reterr_error, &options, &format_str, ":");
                        if (r < 0)
                                return r;
 
                        r = sd_bus_message_exit_container(message);
                        if (r < 0)
                                return r;
 
                        r = mount_image_add(&extension_images, &n_extension_images,
                                            &(MountImage) {
                                                    .source = source,
                                                    .mount_options = options,
                                                    .ignore_enoent = permissive,
                                                    .type = MOUNT_IMAGE_EXTENSION,
                                            });
                        if (r < 0)
                                return r;
                }
                if (r < 0)
                        return r;
 
                r = sd_bus_message_exit_container(message);
                if (r < 0)
                        return r;
 
                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                        if (n_extension_images == 0) {
                                mount_image_free_array(c->extension_images, c->n_extension_images);
                                c->extension_images = NULL;
                                c->n_extension_images = 0;
 
                                unit_write_settingf(u, flags, name, "%s=", name);
                        } else {
                                if (!c->extension_images) {
                                        c->extension_images = TAKE_PTR(extension_images);
                                        c->n_extension_images = n_extension_images;
                                } else
                                        FOREACH_ARRAY(i, extension_images, n_extension_images) {
                                                r = mount_image_add(&c->extension_images, &c->n_extension_images, i);
                                                if (r < 0)
                                                        return r;
                                        }
 
                                unit_write_settingf(u, flags|UNIT_ESCAPE_C|UNIT_ESCAPE_SPECIFIERS,
                                                    name,
                                                    "%s=%s",
                                                    name,
                                                    format_str);
                        }
                }
 
                return 1;
 
        } else if (STR_IN_SET(name, "StateDirectorySymlink", "RuntimeDirectorySymlink", "CacheDirectorySymlink", "LogsDirectorySymlink")) {
                char *source, *destination;
                ExecDirectory *directory;
                uint64_t symlink_flags;
                ExecDirectoryType i;
 
                assert_se((i = exec_directory_type_symlink_from_string(name)) >= 0);
                directory = c->directories + i;
 
                r = sd_bus_message_enter_container(message, 'a', "(sst)");
                if (r < 0)
                        return r;
 
                while ((r = sd_bus_message_read(message, "(sst)", &source, &destination, &symlink_flags)) > 0) {
                        if ((symlink_flags & ~_EXEC_DIRECTORY_FLAGS_PUBLIC) != 0)
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid 'flags' parameter '%" PRIu64 "'", symlink_flags);
                        if (!path_is_valid(source))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not valid.", source);
                        if (path_is_absolute(source))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is absolute.", source);
                        if (!path_is_normalized(source))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Source path %s is not normalized.", source);
                        if (isempty(destination))
                                destination = NULL;
                        else {
                                if (!path_is_valid(destination))
                                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not valid.", destination);
                                if (path_is_absolute(destination))
                                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is absolute.", destination);
                                if (!path_is_normalized(destination))
                                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Destination path %s is not normalized.", destination);
                        }
 
                        if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                                _cleanup_free_ char *destination_escaped = NULL, *source_escaped = NULL;
 
                                r = exec_directory_add(directory, source, destination, symlink_flags);
                                if (r < 0)
                                        return r;
 
                                /* Need to store them in the unit with the escapes, so that they can be parsed again */
                                source_escaped = xescape(source, ":");
                                if (!source_escaped)
                                        return -ENOMEM;
                                if (destination) {
                                        destination_escaped = xescape(destination, ":");
                                        if (!destination_escaped)
                                                return -ENOMEM;
                                }
 
                                unit_write_settingf(
                                                u, flags|UNIT_ESCAPE_SPECIFIERS, exec_directory_type_to_string(i),
                                                "%s=%s%s%s%s",
                                                exec_directory_type_to_string(i),
                                                source_escaped,
                                                destination_escaped || FLAGS_SET(symlink_flags, EXEC_DIRECTORY_READ_ONLY) ? ":" : "",
                                                destination_escaped,
                                                FLAGS_SET(symlink_flags, EXEC_DIRECTORY_READ_ONLY) ? ":ro" : "");
                        }
                }
                if (r < 0)
                        return r;
 
                exec_directory_sort(directory);
 
                r = sd_bus_message_exit_container(message);
                if (r < 0)
                        return r;
 
                return 1;
 
        } else if (STR_IN_SET(name, "RootImagePolicy", "MountImagePolicy", "ExtensionImagePolicy")) {
                _cleanup_(image_policy_freep) ImagePolicy *p = NULL;
                const char *s;
 
                r = sd_bus_message_read(message, "s", &s);
                if (r < 0)
                        return r;
 
                r = image_policy_from_string(s, /* graceful= */ true, &p);
                if (r < 0)
                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Failed to parse image policy string: %s", s);
 
                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                        _cleanup_free_ char *t = NULL;
                        ImagePolicy **pp =
                                streq(name, "RootImagePolicy")  ? &c->root_image_policy :
                                streq(name, "MountImagePolicy") ? &c->mount_image_policy :
                                                                  &c->extension_image_policy;
 
                        r = image_policy_to_string(p, /* simplify= */ true, &t);
                        if (r < 0)
                                return r;
 
                        image_policy_free(*pp);
                        *pp = TAKE_PTR(p);
 
                        unit_write_settingf(
                                        u, flags, name,
                                        "%s=%s",
                                        name,
                                        t); /* no escaping necessary */
                }
 
                return 1;
        }
 
        return 0;
}