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