diff options
author | Lennart Poettering <lennart@poettering.net> | 2015-04-10 14:43:06 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2015-04-10 16:23:46 +0200 |
commit | 2ff7b0a54271c8480024d6d68edff4a92e781052 (patch) | |
tree | 4883cb0ad40483553e06d10a209475f757f273c8 /src/shared | |
parent | 6dd67163b8f173649e7dc73740f35274714caafc (diff) |
util: unify how we parse mode_t strings
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/conf-parser.c | 35 | ||||
-rw-r--r-- | src/shared/util.c | 21 | ||||
-rw-r--r-- | src/shared/util.h | 2 |
3 files changed, 35 insertions, 23 deletions
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index 2148a30c66..aa6a4a6395 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -759,41 +759,30 @@ int config_parse_strv(const char *unit, 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, - 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; - long l; - char *x = NULL; 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; } - *m = (mode_t) l; return 0; } diff --git a/src/shared/util.c b/src/shared/util.c index 8372038d4d..df5f8d1088 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -8159,3 +8159,24 @@ char *shell_maybe_quote(const char *s) { 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; +} diff --git a/src/shared/util.h b/src/shared/util.h index 527867ce8b..56e11c7961 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -1089,3 +1089,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 parse_mode(const char *s, mode_t *ret); |