basic: add new rlimit_format() call

This formats a struct rlimit the way rlimit_parse() expects it.
This commit is contained in:
Lennart Poettering
2016-02-01 22:18:16 +01:00
parent cb9712492f
commit 99d4f5e5c0
3 changed files with 61 additions and 0 deletions
+24
View File
@@ -227,6 +227,30 @@ int rlimit_parse(int resource, const char *val, struct rlimit *ret) {
return 0;
}
int rlimit_format(const struct rlimit *rl, char **ret) {
char *s = NULL;
assert(rl);
assert(ret);
if (rl->rlim_cur >= RLIM_INFINITY && rl->rlim_max >= RLIM_INFINITY)
s = strdup("infinity");
else if (rl->rlim_cur >= RLIM_INFINITY)
(void) asprintf(&s, "infinity:" RLIM_FMT, rl->rlim_max);
else if (rl->rlim_max >= RLIM_INFINITY)
(void) asprintf(&s, RLIM_FMT ":infinity", rl->rlim_cur);
else if (rl->rlim_cur == rl->rlim_max)
(void) asprintf(&s, RLIM_FMT, rl->rlim_cur);
else
(void) asprintf(&s, RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max);
if (!s)
return -ENOMEM;
*ret = s;
return 0;
}
static const char* const rlimit_table[_RLIMIT_MAX] = {
[RLIMIT_CPU] = "LimitCPU",
[RLIMIT_FSIZE] = "LimitFSIZE",
+2
View File
@@ -33,4 +33,6 @@ int setrlimit_closest(int resource, const struct rlimit *rlim);
int rlimit_parse_one(int resource, const char *val, rlim_t *ret);
int rlimit_parse(int resource, const char *val, struct rlimit *ret);
int rlimit_format(const struct rlimit *rl, char **ret);
#define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim })