]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: split read_mount_options helper out for reuse
authorLuca Boccassi <luca.boccassi@microsoft.com>
Thu, 21 Jan 2021 18:31:45 +0000 (18:31 +0000)
committerLuca Boccassi <luca.boccassi@microsoft.com>
Thu, 21 Jan 2021 18:31:45 +0000 (18:31 +0000)
src/core/dbus-execute.c
src/core/dbus-util.c
src/core/dbus-util.h

index 20403df8190877ce279ed7f7569252338321763d..8434ccb48e844244dea8829769683468946c1825 100644 (file)
@@ -1497,74 +1497,6 @@ static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(capability, "t", uint64_t, uint6
 static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(namespace_flag, "t", uint64_t, unsigned long, "%" PRIu64, namespace_flags_to_string);
 static BUS_DEFINE_SET_TRANSIENT_TO_STRING(mount_flags, "t", uint64_t, unsigned long, "%" PRIu64, mount_propagation_flags_to_string_with_check);
 
-/* ret_format_str is an accumulator, so if it has any pre-existing content, new options will be appended to it */
-static int read_mount_options(sd_bus_message *message, sd_bus_error *error, MountOptions **ret_options, char **ret_format_str, const char *separator) {
-        _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
-        _cleanup_free_ char *format_str = NULL;
-        const char *mount_options, *partition;
-        int r;
-
-        assert(message);
-        assert(ret_options);
-        assert(ret_format_str);
-        assert(separator);
-
-        r = sd_bus_message_enter_container(message, 'a', "(ss)");
-        if (r < 0)
-                return r;
-
-        while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) {
-                _cleanup_free_ char *previous = NULL, *escaped = NULL;
-                _cleanup_free_ MountOptions *o = NULL;
-                PartitionDesignator partition_designator;
-
-                if (chars_intersect(mount_options, WHITESPACE))
-                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
-                                                "Invalid mount options string, contains whitespace character(s): %s", mount_options);
-
-                partition_designator = partition_designator_from_string(partition);
-                if (partition_designator < 0)
-                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid partition name %s", partition);
-
-                /* Need to store them in the unit with the escapes, so that they can be parsed again */
-                escaped = shell_escape(mount_options, ":");
-                if (!escaped)
-                        return -ENOMEM;
-
-                previous = TAKE_PTR(format_str);
-                format_str = strjoin(previous, previous ? separator : "", partition, ":", escaped);
-                if (!format_str)
-                        return -ENOMEM;
-
-                o = new(MountOptions, 1);
-                if (!o)
-                        return -ENOMEM;
-                *o = (MountOptions) {
-                        .partition_designator = partition_designator,
-                        .options = strdup(mount_options),
-                };
-                if (!o->options)
-                        return -ENOMEM;
-                LIST_APPEND(mount_options, options, TAKE_PTR(o));
-        }
-        if (r < 0)
-                return r;
-
-        r = sd_bus_message_exit_container(message);
-        if (r < 0)
-                return r;
-
-        if (!LIST_IS_EMPTY(options)) {
-                char *final = strjoin(*ret_format_str, !isempty(*ret_format_str) ? separator : "", format_str);
-                if (!final)
-                        return -ENOMEM;
-                free_and_replace(*ret_format_str, final);
-                LIST_JOIN(mount_options, *ret_options, options);
-        }
-
-        return 0;
-}
-
 int bus_exec_context_set_transient_property(
                 Unit *u,
                 ExecContext *c,
@@ -1599,7 +1531,7 @@ int bus_exec_context_set_transient_property(
                 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
                 _cleanup_free_ char *format_str = NULL;
 
-                r = read_mount_options(message, error, &options, &format_str, " ");
+                r = bus_read_mount_options(message, error, &options, &format_str, " ");
                 if (r < 0)
                         return r;
 
@@ -3407,7 +3339,7 @@ int bus_exec_context_set_transient_property(
                                 return -ENOMEM;
                         free_and_replace(format_str, tuple);
 
-                        r = read_mount_options(message, error, &options, &format_str, ":");
+                        r = bus_read_mount_options(message, error, &options, &format_str, ":");
                         if (r < 0)
                                 return r;
 
index 2d22bc699a34893e2f5e5262fbf0962197fe32e2..6a6dd1ff41267c17139f7429ab9779664a73f568 100644 (file)
@@ -3,6 +3,7 @@
 #include "bus-polkit.h"
 #include "bus-util.h"
 #include "dbus-util.h"
+#include "escape.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "unit-printf.h"
@@ -186,3 +187,78 @@ int bus_verify_manage_units_async_full(
                         &u->manager->polkit_registry,
                         error);
 }
+
+/* ret_format_str is an accumulator, so if it has any pre-existing content, new options will be appended to it */
+int bus_read_mount_options(
+                sd_bus_message *message,
+                sd_bus_error *error,
+                MountOptions **ret_options,
+                char **ret_format_str,
+                const char *separator) {
+
+        _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
+        _cleanup_free_ char *format_str = NULL;
+        const char *mount_options, *partition;
+        int r;
+
+        assert(message);
+        assert(ret_options);
+        assert(separator);
+
+        r = sd_bus_message_enter_container(message, 'a', "(ss)");
+        if (r < 0)
+                return r;
+
+        while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) {
+                _cleanup_free_ char *previous = NULL, *escaped = NULL;
+                _cleanup_free_ MountOptions *o = NULL;
+                PartitionDesignator partition_designator;
+
+                if (chars_intersect(mount_options, WHITESPACE))
+                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
+                                                "Invalid mount options string, contains whitespace character(s): %s", mount_options);
+
+                partition_designator = partition_designator_from_string(partition);
+                if (partition_designator < 0)
+                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid partition name %s", partition);
+
+                /* Need to store the options with the escapes, so that they can be parsed again */
+                escaped = shell_escape(mount_options, ":");
+                if (!escaped)
+                        return -ENOMEM;
+
+                previous = TAKE_PTR(format_str);
+                format_str = strjoin(previous, previous ? separator : "", partition, ":", escaped);
+                if (!format_str)
+                        return -ENOMEM;
+
+                o = new(MountOptions, 1);
+                if (!o)
+                        return -ENOMEM;
+                *o = (MountOptions) {
+                        .partition_designator = partition_designator,
+                        .options = strdup(mount_options),
+                };
+                if (!o->options)
+                        return -ENOMEM;
+                LIST_APPEND(mount_options, options, TAKE_PTR(o));
+        }
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_exit_container(message);
+        if (r < 0)
+                return r;
+
+        if (!LIST_IS_EMPTY(options)) {
+                if (ret_format_str) {
+                        char *final = strjoin(*ret_format_str, !isempty(*ret_format_str) ? separator : "", format_str);
+                        if (!final)
+                                return -ENOMEM;
+                        free_and_replace(*ret_format_str, final);
+                }
+                LIST_JOIN(mount_options, *ret_options, options);
+        }
+
+        return 0;
+}
index e35c632d378434fa5e7ebc409dc1bc7967f2eea2..bd4fd081c5c878d065686bcf56e16870d5a7818a 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "sd-bus.h"
 
+#include "dissect-image.h"
 #include "unit.h"
 
 int bus_property_get_triggered_unit(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
@@ -249,3 +250,5 @@ static inline int bus_set_transient_usec_fix_0(Unit *u, const char *name, usec_t
         return bus_set_transient_usec_internal(u, name, p, true, message, flags, error);
 }
 int bus_verify_manage_units_async_full(Unit *u, const char *verb, int capability, const char *polkit_message, bool interactive, sd_bus_message *call, sd_bus_error *error);
+
+int bus_read_mount_options(sd_bus_message *message, sd_bus_error *error, MountOptions **ret_options, char **ret_format_str, const char *separator);