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