valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone

Blame SOURCES/0432-core-make-parsing-of-RLIMIT_NICE-aware-of-actual-nic.patch

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