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