803fb7
From 99074eebc911728a41167c1962231e11b5e3cddd Mon Sep 17 00:00:00 2001
803fb7
From: Karel Zak <kzak@redhat.com>
803fb7
Date: Fri, 6 Nov 2015 11:06:52 +0100
803fb7
Subject: [PATCH] core: support IEC suffixes for RLIMIT stuff
803fb7
803fb7
Let's make things more user-friendly and support for example
803fb7
803fb7
  LimitAS=16G
803fb7
803fb7
rather than force users to always use LimitAS=16106127360.
803fb7
803fb7
The change is relevant for options:
803fb7
803fb7
  [Default]Limit{FSIZE,DATA,STACK,CORE,RSS,AS,MEMLOCK,MSGQUEUE}
803fb7
803fb7
The patch introduces config_parse_bytes_limit(), it's the same as
803fb7
config_parse_limit() but uses parse_size() tu support the suffixes.
803fb7
803fb7
Addresses: https://github.com/systemd/systemd/issues/1772
803fb7
803fb7
Cherry-picked from: 412ea7a936ebaa5342a4c2abf48b9e408e6ba5dc
803fb7
Related: #1351415
803fb7
---
803fb7
 man/systemd-system.conf.xml           |  6 +++--
803fb7
 man/systemd.exec.xml                  |  4 +++-
803fb7
 src/core/load-fragment-gperf.gperf.m4 | 16 ++++++-------
803fb7
 src/core/load-fragment.c              | 43 +++++++++++++++++++++++++++++++++++
803fb7
 src/core/load-fragment.h              |  1 +
803fb7
 src/core/main.c                       | 16 ++++++-------
803fb7
 6 files changed, 67 insertions(+), 19 deletions(-)
803fb7
803fb7
diff --git a/man/systemd-system.conf.xml b/man/systemd-system.conf.xml
803fb7
index ca25c93a1..b7d9cdee0 100644
803fb7
--- a/man/systemd-system.conf.xml
803fb7
+++ b/man/systemd-system.conf.xml
803fb7
@@ -327,8 +327,10 @@
803fb7
         resource limits for units. See
803fb7
         <citerefentry><refentrytitle>setrlimit</refentrytitle><manvolnum>2</manvolnum></citerefentry>
803fb7
         for details. Use the string <varname>infinity</varname> to
803fb7
-        configure no limit on a specific resource. These settings may
803fb7
-        be overridden in individual units using the corresponding
803fb7
+        configure no limit on a specific resource. The multiplicative suffixes
803fb7
+        K (=1024), M (=1024*1024) and so on for G, T, P and E may be used for
803fb7
+        resource limits measured in bytes (e.g. DefaultLimitAS=16G). These
803fb7
+        settings may be overridden in individual units using the corresponding
803fb7
         LimitXXX= directives. Note that these resource limits are only
803fb7
         defaults for units, they are not applied to PID 1
803fb7
         itself.</para></listitem>
803fb7
diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml
803fb7
index 6af7c7ae5..25aea1655 100644
803fb7
--- a/man/systemd.exec.xml
803fb7
+++ b/man/systemd.exec.xml
803fb7
@@ -559,7 +559,9 @@
803fb7
         of various resources for executed processes. See
803fb7
         <citerefentry><refentrytitle>setrlimit</refentrytitle><manvolnum>2</manvolnum></citerefentry>
803fb7
         for details. Use the string <varname>infinity</varname> to
803fb7
-        configure no limit on a specific resource.</para></listitem>
803fb7
+        configure no limit on a specific resource. The multiplicative suffixes
803fb7
+        K (=1024), M (=1024*1024) and so on for G, T, P and E may be used for
803fb7
+        resource limits measured in bytes (e.g. LimitAS=16G).</para></listitem>
803fb7
 
803fb7
         
803fb7
           <title>Limit directives and their equivalent with ulimit</title>
803fb7
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
803fb7
index 85d979751..c3461a0a6 100644
803fb7
--- a/src/core/load-fragment-gperf.gperf.m4
803fb7
+++ b/src/core/load-fragment-gperf.gperf.m4
803fb7
@@ -59,18 +59,18 @@ $1.SystemCallArchitectures,      config_parse_warn_compat,           DISABLED_CO
803fb7
 $1.SystemCallErrorNumber,        config_parse_warn_compat,           DISABLED_CONFIGURATION,        0
