rlimit-util.c revision 99d4f5e5c0d2532159542519e683f976f881f0f5
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering/***
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering This file is part of systemd.
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering Copyright 2010 Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering (at your option) any later version.
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering systemd is distributed in the hope that it will be useful, but
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering Lesser General Public License for more details.
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering***/
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen#include <errno.h>
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen#include <sys/resource.h>
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering#include "alloc-util.h"
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering#include "extract-word.h"
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering#include "formats-util.h"
93cc7779e0c121b75183920173f37cd1ee9d59cfThomas Hindoe Paaboel Andersen#include "macro.h"
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering#include "missing.h"
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering#include "rlimit-util.h"
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering#include "string-table.h"
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering#include "time-util.h"
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poetteringint setrlimit_closest(int resource, const struct rlimit *rlim) {
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering struct rlimit highest, fixed;
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering assert(rlim);
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering if (setrlimit(resource, rlim) >= 0)
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering return 0;
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering if (errno != EPERM)
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering return -errno;
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering /* So we failed to set the desired setrlimit, then let's try
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering * to get as close as we can */
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering assert_se(getrlimit(resource, &highest) == 0);
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering if (setrlimit(resource, &fixed) < 0)
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering return -errno;
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering return 0;
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering}
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringstatic int rlimit_parse_u64(const char *val, rlim_t *ret) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering uint64_t u;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering int r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert(val);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert(ret);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (streq(val, "infinity")) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering *ret = RLIM_INFINITY;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return 0;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering }
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering /* setrlimit(2) suggests rlim_t is always 64bit on Linux. */
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert_cc(sizeof(rlim_t) == sizeof(uint64_t));
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering r = safe_atou64(val, &u);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (r < 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (u >= (uint64_t) RLIM_INFINITY)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return -ERANGE;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering *ret = (rlim_t) u;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return 0;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering}
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringstatic int rlimit_parse_size(const char *val, rlim_t *ret) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering uint64_t u;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering int r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert(val);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert(ret);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (streq(val, "infinity")) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering *ret = RLIM_INFINITY;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return 0;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering }
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering r = parse_size(val, 1024, &u);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (r < 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (u >= (uint64_t) RLIM_INFINITY)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return -ERANGE;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering *ret = (rlim_t) u;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return 0;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering}
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringstatic int rlimit_parse_sec(const char *val, rlim_t *ret) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering uint64_t u;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering usec_t t;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering int r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert(val);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert(ret);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (streq(val, "infinity")) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering *ret = RLIM_INFINITY;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return 0;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering }
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering r = parse_sec(val, &t);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (r < 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (t == USEC_INFINITY) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering *ret = RLIM_INFINITY;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return 0;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering }
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering u = (uint64_t) DIV_ROUND_UP(t, USEC_PER_SEC);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (u >= (uint64_t) RLIM_INFINITY)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return -ERANGE;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering *ret = (rlim_t) u;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return 0;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering}
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringstatic int rlimit_parse_usec(const char *val, rlim_t *ret) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering usec_t t;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering int r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert(val);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert(ret);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (streq(val, "infinity")) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering *ret = RLIM_INFINITY;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return 0;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering }
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering r = parse_time(val, &t, 1);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (r < 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (t == USEC_INFINITY) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering *ret = RLIM_INFINITY;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return 0;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering }
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering *ret = (rlim_t) t;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return 0;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering}
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringstatic int (*const rlimit_parse_table[_RLIMIT_MAX])(const char *val, rlim_t *ret) = {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_CPU] = rlimit_parse_sec,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_FSIZE] = rlimit_parse_size,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_DATA] = rlimit_parse_size,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_STACK] = rlimit_parse_size,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_CORE] = rlimit_parse_size,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_RSS] = rlimit_parse_size,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_NOFILE] = rlimit_parse_u64,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_AS] = rlimit_parse_size,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_NPROC] = rlimit_parse_u64,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_MEMLOCK] = rlimit_parse_size,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_LOCKS] = rlimit_parse_u64,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_SIGPENDING] = rlimit_parse_u64,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_MSGQUEUE] = rlimit_parse_size,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_NICE] = rlimit_parse_u64,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_RTPRIO] = rlimit_parse_u64,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering [RLIMIT_RTTIME] = rlimit_parse_usec,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering};
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringint rlimit_parse_one(int resource, const char *val, rlim_t *ret) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert(val);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert(ret);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (resource < 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return -EINVAL;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (resource >= _RLIMIT_MAX)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return -EINVAL;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return rlimit_parse_table[resource](val, ret);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering}
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringint rlimit_parse(int resource, const char *val, struct rlimit *ret) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering _cleanup_free_ char *hard = NULL, *soft = NULL;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering rlim_t hl, sl;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering int r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert(val);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert(ret);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering r = extract_first_word(&val, &soft, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (r < 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (r == 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return -EINVAL;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering r = rlimit_parse_one(resource, soft, &sl);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (r < 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering r = extract_first_word(&val, &hard, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (r < 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (!isempty(val))
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return -EINVAL;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (r == 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering hl = sl;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering else {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering r = rlimit_parse_one(resource, hard, &hl);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (r < 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return r;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (sl > hl)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return -EILSEQ;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering }
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering *ret = (struct rlimit) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering .rlim_cur = sl,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering .rlim_max = hl,
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering };
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return 0;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering}
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poetteringint rlimit_format(const struct rlimit *rl, char **ret) {
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering char *s = NULL;
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering assert(rl);
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering assert(ret);
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering if (rl->rlim_cur >= RLIM_INFINITY && rl->rlim_max >= RLIM_INFINITY)
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering s = strdup("infinity");
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering else if (rl->rlim_cur >= RLIM_INFINITY)
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering (void) asprintf(&s, "infinity:" RLIM_FMT, rl->rlim_max);
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering else if (rl->rlim_max >= RLIM_INFINITY)
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering (void) asprintf(&s, RLIM_FMT ":infinity", rl->rlim_cur);
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering else if (rl->rlim_cur == rl->rlim_max)
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering (void) asprintf(&s, RLIM_FMT, rl->rlim_cur);
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering else
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering (void) asprintf(&s, RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max);
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering if (!s)
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering return -ENOMEM;
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering *ret = s;
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering return 0;
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering}
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poetteringstatic const char* const rlimit_table[_RLIMIT_MAX] = {
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_CPU] = "LimitCPU",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_FSIZE] = "LimitFSIZE",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_DATA] = "LimitDATA",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_STACK] = "LimitSTACK",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_CORE] = "LimitCORE",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_RSS] = "LimitRSS",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_NOFILE] = "LimitNOFILE",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_AS] = "LimitAS",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_NPROC] = "LimitNPROC",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_LOCKS] = "LimitLOCKS",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_NICE] = "LimitNICE",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_RTPRIO] = "LimitRTPRIO",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering [RLIMIT_RTTIME] = "LimitRTTIME"
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering};
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP(rlimit, int);