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