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 * $FreeBSD: src/usr.bin/limits/limits.c,v 1.7.2.3 2003/05/22 09:26:57 sheldonh Exp $ 24 */ 25 26 #include <err.h> 27 #include <stdio.h> 28 #include <string.h> 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 #include <sys/param.h> 32 #include <stdlib.h> 33 #include <unistd.h> 34 #include <stdarg.h> 35 #include <ctype.h> 36 #include <errno.h> 37 #include <pwd.h> 38 #include <login_cap.h> 39 #include <sys/time.h> 40 #include <sys/resource.h> 41 42 enum 43 { 44 SH_NONE, 45 SH_SH, /* sh */ 46 SH_CSH, /* csh */ 47 SH_BASH, /* gnu bash */ 48 SH_TCSH, /* tcsh */ 49 SH_KSH, /* (pd)ksh */ 50 SH_ZSH, /* zsh */ 51 SH_RC, /* rc or es */ 52 SH_NUMBER 53 }; 54 55 56 /* eval emitter for popular shells. 57 * Why aren't there any standards here? Most shells support either 58 * the csh 'limit' or sh 'ulimit' command, but each varies just 59 * enough that they aren't very compatible from one to the other. 60 */ 61 static struct { 62 const char * name; /* Name of shell */ 63 const char * inf; /* Name used for 'unlimited' resource */ 64 const char * cmd; /* Intro text */ 65 const char * hard; /* Hard limit text */ 66 const char * soft; /* Soft limit text */ 67 const char * both; /* Hard+Soft limit text */ 68 struct { 69 const char * pfx; 70 const char * sfx; 71 int divisor; 72 } lprm[RLIM_NLIMITS]; 73 } shellparm[] = 74 { 75 { "", "infinity", "Resource limits%s%s:\n", "-max", "-cur", "", 76 { 77 { " cputime%-4s %8s", " secs\n", 1 }, 78 { " filesize%-4s %8s", " kb\n", 1024 }, 79 { " datasize%-4s %8s", " kb\n", 1024 }, 80 { " stacksize%-4s %8s", " kb\n", 1024 }, 81 { " coredumpsize%-4s %8s", " kb\n", 1024 }, 82 { " memoryuse%-4s %8s", " kb\n", 1024 }, 83 { " memorylocked%-4s %8s", " kb\n", 1024 }, 84 { " maxprocesses%-4s %8s", "\n", 1 }, 85 { " openfiles%-4s %8s", "\n", 1 }, 86 { " sbsize%-4s %8s", " bytes\n", 1 }, 87 { " vmemoryuse%-4s %8s", " kb\n", 1024 }, 88 #ifdef RLIMIT_POSIXLOCKS 89 { " posixlocks%-4s %8s", "\n", 1 }, 90 #endif 91 } 92 }, 93 { "sh", "unlimited", "", " -H", " -S", "", 94 { 95 { "ulimit%s -t %s", ";\n", 1 }, 96 { "ulimit%s -f %s", ";\n", 512 }, 97 { "ulimit%s -d %s", ";\n", 1024 }, 98 { "ulimit%s -s %s", ";\n", 1024 }, 99 { "ulimit%s -c %s", ";\n", 512 }, 100 { "ulimit%s -m %s", ";\n", 1024 }, 101 { "ulimit%s -l %s", ";\n", 1024 }, 102 { "ulimit%s -u %s", ";\n", 1 }, 103 { "ulimit%s -n %s", ";\n", 1 }, 104 { "ulimit%s -b %s", ";\n", 1 }, 105 { "ulimit%s -v %s", ";\n", 1024 }, 106 #ifdef RLIMIT_POSIXLOCKS 107 { "ulimit%s -k %s", ";\n", 1 }, 108 #endif 109 } 110 }, 111 { "csh", "unlimited", "", " -h", "", NULL, 112 { 113 { "limit%s cputime %s", ";\n", 1 }, 114 { "limit%s filesize %s", ";\n", 1024 }, 115 { "limit%s datasize %s", ";\n", 1024 }, 116 { "limit%s stacksize %s", ";\n", 1024 }, 117 { "limit%s coredumpsize %s", ";\n", 1024 }, 118 { "limit%s memoryuse %s", ";\n", 1024 }, 119 { "limit%s memorylocked %s", ";\n", 1024 }, 120 { "limit%s maxproc %s", ";\n", 1 }, 121 { "limit%s openfiles %s", ";\n", 1 }, 122 { "limit%s sbsize %s", ";\n", 1 }, 123 { "limit%s vmemoryuse %s", ";\n", 1024 }, 124 #ifdef RLIMIT_POSIXLOCKS 125 { "limit%s advlocks %s", ";\n", 1 }, 126 #endif 127 } 128 }, 129 { "bash|bash2", "unlimited", "", " -H", " -S", "", 130 { 131 { "ulimit%s -t %s", ";\n", 1 }, 132 { "ulimit%s -f %s", ";\n", 1024 }, 133 { "ulimit%s -d %s", ";\n", 1024 }, 134 { "ulimit%s -s %s", ";\n", 1024 }, 135 { "ulimit%s -c %s", ";\n", 1024 }, 136 { "ulimit%s -m %s", ";\n", 1024 }, 137 { "ulimit%s -l %s", ";\n", 1024 }, 138 { "ulimit%s -u %s", ";\n", 1 }, 139 { "ulimit%s -n %s", ";\n", 1 }, 140 { "ulimit%s -b %s", ";\n", 1 }, 141 { "ulimit%s -v %s", ";\n", 1024 }, 142 #ifdef RLIMIT_POSIXLOCKS 143 { "ulimit%s -k %s", ";\n", 1 }, 144 #endif 145 } 146 }, 147 { "tcsh", "unlimited", "", " -h", "", NULL, 148 { 149 { "limit%s cputime %s", ";\n", 1 }, 150 { "limit%s filesize %s", ";\n", 1024 }, 151 { "limit%s datasize %s", ";\n", 1024 }, 152 { "limit%s stacksize %s", ";\n", 1024 }, 153 { "limit%s coredumpsize %s", ";\n", 1024 }, 154 { "limit%s memoryuse %s", ";\n", 1024 }, 155 { "limit%s memorylocked %s", ";\n", 1024 }, 156 { "limit%s maxproc %s", ";\n", 1 }, 157 { "limit%s descriptors %s", ";\n", 1 }, 158 { "limit%s sbsize %s", ";\n", 1 }, 159 { "limit%s vmemoryuse %s", ";\n", 1024 }, 160 #ifdef RLIMIT_POSIXLOCKS 161 { "limit%s advlocks %s", ";\n", 1 }, 162 #endif 163 } 164 }, 165 { "ksh|pdksh", "unlimited", "", " -H", " -S", "", 166 { 167 { "ulimit%s -t %s", ";\n", 1 }, 168 { "ulimit%s -f %s", ";\n", 512 }, 169 { "ulimit%s -d %s", ";\n", 1024 }, 170 { "ulimit%s -s %s", ";\n", 1024 }, 171 { "ulimit%s -c %s", ";\n", 512 }, 172 { "ulimit%s -m %s", ";\n", 1024 }, 173 { "ulimit%s -l %s", ";\n", 1024 }, 174 { "ulimit%s -p %s", ";\n", 1 }, 175 { "ulimit%s -n %s", ";\n", 1 }, 176 { "ulimit%s -b %s", ";\n", 1 }, 177 { "ulimit%s -v %s", ";\n", 1024 }, 178 #ifdef RLIMIT_POSIXLOCKS 179 { "ulimit%s -k %s", ";\n", 1 }, 180 #endif 181 } 182 }, 183 { "zsh", "unlimited", "", " -H", " -S", "", 184 { 185 { "ulimit%s -t %s", ";\n", 1 }, 186 { "ulimit%s -f %s", ";\n", 512 }, 187 { "ulimit%s -d %s", ";\n", 1024 }, 188 { "ulimit%s -s %s", ";\n", 1024 }, 189 { "ulimit%s -c %s", ";\n", 512 }, 190 { "ulimit%s -m %s", ";\n", 1024 }, 191 { "ulimit%s -l %s", ";\n", 1024 }, 192 { "ulimit%s -u %s", ";\n", 1 }, 193 { "ulimit%s -n %s", ";\n", 1 }, 194 { "ulimit%s -b %s", ";\n", 1 }, 195 { "ulimit%s -v %s", ";\n", 1024 }, 196 #ifdef RLIMIT_POSIXLOCKS 197 { "ulimit%s -k %s", ";\n", 1 }, 198 #endif 199 } 200 }, 201 { "rc|es", "unlimited", "", " -h", "", NULL, 202 { 203 { "limit%s cputime %s", ";\n", 1 }, 204 { "limit%s filesize %s", ";\n", 1024 }, 205 { "limit%s datasize %s", ";\n", 1024 }, 206 { "limit%s stacksize %s", ";\n", 1024 }, 207 { "limit%s coredumpsize %s", ";\n", 1024 }, 208 { "limit%s memoryuse %s", ";\n", 1024 }, 209 { "limit%s lockedmemory %s", ";\n", 1024 }, 210 { "limit%s processes %s", ";\n", 1 }, 211 { "limit%s descriptors %s", ";\n", 1 }, 212 { "limit%s sbsize %s", ";\n", 1 }, 213 { "limit%s vmemoryuse %s", ";\n", 1024 }, 214 #ifdef RLIMIT_POSIXLOCKS 215 { "limit%s advlocks %s", ";\n", 1 }, 216 #endif 217 } 218 }, 219 { NULL, NULL, NULL, NULL, NULL, NULL, {} } 220 }; 221 222 static struct { 223 const char * cap; 224 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t); 225 } resources[RLIM_NLIMITS] = { 226 { "cputime", login_getcaptime }, 227 { "filesize", login_getcapsize }, 228 { "datasize", login_getcapsize }, 229 { "stacksize", login_getcapsize }, 230 { "coredumpsize", login_getcapsize }, 231 { "memoryuse", login_getcapsize }, 232 { "memorylocked", login_getcapsize }, 233 { "maxproc", login_getcapnum }, 234 { "openfiles", login_getcapnum }, 235 { "sbsize", login_getcapsize }, 236 { "vmemoryuse", login_getcapsize }, 237 { "posixlocks", login_getcapnum }, 238 }; 239 240 /* 241 * One letter for each resource levels. 242 * NOTE: There is a dependancy on the corresponding 243 * letter index being equal to the resource number. 244 * If sys/resource.h defines are changed, this needs 245 * to be modified accordingly! 246 */ 247 248 #define RCS_STRING "tfdscmlunbvk" 249 250 static rlim_t resource_num(int which, int ch, const char *str); 251 static void usage(void); 252 static int getshelltype(void); 253 static void print_limit(rlim_t limit, unsigned divisor, const char *inf, 254 const char *pfx, const char *sfx, const char *which); 255 extern char **environ; 256 257 static const char rcs_string[] = RCS_STRING; 258 259 int 260 main(int argc, char *argv[]) 261 { 262 char *p, *cls = NULL; 263 char *cleanenv[1]; 264 struct passwd * pwd = NULL; 265 int rcswhich, shelltype; 266 int i, num_limits = 0; 267 int ch, doeval = 0, doall = 0; 268 login_cap_t * lc = NULL; 269 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY; 270 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN; 271 int which_limits[RLIM_NLIMITS]; 272 rlim_t set_limits[RLIM_NLIMITS]; 273 struct rlimit limits[RLIM_NLIMITS]; 274 275 /* init resource tables */ 276 for (i = 0; i < RLIM_NLIMITS; i++) { 277 which_limits[i] = 0; /* Don't set/display any */ 278 set_limits[i] = RLIM_INFINITY; 279 /* Get current resource values */ 280 if (getrlimit(i, &limits[i]) < 0) { 281 limits[i].rlim_cur = -1; 282 limits[i].rlim_max = -1; 283 } 284 } 285 286 optarg = NULL; 287 while ((ch = getopt(argc, argv, ":EeC:U:BSHabc:d:f:k:l:m:n:s:t:u:v:")) != -1) { 288 switch(ch) { 289 case 'a': 290 doall = 1; 291 break; 292 case 'E': 293 environ = cleanenv; 294 cleanenv[0] = NULL; 295 break; 296 case 'e': 297 doeval = 1; 298 break; 299 case 'C': 300 cls = optarg; 301 break; 302 case 'U': 303 if ((pwd = getpwnam(optarg)) == NULL) { 304 if (!isdigit(*optarg) || 305 (pwd = getpwuid(atoi(optarg))) == NULL) { 306 warnx("invalid user `%s'", optarg); 307 usage(); 308 } 309 } 310 break; 311 case 'H': 312 type = HARD; 313 break; 314 case 'S': 315 type = SOFT; 316 break; 317 case 'B': 318 type = SOFT|HARD; 319 break; 320 default: 321 case ':': /* Without arg */ 322 if ((p = strchr(rcs_string, optopt)) != NULL) { 323 rcswhich = p - rcs_string; 324 325 /* 326 * Backwards compatibility with earlier kernel which might 327 * support fewer resources. 328 */ 329 if (rcswhich >= RLIM_NLIMITS) { 330 usage(); 331 break; 332 } 333 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */ 334 --optind; /* back one arg, and make arg NULL */ 335 optarg = NULL; 336 } 337 todo = optarg == NULL ? RCSSEL : RCSSET; 338 if (type == ANY) 339 type = BOTH; 340 which_limits[rcswhich] = optarg ? type : DISPLAYONLY; 341 set_limits[rcswhich] = resource_num(rcswhich, optopt, optarg); 342 num_limits++; 343 break; 344 } 345 /* FALLTHRU */ 346 case '?': 347 usage(); 348 } 349 optarg = NULL; 350 } 351 352 /* If user was specified, get class from that */ 353 if (pwd != NULL) 354 lc = login_getpwclass(pwd); 355 else if (cls != NULL && *cls != '\0') { 356 lc = login_getclassbyname(cls, NULL); 357 if (lc == NULL || strcmp(cls, lc->lc_class) != 0) 358 fprintf(stderr, "login class '%s' non-existent, using %s\n", 359 cls, lc?lc->lc_class:"current settings"); 360 } 361 362 /* If we have a login class, update resource table from that */ 363 if (lc != NULL) { 364 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) { 365 char str[40]; 366 rlim_t val; 367 368 /* current value overridden by resourcename or resourcename-cur */ 369 sprintf(str, "%s-cur", resources[rcswhich].cap); 370 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur); 371 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val); 372 /* maximum value overridden by resourcename or resourcename-max */ 373 sprintf(str, "%s-max", resources[rcswhich].cap); 374 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max); 375 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val); 376 } 377 } 378 379 /* now, let's determine what we wish to do with all this */ 380 381 argv += optind; 382 383 /* If we're setting limits or doing an eval (ie. we're not just 384 * displaying), then check that hard limits are not lower than 385 * soft limits, and force rasing the hard limit if we need to if 386 * we are raising the soft limit, or lower the soft limit if we 387 * are lowering the hard limit. 388 */ 389 if ((*argv || doeval) && getuid() == 0) { 390 391 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) { 392 if (limits[rcswhich].rlim_max != RLIM_INFINITY) { 393 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) { 394 limits[rcswhich].rlim_max = RLIM_INFINITY; 395 which_limits[rcswhich] |= HARD; 396 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) { 397 if (which_limits[rcswhich] == SOFT) { 398 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur; 399 which_limits[rcswhich] |= HARD; 400 } else if (which_limits[rcswhich] == HARD) { 401 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max; 402 which_limits[rcswhich] |= SOFT; 403 } else { 404 /* else.. if we're specifically setting both to 405 * silly values, then let it error out. 406 */ 407 } 408 } 409 } 410 } 411 } 412 413 /* See if we've overridden anything specific on the command line */ 414 if (num_limits && todo == RCSSET) { 415 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) { 416 if (which_limits[rcswhich] & HARD) 417 limits[rcswhich].rlim_max = set_limits[rcswhich]; 418 if (which_limits[rcswhich] & SOFT) 419 limits[rcswhich].rlim_cur = set_limits[rcswhich]; 420 } 421 } 422 423 /* If *argv is not NULL, then we are being asked to 424 * (perhaps) set environment variables and run a program 425 */ 426 if (*argv) { 427 if (doeval) { 428 warnx("-e cannot be used with `cmd' option"); 429 usage(); 430 } 431 432 login_close(lc); 433 434 /* set leading environment variables, like eval(1) */ 435 while (*argv && (p = strchr(*argv, '='))) { 436 *p = '\0'; 437 if (setenv(*argv++, p + 1, 1) == -1) 438 err(1, "setenv: cannot set %s=%s", *argv, p + 1); 439 *p = '='; 440 } 441 442 /* Set limits */ 443 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) { 444 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) 445 if (setrlimit(rcswhich, &limits[rcswhich]) == -1) 446 err(1, "setrlimit %s", resources[rcswhich].cap); 447 } 448 449 if (*argv == NULL) 450 usage(); 451 452 execvp(*argv, argv); 453 err(1, "%s", *argv); 454 } 455 456 shelltype = doeval ? getshelltype() : SH_NONE; 457 458 if (type == ANY) /* Default to soft limits */ 459 type = SOFT; 460 461 /* Display limits */ 462 printf(shellparm[shelltype].cmd, 463 lc ? " for class " : " (current)", 464 lc ? lc->lc_class : ""); 465 466 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) { 467 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) { 468 if (which_limits[rcswhich] == ANY) 469 which_limits[rcswhich] = type; 470 if (shellparm[shelltype].lprm[rcswhich].pfx) { 471 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) { 472 print_limit(limits[rcswhich].rlim_max, 473 shellparm[shelltype].lprm[rcswhich].divisor, 474 shellparm[shelltype].inf, 475 shellparm[shelltype].lprm[rcswhich].pfx, 476 shellparm[shelltype].lprm[rcswhich].sfx, 477 shellparm[shelltype].both); 478 } else { 479 if (which_limits[rcswhich] & HARD) { 480 print_limit(limits[rcswhich].rlim_max, 481 shellparm[shelltype].lprm[rcswhich].divisor, 482 shellparm[shelltype].inf, 483 shellparm[shelltype].lprm[rcswhich].pfx, 484 shellparm[shelltype].lprm[rcswhich].sfx, 485 shellparm[shelltype].hard); 486 } 487 if (which_limits[rcswhich] & SOFT) { 488 print_limit(limits[rcswhich].rlim_cur, 489 shellparm[shelltype].lprm[rcswhich].divisor, 490 shellparm[shelltype].inf, 491 shellparm[shelltype].lprm[rcswhich].pfx, 492 shellparm[shelltype].lprm[rcswhich].sfx, 493 shellparm[shelltype].soft); 494 } 495 } 496 } 497 } 498 } 499 500 login_close(lc); 501 exit(EXIT_SUCCESS); 502 } 503 504 505 static void 506 usage(void) 507 { 508 (void)fprintf(stderr, 509 "usage: limits [-C class|-U user] [-eaSHBE] [-bcdflmnstuvk [val]] [[name=val ...] cmd]\n"); 510 exit(EXIT_FAILURE); 511 } 512 513 static void 514 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which) 515 { 516 char numbr[64]; 517 518 if (limit == RLIM_INFINITY) 519 strcpy(numbr, inf); 520 else 521 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor)); 522 printf(pfx, which, numbr); 523 printf(sfx, which); 524 525 } 526 527 528 static rlim_t 529 resource_num(int which, int ch, const char *str) 530 { 531 rlim_t res = RLIM_INFINITY; 532 533 if (str != NULL && 534 !(strcasecmp(str, "inf") == 0 || 535 strcasecmp(str, "infinity") == 0 || 536 strcasecmp(str, "unlimit") == 0 || 537 strcasecmp(str, "unlimited") == 0)) { 538 const char * s = str; 539 char *e; 540 541 switch (which) { 542 case RLIMIT_CPU: /* time values */ 543 errno = 0; 544 res = 0; 545 while (*s) { 546 rlim_t tim = strtoq(s, &e, 0); 547 if (e == NULL || e == s || errno) 548 break; 549 switch (*e++) { 550 case 0: /* end of string */ 551 e--; 552 default: 553 case 's': case 'S': /* seconds */ 554 break; 555 case 'm': case 'M': /* minutes */ 556 tim *= 60L; 557 break; 558 case 'h': case 'H': /* hours */ 559 tim *= (60L * 60L); 560 break; 561 case 'd': case 'D': /* days */ 562 tim *= (60L * 60L * 24L); 563 break; 564 case 'w': case 'W': /* weeks */ 565 tim *= (60L * 60L * 24L * 7L); 566 break; 567 case 'y': case 'Y': /* Years */ 568 tim *= (60L * 60L * 24L * 365L); 569 } 570 s = e; 571 res += tim; 572 } 573 break; 574 case RLIMIT_FSIZE: /* Size values */ 575 case RLIMIT_DATA: 576 case RLIMIT_STACK: 577 case RLIMIT_CORE: 578 case RLIMIT_RSS: 579 case RLIMIT_MEMLOCK: 580 case RLIMIT_VMEM: 581 errno = 0; 582 res = 0; 583 while (*s) { 584 rlim_t mult, tim = strtoq(s, &e, 0); 585 if (e == NULL || e == s || errno) 586 break; 587 switch (*e++) { 588 case 0: /* end of string */ 589 e--; 590 /* FALLTHROUGH */ 591 default: 592 mult = 1; 593 break; 594 case 'b': case 'B': /* 512-byte blocks */ 595 mult = 512; 596 break; 597 case 'k': case 'K': /* 1024-byte Kilobytes */ 598 mult = 1024; 599 break; 600 case 'm': case 'M': /* 1024-k kbytes */ 601 mult = 1024 * 1024; 602 break; 603 case 'g': case 'G': /* 1Gbyte */ 604 mult = 1024 * 1024 * 1024; 605 break; 606 case 't': case 'T': /* 1TBte */ 607 mult = 1024LL * 1024LL * 1024LL * 1024LL; 608 break; 609 } 610 s = e; 611 res += (tim * mult); 612 } 613 break; 614 case RLIMIT_NPROC: 615 case RLIMIT_NOFILE: 616 res = strtoq(s, &e, 0); 617 s = e; 618 break; 619 } 620 if (*s) { 621 warnx("invalid value -%c `%s'", ch, str); 622 usage(); 623 } 624 } 625 return res; 626 } 627 628 629 static int 630 getshellbyname(const char * shell) 631 { 632 int i; 633 const char * q; 634 const char * p = strrchr(shell, '/'); 635 636 p = p ? p + 1 : shell; 637 for (i = 0; (q = shellparm[i].name) != NULL; i++) { 638 while (*q) { 639 int j = strcspn(q, "|"); 640 641 if (j == 0) 642 break; 643 if (strncmp(p, q, j) == 0) 644 return i; 645 if (*(q += j)) 646 ++q; 647 } 648 } 649 return SH_SH; 650 } 651 652 653 /* 654 * Determine the type of shell our parent process is 655 * This is quite tricky, not 100% reliable and probably 656 * not nearly as thorough as it should be. Basically, this 657 * is a "best guess" only, but hopefully will work in 658 * most cases. 659 */ 660 661 static int 662 getshelltype(void) 663 { 664 pid_t ppid = getppid(); 665 666 if (ppid != 1) { 667 FILE * fp; 668 struct stat st; 669 char procdir[MAXPATHLEN], buf[128]; 670 int l = sprintf(procdir, "/proc/%ld/", (long)ppid); 671 char * shell = getenv("SHELL"); 672 673 if (shell != NULL && stat(shell, &st) != -1) { 674 struct stat st1; 675 676 strcpy(procdir+l, "file"); 677 /* $SHELL is actual shell? */ 678 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0) 679 return getshellbyname(shell); 680 } 681 strcpy(procdir+l, "status"); 682 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) { 683 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t"); 684 fclose(fp); 685 if (p != NULL) 686 return getshellbyname(p); 687 } 688 } 689 return SH_SH; 690 } 691 692