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