anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone
923a60
From 99074eebc911728a41167c1962231e11b5e3cddd Mon Sep 17 00:00:00 2001
923a60
From: Karel Zak <kzak@redhat.com>
923a60
Date: Fri, 6 Nov 2015 11:06:52 +0100
923a60
Subject: [PATCH] core: support IEC suffixes for RLIMIT stuff
923a60
923a60
Let's make things more user-friendly and support for example
923a60
923a60
  LimitAS=16G
923a60
923a60
rather than force users to always use LimitAS=16106127360.
923a60
923a60
The change is relevant for options:
923a60
923a60
  [Default]Limit{FSIZE,DATA,STACK,CORE,RSS,AS,MEMLOCK,MSGQUEUE}
923a60
923a60
The patch introduces config_parse_bytes_limit(), it's the same as
923a60
config_parse_limit() but uses parse_size() tu support the suffixes.
923a60
923a60
Addresses: https://github.com/systemd/systemd/issues/1772
923a60
923a60
Cherry-picked from: 412ea7a936ebaa5342a4c2abf48b9e408e6ba5dc
923a60
Related: #1351415
923a60
---
923a60
 man/systemd-system.conf.xml           |  6 ++--
923a60
 man/systemd.exec.xml                  |  4 ++-
923a60
 src/core/load-fragment-gperf.gperf.m4 | 16 +++++-----
923a60
 src/core/load-fragment.c              | 43 +++++++++++++++++++++++++++
923a60
 src/core/load-fragment.h              |  1 +
923a60
 src/core/main.c                       | 16 +++++-----
923a60
 6 files changed, 67 insertions(+), 19 deletions(-)
923a60
923a60
diff --git a/man/systemd-system.conf.xml b/man/systemd-system.conf.xml
923a60
index ca25c93a1f..b7d9cdee05 100644
923a60
--- a/man/systemd-system.conf.xml
923a60
+++ b/man/systemd-system.conf.xml
923a60
@@ -327,8 +327,10 @@
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. These settings may
923a60
-        be overridden in individual units using the corresponding
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
         itself.</para></listitem>
923a60
diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml
923a60
index 6af7c7ae5d..25aea16553 100644
923a60
--- a/man/systemd.exec.xml
923a60
+++ b/man/systemd.exec.xml
923a60
@@ -559,7 +559,9 @@
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
-        configure no limit on a specific resource.</para></listitem>
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. LimitAS=16G).</para></listitem>
923a60
 
923a60
         
923a60
           <title>Limit directives and their equivalent with ulimit</title>
923a60
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
923a60
index 85d9797514..c3461a0a69 100644
923a60
--- a/src/core/load-fragment-gperf.gperf.m4
923a60
+++ b/src/core/load-fragment-gperf.gperf.m4
923a60
@@ -59,18 +59,18 @@ $1.SystemCallArchitectures,      config_parse_warn_compat,           DISABLED_CO
923a60
 $1.SystemCallErrorNumber,        config_parse_warn_compat,           DISABLED_CONFIGURATION,        0
923a60
 $1.RestrictAddressFamilies,      config_parse_warn_compat,           DISABLED_CONFIGURATION,        0')
923a60
 $1.LimitCPU,                     config_parse_limit,                 RLIMIT_CPU,                    offsetof($1, exec_context.rlimit)
923a60
-$1.LimitFSIZE,                   config_parse_limit,                 RLIMIT_FSIZE,                  offsetof($1, exec_context.rlimit)
923a60
-$1.LimitDATA,                    config_parse_limit,                 RLIMIT_DATA,                   offsetof($1, exec_context.rlimit)
923a60
-$1.LimitSTACK,                   config_parse_limit,                 RLIMIT_STACK,                  offsetof($1, exec_context.rlimit)
923a60
-$1.LimitCORE,                    config_parse_limit,                 RLIMIT_CORE,                   offsetof($1, exec_context.rlimit)
923a60
-$1.LimitRSS,                     config_parse_limit,                 RLIMIT_RSS,                    offsetof($1, exec_context.rlimit)
923a60
+$1.LimitFSIZE,                   config_parse_bytes_limit,           RLIMIT_FSIZE,                  offsetof($1, exec_context.rlimit)
923a60
+$1.LimitDATA,                    config_parse_bytes_limit,           RLIMIT_DATA,                   offsetof($1, exec_context.rlimit)
923a60
+$1.LimitSTACK,                   config_parse_bytes_limit,           RLIMIT_STACK,                  offsetof($1, exec_context.rlimit)
923a60
+$1.LimitCORE,                    config_parse_bytes_limit,           RLIMIT_CORE,                   offsetof($1, exec_context.rlimit)
923a60
+$1.LimitRSS,                     config_parse_bytes_limit,           RLIMIT_RSS,                    offsetof($1, exec_context.rlimit)
923a60
 $1.LimitNOFILE,                  config_parse_limit,                 RLIMIT_NOFILE,                 offsetof($1, exec_context.rlimit)
923a60
-$1.LimitAS,                      config_parse_limit,                 RLIMIT_AS,                     offsetof($1, exec_context.rlimit)
923a60
+$1.LimitAS,                      config_parse_bytes_limit,           RLIMIT_AS,                     offsetof($1, exec_context.rlimit)
923a60
 $1.LimitNPROC,                   config_parse_limit,                 RLIMIT_NPROC,                  offsetof($1, exec_context.rlimit)
