GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/ksh/main.c Lines: 307 356 86.2 %
Date: 2017-11-13 Branches: 176 253 69.6 %

Line Branch Exec Source
1
/*	$OpenBSD: main.c,v 1.83 2017/08/11 23:10:55 guenther Exp $	*/
2
3
/*
4
 * startup, main loop, environments and error handling
5
 */
6
7
#include <sys/stat.h>
8
9
#include <errno.h>
10
#include <fcntl.h>
11
#include <paths.h>
12
#include <pwd.h>
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <string.h>
16
#include <unistd.h>
17
18
#include "sh.h"
19
20
extern char **environ;
21
22
/*
23
 * global data
24
 */
25
26
static void	reclaim(void);
27
static void	remove_temps(struct temp *tp);
28
static int	is_restricted(char *name);
29
static void	init_username(void);
30
31
const char *kshname;
32
pid_t	kshpid;
33
pid_t	procpid;
34
uid_t	ksheuid;
35
int	exstat;
36
int	subst_exstat;
37
const char *safe_prompt;
38
39
Area	aperm;
40
41
struct env	*genv;
42
43
char	shell_flags[FNFLAGS];
44
45
char	null[] = "";
46
47
int shl_stdout_ok;
48
49
unsigned int	ksh_tmout;
50
enum tmout_enum	ksh_tmout_state = TMOUT_EXECUTING;
51
52
int	really_exit;
53
54
int ifs0 = ' ';
55
56
volatile sig_atomic_t	trap;
57
volatile sig_atomic_t	intrsig;
58
volatile sig_atomic_t	fatal_trap;
59
60
Getopt	builtin_opt;
61
Getopt	user_opt;
62
63
struct coproc	coproc;
64
sigset_t	sm_default, sm_sigchld;
65
66
char	*builtin_argv0;
67
int	 builtin_flag;
68
69
char	*current_wd;
70
int	 current_wd_size;
71
72
#ifdef EDIT
73
int	x_cols = 80;
74
#endif /* EDIT */
75
76
/*
77
 * shell initialization
78
 */
79
80
static const char initifs[] = "IFS= \t\n";
81
82
static const char initsubs[] = "${PS2=> } ${PS3=#? } ${PS4=+ }";
83
84
static const char *initcoms [] = {
85
	"typeset", "-r", "KSH_VERSION", NULL,
86
	"typeset", "-x", "SHELL", "PATH", "HOME", NULL,
87
	"typeset", "-ir", "PPID", NULL,
88
	"typeset", "-i", "OPTIND=1", NULL,
89
	"eval", "typeset -i RANDOM MAILCHECK=\"${MAILCHECK-600}\" SECONDS=\"${SECONDS-0}\" TMOUT=\"${TMOUT-0}\"", NULL,
90
	"alias",
91
	 /* Standard ksh aliases */
92
	  "hash=alias -t",	/* not "alias -t --": hash -r needs to work */
93
	  "type=whence -v",
94
#ifdef JOBS
95
	  "stop=kill -STOP",
96
#endif
97
	  "autoload=typeset -fu",
98
	  "functions=typeset -f",
99
#ifdef HISTORY
100
	  "history=fc -l",
101
#endif /* HISTORY */
102
	  "integer=typeset -i",
103
	  "nohup=nohup ",
104
	  "local=typeset",
105
	  "r=fc -s",
106
	 /* Aliases that are builtin commands in at&t */
107
	  "login=exec login",
108
	  NULL,
109
	/* this is what at&t ksh seems to track, with the addition of emacs */
110
	"alias", "-tU",
111
	  "cat", "cc", "chmod", "cp", "date", "ed", "emacs", "grep", "ls",
112
	  "mail", "make", "mv", "pr", "rm", "sed", "sh", "vi", "who",
113
	  NULL,
114
	NULL
115
};
116
117
char username[_PW_NAME_LEN + 1];
118
119
#define version_param  (initcoms[2])
120
121
/* The shell uses its own variation on argv, to build variables like
122
 * $0 and $@.
123
 * Allocate a new array since modifying the original argv will modify
124
 * ps output.
125
 */