803fb7
 $1.RestrictAddressFamilies,      config_parse_warn_compat,           DISABLED_CONFIGURATION,        0')
803fb7
 $1.LimitCPU,                     config_parse_limit,                 RLIMIT_CPU,                    offsetof($1, exec_context.rlimit)
803fb7
-$1.LimitFSIZE,                   config_parse_limit,                 RLIMIT_FSIZE,                  offsetof($1, exec_context.rlimit)
803fb7
-$1.LimitDATA,                    config_parse_limit,                 RLIMIT_DATA,                   offsetof($1, exec_context.rlimit)
803fb7
-$1.LimitSTACK,                   config_parse_limit,                 RLIMIT_STACK,                  offsetof($1, exec_context.rlimit)
803fb7
-$1.LimitCORE,                    config_parse_limit,                 RLIMIT_CORE,                   offsetof($1, exec_context.rlimit)
803fb7
-$1.LimitRSS,                     config_parse_limit,                 RLIMIT_RSS,                    offsetof($1, exec_context.rlimit)
803fb7
+$1.LimitFSIZE,                   config_parse_bytes_limit,           RLIMIT_FSIZE,                  offsetof($1, exec_context.rlimit)
803fb7
+$1.LimitDATA,                    config_parse_bytes_limit,           RLIMIT_DATA,                   offsetof($1, exec_context.rlimit)
803fb7
+$1.LimitSTACK,                   config_parse_bytes_limit,           RLIMIT_STACK,                  offsetof($1, exec_context.rlimit)
803fb7
+$1.LimitCORE,                    config_parse_bytes_limit,           RLIMIT_CORE,                   offsetof($1, exec_context.rlimit)
803fb7
+$1.LimitRSS,                     config_parse_bytes_limit,           RLIMIT_RSS,                    offsetof($1, exec_context.rlimit)
803fb7
 $1.LimitNOFILE,                  config_parse_limit,                 RLIMIT_NOFILE,                 offsetof($1, exec_context.rlimit)
803fb7
-$1.LimitAS,                      config_parse_limit,                 RLIMIT_AS,                     offsetof($1, exec_context.rlimit)
803fb7
+$1.LimitAS,                      config_parse_bytes_limit,           RLIMIT_AS,                     offsetof($1, exec_context.rlimit)
803fb7
 $1.LimitNPROC,                   config_parse_limit,                 RLIMIT_NPROC,                  offsetof($1, exec_context.rlimit)
803fb7
-$1.LimitMEMLOCK,                 config_parse_limit,                 RLIMIT_MEMLOCK,                offsetof($1, exec_context.rlimit)
803fb7
+$1.LimitMEMLOCK,                 config_parse_bytes_limit,           RLIMIT_MEMLOCK,                offsetof($1, exec_context.rlimit)
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_limit,                 RLIMIT_MSGQUEUE,               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.LimitRTPRIO,                  config_parse_limit,                 RLIMIT_RTPRIO,                 offsetof($1, exec_context.rlimit)
803fb7
 $1.LimitRTTIME,                  config_parse_limit,                 RLIMIT_RTTIME,                 offsetof($1, exec_context.rlimit)
803fb7
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
803fb7
index b188ec99d..dbb45b20f 100644
803fb7
--- a/src/core/load-fragment.c
803fb7
+++ b/src/core/load-fragment.c
803fb7
@@ -1119,6 +1119,49 @@ int config_parse_limit(const char *unit,
803fb7
         return 0;
803fb7
 }
803fb7
 
803fb7
+int config_parse_bytes_limit(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
+        uint64_t bytes;
803fb7
+
803fb7
+        assert(filename);
803fb7
+        assert(lvalue);
803fb7
+        assert(rvalue);
803fb7
+        assert(data);
803fb7
+
803fb7
+        rl += ltype;
803fb7
+
803fb7
+        if (streq(rvalue, "infinity"))
803fb7
+                bytes = (uint64_t) RLIM_INFINITY;
803fb7
+        else {
803fb7
+                int r;
803fb7
+
803fb7
+                r = parse_size(rvalue, 1024, &bytes);
803fb7
+                if (r < 0) {
803fb7
+                        log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
803fb7
+                        return 0;
803fb7
+                }
803fb7
+        }
803fb7
+
803fb7
+        if (!*rl) {
803fb7
+                *rl = new(struct rlimit, 1);
803fb7
+                if (!*rl)
803fb7
+                        return log_oom();
803fb7
+        }
803fb7
+
803fb7
+        (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) bytes;
803fb7
+        return 0;
803fb7
+}
803fb7
+
803fb7
 #ifdef HAVE_SYSV_COMPAT
