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