1 /*-
2 * Copyright (c) 1997 by
3 * David L. Nugent <davidn@blaze.net.au>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, is permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. This work was done expressly for inclusion into FreeBSD. Other use
16 * is permitted provided this notation is included.
17 * 4. Absolutely no warranty of function or purpose is made by the authors.
18 * 5. Modifications may be freely made to this file providing the above
19 * conditions are met.
20 *
21 * Display/change(+runprogram)/eval resource limits.
22 */
23
24 #include <sys/cdefs.h>
25 #include <err.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/sysctl.h>
31 #include <sys/user.h>
32 #include <sys/param.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <stdarg.h>
36 #include <stdint.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <pwd.h>
40 #include <login_cap.h>
41 #include <sys/time.h>
42 #include <sys/resource.h>
43
44 enum
45 {
46 SH_NONE,
47 SH_SH, /* sh */
48 SH_CSH, /* csh */
49 SH_BASH, /* gnu bash */
50 SH_TCSH, /* tcsh */
51 SH_KSH, /* (pd)ksh */
52 SH_ZSH, /* zsh */
53 SH_RC, /* rc or es */
54 SH_NUMBER
55 };
56
57
58 /* eval emitter for popular shells.
59 * Why aren't there any standards here? Most shells support either
60 * the csh 'limit' or sh 'ulimit' command, but each varies just
61 * enough that they aren't very compatible from one to the other.
62 */
63 static struct {
64 const char * name; /* Name of shell */
65 const char * inf; /* Name used for 'unlimited' resource */
66 const char * cmd; /* Intro text */
67 const char * hard; /* Hard limit text */
68 const char * soft; /* Soft limit text */
69 const char * both; /* Hard+Soft limit text */
70 struct {
71 const char * pfx;
72 const char * sfx;
73 int divisor;
74 } lprm[RLIM_NLIMITS];
75 } shellparm[] =
76 {
77 { "", "infinity", "Resource limits%s%s:\n", "-max", "-cur", "",
78 {
79 { " cputime%-4s %8s", " secs\n", 1 },
80 { " filesize%-4s %8s", " kB\n", 1024 },
81 { " datasize%-4s %8s", " kB\n", 1024 },
82 { " stacksize%-4s %8s", " kB\n", 1024 },
83 { " coredumpsize%-4s %8s", " kB\n", 1024 },
84 { " memoryuse%-4s %8s", " kB\n", 1024 },
85 { " memorylocked%-4s %8s", " kB\n", 1024 },
86 { " maxprocesses%-4s %8s", "\n", 1 },
87 { " openfiles%-4s %8s", "\n", 1 },
88 { " sbsize%-4s %8s", " bytes\n", 1 },
89 { " vmemoryuse%-4s %8s", " kB\n", 1024 },
90 { " pseudo-terminals%-4s %8s", "\n", 1 },
91 { " swapuse%-4s %8s", " kB\n", 1024 },
92 { " kqueues%-4s %8s", "\n", 1 },
93 { " umtxp%-4s %8s", "\n", 1 },
94 { " pipebuf%-4s %8s", " kB\n", 1024 },
95 { " vms%-4s %8s", "\n", 1 },
96 }
97 },
98 { "sh", "unlimited", "", " -H", " -S", "",
99 {
100 { "ulimit%s -t %s", ";\n", 1 },
101 { "ulimit%s -f %s", ";\n", 512 },
102 { "ulimit%s -d %s", ";\n", 1024 },
103 { "ulimit%s -s %s", ";\n", 1024 },
104 { "ulimit%s -c %s", ";\n", 512 },
105 { "ulimit%s -m %s", ";\n", 1024 },
106 { "ulimit%s -l %s", ";\n", 1024 },
107 { "ulimit%s -u %s", ";\n", 1 },
108 { "ulimit%s -n %s", ";\n", 1 },
109 { "ulimit%s -b %s", ";\n", 1 },
110 { "ulimit%s -v %s", ";\n", 1024 },
111 { "ulimit%s -p %s", ";\n", 1 },
112 { "ulimit%s -w %s", ";\n", 1024 },
113 { "ulimit%s -k %s", ";\n", 1 },
114 { "ulimit%s -o %s", ";\n", 1 },
115 { "ulimit%s -y %s", ";\n", 1024 },
116 { "ulimit%s -V %s", ";\n", 1 },
117 }
118 },
119 { "csh", "unlimited", "", " -h", "", NULL,
120 {
121 { "limit%s cputime %s", ";\n", 1 },
122 { "limit%s filesize %s", ";\n", 1024 },
123 { "limit%s datasize %s", ";\n", 1024 },
124 { "limit%s stacksize %s", ";\n", 1024 },
125 { "limit%s coredumpsize %s", ";\n", 1024 },
126 { "limit%s memoryuse %s", ";\n", 1024 },
127 { "limit%s memorylocked %s", ";\n", 1024 },
128 { "limit%s maxproc %s", ";\n", 1 },
129 { "limit%s openfiles %s", ";\n", 1 },
130 { "limit%s sbsize %s", ";\n", 1 },
131 { "limit%s vmemoryuse %s", ";\n", 1024 },
132 { "limit%s pseudoterminals %s", ";\n", 1 },
133 { "limit%s swapsize %s", ";\n", 1024 },
134 { "limit%s kqueues %s", ";\n", 1 },
135 { "limit%s umtxp %s", ";\n", 1 },
136 }
137 },
138 { "bash|bash2", "unlimited", "", " -H", " -S", "",
139 {
140 { "ulimit%s -t %s", ";\n", 1 },
141 { "ulimit%s -f %s", ";\n", 1024 },
142 { "ulimit%s -d %s", ";\n", 1024 },
143 { "ulimit%s -s %s", ";\n", 1024 },
144 { "ulimit%s -c %s", ";\n", 1024 },
145 { "ulimit%s -m %s", ";\n", 1024 },
146 { "ulimit%s -l %s", ";\n", 1024 },
147 { "ulimit%s -u %s", ";\n", 1 },
148 { "ulimit%s -n %s", ";\n", 1 },
149 { "ulimit%s -b %s", ";\n", 1 },
150 { "ulimit%s -v %s", ";\n", 1024 },
151 { "ulimit%s -p %s", ";\n", 1 },
152 { "ulimit%s -w %s", ";\n", 1024 }
153 }
154 },
155 { "tcsh", "unlimited", "", " -h", "", NULL,
156 {
157 { "limit%s cputime %s", ";\n", 1 },
158 { "limit%s filesize %s", ";\n", 1024 },
159 { "limit%s datasize %s", ";\n", 1024 },
160 { "limit%s stacksize %s", ";\n", 1024 },
161 { "limit%s coredumpsize %s", ";\n", 1024 },
162 { "limit%s memoryuse %s", ";\n", 1024 },
163 { "limit%s memorylocked %s", ";\n", 1024 },
164 { "limit%s maxproc %s", ";\n", 1 },
165 { "limit%s descriptors %s", ";\n", 1 },
166 { "limit%s sbsize %s", ";\n", 1 },
167 { "limit%s vmemoryuse %s", ";\n", 1024 },
168 { "limit%s pseudoterminals %s", ";\n", 1 },
169 { "limit%s swapsize %s", ";\n", 1024 },
170 { "limit%s kqueues %s", ";\n", 1 },
171 { "limit%s umtxp %s", ";\n", 1 },
172 }
173 },
174 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
175 {
176 { "ulimit%s -t %s", ";\n", 1 },
177 { "ulimit%s -f %s", ";\n", 512 },
178 { "ulimit%s -d %s", ";\n", 1024 },
179 { "ulimit%s -s %s", ";\n", 1024 },
180 { "ulimit%s -c %s", ";\n", 512 },
181 { "ulimit%s -m %s", ";\n", 1024 },
182 { "ulimit%s -l %s", ";\n", 1024 },
183 { "ulimit%s -p %s", ";\n", 1 },
184 { "ulimit%s -n %s", ";\n", 1 },
185 { "ulimit%s -b %s", ";\n", 1 },
186 { "ulimit%s -v %s", ";\n", 1024 },
187 { "ulimit%s -p %s", ";\n", 1 },
188 { "ulimit%s -w %s", ";\n", 1024 }
189 }
190 },
191 { "zsh", "unlimited", "", " -H", " -S", "",
192 {
193 { "ulimit%s -t %s", ";\n", 1 },
194 { "ulimit%s -f %s", ";\n", 512 },
195 { "ulimit%s -d %s", ";\n", 1024 },
196 { "ulimit%s -s %s", ";\n", 1024 },
197 { "ulimit%s -c %s", ";\n", 512 },
198 { "ulimit%s -m %s", ";\n", 1024 },
199 { "ulimit%s -l %s", ";\n", 1024 },
200 { "ulimit%s -u %s", ";\n", 1 },
201 { "ulimit%s -n %s", ";\n", 1 },
202 { "ulimit%s -b %s", ";\n", 1 },
203 { "ulimit%s -v %s", ";\n", 1024 },
204 { "ulimit%s -p %s", ";\n", 1 },
205 { "ulimit%s -w %s", ";\n", 1024 }
206 }
207 },
208 { "rc|es", "unlimited", "", " -h", "", NULL,
209 {
210 { "limit%s cputime %s", ";\n", 1 },
211 { "limit%s filesize %s", ";\n", 1024 },
212 { "limit%s datasize %s", ";\n", 1024 },
213 { "limit%s stacksize %s", ";\n", 1024 },
214 { "limit%s coredumpsize %s", ";\n", 1024 },
215 { "limit%s memoryuse %s", ";\n", 1024 },
216 { "limit%s lockedmemory %s", ";\n", 1024 },
217 { "limit%s processes %s", ";\n", 1 },
218 { "limit%s descriptors %s", ";\n", 1 },
219 { "limit%s sbsize %s", ";\n", 1 },
220 { "limit%s vmemoryuse %s", ";\n", 1024 },
221 { "limit%s pseudoterminals %s", ";\n", 1 },
222 { "limit%s swapuse %s", ";\n", 1024 }
223 }
224 },
225 { NULL, NULL, NULL, NULL, NULL, NULL,
226 { }
227 }
228 };
229
230 static struct {
231 const char * cap;
232 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
233 } resources[] = {
234 { "cputime", login_getcaptime },
235 { "filesize", login_getcapsize },
236 { "datasize", login_getcapsize },
237 { "stacksize", login_getcapsize },
238 { "coredumpsize", login_getcapsize },
239 { "memoryuse", login_getcapsize },
240 { "memorylocked", login_getcapsize },
241 { "maxproc", login_getcapnum },
242 { "openfiles", login_getcapnum },
243 { "sbsize", login_getcapsize },
244 { "vmemoryuse", login_getcapsize },
245 { "pseudoterminals",login_getcapnum },
246 { "swapuse", login_getcapsize },
247 { "kqueues", login_getcapnum },
248 { "umtxp", login_getcapnum },
249 { "pipebuf", login_getcapnum },
250 { "vmms", login_getcapnum },
251 };
252
253 _Static_assert(nitems(resources) == RLIM_NLIMITS,
254 "Please add entries to resources[] for the new limits");
255
256 /*
257 * One letter for each resource levels.
258 * NOTE: There is a dependency on the corresponding
259 * letter index being equal to the resource number.
260 * If sys/resource.h defines are changed, this needs
261 * to be modified accordingly!
262 */
263 static const char rcs_string[] = "tfdscmlunbvpwkoyV";
264 _Static_assert(sizeof(rcs_string) - 1 == RLIM_NLIMITS,
265 "Please add letters to rcs_string[] for the new limits");
266
267 static rlim_t resource_num(int which, int ch, const char *str);
268 static void usage(void) __dead2;
269 static int getshelltype(void);
270 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
271 const char *pfx, const char *sfx, const char *which);
272 static void getrlimit_proc(pid_t pid, int resource, struct rlimit *rlp);
273 static void setrlimit_proc(pid_t pid, int resource, const struct rlimit *rlp);
274 extern char **environ;
275
276 int
main(int argc,char * argv[])277 main(int argc, char *argv[])
278 {
279 char *p, *cls = NULL;
280 char *cleanenv[1];
281 struct passwd * pwd = NULL;
282 int rcswhich, shelltype;
283 int i, num_limits = 0;
284 int ch, doeval = 0, doall = 0;
285 int rtrn, setproc;
286 login_cap_t * lc = NULL;
287 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
288 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
289 int which_limits[RLIM_NLIMITS];
290 rlim_t set_limits[RLIM_NLIMITS];
291 struct rlimit limits[RLIM_NLIMITS];
292 pid_t pid;
293
294 /* init resource tables */
295 for (i = 0; i < RLIM_NLIMITS; i++) {
296 which_limits[i] = 0; /* Don't set/display any */
297 set_limits[i] = RLIM_INFINITY;
298 }
299
300 pid = -1;
301 optarg = NULL;
302 while ((ch = getopt(argc, argv,
303 ":ab:BC:c:d:Eef:Hk:l:m:n:o:P:p:Ss:t:U:u:V:v:w:y:")) != -1) {
304 switch(ch) {
305 case 'a':
306 doall = 1;
307 break;
308 case 'E':
309 environ = cleanenv;
310 cleanenv[0] = NULL;
311 break;
312 case 'e':
313 doeval = 1;
314 break;
315 case 'C':
316 cls = optarg;
317 break;
318 case 'U':
319 if ((pwd = getpwnam(optarg)) == NULL) {
320 if (!isdigit(*optarg) ||
321 (pwd = getpwuid(atoi(optarg))) == NULL) {
322 warnx("invalid user `%s'", optarg);
323 usage();
324 }
325 }
326 break;
327 case 'H':
328 type = HARD;
329 break;
330 case 'S':
331 type = SOFT;
332 break;
333 case 'B':
334 type = SOFT|HARD;
335 break;
336 case 'P':
337 if (!isdigit(*optarg) || (pid = atoi(optarg)) < 0) {
338 warnx("invalid pid `%s'", optarg);
339 usage();
340 }
341 break;
342 default:
343 case ':': /* Without arg */
344 if ((p = strchr(rcs_string, optopt)) != NULL) {
345 int rcswhich1 = p - rcs_string;
346 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
347 --optind; /* back one arg, and make arg NULL */
348 optarg = NULL;
349 }
350 todo = optarg == NULL ? RCSSEL : RCSSET;
351 if (type == ANY)
352 type = BOTH;
353 which_limits[rcswhich1] = optarg ? type : DISPLAYONLY;
354 set_limits[rcswhich1] = resource_num(rcswhich1, optopt, optarg);
355 num_limits++;
356 break;
357 }
358 /* FALLTHRU */
359 case '?':
360 usage();
361 }
362 optarg = NULL;
363 }
364
365 if (pid != -1) {
366 if (cls != NULL) {
367 warnx("-C cannot be used with -P option");
368 usage();
369 }
370 if (pwd != NULL) {
371 warnx("-U cannot be used with -P option");
372 usage();
373 }
374 }
375
376 /* Get current resource values */
377 setproc = 0;
378 for (i = 0; i < RLIM_NLIMITS; i++) {
379 if (pid == -1) {
380 getrlimit(i, &limits[i]);
381 } else if (doall || num_limits == 0) {
382 getrlimit_proc(pid, i, &limits[i]);
383 } else if (which_limits[i] != 0) {
384 getrlimit_proc(pid, i, &limits[i]);
385 setproc = 1;
386 }
387 }
388
389 /* If user was specified, get class from that */
390 if (pwd != NULL)
391 lc = login_getpwclass(pwd);
392 else if (cls != NULL && *cls != '\0') {
393 lc = login_getclassbyname(cls, NULL);
394 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
395 fprintf(stderr, "login class '%s' non-existent, using %s\n",
396 cls, lc?lc->lc_class:"current settings");
397 }
398
399 /* If we have a login class, update resource table from that */
400 if (lc != NULL) {
401 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
402 char str[40];
403 rlim_t val;
404
405 /* current value overridden by resourcename or resourcename-cur */
406 sprintf(str, "%s-cur", resources[rcswhich].cap);
407 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
408 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
409 /* maximum value overridden by resourcename or resourcename-max */
410 sprintf(str, "%s-max", resources[rcswhich].cap);
411 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
412 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
413 }
414 }
415
416 /* now, let's determine what we wish to do with all this */
417
418 argv += optind;
419
420 /* If we're setting limits or doing an eval (ie. we're not just
421 * displaying), then check that hard limits are not lower than
422 * soft limits, and force rasing the hard limit if we need to if
423 * we are raising the soft limit, or lower the soft limit if we
424 * are lowering the hard limit.
425 */
426 if ((*argv || doeval) && getuid() == 0) {
427
428 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
429 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
430 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
431 limits[rcswhich].rlim_max = RLIM_INFINITY;
432 which_limits[rcswhich] |= HARD;
433 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
434 if (which_limits[rcswhich] == SOFT) {
435 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
436 which_limits[rcswhich] |= HARD;
437 } else if (which_limits[rcswhich] == HARD) {
438 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
439 which_limits[rcswhich] |= SOFT;
440 } else {
441 /* else.. if we're specifically setting both to
442 * silly values, then let it error out.
443 */
444 }
445 }
446 }
447 }
448 }
449
450 /* See if we've overridden anything specific on the command line */
451 if (num_limits && todo == RCSSET) {
452 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
453 if (which_limits[rcswhich] & HARD)
454 limits[rcswhich].rlim_max = set_limits[rcswhich];
455 if (which_limits[rcswhich] & SOFT)
456 limits[rcswhich].rlim_cur = set_limits[rcswhich];
457 }
458 }
459
460 /* If *argv is not NULL, then we are being asked to
461 * (perhaps) set environment variables and run a program
462 */
463 if (*argv) {
464 if (doeval) {
465 warnx("-e cannot be used with `cmd' option");
466 usage();
467 }
468 if (pid != -1) {
469 warnx("-P cannot be used with `cmd' option");
470 usage();
471 }
472
473 login_close(lc);
474
475 /* set leading environment variables, like eval(1) */
476 while (*argv && (p = strchr(*argv, '='))) {
477 *p = '\0';
478 rtrn = setenv(*argv++, p + 1, 1);
479 *p = '=';
480 if (rtrn == -1)
481 err(EXIT_FAILURE, "setenv %s", *argv);
482 }
483
484 /* Set limits */
485 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
486 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
487 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
488 err(1, "setrlimit %s", resources[rcswhich].cap);
489 }
490
491 if (*argv == NULL)
492 usage();
493
494 execvp(*argv, argv);
495 err(1, "%s", *argv);
496 }
497
498 if (setproc) {
499 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
500 if (which_limits[rcswhich] != 0)
501 setrlimit_proc(pid, rcswhich, &limits[rcswhich]);
502 }
503 exit(EXIT_SUCCESS);
504 }
505
506 shelltype = doeval ? getshelltype() : SH_NONE;
507
508 if (type == ANY) /* Default to soft limits */
509 type = SOFT;
510
511 /* Display limits */
512 printf(shellparm[shelltype].cmd,
513 lc ? " for class " : " (current)",
514 lc ? lc->lc_class : "");
515
516 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
517 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
518 if (which_limits[rcswhich] == ANY)
519 which_limits[rcswhich] = type;
520 if (shellparm[shelltype].lprm[rcswhich].pfx) {
521 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
522 print_limit(limits[rcswhich].rlim_max,
523 shellparm[shelltype].lprm[rcswhich].divisor,
524 shellparm[shelltype].inf,
525 shellparm[shelltype].lprm[rcswhich].pfx,
526 shellparm[shelltype].lprm[rcswhich].sfx,
527 shellparm[shelltype].both);
528 } else {
529 if (which_limits[rcswhich] & HARD) {
530 print_limit(limits[rcswhich].rlim_max,
531 shellparm[shelltype].lprm[rcswhich].divisor,
532 shellparm[shelltype].inf,
533 shellparm[shelltype].lprm[rcswhich].pfx,
534 shellparm[shelltype].lprm[rcswhich].sfx,
535 shellparm[shelltype].hard);
536 }
537 if (which_limits[rcswhich] & SOFT) {
538 print_limit(limits[rcswhich].rlim_cur,
539 shellparm[shelltype].lprm[rcswhich].divisor,
540 shellparm[shelltype].inf,
541 shellparm[shelltype].lprm[rcswhich].pfx,
542 shellparm[shelltype].lprm[rcswhich].sfx,
543 shellparm[shelltype].soft);
544 }
545 }
546 }
547 }
548 }
549
550 login_close(lc);
551 exit(EXIT_SUCCESS);
552 }
553
554
555 static void
usage(void)556 usage(void)
557 {
558 (void)fprintf(stderr,
559 "usage: limits [-C class|-P pid|-U user] [-eaSHBE] "
560 "[-bcdfklmnostuVvpwy [val]] [[name=val ...] cmd]\n");
561 exit(EXIT_FAILURE);
562 }
563
564 static void
print_limit(rlim_t limit,unsigned divisor,const char * inf,const char * pfx,const char * sfx,const char * which)565 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
566 {
567 char numbr[64];
568
569 if (limit == RLIM_INFINITY)
570 strlcpy(numbr, inf, sizeof(numbr));
571 else
572 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor));
573 printf(pfx, which, numbr);
574 printf(sfx, which);
575
576 }
577
578
579 static rlim_t
resource_num(int which,int ch,const char * str)580 resource_num(int which, int ch, const char *str)
581 {
582 rlim_t res = RLIM_INFINITY;
583
584 if (str != NULL &&
585 !(strcasecmp(str, "inf") == 0 ||
586 strcasecmp(str, "infinity") == 0 ||
587 strcasecmp(str, "unlimit") == 0 ||
588 strcasecmp(str, "unlimited") == 0)) {
589 const char * s = str;
590 char *e;
591
592 switch (which) {
593 case RLIMIT_CPU: /* time values */
594 errno = 0;
595 res = 0;
596 while (*s) {
597 rlim_t tim = strtoq(s, &e, 0);
598 if (e == NULL || e == s || errno)
599 break;
600 switch (*e++) {
601 case 0: /* end of string */
602 e--;
603 default:
604 case 's': case 'S': /* seconds */
605 break;
606 case 'm': case 'M': /* minutes */
607 tim *= 60L;
608 break;
609 case 'h': case 'H': /* hours */
610 tim *= (60L * 60L);
611 break;
612 case 'd': case 'D': /* days */
613 tim *= (60L * 60L * 24L);
614 break;
615 case 'w': case 'W': /* weeks */
616 tim *= (60L * 60L * 24L * 7L);
617 break;
618 case 'y': case 'Y': /* Years */
619 tim *= (60L * 60L * 24L * 365L);
620 }
621 s = e;
622 res += tim;
623 }
624 break;
625 case RLIMIT_FSIZE: /* Size values */
626 case RLIMIT_DATA:
627 case RLIMIT_STACK:
628 case RLIMIT_CORE:
629 case RLIMIT_RSS:
630 case RLIMIT_MEMLOCK:
631 case RLIMIT_SBSIZE:
632 case RLIMIT_VMEM:
633 case RLIMIT_SWAP:
634 case RLIMIT_PIPEBUF:
635 errno = 0;
636 res = 0;
637 while (*s) {
638 rlim_t mult, tim = strtoq(s, &e, 0);
639 if (e == NULL || e == s || errno)
640 break;
641 switch (*e++) {
642 case 0: /* end of string */
643 e--;
644 default:
645 mult = 1;
646 break;
647 case 'b': case 'B': /* 512-byte blocks */
648 mult = 512;
649 break;
650 case 'k': case 'K': /* 1024-byte Kilobytes */
651 mult = 1024;
652 break;
653 case 'm': case 'M': /* 1024-k kbytes */
654 mult = 1024 * 1024;
655 break;
656 case 'g': case 'G': /* 1Gbyte */
657 mult = 1024 * 1024 * 1024;
658 break;
659 case 't': case 'T': /* 1TBte */
660 mult = 1024LL * 1024LL * 1024LL * 1024LL;
661 break;
662 }
663 s = e;
664 res += (tim * mult);
665 }
666 break;
667 case RLIMIT_NPROC:
668 case RLIMIT_NOFILE:
669 case RLIMIT_NPTS:
670 case RLIMIT_KQUEUES:
671 case RLIMIT_UMTXP:
672 case RLIMIT_VMM:
673 res = strtoq(s, &e, 0);
674 s = e;
675 break;
676 }
677 if (*s) {
678 warnx("invalid value -%c `%s'", ch, str);
679 usage();
680 }
681 }
682 return res;
683 }
684
685
686 static int
getshellbyname(const char * shell)687 getshellbyname(const char * shell)
688 {
689 int i;
690 const char * q;
691 const char * p = strrchr(shell, '/');
692
693 p = p ? p+1 : shell;
694 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
695 while (*q) {
696 int j = strcspn(q, "|");
697
698 if (j == 0)
699 break;
700 if (strncmp(p, q, j) == 0)
701 return i;
702 if (*(q += j))
703 ++q;
704 }
705 }
706 return SH_SH;
707 }
708
709
710 /*
711 * Determine the type of shell our parent process is
712 * This is quite tricky, not 100% reliable and probably
713 * not nearly as thorough as it should be. Basically, this
714 * is a "best guess" only, but hopefully will work in
715 * most cases.
716 */
717
718 static int
getshelltype(void)719 getshelltype(void)
720 {
721 pid_t ppid = getppid();
722
723 if (ppid != 1) {
724 struct kinfo_proc kp;
725 struct stat st;
726 char path[MAXPATHLEN];
727 char * shell = getenv("SHELL");
728 int mib[4];
729 size_t len;
730
731 mib[0] = CTL_KERN;
732 mib[1] = KERN_PROC;
733 mib[3] = ppid;
734
735 if (shell != NULL && stat(shell, &st) != -1) {
736 struct stat st1;
737
738 mib[2] = KERN_PROC_PATHNAME;
739 len = sizeof(path);
740 if (sysctl(mib, 4, path, &len, NULL, 0) != -1) {
741 /* $SHELL is actual shell? */
742 if (stat(path, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
743 return getshellbyname(shell);
744 }
745 }
746 mib[2] = KERN_PROC_PID;
747 len = sizeof(kp);
748 if (sysctl(mib, 4, &kp, &len, NULL, 0) != -1)
749 return getshellbyname(kp.ki_comm);
750 }
751 return SH_SH;
752 }
753
754 static void
getrlimit_proc(pid_t pid,int resource,struct rlimit * rlp)755 getrlimit_proc(pid_t pid, int resource, struct rlimit *rlp)
756 {
757 int error;
758 int name[5];
759 size_t len;
760
761 name[0] = CTL_KERN;
762 name[1] = KERN_PROC;
763 name[2] = KERN_PROC_RLIMIT;
764 name[3] = pid;
765 name[4] = resource;
766 len = sizeof(*rlp);
767 error = sysctl(name, 5, rlp, &len, NULL, 0);
768 if (error == -1)
769 err(EXIT_FAILURE, "sysctl: kern.proc.rlimit: %d", pid);
770 if (len != sizeof(*rlp))
771 errx(EXIT_FAILURE, "sysctl() returns wrong size");
772 }
773
774 static void
setrlimit_proc(pid_t pid,int resource,const struct rlimit * rlp)775 setrlimit_proc(pid_t pid, int resource, const struct rlimit *rlp)
776 {
777 int error;
778 int name[5];
779
780 name[0] = CTL_KERN;
781 name[1] = KERN_PROC;
782 name[2] = KERN_PROC_RLIMIT;
783 name[3] = pid;
784 name[4] = resource;
785 error = sysctl(name, 5, NULL, 0, rlp, sizeof(*rlp));
786 if (error == -1)
787 err(EXIT_FAILURE, "sysctl: kern.proc.rlimit: %d", pid);
788 }
789