78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering This file is part of systemd.
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering Copyright 2010 Lennart 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 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 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 Poetteringint setrlimit_closest(int resource, const struct rlimit *rlim) {
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 fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringstatic int rlimit_parse_u64(const char *val, rlim_t *ret) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering /* setrlimit(2) suggests rlim_t is always 64bit on Linux. */
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering assert_cc(sizeof(rlim_t) == sizeof(uint64_t));
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringstatic int rlimit_parse_size(const char *val, rlim_t *ret) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringstatic int rlimit_parse_sec(const char *val, rlim_t *ret) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering u = (uint64_t) DIV_ROUND_UP(t, USEC_PER_SEC);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringstatic int rlimit_parse_usec(const char *val, rlim_t *ret) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringstatic int (*const rlimit_parse_table[_RLIMIT_MAX])(const char *val, rlim_t *ret) = {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringint rlimit_parse_one(int resource, const char *val, rlim_t *ret) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering return rlimit_parse_table[resource](val, ret);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringint rlimit_parse(int resource, const char *val, struct rlimit *ret) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering _cleanup_free_ char *hard = NULL, *soft = NULL;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering r = extract_first_word(&val, &soft, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering r = rlimit_parse_one(resource, soft, &sl);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering r = extract_first_word(&val, &hard, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering r = rlimit_parse_one(resource, hard, &hl);
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poetteringint rlimit_format(const struct rlimit *rl, char **ret) {
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering if (rl->rlim_cur >= RLIM_INFINITY && rl->rlim_max >= RLIM_INFINITY)
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering (void) asprintf(&s, "infinity:" RLIM_FMT, rl->rlim_max);
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering (void) asprintf(&s, RLIM_FMT ":infinity", rl->rlim_cur);
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering (void) asprintf(&s, RLIM_FMT, rl->rlim_cur);
99d4f5e5c0d2532159542519e683f976f881f0f5Lennart Poettering (void) asprintf(&s, RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max);
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poetteringstatic const char* const rlimit_table[_RLIMIT_MAX] = {