923a60
From 81a95ec724b7b874f850cb0f32f1981ccc4fb062 Mon Sep 17 00:00:00 2001
923a60
From: Karel Zak <kzak@redhat.com>
923a60
Date: Fri, 20 Nov 2015 12:54:10 +0100
923a60
Subject: [PATCH] core: support <soft:hard> ranges for RLIMIT options
923a60
923a60
The new parser supports:
923a60
923a60
 <value>       - specify both limits to the same value
923a60
 <soft:hard>   - specify both limits
923a60
923a60
the size or time specific suffixes are supported, for example
923a60
923a60
  LimitRTTIME=1sec
923a60
  LimitAS=4G:16G
923a60
923a60
The patch introduces parse_rlimit_range() and rlim type (size, sec,
923a60
usec, etc.) specific parsers. No code is duplicated now.
923a60
923a60
The patch also sync docs for DefaultLimitXXX= and LimitXXX=.
923a60
923a60
References: https://github.com/systemd/systemd/issues/1769
923a60
923a60
Cherry-picked from: 91518d20ddf0376808544576d0ef0883cedc67d4
923a60
Resolves: #1351415
923a60
---
923a60
 man/systemd-system.conf.xml |  27 ++-
923a60
 man/systemd.exec.xml        |   5 +-
923a60
 src/core/load-fragment.c    | 243 ++++++++++---------
923a60
 src/shared/util.c           | 467 ++++++++++++++++++++++++++++++++++++
923a60
 src/shared/util.h           |  14 ++
923a60
 src/test/test-unit-file.c   |  31 +++
923a60
 6 files changed, 667 insertions(+), 120 deletions(-)
923a60
923a60
diff --git a/man/systemd-system.conf.xml b/man/systemd-system.conf.xml
923a60
index b7d9cdee05..39d19bc71a 100644
923a60
--- a/man/systemd-system.conf.xml
923a60
+++ b/man/systemd-system.conf.xml
923a60
@@ -326,13 +326,26 @@
923a60
         <listitem><para>These settings control various default
923a60
         resource limits for units. See
923a60
         <citerefentry><refentrytitle>setrlimit</refentrytitle><manvolnum>2</manvolnum></citerefentry>
923a60
-        for details. Use the string <varname>infinity</varname> to
923a60
-        configure no limit on a specific resource. The multiplicative suffixes
923a60
-        K (=1024), M (=1024*1024) and so on for G, T, P and E may be used for
923a60
-        resource limits measured in bytes (e.g. DefaultLimitAS=16G). These
923a60
-        settings may be overridden in individual units using the corresponding
923a60
-        LimitXXX= directives. Note that these resource limits are only
923a60
-        defaults for units, they are not applied to PID 1
923a60
+        for details. The resource limit is possible to specify in two formats,
923a60
+        <option>value</option> to set soft and hard limits to the same value,
923a60
+        or <option>soft:hard</option> to set both limits individually (e.g. DefaultLimitAS=4G:16G).
923a60
+        Use the string <varname>infinity</varname> to
923a60
+        configure no limit on a specific resource. The multiplicative
923a60
+        suffixes K (=1024), M (=1024*1024) and so on for G, T, P and E
923a60
+        may be used for resource limits measured in bytes
923a60
+        (e.g. DefaultLimitAS=16G). For the limits referring to time values,
923a60
+        the usual time units ms, s, min, h and so on may be used (see
923a60
+        <citerefentry><refentrytitle>systemd.time</refentrytitle><manvolnum>7</manvolnum></citerefentry>
923a60
+        for details). Note that if no time unit is specified for
923a60
+        <varname>DefaultLimitCPU=</varname> the default unit of seconds is
923a60
+        implied, while for <varname>DefaultLimitRTTIME=</varname> the default
923a60
+        unit of microseconds is implied. Also, note that the effective
923a60
+        granularity of the limits might influence their
923a60
+        enforcement. For example, time limits specified for
923a60
+        <varname>DefaultLimitCPU=</varname> will be rounded up implicitly to
923a60
+        multiples of 1s. These  settings may be overridden in individual units
923a60
+        using the corresponding LimitXXX= directives. Note that these resource
923a60
+        limits are only defaults for units, they are not applied to PID 1
923a60
         itself.</para></listitem>
923a60
       </varlistentry>
923a60
     </variablelist>
923a60
diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml
923a60
index cfdcc3d173..0cd469cd98 100644
923a60
--- a/man/systemd.exec.xml
923a60
+++ b/man/systemd.exec.xml
923a60
@@ -558,7 +558,10 @@
923a60
         <listitem><para>These settings set both soft and hard limits
923a60
         of various resources for executed processes. See
923a60
         <citerefentry><refentrytitle>setrlimit</refentrytitle><manvolnum>2</manvolnum></citerefentry>
923a60
-        for details. Use the string <varname>infinity</varname> to
923a60
+        for details. The resource limit is possible to specify in two formats,
923a60
+        <option>value</option> to set soft and hard limits to the same value,
923a60
+        or <option>soft:hard</option> to set both limits individually (e.g. LimitAS=4G:16G).
923a60
+        Use the string <varname>infinity</varname> to
923a60
         configure no limit on a specific resource. The multiplicative