126
static char **
127
make_argv(int argc, char *argv[])
128
{
129
	int i;
130
	char **nargv;
131
132
209526
	nargv = areallocarray(NULL, argc + 1, sizeof(char *), &aperm);
133
104763
	nargv[0] = (char *) kshname;
134
243546
	for (i = 1; i < argc; i++)
135
17010
		nargv[i] = argv[i];
136
104763
	nargv[i] = NULL;
137
138
104763
	return nargv;
139
}
140
141
int
142
main(int argc, char *argv[])
143
{
144
	int i;
145
	int argi;
146
	Source *s;
147
	struct block *l;
148
	int restricted, errexit;
149
	char **wp;
150
209526
	struct env env;
151
	pid_t ppid;
152
153
104763
	kshname = argv[0];
154
155
209526
	if (pledge("stdio rpath wpath cpath fattr flock getpw proc exec tty flock rpath cpath wpath",
156
104763
	    NULL) == -1) {
157
		perror("pledge");
158
		exit(1);
159
	}
160
161
104763
	ainit(&aperm);		/* initialize permanent Area */
162
163
	/* set up base environment */
164
104763
	memset(&env, 0, sizeof(env));
165
104763
	env.type = E_NONE;
166
104763
	ainit(&env.area);
167
104763
	genv = &env;
168
104763
	newblock();		/* set up global l->vars and l->funs */
169
170
	/* Do this first so output routines (eg, errorf, shellf) can work */
171
104763
	initio();
172
173
104763
	initvar();
174
175
104763
	initctypes();
176
177
104763
	inittraps();
178
179
104763
	coproc_init();
180
181
	/* set up variable and command dictionaries */
182
104763
	ktinit(&taliases, APERM, 0);
183
104763
	ktinit(&aliases, APERM, 0);
184
104763
	ktinit(&homedirs, APERM, 0);
185
186
	/* define shell keywords */
187
104763
	initkeywords();
188
189
	/* define built-in commands */
190
104763
	ktinit(&builtins, APERM, 64); /* must be 2^n (currently 40 builtins) */
191
5028624
	for (i = 0; shbuiltins[i].name != NULL; i++)
192
2409549
		builtin(shbuiltins[i].name, shbuiltins[i].func);
193
4190520
	for (i = 0; kshbuiltins[i].name != NULL; i++)
194
1990497
		builtin(kshbuiltins[i].name, kshbuiltins[i].func);
195
196
104763
	init_histvec();
197
198
104763
	def_path = _PATH_DEFPATH;
199
	{
200
104763
		size_t len = confstr(_CS_PATH, NULL, 0);
201
		char *new;
202
203
104763
		if (len > 0) {
204
104763
			confstr(_CS_PATH, new = alloc(len + 1, APERM), len + 1);
205
104763
			def_path = new;
206
104763
		}
207
	}
208
209
	/* Set PATH to def_path (will set the path global variable).
210
	 * (import of environment below will probably change this setting).
211
	 */
212
	{
213
104763
		struct tbl *vp = global("PATH");
214
		/* setstr can't fail here */
215
104763
		setstr(vp, def_path, KSH_RETURN_ERROR);
216
	}
217
218
219
	/* Turn on nohup by default for now - will change to off
220
	 * by default once people are aware of its existence
221
	 * (at&t ksh does not have a nohup option - it always sends
222
	 * the hup).
223
	 */
224
104763
	Flag(FNOHUP) = 1;
225
226
	/* Turn on brace expansion by default.  At&t ksh's that have
227
	 * alternation always have it on.  BUT, posix doesn't have
228
	 * brace expansion, so set this before setting up FPOSIX
229
	 * (change_flag() clears FBRACEEXPAND when FPOSIX is set).
230
	 */
231
#ifdef BRACE_EXPAND
232
104763
	Flag(FBRACEEXPAND) = 1;
233
#endif /* BRACE_EXPAND */
234
235
	/* set posix flag just before environment so that it will have
236
	 * exactly the same effect as the POSIXLY_CORRECT environment
237
	 * variable.  If this needs to be done sooner to ensure correct posix
238
	 * operation, an initial scan of the environment will also have
239
	 * done sooner.
240
	 */
241
#ifdef POSIXLY_CORRECT
242
	change_flag(FPOSIX, OF_SPECIAL, 1);
243
#endif /* POSIXLY_CORRECT */
244
245
	/* Check to see if we're /bin/sh. */
246

271215
	if (!strcmp(kshname, "sh") || !strcmp(kshname, "-sh") ||
247
83224
	    (strlen(kshname) >= 3 &&
248
83224
	    !strcmp(&kshname[strlen(kshname) - 3], "/sh"))) {
249
101879
		Flag(FSH) = 1;
250
101879
		version_param = "SH_VERSION";
251
101879
	}
252
253
	/* Set edit mode to emacs by default, may be overridden
254
	 * by the environment or the user.  Also, we want tab completion
255
	 * on in vi by default. */
256
#if defined(EDIT) && defined(EMACS)
257
104763
	change_flag(FEMACS, OF_SPECIAL, 1);
258
#endif /* EDIT && EMACS */
259
#if defined(EDIT) && defined(VI)
260
104763
	Flag(FVITABCOMPLETE) = 1;
261
#endif /* EDIT && VI */
262
263
	/* import environment */
264
104763
	if (environ != NULL)
265
5130704
		for (wp = environ; *wp != NULL; wp++)
266
2460589
			typeset(*wp, IMPORT|EXPORT, 0, 0, 0);
267
268
104763
	kshpid = procpid = getpid();
269
104763
	typeset(initifs, 0, 0, 0, 0);	/* for security */
270
271
	/* assign default shell variable values */
272
104763
	substitute(initsubs, 0);
273
274
	/* Figure out the current working directory and set $PWD */
275
	{
276
104763
		struct stat s_pwd, s_dot;
277
104763
		struct tbl *pwd_v = global("PWD");
278
104763
		char *pwd = str_val(pwd_v);
279
		char *pwdx = pwd;
280
281
		/* Try to use existing $PWD if it is valid */
282

207520
		if (pwd[0] != '/' ||
283

205514
		    stat(pwd, &s_pwd) < 0 || stat(".", &s_dot) < 0 ||
284
102757
		    s_pwd.st_dev != s_dot.st_dev ||
285
102757
		    s_pwd.st_ino != s_dot.st_ino)
286
2006
			pwdx = NULL;
287
104763
		set_current_wd(pwdx);
288
104763
		if (current_wd[0])
289
104763
			simplify_path(current_wd);
290
		/* Only set pwd if we know where we are or if it had a
291
		 * bogus value
292
		 */
293
104763
		if (current_wd[0] || pwd != null)
294
			/* setstr can't fail here */
295
104763
			setstr(pwd_v, current_wd, KSH_RETURN_ERROR);
296
104763
	}
297
104763
	ppid = getppid();
298
104763
	setint(global("PPID"), (long) ppid);
299
	/* setstr can't fail here */
300
104763
	setstr(global(version_param), ksh_version, KSH_RETURN_ERROR);
301
302
	/* execute initialization statements */
303
1676208
	for (wp = (char**) initcoms; *wp != NULL; wp++) {
304
733341
		shcomexec(wp);
305
11523930
		for (; *wp != NULL; wp++)
306
			;
307
	}
308
309
310
104763
	ksheuid = geteuid();
311
104763
	init_username();
312
104763
	safe_prompt = ksheuid ? "$ " : "# ";
313
	{
314
104763
		struct tbl *vp = global("PS1");
315
316
		/* Set PS1 if it isn't set */
317
104763
		if (!(vp->flag & ISSET)) {
318
			/* setstr can't fail here */
319
104109
			setstr(vp, safe_prompt, KSH_RETURN_ERROR);
320
104109
		}
321
	}
322
323
	/* Set this before parsing arguments */
324
314289
	Flag(FPRIVILEGED) = getuid() != ksheuid || getgid() != getegid();
325
326
	/* this to note if monitor is set on command line (see below) */
327
104763
	Flag(FMONITOR) = 127;
328
104763
	argi = parse_args(argv, OF_CMDLINE, NULL);
329
104763
	if (argi < 0)
330
		exit(1);
331
332
104763
	if (Flag(FCOMMAND)) {
333
89819
		s = pushs(SSTRING, ATEMP);
334
89819
		if (!(s->start = s->str = argv[argi++]))
335
			errorf("-c requires an argument");
336
89819
		if (argv[argi])
337
1
			kshname = argv[argi++];
338
14944
	} else if (argi < argc && !Flag(FSTDIN)) {
339
12338
		s = pushs(SFILE, ATEMP);
340
12338
		s->file = argv[argi++];
341
12338
		s->u.shf = shf_open(s->file, O_RDONLY, 0, SHF_MAPHI|SHF_CLEXEC);
342
12338
		if (s->u.shf == NULL) {
343
			exstat = 127; /* POSIX */
344
			errorf("%s: %s", s->file, strerror(errno));
345
		}
346
12338
		kshname = s->file;
347
12338
	} else {
348
2606
		Flag(FSTDIN) = 1;
349
2606
		s = pushs(SSTDIN, ATEMP);
350
2606
		s->file = "<stdin>";
351
2606
		s->u.shf = shf_fdopen(0, SHF_RD | can_seek(0), NULL);
352

3264
		if (isatty(0) && isatty(2)) {
353
658
			Flag(FTALKING) = Flag(FTALKING_I) = 1;
354
			/* The following only if isatty(0) */
355
658
			s->flags |= SF_TTY;
356
658
			s->u.shf->flags |= SHF_INTERRUPT;
357
658
			s->file = NULL;
358
658
		}
359
	}
360
361
	/* This bizarreness is mandated by POSIX */
362
	{
363
104763
		struct stat s_stdin;
364
365

312233
		if (fstat(0, &s_stdin) >= 0 && S_ISCHR(s_stdin.st_mode) &&
366
102707
		    Flag(FTALKING))
367
658
			reset_nonblock(0);
368
104763
	}
369
370
	/* initialize job control */
371
104763
	i = Flag(FMONITOR) != 127;
372
104763
	Flag(FMONITOR) = 0;
373
104763
	j_init(i);
374
#ifdef EDIT
375
	/* Do this after j_init(), as tty_fd is not initialized 'til then */
376
104763
	if (Flag(FTALKING))
377
880
		x_init();
378
#endif
379
380
104763
	l = genv->loc;
381
104763
	l->argv = make_argv(argc - (argi - 1), &argv[argi - 1]);
382
104763
	l->argc = argc - argi;
383
104763
	getopts_reset(1);
384
385
	/* Disable during .profile/ENV reading */
386
104763
	restricted = Flag(FRESTRICTED);
387
104763
	Flag(FRESTRICTED) = 0;
388
104763
	errexit = Flag(FERREXIT);
389
104763
	Flag(FERREXIT) = 0;
390
391
	/* Do this before profile/$ENV so that if it causes problems in them,
392
	 * user will know why things broke.
393
	 */
394

104763
	if (!current_wd[0] && Flag(FTALKING))
395
		warningf(false, "Cannot determine current working directory");
396
397
104763
	if (Flag(FLOGIN)) {
398
8
		include(KSH_SYSTEM_PROFILE, 0, NULL, 1);
399
8
		if (!Flag(FPRIVILEGED))
400
8
			include(substitute("$HOME/.profile", 0), 0, NULL, 1);
401
	}
402
403
104763
	if (Flag(FPRIVILEGED))
404
		include("/etc/suid_profile", 0, NULL, 1);
405
104763
	else if (Flag(FTALKING)) {
406
		char *env_file;
407
408
		/* include $ENV */
409
880
		env_file = str_val(global("ENV"));
410
411
#ifdef DEFAULT_ENV
412
		/* If env isn't set, include default environment */
413
		if (env_file == null)
414
			env_file = DEFAULT_ENV;
415
#endif /* DEFAULT_ENV */
416
880
		env_file = substitute(env_file, DOTILDE);
417
880
		if (*env_file != '\0')
418
192
			include(env_file, 0, NULL, 1);
419
880
	}
420
421

209526
	if (is_restricted(argv[0]) || is_restricted(str_val(global("SHELL"))))
422
		restricted = 1;
423
104763
	if (restricted) {
424
		static const char *const restr_com[] = {
425
			"typeset", "-r", "PATH",
426
			"ENV", "SHELL",
427
			NULL
428
		};
429
654
		shcomexec((char **) restr_com);
430
		/* After typeset command... */
431
654
		Flag(FRESTRICTED) = 1;
432
654
	}
433
104763
	if (errexit)
434
56907
		Flag(FERREXIT) = 1;
435
436
104763
	if (Flag(FTALKING)) {
437
880
		hist_init(s);
438
880
		alarm_init();
439
880
	} else
440
103883
		Flag(FTRACKALL) = 1;	/* set after ENV */
441
442
104763
	shell(s, true);	/* doesn't return */
443
104763
	return 0;
444
104763
}
445
446
static void
447
init_username(void)
448
{
449
	char *p;
450
209526
	struct tbl *vp = global("USER");
451
452
104763
	if (vp->flag & ISSET)
453
209526
		p = ksheuid == 0 ? "root" : str_val(vp);
454
	else
455
		p = getlogin();
456
457
104763
	strlcpy(username, p != NULL ? p : "?", sizeof username);
458
104763
}
459
460
int
461
include(const char *name, int argc, char **argv, int intr_ok)
462
{
463
3188
	Source *volatile s = NULL;
464
	struct shf *shf;
465
1594
	char **volatile old_argv;
466
1594
	volatile int old_argc;
467
	int i;
468
469
1594
	shf = shf_open(name, O_RDONLY, 0, SHF_MAPHI|SHF_CLEXEC);
470
1594
	if (shf == NULL)
471
8
		return -1;
472
473
1586
	if (argv) {
474
		old_argv = genv->loc->argv;
475
		old_argc = genv->loc->argc;
476
	} else {
477
1586
		old_argv = NULL;
478
1586
		old_argc = 0;
479
	}
480
1586
	newenv(E_INCL);
481
1586
	i = sigsetjmp(genv->jbuf, 0);
482
1586
	if (i) {
483
		quitenv(s ? s->u.shf : NULL);
484
		if (old_argv) {
485
			genv->loc->argv = old_argv;
486
			genv->loc->argc = old_argc;
487
		}
488
		switch (i) {
489
		case LRETURN:
490
		case LERROR:
491
			return exstat & 0xff; /* see below */
492
		case LINTR:
493
			/* intr_ok is set if we are including .profile or $ENV.
494
			 * If user ^C's out, we don't want to kill the shell...
495
			 */
496
			if (intr_ok && (exstat - 128) != SIGTERM)
497
				return 1;
498
			/* FALLTHROUGH */
499
		case LEXIT:
500
		case LLEAVE:
501
		case LSHELL:
502
			unwind(i);
503
			/* NOTREACHED */
504
		default:
505
			internal_errorf(1, "include: %d", i);
506
			/* NOTREACHED */
507
		}
508
	}
509
1586
	if (argv) {
510
		genv->loc->argv = argv;
511
		genv->loc->argc = argc;
512
	}
513
1586
	s = pushs(SFILE, ATEMP);
514
1586
	s->u.shf = shf;
515
1586
	s->file = str_save(name, ATEMP);
516
1586
	i = shell(s, false);
517
1586
	quitenv(s->u.shf);
518
1586
	if (old_argv) {
519
		genv->loc->argv = old_argv;
520
		genv->loc->argc = old_argc;
521
	}
522
1586
	return i & 0xff;	/* & 0xff to ensure value not -1 */
523
1594
}
524
525
/*
526
 * spawn a command into a shell optionally keeping track of line
527
 * number.
528
 */
