chiark / gitweb /
util: unify how we parse mode_t strings
authorLennart Poettering <lennart@poettering.net>
Fri, 10 Apr 2015 12:43:06 +0000 (14:43 +0200)
committerSven Eden <yamakuzure@gmx.net>
Tue, 14 Mar 2017 06:58:15 +0000 (07:58 +0100)
src/shared/conf-parser.c
src/shared/util.c
src/shared/util.h

index 2148a30c66c1791e8dd233372a38d82b6d38736f..aa6a4a6395e0a1de5b00282c3e50fa5fb68affe9 100644 (file)
@@ -759,41 +759,30 @@ int config_parse_strv(const char *unit,
         return 0;
 }
 
         return 0;
 }
 
-int config_parse_mode(const char *unit,
-                      const char *filename,
-                      unsigned line,
-                      const char *section,
+int config_parse_mode(
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
                       unsigned section_line,
                       unsigned section_line,
-                      const char *lvalue,
-                      int ltype,
-                      const char *rvalue,
-                      void *data,
-                      void *userdata) {
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
 
         mode_t *m = data;
 
         mode_t *m = data;
-        long l;
-        char *x = NULL;
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
         assert(data);
 
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
         assert(data);
 
-        errno = 0;
-        l = strtol(rvalue, &x, 8);
-        if (!x || x == rvalue || *x || errno) {
-                log_syntax(unit, LOG_ERR, filename, line, errno,
-                           "Failed to parse mode value, ignoring: %s", rvalue);
-                return 0;
-        }
-
-        if (l < 0000 || l > 07777) {
-                log_syntax(unit, LOG_ERR, filename, line, ERANGE,
-                           "Mode value out of range, ignoring: %s", rvalue);
+        if (parse_mode(rvalue, m) < 0) {
+                log_syntax(unit, LOG_ERR, filename, line, errno, "Failed to parse mode value, ignoring: %s", rvalue);
                 return 0;
         }
 
                 return 0;
         }
 
-        *m = (mode_t) l;
         return 0;
 }
 
         return 0;
 }
 
index aea5364b585633f9eb08ffb78f0eb8541dd08532..6f0fb162deb137df474219ae41f89b322b2c165f 100644 (file)
@@ -8115,3 +8115,24 @@ char *shell_maybe_quote(const char *s) {
 
         return r;
 }
 
         return r;
 }
+
+int parse_mode(const char *s, mode_t *ret) {
+        char *x;
+        long l;
+
+        assert(s);
+        assert(ret);
+
+        errno = 0;
+        l = strtol(s, &x, 8);
+        if (errno != 0)
+                return -errno;
+
+        if (!x || x == s || *x)
+                return -EINVAL;
+        if (l < 0 || l  > 07777)
+                return -ERANGE;
+
+        *ret = (mode_t) l;
+        return 0;
+}
index 00e33f049258ab79fbdab21ba2607e6d825a9f5a..dc2611fa6df3c3001ad615362ea283f5f1baae5b 100644 (file)
@@ -1088,3 +1088,5 @@ void cmsg_close_all(struct msghdr *mh);
 int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
 
 char *shell_maybe_quote(const char *s);
 int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
 
 char *shell_maybe_quote(const char *s);
+
+int parse_mode(const char *s, mode_t *ret);