ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
923a60
From c56c1f6c2b683d6f20a7e8caeecec6c3cb76798f Mon Sep 17 00:00:00 2001
923a60
From: Lukas Nykryn <lnykryn@redhat.com>
923a60
Date: Tue, 3 Jan 2017 14:21:25 +0100
923a60
Subject: [PATCH] core: make parsing of RLIMIT_NICE aware of actual nice levels
923a60
923a60
RHEL-only
923a60
(most of code taken from 29857001854a02c292f1f3b324e7a66831e859c8)
923a60
923a60
Resolves: #1409588
923a60
---
923a60
 man/systemd.exec.xml                  |  7 ++-
923a60
 src/core/load-fragment-gperf.gperf.m4 |  2 +-
923a60
 src/core/load-fragment.c              | 72 +++++++++++++++++++++++++++
923a60
 src/core/load-fragment.h              |  1 +
923a60
 src/core/main.c                       |  2 +-
923a60
 5 files changed, 81 insertions(+), 3 deletions(-)
923a60
923a60
diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml
923a60
index 0cd469cd98..c5199d3a54 100644
923a60
--- a/man/systemd.exec.xml
923a60
+++ b/man/systemd.exec.xml
923a60
@@ -575,7 +575,12 @@
923a60
         granularity of the limits might influence their
923a60
         enforcement. For example, time limits specified for
923a60
         <varname>LimitCPU=</varname> will be rounded up implicitly to
923a60
-        multiples of 1s.</para>
923a60
+        multiples of 1s. For <varname>LimitNICE=</varname> the value
923a60
+        may be specified in two syntaxes: if prefixed with <literal>+</literal>
923a60
+        or <literal>-</literal>, the value is understood as regular Linux
923a60
+        nice value in the range -20..19. If not prefixed like this the value
923a60
+        is understood as raw resource limit parameter in the range 0..40 (with 0 being
923a60
+        equivalent to 1).</para>
923a60
 
923a60
         <para>Note that most process resource limits configured with
923a60
         these options are per-process, and processes may fork in order
923a60
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
923a60
index 45d1ead45b..f3a6e13d9f 100644
923a60
--- a/src/core/load-fragment-gperf.gperf.m4
923a60
+++ b/src/core/load-fragment-gperf.gperf.m4
923a60
@@ -71,7 +71,7 @@ $1.LimitMEMLOCK,                 config_parse_bytes_limit,           RLIMIT_MEML
923a60
 $1.LimitLOCKS,                   config_parse_limit,                 RLIMIT_LOCKS,                  offsetof($1, exec_context.rlimit)
923a60
 $1.LimitSIGPENDING,              config_parse_limit,                 RLIMIT_SIGPENDING,             offsetof($1, exec_context.rlimit)
923a60
 $1.LimitMSGQUEUE,                config_parse_bytes_limit,           RLIMIT_MSGQUEUE,               offsetof($1, exec_context.rlimit)
923a60
-$1.LimitNICE,                    config_parse_limit,                 RLIMIT_NICE,                   offsetof($1, exec_context.rlimit)
923a60
+$1.LimitNICE,                    config_parse_nice_limit,            RLIMIT_NICE,                   offsetof($1, exec_context.rlimit)
923a60
 $1.LimitRTPRIO,                  config_parse_limit,                 RLIMIT_RTPRIO,                 offsetof($1, exec_context.rlimit)
923a60
 $1.LimitRTTIME,                  config_parse_usec_limit,            RLIMIT_RTTIME,                 offsetof($1, exec_context.rlimit)
923a60
 $1.ReadWriteDirectories,         config_parse_namespace_path_strv,   0,                             offsetof($1, exec_context.read_write_dirs)
923a60
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
923a60
index 7056419715..3a3c456da5 100644
923a60
--- a/src/core/load-fragment.c
923a60
+++ b/src/core/load-fragment.c
923a60
@@ -1154,6 +1154,56 @@ static int rlim_parse_usec(const char *val, rlim_t *res) {
923a60
         return r;
923a60
 }
923a60
 
