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