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