923a60
-$1.LimitMEMLOCK,                 config_parse_limit,                 RLIMIT_MEMLOCK,                offsetof($1, exec_context.rlimit)
923a60
+$1.LimitMEMLOCK,                 config_parse_bytes_limit,           RLIMIT_MEMLOCK,                offsetof($1, exec_context.rlimit)
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_limit,                 RLIMIT_MSGQUEUE,               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.LimitRTPRIO,                  config_parse_limit,                 RLIMIT_RTPRIO,                 offsetof($1, exec_context.rlimit)
923a60
 $1.LimitRTTIME,                  config_parse_limit,                 RLIMIT_RTTIME,                 offsetof($1, exec_context.rlimit)
923a60
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
923a60
index b188ec99d9..dbb45b20f3 100644
923a60
--- a/src/core/load-fragment.c
923a60
+++ b/src/core/load-fragment.c
923a60
@@ -1119,6 +1119,49 @@ int config_parse_limit(const char *unit,
923a60
         return 0;
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
+
923a60
+        struct rlimit **rl = data;
923a60
+        uint64_t bytes;
923a60
+
923a60
+        assert(filename);
923a60
+        assert(lvalue);
923a60
+        assert(rvalue);
923a60
+        assert(data);
923a60
+
923a60
+        rl += ltype;
923a60
+
923a60
+        if (streq(rvalue, "infinity"))
923a60
+                bytes = (uint64_t) RLIM_INFINITY;
923a60
+        else {
923a60
+                int 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
+        }
923a60
+
923a60
+        if (!*rl) {
923a60
+                *rl = new(struct rlimit, 1);
923a60
+                if (!*rl)
923a60
+                        return log_oom();
923a60
+        }
923a60
+
923a60
+        (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) bytes;
923a60
+        return 0;
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/core/load-fragment.h b/src/core/load-fragment.h
923a60
index ce10d03c3f..2d509d0cb4 100644
923a60
--- a/src/core/load-fragment.h
923a60
+++ b/src/core/load-fragment.h
923a60
@@ -56,6 +56,7 @@ int config_parse_exec_capabilities(const char *unit, const char *filename, unsig
923a60
 int config_parse_exec_secure_bits(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_bounding_set(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_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_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_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 2aec40b0bb..60ea36c3cb 100644
923a60
--- a/src/core/main.c
923a60
+++ b/src/core/main.c
923a60
@@ -655,18 +655,18 @@ static int parse_config_file(void) {
923a60
                 { "Manager", "DefaultStartLimitBurst",    config_parse_unsigned,         0, &arg_default_start_limit_burst         },
923a60
                 { "Manager", "DefaultEnvironment",        config_parse_environ,          0, &arg_default_environment               },
923a60
                 { "Manager", "DefaultLimitCPU",           config_parse_limit,            0, &arg_default_rlimit[RLIMIT_CPU]        },
923a60
-                { "Manager", "DefaultLimitFSIZE",         config_parse_limit,            0, &arg_default_rlimit[RLIMIT_FSIZE]      },
923a60
-                { "Manager", "DefaultLimitDATA",          config_parse_limit,            0, &arg_default_rlimit[RLIMIT_DATA]       },
923a60
-                { "Manager", "DefaultLimitSTACK",         config_parse_limit,            0, &arg_default_rlimit[RLIMIT_STACK]      },
923a60
-                { "Manager", "DefaultLimitCORE",          config_parse_limit,            0, &arg_default_rlimit[RLIMIT_CORE]       },
923a60
-                { "Manager", "DefaultLimitRSS",           config_parse_limit,            0, &arg_default_rlimit[RLIMIT_RSS]        },
923a60
+                { "Manager", "DefaultLimitFSIZE",         config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_FSIZE]      },
923a60
+                { "Manager", "DefaultLimitDATA",          config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_DATA]       },
923a60
+                { "Manager", "DefaultLimitSTACK",         config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_STACK]      },
923a60
+                { "Manager", "DefaultLimitCORE",          config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_CORE]       },
923a60
+                { "Manager", "DefaultLimitRSS",           config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_RSS]        },
923a60
                 { "Manager", "DefaultLimitNOFILE",        config_parse_limit,            0, &arg_default_rlimit[RLIMIT_NOFILE]     },
923a60
-                { "Manager", "DefaultLimitAS",            config_parse_limit,            0, &arg_default_rlimit[RLIMIT_AS]         },
923a60
+                { "Manager", "DefaultLimitAS",            config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_AS]         },
923a60
                 { "Manager", "DefaultLimitNPROC",         config_parse_limit,            0, &arg_default_rlimit[RLIMIT_NPROC]      },
923a60
-                { "Manager", "DefaultLimitMEMLOCK",       config_parse_limit,            0, &arg_default_rlimit[RLIMIT_MEMLOCK]    },
923a60
+                { "Manager", "DefaultLimitMEMLOCK",       config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_MEMLOCK]    },
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_limit,            0, &arg_default_rlimit[RLIMIT_MSGQUEUE]   },
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", "DefaultLimitRTPRIO",        config_parse_limit,            0, &arg_default_rlimit[RLIMIT_RTPRIO]     },
923a60
                 { "Manager", "DefaultLimitRTTIME",        config_parse_limit,            0, &arg_default_rlimit[RLIMIT_RTTIME]     },