rlimit-util.c revision 8b43440b7ef4b81c69c31de7ff820dc07a780254
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering/***
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering This file is part of systemd.
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering Copyright 2010 Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering (at your option) any later version.
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering systemd is distributed in the hope that it will be useful, but
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering Lesser General Public License for more details.
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering***/
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering#include "missing.h"
4871690d9e32608bbd9b18505b5326c2079c9690Allin Cottrell#include "rlimit-util.h"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering#include "string-table.h"
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering#include "util.h"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poetteringint setrlimit_closest(int resource, const struct rlimit *rlim) {
1dfa7e79a60de680086b1d93fcc3629b463f58bdLennart Poettering struct rlimit highest, fixed;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(rlim);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (setrlimit(resource, rlim) >= 0)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return 0;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen if (errno != EPERM)
a09abc4ae0bdc0200324eaa0416f23ff2170ec4eLennart Poettering return -errno;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
d18d46ecea80a7f07415edb9264af6a254fd70bbZbigniew Jędrzejewski-Szmek /* So we failed to set the desired setrlimit, then let's try
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering * to get as close as we can */
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering assert_se(getrlimit(resource, &highest) == 0);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering if (setrlimit(resource, &fixed) < 0)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return -errno;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return 0;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering}
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poetteringstatic const char* const rlimit_table[_RLIMIT_MAX] = {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering [RLIMIT_CPU] = "LimitCPU",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering [RLIMIT_FSIZE] = "LimitFSIZE",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering [RLIMIT_DATA] = "LimitDATA",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering [RLIMIT_STACK] = "LimitSTACK",
d18d46ecea80a7f07415edb9264af6a254fd70bbZbigniew Jędrzejewski-Szmek [RLIMIT_CORE] = "LimitCORE",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering [RLIMIT_RSS] = "LimitRSS",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering [RLIMIT_NOFILE] = "LimitNOFILE",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering [RLIMIT_AS] = "LimitAS",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering [RLIMIT_NPROC] = "LimitNPROC",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering [RLIMIT_LOCKS] = "LimitLOCKS",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
d18d46ecea80a7f07415edb9264af6a254fd70bbZbigniew Jędrzejewski-Szmek [RLIMIT_NICE] = "LimitNICE",
d18d46ecea80a7f07415edb9264af6a254fd70bbZbigniew Jędrzejewski-Szmek [RLIMIT_RTPRIO] = "LimitRTPRIO",
d18d46ecea80a7f07415edb9264af6a254fd70bbZbigniew Jędrzejewski-Szmek [RLIMIT_RTTIME] = "LimitRTTIME"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering};
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP(rlimit, int);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering