Skip to content

Commit

Permalink
core: support <soft:hard> ranges for RLIMIT options
Browse files Browse the repository at this point in the history
The new parser supports:

 <value>       - specify both limits to the same value
 <soft:hard>   - specify both limits

the size or time specific suffixes are supported, for example

  LimitRTTIME=1sec
  LimitAS=4G:16G

The patch introduces parse_rlimit_range() and rlim type (size, sec,
usec, etc.) specific parsers. No code is duplicated now.

The patch also sync docs for DefaultLimitXXX= and LimitXXX=.

References: systemd/systemd#1769

Cherry-picked from: 91518d2
Resolves: #1351415
  • Loading branch information
karelzak authored and apovichuk-stratoscale committed Jun 18, 2017
1 parent 1e5154a commit 539bd33
Show file tree
Hide file tree
Showing 6 changed files with 667 additions and 120 deletions.
27 changes: 20 additions & 7 deletions man/systemd-system.conf.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,26 @@
<listitem><para>These settings control various default
resource limits for units. See
<citerefentry><refentrytitle>setrlimit</refentrytitle><manvolnum>2</manvolnum></citerefentry>
for details. Use the string <varname>infinity</varname> to
configure no limit on a specific resource. The multiplicative suffixes
K (=1024), M (=1024*1024) and so on for G, T, P and E may be used for
resource limits measured in bytes (e.g. DefaultLimitAS=16G). These
settings may be overridden in individual units using the corresponding
LimitXXX= directives. Note that these resource limits are only
defaults for units, they are not applied to PID 1
for details. The resource limit is possible to specify in two formats,
<option>value</option> to set soft and hard limits to the same value,
or <option>soft:hard</option> to set both limits individually (e.g. DefaultLimitAS=4G:16G).
Use the string <varname>infinity</varname> to
configure no limit on a specific resource. The multiplicative
suffixes K (=1024), M (=1024*1024) and so on for G, T, P and E
may be used for resource limits measured in bytes
(e.g. DefaultLimitAS=16G). For the limits referring to time values,
the usual time units ms, s, min, h and so on may be used (see
<citerefentry><refentrytitle>systemd.time</refentrytitle><manvolnum>7</manvolnum></citerefentry>
for details). Note that if no time unit is specified for
<varname>DefaultLimitCPU=</varname> the default unit of seconds is
implied, while for <varname>DefaultLimitRTTIME=</varname> the default
unit of microseconds is implied. Also, note that the effective
granularity of the limits might influence their
enforcement. For example, time limits specified for
<varname>DefaultLimitCPU=</varname> will be rounded up implicitly to
multiples of 1s. These settings may be overridden in individual units
using the corresponding LimitXXX= directives. Note that these resource
limits are only defaults for units, they are not applied to PID 1
itself.</para></listitem>
</varlistentry>
</variablelist>
Expand Down
5 changes: 4 additions & 1 deletion man/systemd.exec.xml
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,10 @@
<listitem><para>These settings set both soft and hard limits
of various resources for executed processes. See
<citerefentry><refentrytitle>setrlimit</refentrytitle><manvolnum>2</manvolnum></citerefentry>
for details. Use the string <varname>infinity</varname> to
for details. The resource limit is possible to specify in two formats,
<option>value</option> to set soft and hard limits to the same value,
or <option>soft:hard</option> to set both limits individually (e.g. LimitAS=4G:16G).
Use the string <varname>infinity</varname> to
configure no limit on a specific resource. The multiplicative
suffixes K (=1024), M (=1024*1024) and so on for G, T, P and E
may be used for resource limits measured in bytes
Expand Down
243 changes: 131 additions & 112 deletions src/core/load-fragment.c
Original file line number Diff line number Diff line change
Expand Up @@ -1075,94 +1075,121 @@ int config_parse_bounding_set(const char *unit,
return 0;
}

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) {

struct rlimit **rl = data;
unsigned long long u;
static int rlim_parse_u64(const char *val, rlim_t *res) {
int r = 0;

assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
if (streq(val, "infinity"))
*res = RLIM_INFINITY;
else {
uint64_t u;

rl += ltype;
/* setrlimit(2) suggests rlim_t is always 64bit on Linux. */
assert_cc(sizeof(rlim_t) == sizeof(uint64_t));

r = safe_atou64(val, &u);
if (r >= 0 && u >= (uint64_t) RLIM_INFINITY)
r = -ERANGE;
if (r == 0)
*res = (rlim_t) u;
}
return r;
}

if (streq(rvalue, "infinity"))
u = (unsigned long long) RLIM_INFINITY;
static int rlim_parse_size(const char *val, rlim_t *res) {
int r = 0;

if (streq(val, "infinity"))
*res = RLIM_INFINITY;
else {
int r;
off_t u;

r = safe_atollu(rvalue, &u);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, -r,
"Failed to parse resource value, ignoring: %s", rvalue);
return 0;
}
r = parse_size(val, 1024, &u);
if (r >= 0 && u >= (off_t) RLIM_INFINITY)
r = -ERANGE;
if (r == 0)
*res = (rlim_t) u;
}
return r;
}