923a60
         suffixes K (=1024), M (=1024*1024) and so on for G, T, P and E
923a60
         may be used for resource limits measured in bytes
923a60
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
923a60
index 8afe9d7e83..d307f1c743 100644
923a60
--- a/src/core/load-fragment.c
923a60
+++ b/src/core/load-fragment.c
923a60
@@ -1075,81 +1075,108 @@ int config_parse_bounding_set(const char *unit,
923a60
         return 0;
923a60
 }
923a60
 
923a60
-int config_parse_limit(const char *unit,
923a60
-                       const char *filename,
923a60
-                       unsigned line,
923a60
-                       const char *section,
923a60
-                       unsigned section_line,
923a60
-                       const char *lvalue,
923a60
-                       int ltype,
923a60
-                       const char *rvalue,
923a60
-                       void *data,
923a60
-                       void *userdata) {
923a60
 
923a60
-        struct rlimit **rl = data;
923a60
-        unsigned long long u;
923a60
+static int rlim_parse_u64(const char *val, rlim_t *res) {
923a60
+        int r = 0;
923a60
 
923a60
-        assert(filename);
923a60
-        assert(lvalue);
923a60
-        assert(rvalue);
923a60
-        assert(data);
923a60
+        if (streq(val, "infinity"))
923a60
+                *res = RLIM_INFINITY;
923a60
+        else {
923a60
+                uint64_t u;
923a60
 
923a60
-        rl += ltype;
923a60
+                /* setrlimit(2) suggests rlim_t is always 64bit on Linux. */
923a60
+                assert_cc(sizeof(rlim_t) == sizeof(uint64_t));
923a60
+
923a60
+                r = safe_atou64(val, &u);
923a60
+                if (r >= 0 && u >= (uint64_t) RLIM_INFINITY)
923a60
+                        r = -ERANGE;
923a60
+                if (r == 0)
923a60
+                        *res = (rlim_t) u;
923a60
+        }
923a60
+        return r;
923a60
+}
923a60
 
923a60
-        if (streq(rvalue, "infinity"))
923a60
-                u = (unsigned long long) RLIM_INFINITY;
923a60
+static int rlim_parse_size(const char *val, rlim_t *res) {
923a60
+        int r = 0;
923a60
+
923a60
+        if (streq(val, "infinity"))
923a60
+                *res = RLIM_INFINITY;
923a60
         else {
923a60
-                int r;
923a60
+                off_t u;
923a60
 
923a60
-                r = safe_atollu(rvalue, &u);
923a60
-                if (r < 0) {
923a60
-                        log_syntax(unit, LOG_ERR, filename, line, -r,
923a60
-                                   "Failed to parse resource value, ignoring: %s", rvalue);
923a60
-                        return 0;
923a60
-                }
923a60
+                r = parse_size(val, 1024, &u);
923a60
+                if (r >= 0 && u >= (off_t) RLIM_INFINITY)
923a60
+                        r = -ERANGE;
923a60
+                if (r == 0)
923a60
+                        *res = (rlim_t) u;
923a60
         }
923a60
+        return r;
923a60
+}
923a60
 
923a60
-        if (!*rl) {
923a60
-                *rl = new(struct rlimit, 1);
923a60
-                if (!*rl)
923a60
-                        return log_oom();
923a60
-        }
923a60
+static int rlim_parse_sec(const char *val, rlim_t *res) {
923a60
+        int r = 0;
923a60
 
923a60
-        (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
923a60
-        return 0;
923a60
+        if (streq(val, "infinity"))
923a60
+                *res = RLIM_INFINITY;
923a60
+        else {
923a60
+                usec_t t;
923a60
+
923a60
+                r = parse_sec(val, &t);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
+                if (t == USEC_INFINITY)
923a60
+                        *res = RLIM_INFINITY;
923a60
+                else
923a60
+                        *res = (rlim_t) (DIV_ROUND_UP(t, USEC_PER_SEC));
923a60
+
923a60
+        }
923a60
+        return r;
923a60
 }
923a60
 
923a60
-int config_parse_bytes_limit(const char *unit,
923a60
-                       const char *filename,
923a60
-                       unsigned line,
923a60
-                       const char *section,
923a60
-                       unsigned section_line,
923a60
-                       const char *lvalue,
923a60
-                       int ltype,
923a60
-                       const char *rvalue,
923a60
-                       void *data,
923a60
-                       void *userdata) {
923a60
+static int rlim_parse_usec(const char *val, rlim_t *res) {
923a60
+        int r = 0;
923a60
 
923a60
-        struct rlimit **rl = data;
923a60
-        uint64_t bytes;
923a60
+        if (streq(val, "infinity"))
923a60
+                *res = RLIM_INFINITY;
923a60
+        else {
923a60
+                usec_t t;
923a60
 
923a60
-        assert(filename);
923a60
-        assert(lvalue);
923a60
-        assert(rvalue);
923a60
-        assert(data);
923a60
+                r = parse_time(val, &t, 1);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
+                if (t == USEC_INFINITY)
923a60
+                        *res = RLIM_INFINITY;
923a60
+                else
923a60
+                        *res = (rlim_t) t;
923a60
+        }
923a60
+        return r;
923a60
+}
923a60
 
923a60
-        rl += ltype;
923a60
+static int parse_rlimit_range(
923a60
+                const char *unit,
923a60
+                const char *filename,
923a60
+                unsigned line,
923a60
+                const char *value,
923a60
+                struct rlimit **rl,
923a60
+                int (*rlim_parser)(const char *, rlim_t *)) {
923a60
 
923a60
-        if (streq(rvalue, "infinity"))
923a60
-                bytes = (uint64_t) RLIM_INFINITY;
923a60
-        else {
923a60
-                int r;
923a60
+        rlim_t soft, hard;
923a60
+        _cleanup_free_ char *sword = NULL, *hword = NULL;
923a60
+        int nwords, r;
923a60
 
923a60
-                r = parse_size(rvalue, 1024, &bytes);
923a60
-                if (r < 0) {
923a60
-                        log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
923a60
-                        return 0;
923a60
-                }
923a60
+        assert(value);
923a60
+
923a60
+        /* <value> or <soft:hard> */
923a60
+        nwords = extract_many_words(&value, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &sword, &hword, NULL);
923a60
+        r = nwords < 0 ? nwords : nwords == 0 ? -EINVAL : 0;
923a60
+
923a60
+        if (r == 0)
923a60
+                r = rlim_parser(sword, &soft);
923a60
+        if (r == 0 && nwords == 2)
923a60
+                r = rlim_parser(hword, &hard);
923a60
+        if (r < 0) {
923a60
+                log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", value);
923a60
+                return 0;
923a60
         }
923a60
 
923a60
         if (!*rl) {
923a60
@@ -1157,12 +1184,12 @@ int config_parse_bytes_limit(const char *unit,
923a60
                 if (!*rl)
923a60
                         return log_oom();
923a60
         }
923a60
-
923a60
-        (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) bytes;
923a60
+        (*rl)->rlim_cur = soft;
923a60
+        (*rl)->rlim_max = nwords == 2 ? hard : soft;
923a60
         return 0;
923a60
 }
923a60
 
923a60
-int config_parse_sec_limit(
923a60
+int config_parse_limit(
923a60
                 const char *unit,
923a60
                 const char *filename,
923a60
                 unsigned line,
923a60
@@ -1175,8 +1202,6 @@ int config_parse_sec_limit(
923a60
                 void *userdata) {
923a60
 
923a60
         struct rlimit **rl = data;
923a60
-        rlim_t seconds;
923a60
-        int r;
923a60
 
923a60
         assert(filename);
923a60
         assert(lvalue);
923a60
@@ -1184,36 +1209,33 @@ int config_parse_sec_limit(
923a60
         assert(data);
923a60
 
923a60
         rl += ltype;
923a60
+        return parse_rlimit_range(unit, filename, line, rvalue, rl, rlim_parse_u64);
923a60
+}
923a60
 
923a60
-        if (streq(rvalue, "infinity"))
923a60
-                seconds = RLIM_INFINITY;
923a60
-        else {
923a60
-                usec_t t;
923a60
-
923a60
-                r = parse_sec(rvalue, &t);
923a60
-                if (r < 0) {
923a60
-                        log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
923a60
-                        return 0;
923a60
-                }
923a60
+int config_parse_bytes_limit(
923a60
+                const char *unit,
923a60
+                const char *filename,
923a60
+                unsigned line,
923a60
+                const char *section,
923a60
+                unsigned section_line,
923a60
+                const char *lvalue,
923a60
+                int ltype,
923a60
+                const char *rvalue,
923a60
+                void *data,
923a60
+                void *userdata) {
923a60
 
923a60
-                if (t == USEC_INFINITY)
923a60
-                        seconds = RLIM_INFINITY;
923a60
-                else
923a60
-                        seconds = (rlim_t) (DIV_ROUND_UP(t, USEC_PER_SEC));
923a60
-        }
923a60
+        struct rlimit **rl = data;
923a60
 
923a60
-        if (!*rl) {
923a60
-                *rl = new(struct rlimit, 1);
923a60
-                if (!*rl)
923a60
-                        return log_oom();
923a60
-        }
923a60
+        assert(filename);
923a60
+        assert(lvalue);
923a60
+        assert(rvalue);
923a60
+        assert(data);
923a60
 
923a60
-        (*rl)->rlim_cur = (*rl)->rlim_max = seconds;
923a60
-        return 0;
923a60
+        rl += ltype;
923a60
+        return parse_rlimit_range(unit, filename, line, rvalue, rl, rlim_parse_size);
923a60
 }
923a60
 
923a60
-
923a60
-int config_parse_usec_limit(
923a60
+int config_parse_sec_limit(
923a60
                 const char *unit,
923a60
                 const char *filename,
923a60
                 unsigned line,
923a60
@@ -1226,8 +1248,6 @@ int config_parse_usec_limit(
923a60
                 void *userdata) {
923a60
 
923a60
         struct rlimit **rl = data;
923a60
-        rlim_t useconds;
923a60
-        int r;
923a60
 
923a60
         assert(filename);
923a60
         assert(lvalue);
923a60
@@ -1235,34 +1255,33 @@ int config_parse_usec_limit(
923a60
         assert(data);
923a60
 
923a60
         rl += ltype;
923a60
+        return parse_rlimit_range(unit, filename, line, rvalue, rl, rlim_parse_sec);
923a60
+}
923a60
 
923a60
-        if (streq(rvalue, "infinity"))
923a60
-                useconds = RLIM_INFINITY;
923a60
-        else {
923a60
-                usec_t t;
923a60
-
923a60
-                r = parse_time(rvalue, &t, 1);
923a60
-                if (r < 0) {
923a60
-                        log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
923a60
-                        return 0;
923a60
-                }
923a60
+int config_parse_usec_limit(
923a60
+                const char *unit,
923a60
+                const char *filename,
923a60
+                unsigned line,
923a60
+                const char *section,
923a60
+                unsigned section_line,
923a60
+                const char *lvalue,
923a60
+                int ltype,
923a60
+                const char *rvalue,
923a60
+                void *data,
923a60
+                void *userdata) {
923a60
 
923a60
-                if (t == USEC_INFINITY)
923a60
-                        useconds = RLIM_INFINITY;
923a60
-                else
923a60
-                        useconds = (rlim_t) t;
923a60
-        }
923a60
+        struct rlimit **rl = data;
923a60
 
923a60
-        if (!*rl) {
923a60
-                *rl = new(struct rlimit, 1);
923a60
-                if (!*rl)
923a60
-                        return log_oom();
923a60
-        }
923a60
+        assert(filename);
923a60
+        assert(lvalue);
923a60
+        assert(rvalue);
923a60
+        assert(data);
923a60
 
923a60
-        (*rl)->rlim_cur = (*rl)->rlim_max = useconds;
923a60
-        return 0;
923a60
+        rl += ltype;
923a60
+        return parse_rlimit_range(unit, filename, line, rvalue, rl, rlim_parse_usec);
923a60
 }
923a60
 
923a60
+
923a60
 #ifdef HAVE_SYSV_COMPAT
923a60
 int config_parse_sysv_priority(const char *unit,
923a60
                                const char *filename,
923a60
diff --git a/src/shared/util.c b/src/shared/util.c
923a60
index 036677eb46..f75ed9dd42 100644
923a60
--- a/src/shared/util.c
923a60
+++ b/src/shared/util.c
923a60
@@ -93,6 +93,7 @@
923a60
 #include "virt.h"
923a60
 #include "def.h"
923a60
 #include "sparse-endian.h"
923a60
+#include "conf-parser.h"
923a60
 
923a60
 int saved_argc = 0;
923a60
 char **saved_argv = NULL;
923a60
@@ -100,6 +101,8 @@ char **saved_argv = NULL;
923a60
 static volatile unsigned cached_columns = 0;
923a60
 static volatile unsigned cached_lines = 0;
923a60
 
923a60
+bool unichar_is_valid(int32_t ch);
923a60
+
923a60
 size_t page_size(void) {
923a60
         static thread_local size_t pgsz = 0;
923a60
         long r;
923a60
@@ -1365,6 +1368,207 @@ char *cescape(const char *s) {
923a60
         return r;
923a60
 }
923a60
 
923a60
+bool unichar_is_valid(int32_t ch) {
923a60
+
923a60
+        if (ch >= 0x110000) /* End of unicode space */
923a60
+                return false;
923a60
+        if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
923a60
+                return false;
923a60
+        if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */
923a60
+                return false;
923a60
+        if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
923a60
+                return false;
923a60
+
923a60
+        return true;
923a60
+}
923a60
+
923a60
+int cunescape_one(const char *p, size_t length, int32_t *ret, bool *eight_bit) {
923a60
+        int r = 1;
923a60
+
923a60
+        assert(p);
923a60
+        assert(*p);
923a60
+        assert(ret);
923a60
+
923a60
+        /* Unescapes C style. Returns the unescaped character in ret.
923a60
+         * Sets *eight_bit to true if the escaped sequence either fits in
923a60
+         * one byte in UTF-8 or is a non-unicode literal byte and should
923a60
+         * instead be copied directly.
923a60
+         */
923a60
+
923a60
+        if (length != (size_t) -1 && length < 1)
923a60
+                return -EINVAL;
923a60
+
923a60
+        switch (p[0]) {
923a60
+
923a60
+        case 'a':
923a60
+                *ret = '\a';
923a60
+                break;
923a60
+        case 'b':
923a60
+                *ret = '\b';
923a60
+                break;
923a60
+        case 'f':
923a60
+                *ret = '\f';
923a60
+                break;
923a60
+        case 'n':
923a60
+                *ret = '\n';
923a60
+                break;
923a60
+        case 'r':
923a60
+                *ret = '\r';
923a60
+                break;
923a60
+        case 't':
923a60
+                *ret = '\t';
923a60
+                break;
923a60
+        case 'v':
923a60
+                *ret = '\v';
923a60
+                break;
923a60
+        case '\\':
923a60
+                *ret = '\\';
923a60
+                break;
923a60
+        case '"':
923a60
+                *ret = '"';
923a60
+                break;
923a60
+        case '\'':
923a60
+                *ret = '\'';
923a60
+                break;
923a60
+
923a60
+        case 's':
923a60
+                /* This is an extension of the XDG syntax files */
923a60
+                *ret = ' ';
923a60
+                break;
923a60
+
923a60
+        case 'x': {
923a60
+                /* hexadecimal encoding */
923a60
+                int a, b;
923a60
+
923a60
+                if (length != (size_t) -1 && length < 3)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                a = unhexchar(p[1]);
923a60
+                if (a < 0)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                b = unhexchar(p[2]);
923a60
+                if (b < 0)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                /* Don't allow NUL bytes */
923a60
+                if (a == 0 && b == 0)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                *ret = (a << 4U) | b;
923a60
+                *eight_bit = true;
923a60
+                r = 3;
923a60
+                break;
923a60
+        }
923a60
+
923a60
+        case 'u': {
923a60
+                /* C++11 style 16bit unicode */
923a60
+
923a60
+                int a[4];
923a60
+                unsigned i;
923a60
+                uint32_t c;
923a60
+
923a60
+                if (length != (size_t) -1 && length < 5)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                for (i = 0; i < 4; i++) {
923a60
+                        a[i] = unhexchar(p[1 + i]);
923a60
+                        if (a[i] < 0)
923a60
+                                return a[i];
923a60
+                }
923a60
+
923a60
+                c = ((uint32_t) a[0] << 12U) | ((uint32_t) a[1] << 8U) | ((uint32_t) a[2] << 4U) | (uint32_t) a[3];
923a60
+
923a60
+                /* Don't allow 0 chars */
923a60
+                if (c == 0)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                *ret = c;
923a60
+                r = 5;
923a60
+                break;
923a60
+        }
923a60
+
923a60
+        case 'U': {
923a60
+                /* C++11 style 32bit unicode */
923a60
+
923a60
+                int a[8];
923a60
+                unsigned i;
923a60
+                int32_t c;
923a60
+
923a60
+                if (length != (size_t) -1 && length < 9)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                for (i = 0; i < 8; i++) {
923a60
+                        a[i] = unhexchar(p[1 + i]);
923a60
+                        if (a[i] < 0)
923a60
+                                return a[i];
923a60
+                }
923a60
+
923a60
+                c = ((uint32_t) a[0] << 28U) | ((uint32_t) a[1] << 24U) | ((uint32_t) a[2] << 20U) | ((uint32_t) a[3] << 16U) |
923a60
+                    ((uint32_t) a[4] << 12U) | ((uint32_t) a[5] <<  8U) | ((uint32_t) a[6] <<  4U) |  (uint32_t) a[7];
923a60
+
923a60
+                /* Don't allow 0 chars */
923a60
+                if (c == 0)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                /* Don't allow invalid code points */
923a60
+                if (!unichar_is_valid(c))
923a60
+                        return -EINVAL;
923a60
+
923a60
+                *ret = c;
923a60
+                r = 9;
923a60
+                break;
923a60
+        }
923a60
+
923a60
+        case '0':
923a60
+        case '1':
923a60
+        case '2':
923a60
+        case '3':
923a60
+        case '4':
923a60
+        case '5':
923a60
+        case '6':
923a60
+        case '7': {
923a60
+                /* octal encoding */
923a60
+                int a, b, c;
923a60
+                int32_t m;
923a60
+
923a60
+                if (length != (size_t) -1 && length < 3)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                a = unoctchar(p[0]);
923a60
+                if (a < 0)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                b = unoctchar(p[1]);
923a60
+                if (b < 0)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                c = unoctchar(p[2]);
923a60
+                if (c < 0)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                /* don't allow NUL bytes */
923a60
+                if (a == 0 && b == 0 && c == 0)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                /* Don't allow bytes above 255 */
923a60
+                m = ((uint32_t) a << 6U) | ((uint32_t) b << 3U) | (uint32_t) c;
923a60
+                if (m > 255)
923a60
+                        return -EINVAL;
923a60
+
923a60
+                *ret = m;
923a60
+                *eight_bit = true;
923a60
+                r = 3;
923a60
+                break;
923a60
+        }
923a60
+
923a60
+        default:
923a60
+                return -EINVAL;
923a60
+        }
923a60
+
923a60
+        return r;
923a60
+}
923a60
+
923a60
 char *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix) {
923a60
         char *r, *t;
923a60
         const char *f;
923a60
@@ -8207,3 +8411,266 @@ bool colors_enabled(void) {
923a60
 
923a60
         return parse_boolean(colors) != 0;
923a60
 }
923a60
+
923a60
+int extract_first_word(const char **p, char **ret, const char *separators, ExtractFlags flags) {
923a60
+        _cleanup_free_ char *s = NULL;
923a60
+        size_t allocated = 0, sz = 0;
923a60
+        char c;
923a60
+        int r;
923a60
+
923a60
+        char quote = 0;                 /* 0 or ' or " */
923a60
+        bool backslash = false;         /* whether we've just seen a backslash */
923a60
+
923a60
+        assert(p);
923a60
+        assert(ret);
923a60
+
923a60
+        /* Bail early if called after last value or with no input */
923a60
+        if (!*p)
923a60
+                goto finish_force_terminate;
923a60
+        c = **p;
923a60
+
923a60
+        if (!separators)
923a60
+                separators = WHITESPACE;
923a60
+
923a60
+        /* Parses the first word of a string, and returns it in
923a60
+         * *ret. Removes all quotes in the process. When parsing fails
923a60
+         * (because of an uneven number of quotes or similar), leaves
923a60
+         * the pointer *p at the first invalid character. */
923a60
+
923a60
+        if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
923a60
+                if (!GREEDY_REALLOC(s, allocated, sz+1))
923a60
+                        return -ENOMEM;
923a60
+
923a60
+        for (;; (*p)++, c = **p) {
923a60
+                if (c == 0)
923a60
+                        goto finish_force_terminate;
923a60
+                else if (strchr(separators, c)) {
923a60
+                        if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
923a60
+                                (*p)++;
923a60
+                                goto finish_force_next;
923a60
+                        }
923a60
+                } else {
923a60
+                        /* We found a non-blank character, so we will always
923a60
+                         * want to return a string (even if it is empty),
923a60
+                         * allocate it here. */
923a60
+                        if (!GREEDY_REALLOC(s, allocated, sz+1))
923a60
+                                return -ENOMEM;
923a60
+                        break;
923a60
+                }
923a60
+        }
923a60
+
923a60
+        for (;; (*p)++, c = **p) {
923a60
+                if (backslash) {
923a60
+                        if (!GREEDY_REALLOC(s, allocated, sz+7))
923a60
+                                return -ENOMEM;
923a60
+
923a60
+                        if (c == 0) {
923a60
+                                if ((flags & EXTRACT_CUNESCAPE_RELAX) &&
923a60
+                                    (!quote || flags & EXTRACT_RELAX)) {
923a60
+                                        /* If we find an unquoted trailing backslash and we're in
923a60
+                                         * EXTRACT_CUNESCAPE_RELAX mode, keep it verbatim in the
923a60
+                                         * output.
923a60
+                                         *
923a60
+                                         * Unbalanced quotes will only be allowed in EXTRACT_RELAX
923a60
+                                         * mode, EXTRACT_CUNESCAPE_RELAX mode does not allow them.
923a60
+                                         */
923a60
+                                        s[sz++] = '\\';
923a60
+                                        goto finish_force_terminate;
923a60
+                                }
923a60
+                                if (flags & EXTRACT_RELAX)
923a60
+                                        goto finish_force_terminate;
923a60
+                                return -EINVAL;
923a60
+                        }
923a60
+
923a60
+                        if (flags & EXTRACT_CUNESCAPE) {
923a60
+                                bool eight_bit = false;
923a60
+                                int32_t u;
923a60
+
923a60
+                                r = cunescape_one(*p, (size_t) -1, &u, &eight_bit);
923a60
+                                if (r < 0) {
923a60
+                                        if (flags & EXTRACT_CUNESCAPE_RELAX) {
923a60
+                                                s[sz++] = '\\';
923a60
+                                                s[sz++] = c;
923a60
+                                        } else
923a60
+                                                return -EINVAL;
923a60
+                                } else {
923a60
+                                        (*p) += r - 1;
923a60
+
923a60
+                                        if (eight_bit)
923a60
+                                                s[sz++] = u;
923a60
+                                        else
923a60
+                                                sz += utf8_encode_unichar(s + sz, u);
923a60
+                                }
923a60
+                        } else
923a60
+                                s[sz++] = c;
923a60
+
923a60
+                        backslash = false;
923a60
+
923a60
+                } else if (quote) {     /* inside either single or double quotes */
923a60
+                        for (;; (*p)++, c = **p) {
923a60
+                                if (c == 0) {
923a60
+                                        if (flags & EXTRACT_RELAX)
923a60
+                                                goto finish_force_terminate;
923a60
+                                        return -EINVAL;
923a60
+                                } else if (c == quote) {        /* found the end quote */
923a60
+                                        quote = 0;
923a60
+                                        break;
923a60
+                                } else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {
923a60
+                                        backslash = true;
923a60
+                                        break;
923a60
+                                } else {
923a60
+                                        if (!GREEDY_REALLOC(s, allocated, sz+2))
923a60
+                                                return -ENOMEM;
923a60
+
923a60
+                                        s[sz++] = c;
923a60
+                                }
923a60
+                        }
923a60
+
923a60
+                } else {
923a60
+                        for (;; (*p)++, c = **p) {
923a60
+                                if (c == 0)
923a60
+                                        goto finish_force_terminate;
923a60
+                                else if ((c == '\'' || c == '"') && (flags & EXTRACT_QUOTES)) {
923a60
+                                        quote = c;
923a60
+                                        break;
923a60
+                                } else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {
923a60
+                                        backslash = true;
923a60
+                                        break;
923a60
+                                } else if (strchr(separators, c)) {
923a60
+                                        if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
923a60
+                                                (*p)++;
923a60
+                                                goto finish_force_next;
923a60
+                                        }
923a60
+                                        /* Skip additional coalesced separators. */
923a60
+                                        for (;; (*p)++, c = **p) {
923a60
+                                                if (c == 0)
923a60
+                                                        goto finish_force_terminate;
923a60
+                                                if (!strchr(separators, c))
923a60
+                                                        break;
923a60
+                                        }
923a60
+                                        goto finish;
923a60
+
923a60
+                                } else {
923a60
+                                        if (!GREEDY_REALLOC(s, allocated, sz+2))
923a60
+                                                return -ENOMEM;
923a60
+
923a60
+                                        s[sz++] = c;
923a60
+                                }
923a60
+                        }
923a60
+                }
923a60
+        }
923a60
+
923a60
+finish_force_terminate:
923a60
+        *p = NULL;
923a60
+finish:
923a60
+        if (!s) {
923a60
+                *p = NULL;
923a60
+                *ret = NULL;
923a60
+                return 0;
923a60
+        }
923a60
+
923a60
+finish_force_next:
923a60
+        s[sz] = 0;
923a60
+        *ret = s;
923a60
+        s = NULL;
923a60
+
923a60
+        return 1;
923a60
+}
923a60
+
923a60
+int extract_first_word_and_warn(
923a60
+                const char **p,
923a60
+                char **ret,
923a60
+                const char *separators,
923a60
+                ExtractFlags flags,
923a60
+                const char *unit,
923a60
+                const char *filename,
923a60
+                unsigned line,
923a60
+                const char *rvalue) {
923a60
+
923a60
+        /* Try to unquote it, if it fails, warn about it and try again
923a60
+         * but this time using EXTRACT_CUNESCAPE_RELAX to keep the
923a60
+         * backslashes verbatim in invalid escape sequences. */
923a60
+
923a60
+        const char *save;
923a60
+        int r;
923a60
+
923a60
+        save = *p;
923a60
+        r = extract_first_word(p, ret, separators, flags);
923a60
+        if (r >= 0)
923a60
+                return r;
923a60
+
923a60
+        if (r == -EINVAL && !(flags & EXTRACT_CUNESCAPE_RELAX)) {
923a60
+
923a60
+                /* Retry it with EXTRACT_CUNESCAPE_RELAX. */
923a60
+                *p = save;
923a60
+                r = extract_first_word(p, ret, separators, flags|EXTRACT_CUNESCAPE_RELAX);
923a60
+                if (r >= 0) {
923a60
+                        /* It worked this time, hence it must have been an invalid escape sequence we could correct. */
923a60
+                        log_syntax(unit, LOG_WARNING, filename, line, EINVAL, "Invalid escape sequences in line, correcting: \"%s\"", rvalue);
923a60
+                        return r;
923a60
+                }
923a60
+
923a60
+                /* If it's still EINVAL; then it must be unbalanced quoting, report this. */
923a60
+                if (r == -EINVAL)
923a60
+                        return log_syntax(unit, LOG_ERR, filename, line, r, "Unbalanced quoting, ignoring: \"%s\"", rvalue);
923a60
+        }
923a60
+
923a60
+        /* Can be any error, report it */
923a60
+        return log_syntax(unit, LOG_ERR, filename, line, r, "Unable to decode word \"%s\", ignoring: %m", rvalue);
923a60
+}
923a60
+
923a60
+int extract_many_words(const char **p, const char *separators, ExtractFlags flags, ...) {
923a60
+        va_list ap;
923a60
+        char **l;
923a60
+        int n = 0, i, c, r;
923a60
+
923a60
+        /* Parses a number of words from a string, stripping any
923a60
+         * quotes if necessary. */
923a60
+
923a60
+        assert(p);
923a60
+
923a60
+        /* Count how many words are expected */
923a60
+        va_start(ap, flags);
923a60
+        for (;;) {
923a60
+                if (!va_arg(ap, char **))
923a60
+                        break;
923a60
+                n++;
923a60
+        }
923a60
+        va_end(ap);
923a60
+
923a60
+        if (n <= 0)
923a60
+                return 0;
923a60
+
923a60
+        /* Read all words into a temporary array */
923a60
+        l = newa0(char*, n);
923a60
+        for (c = 0; c < n; c++) {
923a60
+
923a60
+                r = extract_first_word(p, &l[c], separators, flags);
923a60
+                if (r < 0) {
923a60
+                        int j;
923a60
+
923a60
+                        for (j = 0; j < c; j++)
923a60
+                                free(l[j]);
923a60
+
923a60
+                        return r;
923a60
+                }
923a60
+
923a60
+                if (r == 0)
923a60
+                        break;
923a60
+        }
923a60
+
923a60
+        /* If we managed to parse all words, return them in the passed
923a60
+         * in parameters */
923a60
+        va_start(ap, flags);
923a60
+        for (i = 0; i < n; i++) {
923a60
+                char **v;
923a60
+
923a60
+                v = va_arg(ap, char **);
923a60
+                assert(v);
923a60
+
923a60
+                *v = l[i];
923a60
+        }
923a60
+        va_end(ap);
923a60
+
923a60
+        return c;
923a60
+}
923a60
diff --git a/src/shared/util.h b/src/shared/util.h
923a60
index a441e44ff9..be04524cc9 100644
923a60
--- a/src/shared/util.h
923a60
+++ b/src/shared/util.h
923a60
@@ -315,6 +315,7 @@ int undecchar(char c) _const_;
923a60
 char *cescape(const char *s);
923a60
 char *cunescape(const char *s);
923a60
 char *cunescape_length(const char *s, size_t length);
923a60
+int cunescape_one(const char *p, size_t length, int32_t *ret, bool *eight_bit);
923a60
 char *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix);
923a60
 
923a60
 char *xescape(const char *s, const char *bad);
923a60
@@ -1082,3 +1083,16 @@ void sigkill_wait(pid_t *pid);
923a60
 int syslog_parse_priority(const char **p, int *priority, bool with_facility);
923a60
 
923a60
 char *shell_maybe_quote(const char *s);
923a60
+
923a60
+typedef enum ExtractFlags {
923a60
+        EXTRACT_RELAX                    = 1,
923a60
+        EXTRACT_CUNESCAPE                = 2,
923a60
+        EXTRACT_CUNESCAPE_RELAX          = 4,
923a60
+        EXTRACT_QUOTES                   = 8,
923a60
+        EXTRACT_DONT_COALESCE_SEPARATORS = 16,
923a60
+        EXTRACT_RETAIN_ESCAPE            = 32,
923a60
+} ExtractFlags;
923a60
+
923a60
+int extract_first_word(const char **p, char **ret, const char *separators, ExtractFlags flags);
923a60
+int extract_first_word_and_warn(const char **p, char **ret, const char *separators, ExtractFlags flags, const char *unit, const char *filename, unsigned line, const char *rvalue);
923a60
+int extract_many_words(const char **p, const char *separators, ExtractFlags flags, ...) _sentinel_;
923a60
diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c
923a60
index 87c81ccd71..931dfeda88 100644
923a60
--- a/src/test/test-unit-file.c
923a60
+++ b/src/test/test-unit-file.c
923a60
@@ -554,11 +554,22 @@ static void test_config_parse_rlimit(void) {
923a60
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 55);
923a60
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max);
923a60
 
923a60
+
923a60
+        assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55:66", rl, NULL) >= 0);
923a60
+        assert_se(rl[RLIMIT_NOFILE]);
923a60
+        assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 55);
923a60
+        assert_se(rl[RLIMIT_NOFILE]->rlim_max == 66);
923a60
+
923a60
         assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity", rl, NULL) >= 0);
923a60
         assert_se(rl[RLIMIT_NOFILE]);
923a60
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == RLIM_INFINITY);
923a60
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max);
923a60
 
923a60
+        assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity:infinity", rl, NULL) >= 0);
923a60
+        assert_se(rl[RLIMIT_NOFILE]);
923a60
+        assert_se(rl[RLIMIT_NOFILE]->rlim_cur == RLIM_INFINITY);
923a60
+        assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max);
923a60
+
923a60
         free(rl[RLIMIT_NOFILE]);
923a60
         assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "56", rl, NULL) >= 0);
923a60
         assert_se(rl[RLIMIT_CPU]);
923a60
@@ -570,6 +581,11 @@ static void test_config_parse_rlimit(void) {
923a60
         assert_se(rl[RLIMIT_CPU]->rlim_cur == 57);
923a60
         assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max);
923a60
 
923a60
+        assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "40s:1m", rl, NULL) >= 0);
923a60
+        assert_se(rl[RLIMIT_CPU]);
923a60
+        assert_se(rl[RLIMIT_CPU]->rlim_cur == 40);
923a60
+        assert_se(rl[RLIMIT_CPU]->rlim_max == 60);
923a60
+
923a60
         assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "infinity", rl, NULL) >= 0);
