]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Modify mount_propagation_flags_from_string to return a normal int code
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 3 Dec 2016 18:57:42 +0000 (13:57 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 17 Dec 2016 18:57:04 +0000 (13:57 -0500)
This means that callers can distiguish an error from flags==0,
and don't have to special-case the empty string.

src/basic/mount-util.c
src/basic/mount-util.h
src/core/dbus-execute.c
src/core/load-fragment.c
src/shared/bus-unit-util.c
src/test/test-mount-util.c

index 8970050408f547e154fafd72f973123d9ecbcbdb..840e94a5535502f5783febda9f88a15f95e04997 100644 (file)
@@ -693,13 +693,12 @@ int umount_verbose(const char *what) {
 const char *mount_propagation_flags_to_string(unsigned long flags) {
 
         switch (flags & (MS_SHARED|MS_SLAVE|MS_PRIVATE)) {
-
+        case 0:
+                return "";
         case MS_SHARED:
                 return "shared";
-
         case MS_SLAVE:
                 return "slave";
-
         case MS_PRIVATE:
                 return "private";
         }
@@ -707,17 +706,18 @@ const char *mount_propagation_flags_to_string(unsigned long flags) {
         return NULL;
 }
 
-unsigned long mount_propagation_flags_from_string(const char *name) {
 
-        if (isempty(name))
-                return 0;
-
-        if (streq(name, "shared"))
-                return MS_SHARED;
-        if (streq(name, "slave"))
-                return MS_SLAVE;
-        if (streq(name, "private"))
-                return MS_PRIVATE;
+int mount_propagation_flags_from_string(const char *name, unsigned long *ret) {
 
+        if (isempty(name))
+                *ret = 0;
+        else if (streq(name, "shared"))
+                *ret = MS_SHARED;
+        else if (streq(name, "slave"))
+                *ret = MS_SLAVE;
+        else if (streq(name, "private"))
+                *ret = MS_PRIVATE;
+        else
+                return -EINVAL;
         return 0;
 }
index c8049198d448cd29751a1a4c0a5602f2b5f8302b..1615c94e9abe73c3cd4a65055df1cba2a586fafd 100644 (file)
@@ -63,4 +63,4 @@ int mount_verbose(
 int umount_verbose(const char *where);
 
 const char *mount_propagation_flags_to_string(unsigned long flags);
-unsigned long mount_propagation_flags_from_string(const char *name);
+int mount_propagation_flags_from_string(const char *name, unsigned long *ret);
index b3fc0ff5c339494f44552eed164956dcdd223613..9960b7a811383d0d2a334e5a0de64b68e41fdf8e 100644 (file)
@@ -1671,7 +1671,7 @@ int bus_exec_context_set_transient_property(
                 if (mode != UNIT_CHECK) {
                         c->mount_flags = flags;
 
-                        unit_write_drop_in_private_format(u, mode, name, "%s=%s", name, strempty(mount_propagation_flags_to_string(flags)));
+                        unit_write_drop_in_private_format(u, mode, name, "%s=%s", name, mount_propagation_flags_to_string(flags));
                 }
 
                 return 1;
index f325d853c65fb0301e4e3f0ca416050f6e3b470c..bc0cf73d39a44c781ef7254860b9e1139bdb1785 100644 (file)
@@ -1278,25 +1278,17 @@ int config_parse_exec_mount_flags(
                 void *userdata) {
 
 
-        unsigned long flags;
         ExecContext *c = data;
+        int r;
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
         assert(data);
 
-        if (isempty(rvalue))
-                flags = 0;
-        else {
-                flags = mount_propagation_flags_from_string(rvalue);
-                if (flags == 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse mount flag %s, ignoring.", rvalue);
-                        return 0;
-                }
-        }
-
-        c->mount_flags = flags;
+        r = mount_propagation_flags_from_string(rvalue, &c->mount_flags);
+        if (r < 0)
+                log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse mount flag %s, ignoring.", rvalue);
 
         return 0;
 }
index b030b3b9d18e270189705d97f3811ece7c94b3b3..829be2c6da020c49b3db980c52d428e8c01a3bb1 100644 (file)
@@ -579,15 +579,9 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
         else if (streq(field, "MountFlags")) {
                 unsigned long f;
 
-                if (isempty(eq))
-                        f = 0;
-                else {
-                        f = mount_propagation_flags_from_string(eq);
-                        if (f == 0) {
-                                log_error("Failed to parse mount propagation type: %s", eq);
-                                return -EINVAL;
-                        }
-                }
+                r = mount_propagation_flags_from_string(eq, &f);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to parse mount propagation flags: %s", eq);
 
                 r = sd_bus_message_append(m, "v", "t", f);
         } else if (STR_IN_SET(field, "BindPaths", "BindReadOnlyPaths")) {
index da7f35623b82936444b7ca7588b6bb573e13319e..7c5929d009987b3e9f086ff1429dcd5473457a9b 100644 (file)
 #include "mount-util.h"
 #include "string-util.h"
 
-static void test_mount_propagation_flags(const char *name, unsigned long f) {
-        assert(mount_propagation_flags_from_string(name) == f);
+static void test_mount_propagation_flags(const char *name, int ret, unsigned long expected) {
+        long unsigned flags;
 
-        if (f != 0)
-                assert_se(streq_ptr(mount_propagation_flags_to_string(f), name));
+        assert(mount_propagation_flags_from_string(name, &flags) == ret);
+
+        if (ret >= 0) {
+                const char *c;
+
+                assert_se(flags == expected);
+
+                c = mount_propagation_flags_to_string(flags);
+                if (isempty(name))
+                        assert_se(isempty(c));
+                else
+                        assert_se(streq(c, name));
+        }
 }
 
 int main(int argc, char *argv[]) {
 
         log_set_max_level(LOG_DEBUG);
 
-        test_mount_propagation_flags("shared", MS_SHARED);
-        test_mount_propagation_flags("slave", MS_SLAVE);
-        test_mount_propagation_flags("private", MS_PRIVATE);
-        test_mount_propagation_flags(NULL, 0);
-        test_mount_propagation_flags("", 0);
-        test_mount_propagation_flags("xxxx", 0);
+        test_mount_propagation_flags("shared", 0, MS_SHARED);
+        test_mount_propagation_flags("slave", 0, MS_SLAVE);
+        test_mount_propagation_flags("private", 0, MS_PRIVATE);
+        test_mount_propagation_flags(NULL, 0, 0);
+        test_mount_propagation_flags("", 0, 0);
+        test_mount_propagation_flags("xxxx", -EINVAL, 0);
+        test_mount_propagation_flags(" ", -EINVAL, 0);
 
         return 0;
 }