803fb7
 int config_parse_sysv_priority(const char *unit,
803fb7
                                const char *filename,
803fb7
diff --git a/src/core/load-fragment.h b/src/core/load-fragment.h
803fb7
index ce10d03c3..2d509d0cb 100644
803fb7
--- a/src/core/load-fragment.h
803fb7
+++ b/src/core/load-fragment.h
803fb7
@@ -56,6 +56,7 @@ int config_parse_exec_capabilities(const char *unit, const char *filename, unsig
803fb7
 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);
803fb7
 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);
803fb7
 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);
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_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 2aec40b0b..60ea36c3c 100644
803fb7
--- a/src/core/main.c
803fb7
+++ b/src/core/main.c
803fb7
@@ -655,18 +655,18 @@ static int parse_config_file(void) {
803fb7
                 { "Manager", "DefaultStartLimitBurst",    config_parse_unsigned,         0, &arg_default_start_limit_burst         },
803fb7
                 { "Manager", "DefaultEnvironment",        config_parse_environ,          0, &arg_default_environment               },
803fb7
                 { "Manager", "DefaultLimitCPU",           config_parse_limit,            0, &arg_default_rlimit[RLIMIT_CPU]        },
803fb7
-                { "Manager", "DefaultLimitFSIZE",         config_parse_limit,            0, &arg_default_rlimit[RLIMIT_FSIZE]      },
803fb7
-                { "Manager", "DefaultLimitDATA",          config_parse_limit,            0, &arg_default_rlimit[RLIMIT_DATA]       },
803fb7
-                { "Manager", "DefaultLimitSTACK",         config_parse_limit,            0, &arg_default_rlimit[RLIMIT_STACK]      },
803fb7
-                { "Manager", "DefaultLimitCORE",          config_parse_limit,            0, &arg_default_rlimit[RLIMIT_CORE]       },
803fb7
-                { "Manager", "DefaultLimitRSS",           config_parse_limit,            0, &arg_default_rlimit[RLIMIT_RSS]        },
803fb7
+                { "Manager", "DefaultLimitFSIZE",         config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_FSIZE]      },
803fb7
+                { "Manager", "DefaultLimitDATA",          config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_DATA]       },
803fb7
+                { "Manager", "DefaultLimitSTACK",         config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_STACK]      },
803fb7
+                { "Manager", "DefaultLimitCORE",          config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_CORE]       },
803fb7
+                { "Manager", "DefaultLimitRSS",           config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_RSS]        },
803fb7
                 { "Manager", "DefaultLimitNOFILE",        config_parse_limit,            0, &arg_default_rlimit[RLIMIT_NOFILE]     },
803fb7
-                { "Manager", "DefaultLimitAS",            config_parse_limit,            0, &arg_default_rlimit[RLIMIT_AS]         },
803fb7
+                { "Manager", "DefaultLimitAS",            config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_AS]         },
803fb7
                 { "Manager", "DefaultLimitNPROC",         config_parse_limit,            0, &arg_default_rlimit[RLIMIT_NPROC]      },
803fb7
-                { "Manager", "DefaultLimitMEMLOCK",       config_parse_limit,            0, &arg_default_rlimit[RLIMIT_MEMLOCK]    },
803fb7
+                { "Manager", "DefaultLimitMEMLOCK",       config_parse_bytes_limit,      0, &arg_default_rlimit[RLIMIT_MEMLOCK]    },
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_limit,            0, &arg_default_rlimit[RLIMIT_MSGQUEUE]   },
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", "DefaultLimitRTPRIO",        config_parse_limit,            0, &arg_default_rlimit[RLIMIT_RTPRIO]     },
803fb7
                 { "Manager", "DefaultLimitRTTIME",        config_parse_limit,            0, &arg_default_rlimit[RLIMIT_RTTIME]     },