/src/proftpd/src/rlimit.c
Line | Count | Source |
1 | | /* |
2 | | * ProFTPD - FTP server daemon |
3 | | * Copyright (c) 2013-2016 The ProFTPD Project team |
4 | | * |
5 | | * This program is free software; you can redistribute it and/or modify |
6 | | * it under the terms of the GNU General Public License as published by |
7 | | * the Free Software Foundation; either version 2 of the License, or |
8 | | * (at your option) any later version. |
9 | | * |
10 | | * This program is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | * GNU General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU General Public License |
16 | | * along with this program; if not, write to the Free Software |
17 | | * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. |
18 | | * |
19 | | * As a special exemption, The ProFTPD Project team and other respective |
20 | | * copyright holders give permission to link this program with OpenSSL, and |
21 | | * distribute the resulting executable, without including the source code for |
22 | | * OpenSSL in the source distribution. |
23 | | */ |
24 | | |
25 | | /* Resource limits implementation */ |
26 | | |
27 | | #include "conf.h" |
28 | | |
29 | 0 | static int get_rlimit(int resource, rlim_t *current, rlim_t *max) { |
30 | 0 | struct rlimit rlim; |
31 | 0 | int res; |
32 | |
|
33 | 0 | if (current == NULL && |
34 | 0 | max == NULL) { |
35 | 0 | errno = EINVAL; |
36 | 0 | return -1; |
37 | 0 | } |
38 | | |
39 | 0 | res = getrlimit(resource, &rlim); |
40 | 0 | if (res < 0) { |
41 | | /* Some libcs use EPERM instead of ENOSYS; weird. */ |
42 | 0 | if (errno == EPERM) { |
43 | 0 | errno = ENOSYS; |
44 | 0 | } |
45 | |
|
46 | 0 | return res; |
47 | 0 | } |
48 | | |
49 | 0 | if (current != NULL) { |
50 | 0 | *current = rlim.rlim_cur; |
51 | 0 | } |
52 | |
|
53 | 0 | if (max != NULL) { |
54 | 0 | *max = rlim.rlim_max; |
55 | 0 | } |
56 | |
|
57 | 0 | return 0; |
58 | 0 | } |
59 | | |
60 | 0 | static int set_rlimit(int resource, rlim_t current, rlim_t max) { |
61 | 0 | struct rlimit rlim; |
62 | 0 | int res; |
63 | |
|
64 | 0 | rlim.rlim_cur = current; |
65 | 0 | rlim.rlim_max = max; |
66 | |
|
67 | 0 | res = setrlimit(resource, &rlim); |
68 | 0 | return res; |
69 | 0 | } |
70 | | |
71 | 0 | int pr_rlimit_get_core(rlim_t *current, rlim_t *max) { |
72 | 0 | #if defined(RLIMIT_CORE) |
73 | 0 | return get_rlimit(RLIMIT_CORE, current, max); |
74 | |
|
75 | | #else |
76 | | errno = ENOSYS; |
77 | | return -1; |
78 | | #endif /* No RLIMIT_CORE */ |
79 | 0 | } |
80 | | |
81 | 0 | int pr_rlimit_set_core(rlim_t current, rlim_t max) { |
82 | 0 | #if defined(RLIMIT_CORE) |
83 | 0 | return set_rlimit(RLIMIT_CORE, current, max); |
84 | |
|
85 | | #else |
86 | | errno = ENOSYS; |
87 | | return -1; |
88 | | #endif /* No RLIMIT_CORE */ |
89 | 0 | } |
90 | | |
91 | 0 | int pr_rlimit_get_cpu(rlim_t *current, rlim_t *max) { |
92 | 0 | #if defined(RLIMIT_CPU) |
93 | 0 | return get_rlimit(RLIMIT_CPU, current, max); |
94 | |
|
95 | | #else |
96 | | errno = ENOSYS; |
97 | | return -1; |
98 | | #endif /* No RLIMIT_CPU */ |
99 | 0 | } |
100 | | |
101 | 0 | int pr_rlimit_set_cpu(rlim_t current, rlim_t max) { |
102 | 0 | #if defined(RLIMIT_CPU) |
103 | 0 | return set_rlimit(RLIMIT_CPU, current, max); |
104 | |
|
105 | | #else |
106 | | errno = ENOSYS; |
107 | | return -1; |
108 | | #endif /* No RLIMIT_CPU */ |
109 | 0 | } |
110 | | |
111 | 0 | int pr_rlimit_get_files(rlim_t *current, rlim_t *max) { |
112 | 0 | #if defined(RLIMIT_NOFILE) |
113 | 0 | return get_rlimit(RLIMIT_NOFILE, current, max); |
114 | |
|
115 | | #elif defined(RLIMIT_OFILE) |
116 | | return get_rlimit(RLIMIT_OFILE, current, max); |
117 | | |
118 | | #else |
119 | | errno = ENOSYS; |
120 | | return -1; |
121 | | #endif /* No RLIMIT_NOFILE or RLIMIT_OFILE */ |
122 | 0 | } |
123 | | |
124 | 0 | int pr_rlimit_set_files(rlim_t current, rlim_t max) { |
125 | 0 | #if defined(RLIMIT_NOFILE) |
126 | 0 | return set_rlimit(RLIMIT_NOFILE, current, max); |
127 | |
|
128 | | #elif defined(RLIMIT_OFILE) |
129 | | return set_rlimit(RLIMIT_OFILE, current, max); |
130 | | |
131 | | #else |
132 | | errno = ENOSYS; |
133 | | return -1; |
134 | | #endif /* No RLIMIT_NOFILE or RLIMIT_OFILE */ |
135 | 0 | } |
136 | | |
137 | 0 | int pr_rlimit_get_memory(rlim_t *current, rlim_t *max) { |
138 | 0 | #if defined(RLIMIT_AS) |
139 | 0 | return get_rlimit(RLIMIT_AS, current, max); |
140 | |
|
141 | | #elif defined(RLIMIT_DATA) |
142 | | return get_rlimit(RLIMIT_DATA, current, max); |
143 | | |
144 | | #elif defined(RLIMIT_VMEM) |
145 | | return get_rlimit(RLIMIT_VMEM, current, max); |
146 | | |
147 | | #else |
148 | | errno = ENOSYS; |
149 | | return -1; |
150 | | #endif /* No RLIMIT_AS, RLIMIT_DATA, or RLIMIT_VMEM. */ |
151 | 0 | } |
152 | | |
153 | 0 | int pr_rlimit_set_memory(rlim_t current, rlim_t max) { |
154 | 0 | #if defined(RLIMIT_AS) |
155 | 0 | return set_rlimit(RLIMIT_AS, current, max); |
156 | |
|
157 | | #elif defined(RLIMIT_DATA) |
158 | | return set_rlimit(RLIMIT_DATA, current, max); |
159 | | |
160 | | #else |
161 | | errno = ENOSYS; |
162 | | return -1; |
163 | | #endif /* No RLIMIT_AS or RLIMIT_DATA */ |
164 | 0 | } |
165 | | |
166 | 0 | int pr_rlimit_get_nproc(rlim_t *current, rlim_t *max) { |
167 | 0 | #if defined(RLIMIT_NPROC) |
168 | 0 | return get_rlimit(RLIMIT_NPROC, current, max); |
169 | |
|
170 | | #else |
171 | | errno = ENOSYS; |
172 | | return -1; |
173 | | #endif /* No RLIMIT_NPROC */ |
174 | 0 | } |
175 | | |
176 | 0 | int pr_rlimit_set_nproc(rlim_t current, rlim_t max) { |
177 | 0 | #if defined(RLIMIT_NPROC) |
178 | 0 | return set_rlimit(RLIMIT_NPROC, current, max); |
179 | |
|
180 | | #else |
181 | | errno = ENOSYS; |
182 | | return -1; |
183 | | #endif /* No RLIMIT_NPROC */ |
184 | 0 | } |
185 | | |