basic: add new rlimit_format() call
This formats a struct rlimit the way rlimit_parse() expects it.
This commit is contained in:
parent
cb9712492f
commit
99d4f5e5c0
|
@ -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",
|
||||
|
|
|
@ -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 })
|
||||
|
|
|
@ -17,12 +17,37 @@
|
|||
|
||||
#include <sys/resource.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "capability-util.h"
|
||||
#include "macro.h"
|
||||
#include "rlimit-util.h"
|
||||
#include "string-util.h"
|
||||
#include "util.h"
|
||||
|
||||
static void test_rlimit_parse_format(int resource, const char *string, rlim_t soft, rlim_t hard, int ret, const char *formatted) {
|
||||
_cleanup_free_ char *f = NULL;
|
||||
struct rlimit rl = {
|
||||
.rlim_cur = 4711,
|
||||
.rlim_max = 4712,
|
||||
}, rl2 = {
|
||||
.rlim_cur = 4713,
|
||||
.rlim_max = 4714
|
||||
};
|
||||
|
||||
assert_se(rlimit_parse(resource, string, &rl) == ret);
|
||||
if (ret < 0)
|
||||
return;
|
||||
|
||||
assert_se(rl.rlim_cur == soft);
|
||||
assert_se(rl.rlim_max == hard);
|
||||
|
||||
assert_se(rlimit_format(&rl, &f) >= 0);
|
||||
assert_se(streq(formatted, f));
|
||||
|
||||
assert_se(rlimit_parse(resource, formatted, &rl2) >= 0);
|
||||
assert_se(memcmp(&rl, &rl2, sizeof(struct rlimit)) == 0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
struct rlimit old, new, high;
|
||||
struct rlimit err = {
|
||||
|
@ -65,5 +90,15 @@ int main(int argc, char *argv[]) {
|
|||
assert_se(old.rlim_cur == new.rlim_cur);
|
||||
assert_se(old.rlim_max == new.rlim_max);
|
||||
|
||||
test_rlimit_parse_format(RLIMIT_NOFILE, "4:5", 4, 5, 0, "4:5");
|
||||
test_rlimit_parse_format(RLIMIT_NOFILE, "6", 6, 6, 0, "6");
|
||||
test_rlimit_parse_format(RLIMIT_NOFILE, "infinity", RLIM_INFINITY, RLIM_INFINITY, 0, "infinity");
|
||||
test_rlimit_parse_format(RLIMIT_NOFILE, "infinity:infinity", RLIM_INFINITY, RLIM_INFINITY, 0, "infinity");
|
||||
test_rlimit_parse_format(RLIMIT_NOFILE, "8:infinity", 8, RLIM_INFINITY, 0, "8:infinity");
|
||||
test_rlimit_parse_format(RLIMIT_CPU, "25min:13h", (25*USEC_PER_MINUTE) / USEC_PER_SEC, (13*USEC_PER_HOUR) / USEC_PER_SEC, 0, "1500:46800");
|
||||
test_rlimit_parse_format(RLIMIT_NOFILE, "", 0, 0, -EINVAL, NULL);
|
||||
test_rlimit_parse_format(RLIMIT_NOFILE, "5:4", 0, 0, -EILSEQ, NULL);
|
||||
test_rlimit_parse_format(RLIMIT_NOFILE, "5:4:3", 0, 0, -EINVAL, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue