conf: Make rlimit [hard|soft] optional => possible to set both

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2017-12-29 20:15:12 +01:00
1 parent f2d142ef57
commit b04983c3f3
2 files changed
+30 -21

No files matched your search

+11 -8
View File
@@ -51,15 +51,15 @@ Syntax
* `network <PATH>` * `network <PATH>`
Script or program to bring up networking, with optional arguments Script or program to bring up networking, with optional arguments
* `rlimit <hard|soft> RESOURCE <LIMIT|unlimited>` * `rlimit [hard|soft] RESOURCE <LIMIT|unlimited>` Set the hard or soft
Set the hard or soft limit for a resource. `RESOURCE` is a lower-case limit for a resource, or both if that argument is omitted. `RESOURCE`
string matching the `RLIMIT_` constants from `setrlimit(2)`, without is the lower-case `RLIMIT_` string constants from `setrlimit(2)`,
the prefix. E.g. to set `RLIMIT_CPU`, use `cpu`. without the prefix. E.g. to set `RLIMIT_CPU`, use `cpu`.
The limit is an integer that depends on the resource being modified, LIMIT is an integer that depends on the resource being modified, see
see the man page, or the kernel `/proc/PID/limits` file, for more the man page, or the kernel `/proc/PID/limits` file, for details.
information. Finit versions before v3.1 used `infinity` for Finit versions before v3.1 used `infinity` for `unlimited`, which is
`unlimited`, which is still a supported keyword. still supported, albeit deprecated.
```shell ```shell
# No process is allowed more than 8MB of address space # No process is allowed more than 8MB of address space
@@ -67,6 +67,9 @@ Syntax
# Core dumps may be arbitrarily large # Core dumps may be arbitrarily large
rlimit soft core infinity rlimit soft core infinity
# CPU limit for all services, soft & hard = 10 sec
rlimit cpu 10
``` ```
* `runlevel <N>` * `runlevel <N>`
+19 -13
View File
@@ -221,11 +221,12 @@ char *lim2str(struct rlimit *rlim)
return buf; return buf;
} }
/* First form: `rlimit <hard|soft> RESOURCE LIMIT` */
void conf_parse_rlimit(char *line, struct rlimit arr[]) void conf_parse_rlimit(char *line, struct rlimit arr[])
{ {
char *level, *limit, *val; char *level, *limit, *val;
int resource = -1; int resource = -1;
rlim_t cfg, *set; rlim_t cfg;
level = strtok(line, " \t"); level = strtok(line, " \t");
if (!level) if (!level)
@@ -235,21 +236,18 @@ void conf_parse_rlimit(char *line, struct rlimit arr[])
if (!limit) if (!limit)
goto error; goto error;
val = strtok(NULL, " \t");
if (!val) {
/* Second form: `rlimit RESOURCE LIMIT` */
val = limit;
limit = level;
level = "both";
}
resource = str2rlim(limit); resource = str2rlim(limit);
if (resource < 0 || resource > RLIMIT_NLIMITS) if (resource < 0 || resource > RLIMIT_NLIMITS)
goto error; 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) */ /* Official keyword from v3.1 is `unlimited`, from prlimit(1) */
if (!strcmp(val, "unlimited") || !strcmp(val, "infinity")) { if (!strcmp(val, "unlimited") || !strcmp(val, "infinity")) {
cfg = RLIM_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; return;
error: error:
logit(LOG_WARNING, "rlimit: parse error"); logit(LOG_WARNING, "rlimit: parse error");