923a60
         assert_se(rl[RLIMIT_CPU]);
923a60
         assert_se(rl[RLIMIT_CPU]->rlim_cur == RLIM_INFINITY);
923a60
@@ -587,16 +603,31 @@ static void test_config_parse_rlimit(void) {
923a60
         assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 58);
923a60
         assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
923a60
 
923a60
+        assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58:60", rl, NULL) >= 0);
923a60
+        assert_se(rl[RLIMIT_RTTIME]);
923a60
+        assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 58);
923a60
+        assert_se(rl[RLIMIT_RTTIME]->rlim_max == 60);
923a60
+
923a60
         assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s", rl, NULL) >= 0);
923a60
         assert_se(rl[RLIMIT_RTTIME]);
923a60
         assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 59 * USEC_PER_SEC);
923a60
         assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
923a60
 
923a60
+        assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s:123s", rl, NULL) >= 0);
923a60
+        assert_se(rl[RLIMIT_RTTIME]);
923a60
+        assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 59 * USEC_PER_SEC);
923a60
+        assert_se(rl[RLIMIT_RTTIME]->rlim_max == 123 * USEC_PER_SEC);
923a60
+
923a60
         assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity", rl, NULL) >= 0);
923a60
         assert_se(rl[RLIMIT_RTTIME]);
923a60
         assert_se(rl[RLIMIT_RTTIME]->rlim_cur == RLIM_INFINITY);
923a60
         assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
923a60
 
923a60
+        assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity:infinity", rl, NULL) >= 0);
923a60
+        assert_se(rl[RLIMIT_RTTIME]);
923a60
+        assert_se(rl[RLIMIT_RTTIME]->rlim_cur == RLIM_INFINITY);
923a60
+        assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
923a60
+
923a60
         assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "2345ms", rl, NULL) >= 0);
923a60
         assert_se(rl[RLIMIT_RTTIME]);
923a60
         assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 2345 * USEC_PER_MSEC);