923a60
+static int rlim_parse_nice(const char *val, rlim_t *ret) {
923a60
+        uint64_t rl;
923a60
+        int r;
923a60
+
923a60
+        /* So, Linux is weird. The range for RLIMIT_NICE is 40..1, mapping to the nice levels -20..19. However, the
923a60
+         * RLIMIT_NICE limit defaults to 0 by the kernel, i.e. a value that maps to nice level 20, which of course is
923a60
+         * bogus and does not exist. In order to permit parsing the RLIMIT_NICE of 0 here we hence implement a slight
923a60
+         * asymmetry: when parsing as positive nice level we permit 0..19. When parsing as negative nice level, we
923a60
+         * permit -20..0. But when parsing as raw resource limit value then we also allow the special value 0.
923a60
+         *
923a60
+         * Yeah, Linux is quality engineering sometimes... */
923a60
+
923a60
+        if (val[0] == '+') {
923a60
+
923a60
+                /* Prefixed with "+": Parse as positive user-friendly nice value */
923a60
+                r = safe_atou64(val + 1, &rl);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
+
923a60
+                if (rl >= PRIO_MAX)
923a60
+                        return -ERANGE;
923a60
+
923a60
+                rl = 20 - rl;
923a60
+
923a60
+        } else if (val[0] == '-') {
923a60
+
923a60
+                /* Prefixed with "-": Parse as negative user-friendly nice value */
923a60
+                r = safe_atou64(val + 1, &rl);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
+
923a60
+                if (rl > (uint64_t) (-PRIO_MIN))
923a60
+                        return -ERANGE;
923a60
+
923a60
+                rl = 20 + rl;
923a60
+        } else {
923a60
+
923a60
+                /* Not prefixed: parse as raw resource limit value */
923a60
+                r = safe_atou64(val, &rl);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
+
923a60
+                if (rl > (uint64_t) (20 - PRIO_MIN))
923a60
+                        return -ERANGE;
923a60
+        }
923a60
+
923a60
+        *ret = (rlim_t) rl;
923a60
+        return 0;
923a60
+}
923a60
+
923a60
 static int parse_rlimit_range(
923a60
                 const char *unit,
923a60
                 const char *filename,
923a60
@@ -1286,6 +1336,28 @@ int config_parse_usec_limit(
923a60
         return parse_rlimit_range(unit, filename, line, rvalue, rl, rlim_parse_usec);
923a60
 }
923a60
 
923a60
+int config_parse_nice_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
+        struct rlimit **rl = data;
923a60
+
923a60
+        assert(filename);
923a60
+        assert(lvalue);
923a60
+        assert(rvalue);
923a60
+        assert(data);
923a60
+
923a60
+        rl += ltype;
923a60
+        return parse_rlimit_range(unit, filename, line, rvalue, rl, rlim_parse_nice);
923a60
+}
923a60
 
923a60
 #ifdef HAVE_SYSV_COMPAT
923a60
 int config_parse_sysv_priority(const char *unit,
923a60
diff --git a/src/core/load-fragment.h b/src/core/load-fragment.h
923a60
index 6114796126..7c69e53699 100644
923a60
--- a/src/core/load-fragment.h
923a60
+++ b/src/core/load-fragment.h
923a60
@@ -59,6 +59,7 @@ int config_parse_limit(const char *unit, const char *filename, unsigned line, co
923a60
 int config_parse_bytes_limit(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);
923a60
 int config_parse_sec_limit(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);
923a60
 int config_parse_usec_limit(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);
923a60
+int config_parse_nice_limit(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);
923a60
 int config_parse_sysv_priority(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);
923a60
 int config_parse_kill_signal(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);
923a60
 int config_parse_exec_mount_flags(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);
923a60
diff --git a/src/core/main.c b/src/core/main.c
923a60
index 6f8367632c..820cbc3e53 100644
923a60
--- a/src/core/main.c
923a60
+++ b/src/core/main.c
923a60
@@ -669,7 +669,7 @@ static int parse_config_file(void) {
923a60
                 { "Manager", "DefaultLimitLOCKS",         config_parse_limit,            0, &arg_default_rlimit[RLIMIT_LOCKS]      },
923a60
                 { "Manager", "DefaultLimitSIGPENDING",    config_parse_limit,            0, &arg_default_rlimit[RLIMIT_SIGPENDING] },
923a60
                 { "Manager", "DefaultLimitMSGQUEUE",      config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_MSGQUEUE]   },
923a60
-                { "Manager", "DefaultLimitNICE",          config_parse_limit,            0, &arg_default_rlimit[RLIMIT_NICE]       },
923a60
+                { "Manager", "DefaultLimitNICE",          config_parse_nice_limit,       0, &arg_default_rlimit[RLIMIT_NICE]       },
923a60
                 { "Manager", "DefaultLimitRTPRIO",        config_parse_limit,            0, &arg_default_rlimit[RLIMIT_RTPRIO]     },
923a60
                 { "Manager", "DefaultLimitRTTIME",        config_parse_limit,            0, &arg_default_rlimit[RLIMIT_RTTIME]     },
923a60
                 { "Manager", "DefaultCPUAccounting",      config_parse_bool,             0, &arg_default_cpu_accounting            },