if (!*rl) {
*rl = new(struct rlimit, 1);
if (!*rl)
return log_oom();
}
static int rlim_parse_sec(const char *val, rlim_t *res) {
int r = 0;

(*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
return 0;
if (streq(val, "infinity"))
*res = RLIM_INFINITY;
else {
usec_t t;

r = parse_sec(val, &t);
if (r < 0)
return r;
if (t == USEC_INFINITY)
*res = RLIM_INFINITY;
else
*res = (rlim_t) (DIV_ROUND_UP(t, USEC_PER_SEC));

}
return r;
}

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) {
static int rlim_parse_usec(const char *val, rlim_t *res) {
int r = 0;

struct rlimit **rl = data;
uint64_t bytes;
if (streq(val, "infinity"))
*res = RLIM_INFINITY;
else {
usec_t t;

assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
r = parse_time(val, &t, 1);
if (r < 0)
return r;
if (t == USEC_INFINITY)
*res = RLIM_INFINITY;
else
*res = (rlim_t) t;
}
return r;
}

rl += ltype;
static int parse_rlimit_range(
const char *unit,
const char *filename,
unsigned line,
const char *value,
struct rlimit **rl,
int (*rlim_parser)(const char *, rlim_t *)) {

if (streq(rvalue, "infinity"))
bytes = (uint64_t) RLIM_INFINITY;
else {
int r;
rlim_t soft, hard;
_cleanup_free_ char *sword = NULL, *hword = NULL;
int nwords, r;

r = parse_size(rvalue, 1024, &bytes);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
return 0;
}
assert(value);

/* <value> or <soft:hard> */
nwords = extract_many_words(&value, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &sword, &hword, NULL);
r = nwords < 0 ? nwords : nwords == 0 ? -EINVAL : 0;

if (r == 0)
r = rlim_parser(sword, &soft);
if (r == 0 && nwords == 2)
r = rlim_parser(hword, &hard);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", value);
return 0;
}

if (!*rl) {
*rl = new(struct rlimit, 1);
if (!*rl)
return log_oom();
}

(*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) bytes;
(*rl)->rlim_cur = soft;
(*rl)->rlim_max = nwords == 2 ? hard : soft;
return 0;
}

int config_parse_sec_limit(
int config_parse_limit(
const char *unit,
const char *filename,
unsigned line,
Expand All @@ -1175,45 +1202,40 @@ int config_parse_sec_limit(
void *userdata) {

struct rlimit **rl = data;
rlim_t seconds;
int r;

assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);

rl += ltype;
return parse_rlimit_range(unit, filename, line, rvalue, rl, rlim_parse_u64);
}

if (streq(rvalue, "infinity"))
seconds = RLIM_INFINITY;
else {
usec_t t;

r = parse_sec(rvalue, &t);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
return 0;
}
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) {

if (t == USEC_INFINITY)
seconds = RLIM_INFINITY;
else
seconds = (rlim_t) (DIV_ROUND_UP(t, USEC_PER_SEC));
}
struct rlimit **rl = data;

if (!*rl) {
*rl = new(struct rlimit, 1);
if (!*rl)
return log_oom();
}
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);

(*rl)->rlim_cur = (*rl)->rlim_max = seconds;
return 0;
rl += ltype;
return parse_rlimit_range(unit, filename, line, rvalue, rl, rlim_parse_size);
}


int config_parse_usec_limit(
int config_parse_sec_limit(
const char *unit,
const char *filename,
unsigned line,
Expand All @@ -1226,43 +1248,40 @@ int config_parse_usec_limit(
void *userdata) {

struct rlimit **rl = data;
rlim_t useconds;
int r;

assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);

rl += ltype;
return parse_rlimit_range(unit, filename, line, rvalue, rl, rlim_parse_sec);
}

if (streq(rvalue, "infinity"))
useconds = RLIM_INFINITY;
else {
usec_t t;

r = parse_time(rvalue, &t, 1);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
return 0;
}
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) {

if (t == USEC_INFINITY)
useconds = RLIM_INFINITY;
else
useconds = (rlim_t) t;
}
struct rlimit **rl = data;

if (!*rl) {
*rl = new(struct rlimit, 1);
if (!*rl)
return log_oom();
}
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);

(*rl)->rlim_cur = (*rl)->rlim_max = useconds;
return 0;
rl += ltype;
return parse_rlimit_range(unit, filename, line, rvalue, rl, rlim_parse_usec);
}


#ifdef HAVE_SYSV_COMPAT
int config_parse_sysv_priority(const char *unit,
const char *filename,
Expand Down
Loading

0 comments on commit 539bd33

Please sign in to comment.