1 // Functions used for implementing the ulimit builtin.
2 #include "config.h"  // IWYU pragma: keep
3 
4 #include <sys/resource.h>
5 
6 #include <cerrno>
7 #include <cstddef>
8 
9 #include "builtin.h"
10 #include "common.h"
11 #include "fallback.h"  // IWYU pragma: keep
12 #include "io.h"
13 #include "wgetopt.h"
14 #include "wutil.h"  // IWYU pragma: keep
15 
16 class parser_t;
17 
18 /// Struct describing a resource limit.
19 struct resource_t {
20     int resource;         // resource ID
21     const wchar_t *desc;  // description of resource
22     wchar_t switch_char;  // switch used on commandline to specify resource
23     int multiplier;       // the implicit multiplier used when setting getting values
24 };
25 
26 /// Array of resource_t structs, describing all known resource types.
27 static const struct resource_t resource_arr[] = {
28     {RLIMIT_CORE, L"Maximum size of core files created", L'c', 1024},
29     {RLIMIT_DATA, L"Maximum size of a process’s data segment", L'd', 1024},
30     {RLIMIT_FSIZE, L"Maximum size of files created by the shell", L'f', 1024},
31 #ifdef RLIMIT_MEMLOCK
32     {RLIMIT_MEMLOCK, L"Maximum size that may be locked into memory", L'l', 1024},
33 #endif
34 #ifdef RLIMIT_RSS
35     {RLIMIT_RSS, L"Maximum resident set size", L'm', 1024},
36 #endif
37     {RLIMIT_NOFILE, L"Maximum number of open file descriptors", L'n', 1},
38     {RLIMIT_STACK, L"Maximum stack size", L's', 1024},
39     {RLIMIT_CPU, L"Maximum amount of cpu time in seconds", L't', 1},
40 #ifdef RLIMIT_NPROC
41     {RLIMIT_NPROC, L"Maximum number of processes available to a single user", L'u', 1},
42 #endif
43 #ifdef RLIMIT_AS
44     {RLIMIT_AS, L"Maximum amount of virtual memory available to the shell", L'v', 1024},
45 #endif
46     {0, nullptr, 0, 0}};
47 
48 /// Get the implicit multiplication factor for the specified resource limit.
get_multiplier(int what)49 static int get_multiplier(int what) {
50     for (int i = 0; resource_arr[i].desc; i++) {
51         if (resource_arr[i].resource == what) {
52             return resource_arr[i].multiplier;
53         }
54     }
55     return -1;
56 }
57 
58 /// Return the value for the specified resource limit. This function does _not_ multiply the limit
59 /// value by the multiplier constant used by the commandline ulimit.
get(int resource,int hard)60 static rlim_t get(int resource, int hard) {
61     struct rlimit ls;
62 
63     getrlimit(resource, &ls);
64 
65     return hard ? ls.rlim_max : ls.rlim_cur;
66 }
67 
68 /// Print the value of the specified resource limit.
print(int resource,int hard,io_streams_t & streams)69 static void print(int resource, int hard, io_streams_t &streams) {
70     rlim_t l = get(resource, hard);
71 
72     if (l == RLIM_INFINITY)
73         streams.out.append(L"unlimited\n");
74     else
75         streams.out.append_format(L"%d\n", l / get_multiplier(resource));
76 }
77 
78 /// Print values of all resource limits.
print_all(int hard,io_streams_t & streams)79 static void print_all(int hard, io_streams_t &streams) {
80     int i;
81     int w = 0;
82 
83     for (i = 0; resource_arr[i].desc; i++) {
84         w = std::max(w, fish_wcswidth(resource_arr[i].desc));
85     }
86 
87     for (i = 0; resource_arr[i].desc; i++) {
88         struct rlimit ls;
89         rlim_t l;
90         getrlimit(resource_arr[i].resource, &ls);
91         l = hard ? ls.rlim_max : ls.rlim_cur;
92 
93         const wchar_t *unit =
94             ((resource_arr[i].resource == RLIMIT_CPU)
95                  ? L"(seconds, "
96                  : (get_multiplier(resource_arr[i].resource) == 1 ? L"(" : L"(kB, "));
97 
98         streams.out.append_format(L"%-*ls %10ls-%lc) ", w, resource_arr[i].desc, unit,
99                                   resource_arr[i].switch_char);
100 
101         if (l == RLIM_INFINITY) {
102             streams.out.append(L"unlimited\n");
103         } else {
104             streams.out.append_format(L"%d\n", l / get_multiplier(resource_arr[i].resource));
105         }
106     }
107 }
108 
109 /// Returns the description for the specified resource limit.
get_desc(int what)110 static const wchar_t *get_desc(int what) {
111     int i;
112 
113     for (i = 0; resource_arr[i].desc; i++) {
114         if (resource_arr[i].resource == what) {
115             return resource_arr[i].desc;
116         }
117     }
118     return L"Not a resource";
119 }
120 
121 /// Set the new value of the specified resource limit. This function does _not_ multiply the limit
122 // value by the multiplier constant used by the commandline ulimit.
set_limit(int resource,int hard,int soft,rlim_t value,io_streams_t & streams)123 static int set_limit(int resource, int hard, int soft, rlim_t value, io_streams_t &streams) {
124     struct rlimit ls;
125 
126     getrlimit(resource, &ls);
127     if (hard) ls.rlim_max = value;
128     if (soft) {
129         ls.rlim_cur = value;
130 
131         // Do not attempt to set the soft limit higher than the hard limit.
132         if ((value == RLIM_INFINITY && ls.rlim_max != RLIM_INFINITY) ||
133             (value != RLIM_INFINITY && ls.rlim_max != RLIM_INFINITY && value > ls.rlim_max)) {
134             ls.rlim_cur = ls.rlim_max;
135         }
136     }
137 
138     if (setrlimit(resource, &ls)) {
139         if (errno == EPERM) {
140             streams.err.append_format(
141                 L"ulimit: Permission denied when changing resource of type '%ls'\n",
142                 get_desc(resource));
143         } else {
144             builtin_wperror(L"ulimit", streams);
145         }
146         return STATUS_CMD_ERROR;
147     }
148     return STATUS_CMD_OK;
149 }
150 
151 /// The ulimit builtin, used for setting resource limits.
builtin_ulimit(parser_t & parser,io_streams_t & streams,const wchar_t ** argv)152 maybe_t<int> builtin_ulimit(parser_t &parser, io_streams_t &streams, const wchar_t **argv) {
153     const wchar_t *cmd = argv[0];
154     int argc = builtin_count_args(argv);
155     bool report_all = false;
156     bool hard = false;
157     bool soft = false;
158     int what = RLIMIT_FSIZE;
159 
160     static const wchar_t *const short_options = L":HSacdflmnstuvh";
161     static const struct woption long_options[] = {
162         {L"all", no_argument, nullptr, 'a'},
163         {L"hard", no_argument, nullptr, 'H'},
164         {L"soft", no_argument, nullptr, 'S'},
165         {L"core-size", no_argument, nullptr, 'c'},
166         {L"data-size", no_argument, nullptr, 'd'},
167         {L"file-size", no_argument, nullptr, 'f'},
168         {L"lock-size", no_argument, nullptr, 'l'},
169         {L"resident-set-size", no_argument, nullptr, 'm'},
170         {L"file-descriptor-count", no_argument, nullptr, 'n'},
171         {L"stack-size", no_argument, nullptr, 's'},
172         {L"cpu-time", no_argument, nullptr, 't'},
173         {L"process-count", no_argument, nullptr, 'u'},
174         {L"virtual-memory-size", no_argument, nullptr, 'v'},
175         {L"help", no_argument, nullptr, 'h'},
176         {nullptr, 0, nullptr, 0}};
177 
178     int opt;
179     wgetopter_t w;
180     while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) {
181         switch (opt) {
182             case 'a': {
183                 report_all = true;
184                 break;
185             }
186             case 'H': {
187                 hard = true;
188                 break;
189             }
190             case 'S': {
191                 soft = true;
192                 break;
193             }
194             case 'c': {
195                 what = RLIMIT_CORE;
196                 break;
197             }
198             case 'd': {
199                 what = RLIMIT_DATA;
200                 break;
201             }
202             case 'f': {
203                 what = RLIMIT_FSIZE;
204                 break;
205             }
206 #ifdef RLIMIT_MEMLOCK
207             case 'l': {
208                 what = RLIMIT_MEMLOCK;
209                 break;
210             }
211 #endif
212 #ifdef RLIMIT_RSS
213             case 'm': {
214                 what = RLIMIT_RSS;
215                 break;
216             }
217 #endif
218             case 'n': {
219                 what = RLIMIT_NOFILE;
220                 break;
221             }
222             case 's': {
223                 what = RLIMIT_STACK;
224                 break;
225             }
226             case 't': {
227                 what = RLIMIT_CPU;
228                 break;
229             }
230 #ifdef RLIMIT_NPROC
231             case 'u': {
232                 what = RLIMIT_NPROC;
233                 break;
234             }
235 #endif
236 #ifdef RLIMIT_AS
237             case 'v': {
238                 what = RLIMIT_AS;
239                 break;
240             }
241 #endif
242             case 'h': {
243                 builtin_print_help(parser, streams, cmd);
244                 return STATUS_CMD_OK;
245             }
246             case ':': {
247                 builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
248                 return STATUS_INVALID_ARGS;
249             }
250             case '?': {
251                 builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
252                 return STATUS_INVALID_ARGS;
253             }
254             default: {
255                 DIE("unexpected retval from wgetopt_long");
256             }
257         }
258     }
259 
260     if (report_all) {
261         print_all(hard, streams);
262         return STATUS_CMD_OK;
263     }
264 
265     int arg_count = argc - w.woptind;
266     if (arg_count == 0) {
267         // Show current limit value.
268         print(what, hard, streams);
269         return STATUS_CMD_OK;
270     } else if (arg_count != 1) {
271         streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, cmd);
272         builtin_print_error_trailer(parser, streams.err, cmd);
273         return STATUS_INVALID_ARGS;
274     }
275 
276     // Change current limit value.
277     if (!hard && !soft) {
278         // Set both hard and soft limits if neither was specified.
279         hard = soft = true;
280     }
281 
282     rlim_t new_limit;
283     if (*argv[w.woptind] == L'\0') {
284         streams.err.append_format(_(L"%ls: New limit cannot be an empty string\n"), cmd);
285         builtin_print_error_trailer(parser, streams.err, cmd);
286         return STATUS_INVALID_ARGS;
287     } else if (wcscasecmp(argv[w.woptind], L"unlimited") == 0) {
288         new_limit = RLIM_INFINITY;
289     } else if (wcscasecmp(argv[w.woptind], L"hard") == 0) {
290         new_limit = get(what, 1);
291     } else if (wcscasecmp(argv[w.woptind], L"soft") == 0) {
292         new_limit = get(what, soft);
293     } else {
294         new_limit = fish_wcstol(argv[w.woptind]);
295         if (errno) {
296             streams.err.append_format(_(L"%ls: Invalid limit '%ls'\n"), cmd, argv[w.woptind]);
297             builtin_print_error_trailer(parser, streams.err, cmd);
298             return STATUS_INVALID_ARGS;
299         }
300         new_limit *= get_multiplier(what);
301     }
302 
303     return set_limit(what, hard, soft, new_limit, streams);
304 }
305