From b04983c3f35566f38fed2f18c9711541d0c21e27 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Fri, 29 Dec 2017 20:08:02 +0100 Subject: [PATCH] conf: Make rlimit [hard|soft] optional => possible to set both Signed-off-by: Joachim Nilsson --- doc/config.md | 19 +++++++++++-------- src/conf.c | 32 +++++++++++++++++++------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/doc/config.md b/doc/config.md index f8b54c19..e2fec6d9 100644 --- a/doc/config.md +++ b/doc/config.md @@ -51,15 +51,15 @@ Syntax * `network ` Script or program to bring up networking, with optional arguments -* `rlimit RESOURCE ` - Set the hard or soft limit for a resource. `RESOURCE` is a lower-case - string matching the `RLIMIT_` constants from `setrlimit(2)`, without - the prefix. E.g. to set `RLIMIT_CPU`, use `cpu`. +* `rlimit [hard|soft] RESOURCE ` Set the hard or soft + limit for a resource, or both if that argument is omitted. `RESOURCE` + is the lower-case `RLIMIT_` string constants from `setrlimit(2)`, + without the prefix. E.g. to set `RLIMIT_CPU`, use `cpu`. - The limit is an integer that depends on the resource being modified, - see the man page, or the kernel `/proc/PID/limits` file, for more - information. Finit versions before v3.1 used `infinity` for - `unlimited`, which is still a supported keyword. + LIMIT is an integer that depends on the resource being modified, see + the man page, or the kernel `/proc/PID/limits` file, for details. + Finit versions before v3.1 used `infinity` for `unlimited`, which is + still supported, albeit deprecated. ```shell # No process is allowed more than 8MB of address space @@ -67,6 +67,9 @@ Syntax # Core dumps may be arbitrarily large rlimit soft core infinity + + # CPU limit for all services, soft & hard = 10 sec + rlimit cpu 10 ``` * `runlevel ` diff --git a/src/conf.c b/src/conf.c index 9cb153fa..cecc4c52 100644 --- a/src/conf.c +++ b/src/conf.c @@ -221,11 +221,12 @@ char *lim2str(struct rlimit *rlim) return buf; } +/* First form: `rlimit RESOURCE LIMIT` */ void conf_parse_rlimit(char *line, struct rlimit arr[]) { char *level, *limit, *val; int resource = -1; - rlim_t cfg, *set; + rlim_t cfg; level = strtok(line, " \t"); if (!level) @@ -235,21 +236,18 @@ void conf_parse_rlimit(char *line, struct rlimit arr[]) if (!limit) goto error; + val = strtok(NULL, " \t"); + if (!val) { + /* Second form: `rlimit RESOURCE LIMIT` */ + val = limit; + limit = level; + level = "both"; + } + resource = str2rlim(limit); if (resource < 0 || resource > RLIMIT_NLIMITS) goto error; - val = strtok(NULL, " \t"); - if (!val) - goto error; - - if (!strcmp(level, "soft")) - set = &arr[resource].rlim_cur; - else if (!strcmp(level, "hard")) - set = &arr[resource].rlim_max; - else - goto error; - /* Official keyword from v3.1 is `unlimited`, from prlimit(1) */ if (!strcmp(val, "unlimited") || !strcmp(val, "infinity")) { cfg = RLIM_INFINITY; @@ -264,7 +262,15 @@ void conf_parse_rlimit(char *line, struct rlimit arr[]) } } - *set = cfg; + if (!strcmp(level, "soft")) + arr[resource].rlim_cur = cfg; + else if (!strcmp(level, "hard")) + arr[resource].rlim_max = cfg; + else if (!strcmp(level, "both")) + arr[resource].rlim_max = arr[resource].rlim_cur = cfg; + else + goto error; + return; error: logit(LOG_WARNING, "rlimit: parse error");