diff options
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/rlimit-util.c | 24 | ||||
-rw-r--r-- | src/basic/rlimit-util.h | 2 |
2 files changed, 26 insertions, 0 deletions
diff --git a/src/basic/rlimit-util.c b/src/basic/rlimit-util.c index 11476ac8d1..8a921a27cb 100644 --- a/src/basic/rlimit-util.c +++ b/src/basic/rlimit-util.c @@ -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", diff --git a/src/basic/rlimit-util.h b/src/basic/rlimit-util.h index fd2f7ff7cf..abf3c57934 100644 --- a/src/basic/rlimit-util.h +++ b/src/basic/rlimit-util.h @@ -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 }) |