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