]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | #include "missing.h" | |
23 | #include "rlimit-util.h" | |
24 | #include "util.h" | |
25 | ||
26 | int setrlimit_closest(int resource, const struct rlimit *rlim) { | |
27 | struct rlimit highest, fixed; | |
28 | ||
29 | assert(rlim); | |
30 | ||
31 | if (setrlimit(resource, rlim) >= 0) | |
32 | return 0; | |
33 | ||
34 | if (errno != EPERM) | |
35 | return -errno; | |
36 | ||
37 | /* So we failed to set the desired setrlimit, then let's try | |
38 | * to get as close as we can */ | |
39 | assert_se(getrlimit(resource, &highest) == 0); | |
40 | ||
41 | fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max); | |
42 | fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max); | |
43 | ||
44 | if (setrlimit(resource, &fixed) < 0) | |
45 | return -errno; | |
46 | ||
47 | return 0; | |
48 | } | |
49 | ||
50 | static const char* const rlimit_table[_RLIMIT_MAX] = { | |
51 | [RLIMIT_CPU] = "LimitCPU", | |
52 | [RLIMIT_FSIZE] = "LimitFSIZE", | |
53 | [RLIMIT_DATA] = "LimitDATA", | |
54 | [RLIMIT_STACK] = "LimitSTACK", | |
55 | [RLIMIT_CORE] = "LimitCORE", | |
56 | [RLIMIT_RSS] = "LimitRSS", | |
57 | [RLIMIT_NOFILE] = "LimitNOFILE", | |
58 | [RLIMIT_AS] = "LimitAS", | |
59 | [RLIMIT_NPROC] = "LimitNPROC", | |
60 | [RLIMIT_MEMLOCK] = "LimitMEMLOCK", | |
61 | [RLIMIT_LOCKS] = "LimitLOCKS", | |
62 | [RLIMIT_SIGPENDING] = "LimitSIGPENDING", | |
63 | [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE", | |
64 | [RLIMIT_NICE] = "LimitNICE", | |
65 | [RLIMIT_RTPRIO] = "LimitRTPRIO", | |
66 | [RLIMIT_RTTIME] = "LimitRTTIME" | |
67 | }; | |
68 | ||
69 | DEFINE_STRING_TABLE_LOOKUP(rlimit, int); |