summaryrefslogtreecommitdiff
path: root/src/basic/rlimit-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2016-02-01 21:15:07 +0100
committerLennart Poettering <lennart@poettering.net>2016-02-01 22:18:16 +0100
commit99d4f5e5c0d2532159542519e683f976f881f0f5 (patch)
treeb80732d258a9a3abf896231996eb8e8856c1da30 /src/basic/rlimit-util.c
parentcb9712492f94153b7ce6fc03d6dd3fd95c87baa5 (diff)
basic: add new rlimit_format() call
This formats a struct rlimit the way rlimit_parse() expects it.
Diffstat (limited to 'src/basic/rlimit-util.c')
-rw-r--r--src/basic/rlimit-util.c24
1 files changed, 24 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",