]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/rlimit-util.c
Merge pull request #2080 from chaloulo/split-mode-host-remove-port-from-journal-filename
[thirdparty/systemd.git] / src / basic / rlimit-util.c
CommitLineData
78f22b97
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
11c3a366
TA
22#include <errno.h>
23#include <sys/resource.h>
24
93cc7779 25#include "macro.h"
78f22b97
LP
26#include "missing.h"
27#include "rlimit-util.h"
8b43440b 28#include "string-table.h"
78f22b97
LP
29
30int setrlimit_closest(int resource, const struct rlimit *rlim) {
31 struct rlimit highest, fixed;
32
33 assert(rlim);
34
35 if (setrlimit(resource, rlim) >= 0)
36 return 0;
37
38 if (errno != EPERM)
39 return -errno;
40
41 /* So we failed to set the desired setrlimit, then let's try
42 * to get as close as we can */
43 assert_se(getrlimit(resource, &highest) == 0);
44
45 fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
46 fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
47
48 if (setrlimit(resource, &fixed) < 0)
49 return -errno;
50
51 return 0;
52}
53
54static const char* const rlimit_table[_RLIMIT_MAX] = {
55 [RLIMIT_CPU] = "LimitCPU",
56 [RLIMIT_FSIZE] = "LimitFSIZE",
57 [RLIMIT_DATA] = "LimitDATA",
58 [RLIMIT_STACK] = "LimitSTACK",
59 [RLIMIT_CORE] = "LimitCORE",
60 [RLIMIT_RSS] = "LimitRSS",
61 [RLIMIT_NOFILE] = "LimitNOFILE",
62 [RLIMIT_AS] = "LimitAS",
63 [RLIMIT_NPROC] = "LimitNPROC",
64 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
65 [RLIMIT_LOCKS] = "LimitLOCKS",
66 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
67 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
68 [RLIMIT_NICE] = "LimitNICE",
69 [RLIMIT_RTPRIO] = "LimitRTPRIO",
70 [RLIMIT_RTTIME] = "LimitRTTIME"
71};
72
73DEFINE_STRING_TABLE_LOOKUP(rlimit, int);