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