529
int
530
command(const char *comm, int line)
531
{
532
	Source *s;
533
534
468
	s = pushs(SSTRING, ATEMP);
535
234
	s->start = s->str = comm;
536
234
	s->line = line;
537
234
	return shell(s, false);
538
}
539
540
/*
541
 * run the commands from the input source, returning status.
542
 */
543
int
544
shell(Source *volatile s, volatile int toplevel)
545
{
546
	struct op *t;
547
225804
	volatile int wastty = s->flags & SF_TTY;
548
225804
	volatile int attempts = 13;
549
452736
	volatile int interactive = Flag(FTALKING) && toplevel;
550
225804
	Source *volatile old_source = source;
551
	int i;
552
553
225804
	newenv(E_PARSE);
554
225804
	if (interactive)
555
880
		really_exit = 0;
556
295791
	i = sigsetjmp(genv->jbuf, 0);
557
295791
	if (i) {
558

70141
		switch (i) {
559
		case LINTR: /* we get here if SIGINT not caught or ignored */
560
		case LERROR:
561
		case LSHELL:
562
168
			if (interactive) {
563
14
				if (i == LINTR)
564
1
					shellf("\n");
565
				/* Reset any eof that was read as part of a
566
				 * multiline command.
567
				 */
568

14
				if (Flag(FIGNOREEOF) && s->type == SEOF &&
569
				    wastty)
570
					s->type = SSTDIN;
571
				/* Used by exit command to get back to
572
				 * top level shell.  Kind of strange since
573
				 * interactive is set if we are reading from
574
				 * a tty, but to have stopped jobs, one only
575
				 * needs FMONITOR set (not FTALKING/SF_TTY)...
576
				 */
577
				/* toss any input we have so far */
578
14
				s->start = s->str = null;
579
14
				break;
580
			}
581
			/* FALLTHROUGH */
582
		case LEXIT:
583
		case LLEAVE:
584
		case LRETURN:
585
			source = old_source;
586
			quitenv(NULL);
587
			unwind(i);	/* keep on going */
588
			/* NOTREACHED */
589
		default:
590
			source = old_source;
591
			quitenv(NULL);
592
			internal_errorf(1, "shell: %d", i);
593
			/* NOTREACHED */
594
		}
595
	}
596
597
1153335
	while (1) {
598
1153335
		if (trap)
599
78907
			runtraps(0);
600
601
1153332
		if (s->next == NULL) {
602
2306664
			if (Flag(FVERBOSE))
603
1153332
				s->flags |= SF_ECHO;
604
			else
605
1153332
				s->flags &= ~SF_ECHO;
606
1153332
		}
607
608
1153332
		if (interactive) {
609
1664
			got_sigwinch = 1;
610
1664
			j_notify();
611
1664
			mcheck();
612
1664
			set_prompt(PS1, s);
613
1664
		}
614
615
1152665
		t = compile(s);
616

1861832
		if (t != NULL && t->type == TEOF) {
617

189160
			if (wastty && Flag(FIGNOREEOF) && --attempts > 0) {
618
				shellf("Use `exit' to leave ksh\n");
619
				s->type = SSTDIN;
620

189160
			} else if (wastty && !really_exit &&
621
4
			    j_stopped_running()) {
622
				really_exit = 1;
623
				s->type = SSTDIN;
624
			} else {
625
				/* this for POSIX, which says EXIT traps
626
				 * shall be taken in the environment
627
				 * immediately after the last command
628
				 * executed.
629
				 */
630
189156
				if (toplevel)
631
					unwind(LEXIT);
632
				break;
633
			}
634
		}
635
636

1483520
		if (t && (!Flag(FNOEXEC) || (s->flags & SF_TTY)))
637
520011
			exstat = execute(t, 0, NULL);
638
639

1895555
		if (t != NULL && t->type != TEOF && interactive && really_exit)
640
			really_exit = 0;
641
642
927517
		reclaim();
643
	}
644
120891
	quitenv(NULL);
645
120891
	source = old_source;
646
241782
	return exstat;
647
120891
}
648
649
/* return to closest error handler or shell(), exit if none found */
650
void
651
unwind(int i)
652
{
653
	/* ordering for EXIT vs ERR is a bit odd (this is what at&t ksh does) */
654

704401
	if (i == LEXIT || (Flag(FERREXIT) && (i == LERROR || i == LINTR) &&
655
56959
	    sigtraps[SIGEXIT_].trap)) {
656
71887
		if (trap)
657
470
			runtraps(0);
658
71887
		runtrap(&sigtraps[SIGEXIT_]);
659
		i = LLEAVE;
660

277741
	} else if (Flag(FERREXIT) && (i == LERROR || i == LINTR)) {
661
157
		if (trap)
662
144
			runtraps(0);
663
147
		runtrap(&sigtraps[SIGERR_]);
664
		i = LLEAVE;
665
147
	}
666
495830
	while (1) {
667

460920
		switch (genv->type) {
668
		case E_PARSE:
669
		case E_FUNC:
670
		case E_INCL:
671
		case E_LOOP:
672
		case E_ERRH:
673
			siglongjmp(genv->jbuf, i);
674
			/* NOTREACHED */
675
676
		case E_NONE:
677
69847
			if (i == LINTR)
678
10
				genv->flags |= EF_FAKE_SIGDIE;
679
			/* FALLTHROUGH */
680
681
		default:
682
275178
			quitenv(NULL);
683
			/*
684
			 * quitenv() may have reclaimed the memory
685
			 * used by source which will end badly when
686
			 * we jump to a function that expects it to
687
			 * be valid
688
			 */
689
275178
			source = NULL;
690
		}
691
	}
692
}
693
694
void
695
newenv(int type)
696
{
697
	struct env *ep;
698
699
21810224
	ep = alloc(sizeof(*ep), ATEMP);
700
10905112
	ep->type = type;
701
10905112
	ep->flags = 0;
702
10905112
	ainit(&ep->area);
703
10905112
	ep->loc = genv->loc;
704
10905112
	ep->savefd = NULL;
705
10905112
	ep->oenv = genv;
706
10905112
	ep->temps = NULL;
707
10905112
	genv = ep;
708
10905112
}
709
710
void
711
quitenv(struct shf *shf)
712
{
713
21728208
	struct env *ep = genv;
714
	int fd;
715
716

21623445
	if (ep->oenv && ep->oenv->loc != ep->loc)
717
2521491
		popblock();
718
10864104
	if (ep->savefd != NULL) {
719
66805002
		for (fd = 0; fd < NUFILE; fd++)
720
			/* if ep->savefd[fd] < 0, means fd was closed */
721
32390304
			if (ep->savefd[fd])
722
1016287
				restfd(fd, ep->savefd[fd]);
723
1012197
		if (ep->savefd[2]) /* Clear any write errors */
724
3270
			shf_reopen(2, SHF_WR, shl_out);
725
	}
726
727
	/* Bottom of the stack.
728
	 * Either main shell is exiting or cleanup_parents_env() was called.
729
	 */
730
10864104
	if (ep->oenv == NULL) {
731
104763
		if (ep->type == E_NONE) {	/* Main shell exiting? */
732
69847
			if (Flag(FTALKING))
733
856
				hist_finish();
734
69847
			j_exit();
735
69847
			if (ep->flags & EF_FAKE_SIGDIE) {
736
10
				int sig = exstat - 128;
737
738
				/* ham up our death a bit (at&t ksh
739
				 * only seems to do this for SIGTERM)
740
				 * Don't do it for SIGQUIT, since we'd
741
				 * dump a core..
742
				 */
743

20
				if ((sig == SIGINT || sig == SIGTERM) &&
744
10
				    getpgrp() == kshpid) {
745
					setsig(&sigtraps[sig], SIG_DFL,
746
					    SS_RESTORE_CURR|SS_FORCE);
747
					kill(0, sig);
748
				}
749
10
			}
750
		}
751
104763
		if (shf)
752
6
			shf_close(shf);
753
		reclaim();
754
		exit(exstat);
755
	}
756
10759341
	if (shf)
757
1586
		shf_close(shf);
758
10759341
	reclaim();
759
760
10759341
	genv = genv->oenv;
761
10759341
	afree(ep, ATEMP);
762
10759341
}
763
764
/* Called after a fork to cleanup stuff left over from parents environment */
765
void
766
cleanup_parents_env(void)
767
{
768
	struct env *ep;
769
	int fd;
770
771
	/* Don't clean up temporary files - parent will probably need them.
772
	 * Also, can't easily reclaim memory since variables, etc. could be
773
	 * anywhere.
774
	 */
775
776
	/* close all file descriptors hiding in savefd */
777
475647
	for (ep = genv; ep; ep = ep->oenv) {
778
182592
		if (ep->savefd) {
779
1952808
			for (fd = 0; fd < NUFILE; fd++)
780
946816
				if (ep->savefd[fd] > 0)
781
59027
					close(ep->savefd[fd]);
782
29588
			afree(ep->savefd, &ep->area);
783
29588
			ep->savefd = NULL;
784
29588
		}
785
	}
786
36821
	genv->oenv = NULL;
787
36821
}
788
789
/* Called just before an execve cleanup stuff temporary files */
790
void
791
cleanup_proc_env(void)
792
{
793
	struct env *ep;
794
795
	for (ep = genv; ep; ep = ep->oenv)
796
		remove_temps(ep->temps);
797
}
798
799
/* remove temp files and free ATEMP Area */
800
static void
801
reclaim(void)
802
{
803
23583242
	remove_temps(genv->temps);
804
11791621
	genv->temps = NULL;
805
11791621
	afreeall(&genv->area);
806
11791621
}
807
808
static void
809
remove_temps(struct temp *tp)
810
{
811
812
35379007
	for (; tp != NULL; tp = tp->next)
813
2072
		if (tp->pid == procpid) {
814
2072
			unlink(tp->name);
815
2072
		}
816
11791621
}
817
818
/* Returns true if name refers to a restricted shell */
819
static int
820
is_restricted(char *name)
821
{
822
	char *p;
823
824
419052
	if ((p = strrchr(name, '/')))
825
187978
		name = p + 1;
826
	/* accepts rsh, rksh, rpdksh, pdrksh */
827

419052
	if (strcmp(name, "rsh") && \
828
209526
		strcmp(name, "rksh") && \
829
209526
		strcmp(name, "rpdksh") && \
830
209526
		strcmp(name, "pdrksh"))
831
209526
		return(0);
832
	else
833
		return(1);
834
835
209526
}