• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*	$OpenBSD: main.c,v 1.55 2015/02/09 09:09:30 jsg Exp $	*/
2 /*	$OpenBSD: tty.c,v 1.10 2014/08/10 02:44:26 guenther Exp $	*/
3 /*	$OpenBSD: io.c,v 1.25 2014/08/11 20:28:47 guenther Exp $	*/
4 /*	$OpenBSD: table.c,v 1.15 2012/02/19 07:52:30 otto Exp $	*/
5 
6 /*-
7  * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
8  *		 2011, 2012, 2013, 2014, 2015
9  *	Thorsten Glaser <tg@mirbsd.org>
10  *
11  * Provided that these terms and disclaimer and all copyright notices
12  * are retained or reproduced in an accompanying document, permission
13  * is granted to deal in this work without restriction, including un-
14  * limited rights to use, publicly perform, distribute, sell, modify,
15  * merge, give away, or sublicence.
16  *
17  * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
18  * the utmost extent permitted by applicable law, neither express nor
19  * implied; without malicious intent or gross negligence. In no event
20  * may a licensor, author or contributor be held liable for indirect,
21  * direct, other damage, loss, or other issues arising in any way out
22  * of dealing in the work, even if advised of the possibility of such
23  * damage or existence of a defect, except proven that it results out
24  * of said person's immediate fault when using the work as intended.
25  */
26 
27 #define EXTERN
28 #include "sh.h"
29 
30 #if HAVE_LANGINFO_CODESET
31 #include <langinfo.h>
32 #endif
33 #if HAVE_SETLOCALE_CTYPE
34 #include <locale.h>
35 #endif
36 
37 __RCSID("$MirOS: src/bin/mksh/main.c,v 1.285.2.4 2015/04/19 19:18:20 tg Exp $");
38 
39 extern char **environ;
40 
41 #ifndef MKSHRC_PATH
42 #define MKSHRC_PATH	"~/.mkshrc"
43 #endif
44 
45 #ifndef MKSH_DEFAULT_TMPDIR
46 #define MKSH_DEFAULT_TMPDIR	"/tmp"
47 #endif
48 
49 static uint8_t isuc(const char *);
50 static int main_init(int, const char *[], Source **, struct block **);
51 void chvt_reinit(void);
52 static void reclaim(void);
53 static void remove_temps(struct temp *);
54 static mksh_uari_t rndsetup(void);
55 #ifdef SIGWINCH
56 static void x_sigwinch(int);
57 #endif
58 
59 static const char initifs[] = "IFS= \t\n";
60 
61 static const char initsubs[] =
62     "${PS2=> } ${PS3=#? } ${PS4=+ } ${SECONDS=0} ${TMOUT=0} ${EPOCHREALTIME=}";
63 
64 static const char *initcoms[] = {
65 	Ttypeset, "-r", initvsn, NULL,
66 	Ttypeset, "-x", "HOME", "PATH", "SHELL", NULL,
67 	Ttypeset, "-i10", "COLUMNS", "LINES", "SECONDS", "TMOUT", NULL,
68 	Talias,
69 	"integer=typeset -i",
70 	Tlocal_typeset,
71 	/* not "alias -t --": hash -r needs to work */
72 	"hash=alias -t",
73 	"type=whence -v",
74 #if !defined(ANDROID) && !defined(MKSH_UNEMPLOYED)
75 	/* not in Android for political reasons */
76 	/* not in ARGE mksh due to no job control */
77 	"stop=kill -STOP",
78 #endif
79 	"autoload=typeset -fu",
80 	"functions=typeset -f",
81 	"history=fc -l",
82 	"nameref=typeset -n",
83 	"nohup=nohup ",
84 	Tr_fc_e_dash,
85 	"source=PATH=$PATH:. command .",
86 	"login=exec login",
87 	NULL,
88 	 /* this is what AT&T ksh seems to track, with the addition of emacs */
89 	Talias, "-tU",
90 	"cat", "cc", "chmod", "cp", "date", "ed", "emacs", "grep", "ls",
91 	"make", "mv", "pr", "rm", "sed", "sh", "vi", "who", NULL,
92 	NULL
93 };
94 
95 static const char *restr_com[] = {
96 	Ttypeset, "-r", "PATH", "ENV", "SHELL", NULL
97 };
98 
99 static bool initio_done;
100 
101 /* top-level parsing and execution environment */
102 static struct env env;
103 struct env *e = &env;
104 
105 static mksh_uari_t
rndsetup(void)106 rndsetup(void)
107 {
108 	register uint32_t h;
109 	struct {
110 		ALLOC_ITEM alloc_INT;
111 		void *dataptr, *stkptr, *mallocptr;
112 #if defined(__GLIBC__) && (__GLIBC__ >= 2)
113 		sigjmp_buf jbuf;
114 #endif
115 		struct timeval tv;
116 	} *bufptr;
117 	char *cp;
118 
119 	cp = alloc(sizeof(*bufptr) - ALLOC_SIZE, APERM);
120 #ifdef DEBUG
121 	/* clear the allocated space, for valgrind */
122 	memset(cp, 0, sizeof(*bufptr) - ALLOC_SIZE);
123 #endif
124 	/* undo what alloc() did to the malloc result address */
125 	bufptr = (void *)(cp - ALLOC_SIZE);
126 	/* PIE or something similar provides us with deltas here */
127 	bufptr->dataptr = &rndsetupstate;
128 	/* ASLR in at least Windows, Linux, some BSDs */
129 	bufptr->stkptr = &bufptr;
130 	/* randomised malloc in BSD (and possibly others) */
131 	bufptr->mallocptr = bufptr;
132 #if defined(__GLIBC__) && (__GLIBC__ >= 2)
133 	/* glibc pointer guard */
134 	sigsetjmp(bufptr->jbuf, 1);
135 #endif
136 	/* introduce variation (and yes, second arg MBZ for portability) */
137 	mksh_TIME(bufptr->tv);
138 
139 	h = chvt_rndsetup(bufptr, sizeof(*bufptr));
140 
141 	afree(cp, APERM);
142 	return ((mksh_uari_t)h);
143 }
144 
145 void
chvt_reinit(void)146 chvt_reinit(void)
147 {
148 	kshpid = procpid = getpid();
149 	ksheuid = geteuid();
150 	kshpgrp = getpgrp();
151 	kshppid = getppid();
152 }
153 
154 static const char *empty_argv[] = {
155 	"mksh", NULL
156 };
157 
158 static uint8_t
isuc(const char * cx)159 isuc(const char *cx) {
160 	char *cp, *x;
161 	uint8_t rv = 0;
162 
163 	if (!cx || !*cx)
164 		return (0);
165 
166 	/* uppercase a string duplicate */
167 	strdupx(x, cx, ATEMP);
168 	cp = x;
169 	while ((*cp = ksh_toupper(*cp)))
170 		++cp;
171 
172 	/* check for UTF-8 */
173 	if (strstr(x, "UTF-8") || strstr(x, "UTF8"))
174 		rv = 1;
175 
176 	/* free copy and out */
177 	afree(x, ATEMP);
178 	return (rv);
179 }
180 
181 static int
main_init(int argc,const char * argv[],Source ** sp,struct block ** lp)182 main_init(int argc, const char *argv[], Source **sp, struct block **lp)
183 {
184 	int argi, i;
185 	Source *s = NULL;
186 	struct block *l;
187 	unsigned char restricted_shell, errexit, utf_flag;
188 	char *cp;
189 	const char *ccp, **wp;
190 	struct tbl *vp;
191 	struct stat s_stdin;
192 #if !defined(_PATH_DEFPATH) && defined(_CS_PATH)
193 	ssize_t k;
194 #endif
195 
196 	/* do things like getpgrp() et al. */
197 	chvt_reinit();
198 
199 	/* make sure argv[] is sane */
200 	if (!*argv) {
201 		argv = empty_argv;
202 		argc = 1;
203 	}
204 	kshname = argv[0];
205 
206 	/* initialise permanent Area */
207 	ainit(&aperm);
208 
209 	/* set up base environment */
210 	env.type = E_NONE;
211 	ainit(&env.area);
212 	/* set up global l->vars and l->funs */
213 	newblock();
214 
215 	/* Do this first so output routines (eg, errorf, shellf) can work */
216 	initio();
217 
218 	/* determine the basename (without '-' or path) of the executable */
219 	ccp = kshname;
220 	goto begin_parse_kshname;
221 	while ((i = ccp[argi++])) {
222 		if (i == '/') {
223 			ccp += argi;
224  begin_parse_kshname:
225 			argi = 0;
226 			if (*ccp == '-')
227 				++ccp;
228 		}
229 	}
230 	if (!*ccp)
231 		ccp = empty_argv[0];
232 
233 	/*
234 	 * Turn on nohup by default. (AT&T ksh does not have a nohup
235 	 * option - it always sends the hup).
236 	 */
237 	Flag(FNOHUP) = 1;
238 
239 	/*
240 	 * Turn on brace expansion by default. AT&T kshs that have
241 	 * alternation always have it on.
242 	 */
243 	Flag(FBRACEEXPAND) = 1;
244 
245 	/*
246 	 * Turn on "set -x" inheritance by default.
247 	 */
248 	Flag(FXTRACEREC) = 1;
249 
250 	/* define built-in commands and see if we were called as one */
251 	ktinit(APERM, &builtins,
252 	    /* currently up to 51 builtins: 75% of 128 = 2^7 */
253 	    7);
254 	for (i = 0; mkshbuiltins[i].name != NULL; i++)
255 		if (!strcmp(ccp, builtin(mkshbuiltins[i].name,
256 		    mkshbuiltins[i].func)))
257 			Flag(FAS_BUILTIN) = 1;
258 
259 	if (!Flag(FAS_BUILTIN)) {
260 		/* check for -T option early */
261 		argi = parse_args(argv, OF_FIRSTTIME, NULL);
262 		if (argi < 0)
263 			return (1);
264 
265 #if defined(MKSH_BINSHPOSIX) || defined(MKSH_BINSHREDUCED)
266 		/* are we called as -sh or /bin/sh or so? */
267 		if (!strcmp(ccp, "sh")) {
268 			/* either also turns off braceexpand */
269 #ifdef MKSH_BINSHPOSIX
270 			/* enable better POSIX conformance */
271 			change_flag(FPOSIX, OF_FIRSTTIME, true);
272 #endif
273 #ifdef MKSH_BINSHREDUCED
274 			/* enable kludge/compat mode */
275 			change_flag(FSH, OF_FIRSTTIME, true);
276 #endif
277 		}
278 #endif
279 	}
280 
281 	initvar();
282 
283 	initctypes();
284 
285 	inittraps();
286 
287 	coproc_init();
288 
289 	/* set up variable and command dictionaries */
290 	ktinit(APERM, &taliases, 0);
291 	ktinit(APERM, &aliases, 0);
292 #ifndef MKSH_NOPWNAM
293 	ktinit(APERM, &homedirs, 0);
294 #endif
295 
296 	/* define shell keywords */
297 	initkeywords();
298 
299 	init_histvec();
300 
301 	/* initialise tty size before importing environment */
302 	change_winsz();
303 
304 #ifdef _PATH_DEFPATH
305 	def_path = _PATH_DEFPATH;
306 #else
307 #ifdef _CS_PATH
308 	if ((k = confstr(_CS_PATH, NULL, 0)) > 0 &&
309 	    confstr(_CS_PATH, cp = alloc(k + 1, APERM), k + 1) == k + 1)
310 		def_path = cp;
311 	else
312 #endif
313 		/*
314 		 * this is uniform across all OSes unless it
315 		 * breaks somewhere; don't try to optimise,
316 		 * e.g. add stuff for Interix or remove /usr
317 		 * for HURD, because e.g. Debian GNU/HURD is
318 		 * "keeping a regular /usr"; this is supposed
319 		 * to be a sane 'basic' default PATH
320 		 */
321 		def_path = "/bin:/usr/bin:/sbin:/usr/sbin";
322 #endif
323 
324 	/*
325 	 * Set PATH to def_path (will set the path global variable).
326 	 * (import of environment below will probably change this setting).
327 	 */
328 	vp = global("PATH");
329 	/* setstr can't fail here */
330 	setstr(vp, def_path, KSH_RETURN_ERROR);
331 
332 #ifndef MKSH_NO_CMDLINE_EDITING
333 	/*
334 	 * Set edit mode to emacs by default, may be overridden
335 	 * by the environment or the user. Also, we want tab completion
336 	 * on in vi by default.
337 	 */
338 	change_flag(FEMACS, OF_SPECIAL, true);
339 #if !MKSH_S_NOVI
340 	Flag(FVITABCOMPLETE) = 1;
341 #endif
342 #endif
343 
344 	/* import environment */
345 	if (environ != NULL) {
346 		wp = (const char **)environ;
347 		while (*wp != NULL) {
348 			rndpush(*wp);
349 			typeset(*wp, IMPORT | EXPORT, 0, 0, 0);
350 			++wp;
351 		}
352 	}
353 
354 	/* for security */
355 	typeset(initifs, 0, 0, 0, 0);
356 
357 	/* assign default shell variable values */
358 	substitute(initsubs, 0);
359 
360 	/* Figure out the current working directory and set $PWD */
361 	vp = global("PWD");
362 	cp = str_val(vp);
363 	/* Try to use existing $PWD if it is valid */
364 	set_current_wd((cp[0] == '/' && test_eval(NULL, TO_FILEQ, cp, ".",
365 	    true)) ? cp : NULL);
366 	if (current_wd[0])
367 		simplify_path(current_wd);
368 	/* Only set pwd if we know where we are or if it had a bogus value */
369 	if (current_wd[0] || *cp)
370 		/* setstr can't fail here */
371 		setstr(vp, current_wd, KSH_RETURN_ERROR);
372 
373 	for (wp = initcoms; *wp != NULL; wp++) {
374 		shcomexec(wp);
375 		while (*wp != NULL)
376 			wp++;
377 	}
378 	setint_n(global("OPTIND"), 1, 10);
379 
380 	kshuid = getuid();
381 	kshgid = getgid();
382 	kshegid = getegid();
383 
384 	safe_prompt = ksheuid ? "$ " : "# ";
385 	vp = global("PS1");
386 	/* Set PS1 if unset or we are root and prompt doesn't contain a # */
387 	if (!(vp->flag & ISSET) ||
388 	    (!ksheuid && !strchr(str_val(vp), '#')))
389 		/* setstr can't fail here */
390 		setstr(vp, safe_prompt, KSH_RETURN_ERROR);
391 	setint_n((vp = global("BASHPID")), 0, 10);
392 	vp->flag |= INT_U;
393 	setint_n((vp = global("PGRP")), (mksh_uari_t)kshpgrp, 10);
394 	vp->flag |= INT_U;
395 	setint_n((vp = global("PPID")), (mksh_uari_t)kshppid, 10);
396 	vp->flag |= INT_U;
397 	setint_n((vp = global("USER_ID")), (mksh_uari_t)ksheuid, 10);
398 	vp->flag |= INT_U;
399 	setint_n((vp = global("KSHUID")), (mksh_uari_t)kshuid, 10);
400 	vp->flag |= INT_U;
401 	setint_n((vp = global("KSHEGID")), (mksh_uari_t)kshegid, 10);
402 	vp->flag |= INT_U;
403 	setint_n((vp = global("KSHGID")), (mksh_uari_t)kshgid, 10);
404 	vp->flag |= INT_U;
405 	setint_n((vp = global("RANDOM")), rndsetup(), 10);
406 	vp->flag |= INT_U;
407 	setint_n((vp_pipest = global("PIPESTATUS")), 0, 10);
408 
409 	/* Set this before parsing arguments */
410 	Flag(FPRIVILEGED) = (
411 #if HAVE_ISSETUGID
412 	    issetugid() ||
413 #endif
414 	    kshuid != ksheuid || kshgid != kshegid) ? 2 : 0;
415 
416 	/* this to note if monitor is set on command line (see below) */
417 #ifndef MKSH_UNEMPLOYED
418 	Flag(FMONITOR) = 127;
419 #endif
420 	/* this to note if utf-8 mode is set on command line (see below) */
421 	UTFMODE = 2;
422 
423 	if (!Flag(FAS_BUILTIN)) {
424 		argi = parse_args(argv, OF_CMDLINE, NULL);
425 		if (argi < 0)
426 			return (1);
427 	}
428 
429 	/* process this later only, default to off (hysterical raisins) */
430 	utf_flag = UTFMODE;
431 	UTFMODE = 0;
432 
433 	if (Flag(FAS_BUILTIN)) {
434 		/* auto-detect from environment variables, always */
435 		utf_flag = 3;
436 	} else if (Flag(FCOMMAND)) {
437 		s = pushs(SSTRINGCMDLINE, ATEMP);
438 		if (!(s->start = s->str = argv[argi++]))
439 			errorf("%s %s", "-c", "requires an argument");
440 		while (*s->str) {
441 			if (*s->str != ' ' && ctype(*s->str, C_QUOTE))
442 				break;
443 			s->str++;
444 		}
445 		if (!*s->str)
446 			s->flags |= SF_MAYEXEC;
447 		s->str = s->start;
448 #ifdef MKSH_MIDNIGHTBSD01ASH_COMPAT
449 		/* compatibility to MidnightBSD 0.1 /bin/sh (kludge) */
450 		if (Flag(FSH) && argv[argi] && !strcmp(argv[argi], "--"))
451 			++argi;
452 #endif
453 		if (argv[argi])
454 			kshname = argv[argi++];
455 	} else if (argi < argc && !Flag(FSTDIN)) {
456 		s = pushs(SFILE, ATEMP);
457 		s->file = argv[argi++];
458 		s->u.shf = shf_open(s->file, O_RDONLY, 0,
459 		    SHF_MAPHI | SHF_CLEXEC);
460 		if (s->u.shf == NULL) {
461 			shl_stdout_ok = false;
462 			warningf(true, "%s: %s", s->file, cstrerror(errno));
463 			/* mandated by SUSv4 */
464 			exstat = 127;
465 			unwind(LERROR);
466 		}
467 		kshname = s->file;
468 	} else {
469 		Flag(FSTDIN) = 1;
470 		s = pushs(SSTDIN, ATEMP);
471 		s->file = "<stdin>";
472 		s->u.shf = shf_fdopen(0, SHF_RD | can_seek(0),
473 		    NULL);
474 		if (isatty(0) && isatty(2)) {
475 			Flag(FTALKING) = Flag(FTALKING_I) = 1;
476 			/* The following only if isatty(0) */
477 			s->flags |= SF_TTY;
478 			s->u.shf->flags |= SHF_INTERRUPT;
479 			s->file = NULL;
480 		}
481 	}
482 
483 	/* this bizarreness is mandated by POSIX */
484 	if (fstat(0, &s_stdin) >= 0 && S_ISCHR(s_stdin.st_mode) &&
485 	    Flag(FTALKING))
486 		reset_nonblock(0);
487 
488 	/* initialise job control */
489 	j_init();
490 	/* do this after j_init() which calls tty_init_state() */
491 	if (Flag(FTALKING)) {
492 		if (utf_flag == 2) {
493 #ifndef MKSH_ASSUME_UTF8
494 			/* auto-detect from locale or environment */
495 			utf_flag = 4;
496 #else /* this may not be an #elif */
497 #if MKSH_ASSUME_UTF8
498 			utf_flag = 1;
499 #else
500 			/* always disable UTF-8 (for interactive) */
501 			utf_flag = 0;
502 #endif
503 #endif
504 		}
505 #ifndef MKSH_NO_CMDLINE_EDITING
506 		x_init();
507 #endif
508 	}
509 
510 #ifdef SIGWINCH
511 	sigtraps[SIGWINCH].flags |= TF_SHELL_USES;
512 	setsig(&sigtraps[SIGWINCH], x_sigwinch,
513 	    SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP);
514 #endif
515 
516 	l = e->loc;
517 	if (Flag(FAS_BUILTIN)) {
518 		l->argc = argc;
519 		l->argv = argv;
520 		l->argv[0] = ccp;
521 	} else {
522 		l->argc = argc - argi;
523 		/*
524 		 * allocate a new array because otherwise, when we modify
525 		 * it in-place, ps(1) output changes; the meaning of argc
526 		 * here is slightly different as it excludes kshname, and
527 		 * we add a trailing NULL sentinel as well
528 		 */
529 		l->argv = alloc2(l->argc + 2, sizeof(void *), APERM);
530 		l->argv[0] = kshname;
531 		memcpy(&l->argv[1], &argv[argi], l->argc * sizeof(void *));
532 		l->argv[l->argc + 1] = NULL;
533 		getopts_reset(1);
534 	}
535 
536 	/* divine the initial state of the utf8-mode Flag */
537 	ccp = null;
538 	switch (utf_flag) {
539 
540 	/* auto-detect from locale or environment */
541 	case 4:
542 #if HAVE_SETLOCALE_CTYPE
543 		ccp = setlocale(LC_CTYPE, "");
544 #if HAVE_LANGINFO_CODESET
545 		if (!isuc(ccp))
546 			ccp = nl_langinfo(CODESET);
547 #endif
548 		if (!isuc(ccp))
549 			ccp = null;
550 		/* FALLTHROUGH */
551 #endif
552 
553 	/* auto-detect from environment */
554 	case 3:
555 		/* these were imported from environ earlier */
556 		if (ccp == null)
557 			ccp = str_val(global("LC_ALL"));
558 		if (ccp == null)
559 			ccp = str_val(global("LC_CTYPE"));
560 		if (ccp == null)
561 			ccp = str_val(global("LANG"));
562 		UTFMODE = isuc(ccp);
563 		break;
564 
565 	/* not set on command line, not FTALKING */
566 	case 2:
567 	/* unknown values */
568 	default:
569 		utf_flag = 0;
570 		/* FALLTHROUGH */
571 
572 	/* known values */
573 	case 1:
574 	case 0:
575 		UTFMODE = utf_flag;
576 		break;
577 	}
578 
579 	/* Disable during .profile/ENV reading */
580 	restricted_shell = Flag(FRESTRICTED);
581 	Flag(FRESTRICTED) = 0;
582 	errexit = Flag(FERREXIT);
583 	Flag(FERREXIT) = 0;
584 
585 	/*
586 	 * Do this before profile/$ENV so that if it causes problems in them,
587 	 * user will know why things broke.
588 	 */
589 	if (!current_wd[0] && Flag(FTALKING))
590 		warningf(false, "can't determine current directory");
591 
592 	if (Flag(FLOGIN))
593 		include(MKSH_SYSTEM_PROFILE, 0, NULL, true);
594 	if (!Flag(FPRIVILEGED)) {
595 		if (Flag(FLOGIN))
596 			include(substitute("$HOME/.profile", 0), 0, NULL, true);
597 		if (Flag(FTALKING)) {
598 			cp = substitute(substitute("${ENV:-" MKSHRC_PATH "}",
599 			    0), DOTILDE);
600 			if (cp[0] != '\0')
601 				include(cp, 0, NULL, true);
602 		}
603 	} else {
604 		include(MKSH_SUID_PROFILE, 0, NULL, true);
605 		/* turn off -p if not set explicitly */
606 		if (Flag(FPRIVILEGED) != 1)
607 			change_flag(FPRIVILEGED, OF_INTERNAL, false);
608 	}
609 
610 	if (restricted_shell) {
611 		shcomexec(restr_com);
612 		/* After typeset command... */
613 		Flag(FRESTRICTED) = 1;
614 	}
615 	Flag(FERREXIT) = errexit;
616 
617 	if (Flag(FTALKING) && s)
618 		hist_init(s);
619 	else
620 		/* set after ENV */
621 		Flag(FTRACKALL) = 1;
622 
623 	alarm_init();
624 
625 	*sp = s;
626 	*lp = l;
627 	return (0);
628 }
629 
630 /* this indirection barrier reduces stack usage during normal operation */
631 
632 int
main(int argc,const char * argv[])633 main(int argc, const char *argv[])
634 {
635 	int rv;
636 	Source *s;
637 	struct block *l;
638 
639 	if ((rv = main_init(argc, argv, &s, &l)) == 0) {
640 		if (Flag(FAS_BUILTIN)) {
641 			rv = shcomexec(l->argv);
642 		} else {
643 			shell(s, true);
644 			/* NOTREACHED */
645 		}
646 	}
647 	return (rv);
648 }
649 
650 int
include(const char * name,int argc,const char ** argv,bool intr_ok)651 include(const char *name, int argc, const char **argv, bool intr_ok)
652 {
653 	Source *volatile s = NULL;
654 	struct shf *shf;
655 	const char **volatile old_argv;
656 	volatile int old_argc;
657 	int i;
658 
659 	shf = shf_open(name, O_RDONLY, 0, SHF_MAPHI | SHF_CLEXEC);
660 	if (shf == NULL)
661 		return (-1);
662 
663 	if (argv) {
664 		old_argv = e->loc->argv;
665 		old_argc = e->loc->argc;
666 	} else {
667 		old_argv = NULL;
668 		old_argc = 0;
669 	}
670 	newenv(E_INCL);
671 	if ((i = kshsetjmp(e->jbuf))) {
672 		quitenv(s ? s->u.shf : NULL);
673 		if (old_argv) {
674 			e->loc->argv = old_argv;
675 			e->loc->argc = old_argc;
676 		}
677 		switch (i) {
678 		case LRETURN:
679 		case LERROR:
680 			/* see below */
681 			return (exstat & 0xFF);
682 		case LINTR:
683 			/*
684 			 * intr_ok is set if we are including .profile or $ENV.
685 			 * If user ^Cs out, we don't want to kill the shell...
686 			 */
687 			if (intr_ok && ((exstat & 0xFF) - 128) != SIGTERM)
688 				return (1);
689 			/* FALLTHROUGH */
690 		case LEXIT:
691 		case LLEAVE:
692 		case LSHELL:
693 			unwind(i);
694 			/* NOTREACHED */
695 		default:
696 			internal_errorf("%s %d", "include", i);
697 			/* NOTREACHED */
698 		}
699 	}
700 	if (argv) {
701 		e->loc->argv = argv;
702 		e->loc->argc = argc;
703 	}
704 	s = pushs(SFILE, ATEMP);
705 	s->u.shf = shf;
706 	strdupx(s->file, name, ATEMP);
707 	i = shell(s, false);
708 	quitenv(s->u.shf);
709 	if (old_argv) {
710 		e->loc->argv = old_argv;
711 		e->loc->argc = old_argc;
712 	}
713 	/* & 0xff to ensure value not -1 */
714 	return (i & 0xFF);
715 }
716 
717 /* spawn a command into a shell optionally keeping track of the line number */
718 int
command(const char * comm,int line)719 command(const char *comm, int line)
720 {
721 	Source *s;
722 
723 	s = pushs(SSTRING, ATEMP);
724 	s->start = s->str = comm;
725 	s->line = line;
726 	return (shell(s, false));
727 }
728 
729 /*
730  * run the commands from the input source, returning status.
731  */
732 int
shell(Source * volatile s,volatile bool toplevel)733 shell(Source * volatile s, volatile bool toplevel)
734 {
735 	struct op *t;
736 	volatile bool wastty = tobool(s->flags & SF_TTY);
737 	volatile uint8_t attempts = 13;
738 	volatile bool interactive = Flag(FTALKING) && toplevel;
739 	volatile bool sfirst = true;
740 	Source *volatile old_source = source;
741 	int i;
742 
743 	newenv(E_PARSE);
744 	if (interactive)
745 		really_exit = false;
746 	switch ((i = kshsetjmp(e->jbuf))) {
747 	case 0:
748 		break;
749 	case LINTR:
750 		/* we get here if SIGINT not caught or ignored */
751 	case LERROR:
752 	case LSHELL:
753 		if (interactive) {
754 			if (i == LINTR)
755 				shellf("\n");
756 			/*
757 			 * Reset any eof that was read as part of a
758 			 * multiline command.
759 			 */
760 			if (Flag(FIGNOREEOF) && s->type == SEOF && wastty)
761 				s->type = SSTDIN;
762 			/*
763 			 * Used by exit command to get back to
764 			 * top level shell. Kind of strange since
765 			 * interactive is set if we are reading from
766 			 * a tty, but to have stopped jobs, one only
767 			 * needs FMONITOR set (not FTALKING/SF_TTY)...
768 			 */
769 			/* toss any input we have so far */
770 			yyrecursive_pop(true);
771 			s->start = s->str = null;
772 			retrace_info = NULL;
773 			herep = heres;
774 			break;
775 		}
776 		/* FALLTHROUGH */
777 	case LEXIT:
778 	case LLEAVE:
779 	case LRETURN:
780 		source = old_source;
781 		quitenv(NULL);
782 		/* keep on going */
783 		unwind(i);
784 		/* NOTREACHED */
785 	default:
786 		source = old_source;
787 		quitenv(NULL);
788 		internal_errorf("%s %d", "shell", i);
789 		/* NOTREACHED */
790 	}
791 	while (/* CONSTCOND */ 1) {
792 		if (trap)
793 			runtraps(0);
794 
795 		if (s->next == NULL) {
796 			if (Flag(FVERBOSE))
797 				s->flags |= SF_ECHO;
798 			else
799 				s->flags &= ~SF_ECHO;
800 		}
801 		if (interactive) {
802 			j_notify();
803 			set_prompt(PS1, s);
804 		}
805 		t = compile(s, sfirst);
806 		sfirst = false;
807 		if (!t)
808 			goto source_no_tree;
809 		if (t->type == TEOF) {
810 			if (wastty && Flag(FIGNOREEOF) && --attempts > 0) {
811 				shellf("Use 'exit' to leave mksh\n");
812 				s->type = SSTDIN;
813 			} else if (wastty && !really_exit &&
814 			    j_stopped_running()) {
815 				really_exit = true;
816 				s->type = SSTDIN;
817 			} else {
818 				/*
819 				 * this for POSIX which says EXIT traps
820 				 * shall be taken in the environment
821 				 * immediately after the last command
822 				 * executed.
823 				 */
824 				if (toplevel)
825 					unwind(LEXIT);
826 				break;
827 			}
828 		} else if ((s->flags & SF_MAYEXEC) && t->type == TCOM)
829 			t->u.evalflags |= DOTCOMEXEC;
830 		if (!Flag(FNOEXEC) || (s->flags & SF_TTY))
831 			exstat = execute(t, 0, NULL) & 0xFF;
832 
833 		if (t->type != TEOF && interactive && really_exit)
834 			really_exit = false;
835 
836  source_no_tree:
837 		reclaim();
838 	}
839 	quitenv(NULL);
840 	source = old_source;
841 	return (exstat & 0xFF);
842 }
843 
844 /* return to closest error handler or shell(), exit if none found */
845 /* note: i MUST NOT be 0 */
846 void
unwind(int i)847 unwind(int i)
848 {
849 	/*
850 	 * This is a kludge. We need to restore everything that was
851 	 * changed in the new environment, see cid 1005090337C7A669439
852 	 * and 10050903386452ACBF1, but fail to even save things most of
853 	 * the time. funcs.c:c_eval() changes FERREXIT temporarily to 0,
854 	 * which needs to be restored thus (related to Debian #696823).
855 	 * We did not save the shell flags, so we use a special or'd
856 	 * value here... this is mostly to clean up behind *other*
857 	 * callers of unwind(LERROR) here; exec.c has the regular case.
858 	 */
859 	if (Flag(FERREXIT) & 0x80) {
860 		/* GNU bash does not run this trapsig */
861 		trapsig(ksh_SIGERR);
862 		Flag(FERREXIT) &= ~0x80;
863 	}
864 
865 	/* ordering for EXIT vs ERR is a bit odd (this is what AT&T ksh does) */
866 	if (i == LEXIT || ((i == LERROR || i == LINTR) &&
867 	    sigtraps[ksh_SIGEXIT].trap &&
868 	    (!Flag(FTALKING) || Flag(FERREXIT)))) {
869 		++trap_nested;
870 		runtrap(&sigtraps[ksh_SIGEXIT], trap_nested == 1);
871 		--trap_nested;
872 		i = LLEAVE;
873 	} else if (Flag(FERREXIT) == 1 && (i == LERROR || i == LINTR)) {
874 		++trap_nested;
875 		runtrap(&sigtraps[ksh_SIGERR], trap_nested == 1);
876 		--trap_nested;
877 		i = LLEAVE;
878 	}
879 
880 	while (/* CONSTCOND */ 1) {
881 		switch (e->type) {
882 		case E_PARSE:
883 		case E_FUNC:
884 		case E_INCL:
885 		case E_LOOP:
886 		case E_ERRH:
887 			kshlongjmp(e->jbuf, i);
888 			/* NOTREACHED */
889 		case E_NONE:
890 			if (i == LINTR)
891 				e->flags |= EF_FAKE_SIGDIE;
892 			/* FALLTHROUGH */
893 		default:
894 			quitenv(NULL);
895 			/*
896 			 * quitenv() may have reclaimed the memory
897 			 * used by source which will end badly when
898 			 * we jump to a function that expects it to
899 			 * be valid
900 			 */
901 			source = NULL;
902 		}
903 	}
904 }
905 
906 void
newenv(int type)907 newenv(int type)
908 {
909 	struct env *ep;
910 	char *cp;
911 
912 	/*
913 	 * struct env includes ALLOC_ITEM for alignment constraints
914 	 * so first get the actually used memory, then assign it
915 	 */
916 	cp = alloc(sizeof(struct env) - ALLOC_SIZE, ATEMP);
917 	/* undo what alloc() did to the malloc result address */
918 	ep = (void *)(cp - ALLOC_SIZE);
919 	/* initialise public members of struct env (not the ALLOC_ITEM) */
920 	ainit(&ep->area);
921 	ep->oenv = e;
922 	ep->loc = e->loc;
923 	ep->savefd = NULL;
924 	ep->temps = NULL;
925 	ep->yyrecursive_statep = NULL;
926 	ep->type = type;
927 	ep->flags = 0;
928 	/* jump buffer is invalid because flags == 0 */
929 	e = ep;
930 }
931 
932 void
quitenv(struct shf * shf)933 quitenv(struct shf *shf)
934 {
935 	struct env *ep = e;
936 	char *cp;
937 	int fd;
938 
939 	yyrecursive_pop(true);
940 	while (ep->oenv && ep->oenv->loc != ep->loc)
941 		popblock();
942 	if (ep->savefd != NULL) {
943 		for (fd = 0; fd < NUFILE; fd++)
944 			/* if ep->savefd[fd] < 0, means fd was closed */
945 			if (ep->savefd[fd])
946 				restfd(fd, ep->savefd[fd]);
947 		if (ep->savefd[2])
948 			/* Clear any write errors */
949 			shf_reopen(2, SHF_WR, shl_out);
950 	}
951 	/*
952 	 * Bottom of the stack.
953 	 * Either main shell is exiting or cleanup_parents_env() was called.
954 	 */
955 	if (ep->oenv == NULL) {
956 #ifdef DEBUG_LEAKS
957 		int i;
958 #endif
959 
960 		if (ep->type == E_NONE) {
961 			/* Main shell exiting? */
962 #if HAVE_PERSISTENT_HISTORY
963 			if (Flag(FTALKING))
964 				hist_finish();
965 #endif
966 			j_exit();
967 			if (ep->flags & EF_FAKE_SIGDIE) {
968 				int sig = (exstat & 0xFF) - 128;
969 
970 				/*
971 				 * ham up our death a bit (AT&T ksh
972 				 * only seems to do this for SIGTERM)
973 				 * Don't do it for SIGQUIT, since we'd
974 				 * dump a core..
975 				 */
976 				if ((sig == SIGINT || sig == SIGTERM) &&
977 				    (kshpgrp == kshpid)) {
978 					setsig(&sigtraps[sig], SIG_DFL,
979 					    SS_RESTORE_CURR | SS_FORCE);
980 					kill(0, sig);
981 				}
982 			}
983 		}
984 		if (shf)
985 			shf_close(shf);
986 		reclaim();
987 #ifdef DEBUG_LEAKS
988 #ifndef MKSH_NO_CMDLINE_EDITING
989 		x_done();
990 #endif
991 #ifndef MKSH_NOPROSPECTOFWORK
992 		/* block at least SIGCHLD during/after afreeall */
993 		sigprocmask(SIG_BLOCK, &sm_sigchld, NULL);
994 #endif
995 		afreeall(APERM);
996 		for (fd = 3; fd < NUFILE; fd++)
997 			if ((i = fcntl(fd, F_GETFD, 0)) != -1 &&
998 			    (i & FD_CLOEXEC))
999 				close(fd);
1000 		close(2);
1001 		close(1);
1002 		close(0);
1003 #endif
1004 		exit(exstat & 0xFF);
1005 	}
1006 	if (shf)
1007 		shf_close(shf);
1008 	reclaim();
1009 
1010 	e = e->oenv;
1011 
1012 	/* free the struct env - tricky due to the ALLOC_ITEM inside */
1013 	cp = (void *)ep;
1014 	afree(cp + ALLOC_SIZE, ATEMP);
1015 }
1016 
1017 /* Called after a fork to cleanup stuff left over from parents environment */
1018 void
cleanup_parents_env(void)1019 cleanup_parents_env(void)
1020 {
1021 	struct env *ep;
1022 	int fd;
1023 
1024 	/*
1025 	 * Don't clean up temporary files - parent will probably need them.
1026 	 * Also, can't easily reclaim memory since variables, etc. could be
1027 	 * anywhere.
1028 	 */
1029 
1030 	/* close all file descriptors hiding in savefd */
1031 	for (ep = e; ep; ep = ep->oenv) {
1032 		if (ep->savefd) {
1033 			for (fd = 0; fd < NUFILE; fd++)
1034 				if (ep->savefd[fd] > 0)
1035 					close(ep->savefd[fd]);
1036 			afree(ep->savefd, &ep->area);
1037 			ep->savefd = NULL;
1038 		}
1039 #ifdef DEBUG_LEAKS
1040 		if (ep->type != E_NONE)
1041 			ep->type = E_GONE;
1042 #endif
1043 	}
1044 #ifndef DEBUG_LEAKS
1045 	e->oenv = NULL;
1046 #endif
1047 }
1048 
1049 /* Called just before an execve cleanup stuff temporary files */
1050 void
cleanup_proc_env(void)1051 cleanup_proc_env(void)
1052 {
1053 	struct env *ep;
1054 
1055 	for (ep = e; ep; ep = ep->oenv)
1056 		remove_temps(ep->temps);
1057 }
1058 
1059 /* remove temp files and free ATEMP Area */
1060 static void
reclaim(void)1061 reclaim(void)
1062 {
1063 	struct block *l;
1064 
1065 	while ((l = e->loc) && (!e->oenv || e->oenv->loc != l)) {
1066 		e->loc = l->next;
1067 		afreeall(&l->area);
1068 	}
1069 
1070 	remove_temps(e->temps);
1071 	e->temps = NULL;
1072 	afreeall(&e->area);
1073 }
1074 
1075 static void
remove_temps(struct temp * tp)1076 remove_temps(struct temp *tp)
1077 {
1078 	for (; tp != NULL; tp = tp->next)
1079 		if (tp->pid == procpid)
1080 			unlink(tp->tffn);
1081 }
1082 
1083 /*
1084  * Initialise tty_fd. Used for tracking the size of the terminal,
1085  * saving/resetting tty modes upon forground job completion, and
1086  * for setting up the tty process group. Return values:
1087  *	0 = got controlling tty
1088  *	1 = got terminal but no controlling tty
1089  *	2 = cannot find a terminal
1090  *	3 = cannot dup fd
1091  *	4 = cannot make fd close-on-exec
1092  * An existing tty_fd is cached if no "better" one could be found,
1093  * i.e. if tty_devtty was already set or the new would not set it.
1094  */
1095 int
tty_init_fd(void)1096 tty_init_fd(void)
1097 {
1098 	int fd, rv, eno = 0;
1099 	bool do_close = false, is_devtty = true;
1100 
1101 	if (tty_devtty) {
1102 		/* already got a tty which is /dev/tty */
1103 		return (0);
1104 	}
1105 
1106 #ifdef _UWIN
1107 	/*XXX imake style */
1108 	if (isatty(3)) {
1109 		/* fd 3 on UWIN _is_ /dev/tty (or our controlling tty) */
1110 		fd = 3;
1111 		goto got_fd;
1112 	}
1113 #endif
1114 	if ((fd = open("/dev/tty", O_RDWR, 0)) >= 0) {
1115 		do_close = true;
1116 		goto got_fd;
1117 	}
1118 	eno = errno;
1119 
1120 	if (tty_fd >= 0) {
1121 		/* already got a non-devtty one */
1122 		rv = 1;
1123 		goto out;
1124 	}
1125 	is_devtty = false;
1126 
1127 	if (isatty((fd = 0)) || isatty((fd = 2)))
1128 		goto got_fd;
1129 	/* cannot find one */
1130 	rv = 2;
1131 	/* assert: do_close == false */
1132 	goto out;
1133 
1134  got_fd:
1135 	if ((rv = fcntl(fd, F_DUPFD, FDBASE)) < 0) {
1136 		eno = errno;
1137 		rv = 3;
1138 		goto out;
1139 	}
1140 	if (fcntl(rv, F_SETFD, FD_CLOEXEC) < 0) {
1141 		eno = errno;
1142 		close(rv);
1143 		rv = 4;
1144 		goto out;
1145 	}
1146 	tty_fd = rv;
1147 	tty_devtty = is_devtty;
1148 	rv = eno = 0;
1149  out:
1150 	if (do_close)
1151 		close(fd);
1152 	errno = eno;
1153 	return (rv);
1154 }
1155 
1156 /* A shell error occurred (eg, syntax error, etc.) */
1157 
1158 #define VWARNINGF_ERRORPREFIX	1
1159 #define VWARNINGF_FILELINE	2
1160 #define VWARNINGF_BUILTIN	4
1161 #define VWARNINGF_INTERNAL	8
1162 
1163 static void vwarningf(unsigned int, const char *, va_list)
1164     MKSH_A_FORMAT(__printf__, 2, 0);
1165 
1166 static void
vwarningf(unsigned int flags,const char * fmt,va_list ap)1167 vwarningf(unsigned int flags, const char *fmt, va_list ap)
1168 {
1169 	if (fmt) {
1170 		if (flags & VWARNINGF_INTERNAL)
1171 			shf_fprintf(shl_out, "internal error: ");
1172 		if (flags & VWARNINGF_ERRORPREFIX)
1173 			error_prefix(tobool(flags & VWARNINGF_FILELINE));
1174 		if ((flags & VWARNINGF_BUILTIN) &&
1175 		    /* not set when main() calls parse_args() */
1176 		    builtin_argv0 && builtin_argv0 != kshname)
1177 			shf_fprintf(shl_out, "%s: ", builtin_argv0);
1178 		shf_vfprintf(shl_out, fmt, ap);
1179 		shf_putchar('\n', shl_out);
1180 	}
1181 	shf_flush(shl_out);
1182 }
1183 
1184 void
errorfx(int rc,const char * fmt,...)1185 errorfx(int rc, const char *fmt, ...)
1186 {
1187 	va_list va;
1188 
1189 	exstat = rc;
1190 
1191 	/* debugging: note that stdout not valid */
1192 	shl_stdout_ok = false;
1193 
1194 	va_start(va, fmt);
1195 	vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE, fmt, va);
1196 	va_end(va);
1197 	unwind(LERROR);
1198 }
1199 
1200 void
errorf(const char * fmt,...)1201 errorf(const char *fmt, ...)
1202 {
1203 	va_list va;
1204 
1205 	exstat = 1;
1206 
1207 	/* debugging: note that stdout not valid */
1208 	shl_stdout_ok = false;
1209 
1210 	va_start(va, fmt);
1211 	vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE, fmt, va);
1212 	va_end(va);
1213 	unwind(LERROR);
1214 }
1215 
1216 /* like errorf(), but no unwind is done */
1217 void
warningf(bool fileline,const char * fmt,...)1218 warningf(bool fileline, const char *fmt, ...)
1219 {
1220 	va_list va;
1221 
1222 	va_start(va, fmt);
1223 	vwarningf(VWARNINGF_ERRORPREFIX | (fileline ? VWARNINGF_FILELINE : 0),
1224 	    fmt, va);
1225 	va_end(va);
1226 }
1227 
1228 /*
1229  * Used by built-in utilities to prefix shell and utility name to message
1230  * (also unwinds environments for special builtins).
1231  */
1232 void
bi_errorf(const char * fmt,...)1233 bi_errorf(const char *fmt, ...)
1234 {
1235 	va_list va;
1236 
1237 	/* debugging: note that stdout not valid */
1238 	shl_stdout_ok = false;
1239 
1240 	exstat = 1;
1241 
1242 	va_start(va, fmt);
1243 	vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE |
1244 	    VWARNINGF_BUILTIN, fmt, va);
1245 	va_end(va);
1246 
1247 	/*
1248 	 * POSIX special builtins and ksh special builtins cause
1249 	 * non-interactive shells to exit.
1250 	 * XXX odd use of KEEPASN; also may not want LERROR here
1251 	 */
1252 	if (builtin_spec) {
1253 		builtin_argv0 = NULL;
1254 		unwind(LERROR);
1255 	}
1256 }
1257 
1258 /* Called when something that shouldn't happen does */
1259 void
internal_errorf(const char * fmt,...)1260 internal_errorf(const char *fmt, ...)
1261 {
1262 	va_list va;
1263 
1264 	va_start(va, fmt);
1265 	vwarningf(VWARNINGF_INTERNAL, fmt, va);
1266 	va_end(va);
1267 	unwind(LERROR);
1268 }
1269 
1270 void
internal_warningf(const char * fmt,...)1271 internal_warningf(const char *fmt, ...)
1272 {
1273 	va_list va;
1274 
1275 	va_start(va, fmt);
1276 	vwarningf(VWARNINGF_INTERNAL, fmt, va);
1277 	va_end(va);
1278 }
1279 
1280 /* used by error reporting functions to print "ksh: .kshrc[25]: " */
1281 void
error_prefix(bool fileline)1282 error_prefix(bool fileline)
1283 {
1284 	/* Avoid foo: foo[2]: ... */
1285 	if (!fileline || !source || !source->file ||
1286 	    strcmp(source->file, kshname) != 0)
1287 		shf_fprintf(shl_out, "%s: ", kshname + (*kshname == '-'));
1288 	if (fileline && source && source->file != NULL) {
1289 		shf_fprintf(shl_out, "%s[%lu]: ", source->file,
1290 		    (unsigned long)(source->errline ?
1291 		    source->errline : source->line));
1292 		source->errline = 0;
1293 	}
1294 }
1295 
1296 /* printf to shl_out (stderr) with flush */
1297 void
shellf(const char * fmt,...)1298 shellf(const char *fmt, ...)
1299 {
1300 	va_list va;
1301 
1302 	if (!initio_done)
1303 		/* shl_out may not be set up yet... */
1304 		return;
1305 	va_start(va, fmt);
1306 	shf_vfprintf(shl_out, fmt, va);
1307 	va_end(va);
1308 	shf_flush(shl_out);
1309 }
1310 
1311 /* printf to shl_stdout (stdout) */
1312 void
shprintf(const char * fmt,...)1313 shprintf(const char *fmt, ...)
1314 {
1315 	va_list va;
1316 
1317 	if (!shl_stdout_ok)
1318 		internal_errorf("shl_stdout not valid");
1319 	va_start(va, fmt);
1320 	shf_vfprintf(shl_stdout, fmt, va);
1321 	va_end(va);
1322 }
1323 
1324 /* test if we can seek backwards fd (returns 0 or SHF_UNBUF) */
1325 int
can_seek(int fd)1326 can_seek(int fd)
1327 {
1328 	struct stat statb;
1329 
1330 	return (fstat(fd, &statb) == 0 && !S_ISREG(statb.st_mode) ?
1331 	    SHF_UNBUF : 0);
1332 }
1333 
1334 #ifdef DF
1335 int shl_dbg_fd;
1336 #define NSHF_IOB 4
1337 #else
1338 #define NSHF_IOB 3
1339 #endif
1340 struct shf shf_iob[NSHF_IOB];
1341 
1342 void
initio(void)1343 initio(void)
1344 {
1345 #ifdef DF
1346 	const char *lfp;
1347 #endif
1348 
1349 	/* force buffer allocation */
1350 	shf_fdopen(1, SHF_WR, shl_stdout);
1351 	shf_fdopen(2, SHF_WR, shl_out);
1352 	shf_fdopen(2, SHF_WR, shl_xtrace);
1353 #ifdef DF
1354 	if ((lfp = getenv("SDMKSH_PATH")) == NULL) {
1355 		if ((lfp = getenv("HOME")) == NULL || *lfp != '/')
1356 			errorf("cannot get home directory");
1357 		lfp = shf_smprintf("%s/mksh-dbg.txt", lfp);
1358 	}
1359 
1360 	if ((shl_dbg_fd = open(lfp, O_WRONLY | O_APPEND | O_CREAT, 0600)) < 0)
1361 		errorf("cannot open debug output file %s", lfp);
1362 	if (shl_dbg_fd < FDBASE) {
1363 		int nfd;
1364 
1365 		nfd = fcntl(shl_dbg_fd, F_DUPFD, FDBASE);
1366 		close(shl_dbg_fd);
1367 		if ((shl_dbg_fd = nfd) == -1)
1368 			errorf("cannot dup debug output file");
1369 	}
1370 	fcntl(shl_dbg_fd, F_SETFD, FD_CLOEXEC);
1371 	shf_fdopen(shl_dbg_fd, SHF_WR, shl_dbg);
1372 	DF("=== open ===");
1373 #endif
1374 	initio_done = true;
1375 }
1376 
1377 /* A dup2() with error checking */
1378 int
ksh_dup2(int ofd,int nfd,bool errok)1379 ksh_dup2(int ofd, int nfd, bool errok)
1380 {
1381 	int rv;
1382 
1383 	if (((rv = dup2(ofd, nfd)) < 0) && !errok && (errno != EBADF))
1384 		errorf("too many files open in shell");
1385 
1386 #ifdef __ultrix
1387 	/*XXX imake style */
1388 	if (rv >= 0)
1389 		fcntl(nfd, F_SETFD, 0);
1390 #endif
1391 
1392 	return (rv);
1393 }
1394 
1395 /*
1396  * Move fd from user space (0 <= fd < 10) to shell space (fd >= 10),
1397  * set close-on-exec flag. See FDBASE in sh.h, maybe 24 not 10 here.
1398  */
1399 short
savefd(int fd)1400 savefd(int fd)
1401 {
1402 	int nfd = fd;
1403 
1404 	if (fd < FDBASE && (nfd = fcntl(fd, F_DUPFD, FDBASE)) < 0 &&
1405 	    errno == EBADF)
1406 		return (-1);
1407 	if (nfd < 0 || nfd > SHRT_MAX)
1408 		errorf("too many files open in shell");
1409 	fcntl(nfd, F_SETFD, FD_CLOEXEC);
1410 	return ((short)nfd);
1411 }
1412 
1413 void
restfd(int fd,int ofd)1414 restfd(int fd, int ofd)
1415 {
1416 	if (fd == 2)
1417 		shf_flush(&shf_iob[/* fd */ 2]);
1418 	if (ofd < 0)
1419 		/* original fd closed */
1420 		close(fd);
1421 	else if (fd != ofd) {
1422 		/*XXX: what to do if this dup fails? */
1423 		ksh_dup2(ofd, fd, true);
1424 		close(ofd);
1425 	}
1426 }
1427 
1428 void
openpipe(int * pv)1429 openpipe(int *pv)
1430 {
1431 	int lpv[2];
1432 
1433 	if (pipe(lpv) < 0)
1434 		errorf("can't create pipe - try again");
1435 	pv[0] = savefd(lpv[0]);
1436 	if (pv[0] != lpv[0])
1437 		close(lpv[0]);
1438 	pv[1] = savefd(lpv[1]);
1439 	if (pv[1] != lpv[1])
1440 		close(lpv[1]);
1441 }
1442 
1443 void
closepipe(int * pv)1444 closepipe(int *pv)
1445 {
1446 	close(pv[0]);
1447 	close(pv[1]);
1448 }
1449 
1450 /*
1451  * Called by iosetup() (deals with 2>&4, etc.), c_read, c_print to turn
1452  * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
1453  */
1454 int
check_fd(const char * name,int mode,const char ** emsgp)1455 check_fd(const char *name, int mode, const char **emsgp)
1456 {
1457 	int fd = 0, fl;
1458 
1459 	if (name[0] == 'p' && !name[1])
1460 		return (coproc_getfd(mode, emsgp));
1461 	while (ksh_isdigit(*name)) {
1462 		fd = (fd * 10) + *name - '0';
1463 		if (fd >= FDBASE) {
1464 			if (emsgp)
1465 				*emsgp = "file descriptor too large";
1466 			return (-1);
1467 		}
1468 		++name;
1469 	}
1470 	if (*name) {
1471 		if (emsgp)
1472 			*emsgp = "illegal file descriptor name";
1473 		return (-1);
1474 	}
1475 	if ((fl = fcntl(fd, F_GETFL, 0)) < 0) {
1476 		if (emsgp)
1477 			*emsgp = "bad file descriptor";
1478 		return (-1);
1479 	}
1480 	fl &= O_ACCMODE;
1481 	/*
1482 	 * X_OK is a kludge to disable this check for dups (x<&1):
1483 	 * historical shells never did this check (XXX don't know what
1484 	 * POSIX has to say).
1485 	 */
1486 	if (!(mode & X_OK) && fl != O_RDWR && (
1487 	    ((mode & R_OK) && fl != O_RDONLY) ||
1488 	    ((mode & W_OK) && fl != O_WRONLY))) {
1489 		if (emsgp)
1490 			*emsgp = (fl == O_WRONLY) ?
1491 			    "fd not open for reading" :
1492 			    "fd not open for writing";
1493 		return (-1);
1494 	}
1495 	return (fd);
1496 }
1497 
1498 /* Called once from main */
1499 void
coproc_init(void)1500 coproc_init(void)
1501 {
1502 	coproc.read = coproc.readw = coproc.write = -1;
1503 	coproc.njobs = 0;
1504 	coproc.id = 0;
1505 }
1506 
1507 /* Called by c_read() when eof is read - close fd if it is the co-process fd */
1508 void
coproc_read_close(int fd)1509 coproc_read_close(int fd)
1510 {
1511 	if (coproc.read >= 0 && fd == coproc.read) {
1512 		coproc_readw_close(fd);
1513 		close(coproc.read);
1514 		coproc.read = -1;
1515 	}
1516 }
1517 
1518 /*
1519  * Called by c_read() and by iosetup() to close the other side of the
1520  * read pipe, so reads will actually terminate.
1521  */
1522 void
coproc_readw_close(int fd)1523 coproc_readw_close(int fd)
1524 {
1525 	if (coproc.readw >= 0 && coproc.read >= 0 && fd == coproc.read) {
1526 		close(coproc.readw);
1527 		coproc.readw = -1;
1528 	}
1529 }
1530 
1531 /*
1532  * Called by c_print when a write to a fd fails with EPIPE and by iosetup
1533  * when co-process input is dup'd
1534  */
1535 void
coproc_write_close(int fd)1536 coproc_write_close(int fd)
1537 {
1538 	if (coproc.write >= 0 && fd == coproc.write) {
1539 		close(coproc.write);
1540 		coproc.write = -1;
1541 	}
1542 }
1543 
1544 /*
1545  * Called to check for existence of/value of the co-process file descriptor.
1546  * (Used by check_fd() and by c_read/c_print to deal with -p option).
1547  */
1548 int
coproc_getfd(int mode,const char ** emsgp)1549 coproc_getfd(int mode, const char **emsgp)
1550 {
1551 	int fd = (mode & R_OK) ? coproc.read : coproc.write;
1552 
1553 	if (fd >= 0)
1554 		return (fd);
1555 	if (emsgp)
1556 		*emsgp = "no coprocess";
1557 	return (-1);
1558 }
1559 
1560 /*
1561  * called to close file descriptors related to the coprocess (if any)
1562  * Should be called with SIGCHLD blocked.
1563  */
1564 void
coproc_cleanup(int reuse)1565 coproc_cleanup(int reuse)
1566 {
1567 	/* This to allow co-processes to share output pipe */
1568 	if (!reuse || coproc.readw < 0 || coproc.read < 0) {
1569 		if (coproc.read >= 0) {
1570 			close(coproc.read);
1571 			coproc.read = -1;
1572 		}
1573 		if (coproc.readw >= 0) {
1574 			close(coproc.readw);
1575 			coproc.readw = -1;
1576 		}
1577 	}
1578 	if (coproc.write >= 0) {
1579 		close(coproc.write);
1580 		coproc.write = -1;
1581 	}
1582 }
1583 
1584 struct temp *
maketemp(Area * ap,Temp_type type,struct temp ** tlist)1585 maketemp(Area *ap, Temp_type type, struct temp **tlist)
1586 {
1587 	char *cp;
1588 	size_t len;
1589 	int i, j;
1590 	struct temp *tp;
1591 	const char *dir;
1592 	struct stat sb;
1593 
1594 	dir = tmpdir ? tmpdir : MKSH_DEFAULT_TMPDIR;
1595 	/* add "/shXXXXXX.tmp" plus NUL */
1596 	len = strlen(dir);
1597 	checkoktoadd(len, offsetof(struct temp, tffn[0]) + 14);
1598 	tp = alloc(offsetof(struct temp, tffn[0]) + 14 + len, ap);
1599 
1600 	tp->shf = NULL;
1601 	tp->pid = procpid;
1602 	tp->type = type;
1603 
1604 	if (stat(dir, &sb) || !S_ISDIR(sb.st_mode)) {
1605 		tp->tffn[0] = '\0';
1606 		goto maketemp_out;
1607 	}
1608 
1609 	cp = (void *)tp;
1610 	cp += offsetof(struct temp, tffn[0]);
1611 	memcpy(cp, dir, len);
1612 	cp += len;
1613 	memcpy(cp, "/shXXXXXX.tmp", 14);
1614 	/* point to the first of six Xes */
1615 	cp += 3;
1616 	/* generate random part of filename */
1617 	len = -1;
1618 	do {
1619 		i = rndget() % 36;
1620 		cp[++len] = i < 26 ? 'a' + i : '0' + i - 26;
1621 	} while (len < 5);
1622 
1623 	/* cyclically attempt to open a temporary file */
1624 	while ((i = open(tp->tffn, O_CREAT | O_EXCL | O_RDWR | O_BINARY,
1625 	    0600)) < 0) {
1626 		if (errno != EEXIST)
1627 			goto maketemp_out;
1628 		/* count down from z to a then from 9 to 0 */
1629 		while (cp[len] == '0')
1630 			if (!len--)
1631 				goto maketemp_out;
1632 		if (cp[len] == 'a')
1633 			cp[len] = '9';
1634 		else
1635 			--cp[len];
1636 		/* do another cycle */
1637 	}
1638 
1639 	if (type == TT_FUNSUB) {
1640 		/* map us high and mark as close-on-exec */
1641 		if ((j = savefd(i)) != i) {
1642 			close(i);
1643 			i = j;
1644 		}
1645 
1646 		/* operation mode for the shf */
1647 		j = SHF_RD;
1648 	} else
1649 		j = SHF_WR;
1650 
1651 	/* shf_fdopen cannot fail, so no fd leak */
1652 	tp->shf = shf_fdopen(i, j, NULL);
1653 
1654  maketemp_out:
1655 	tp->next = *tlist;
1656 	*tlist = tp;
1657 	return (tp);
1658 }
1659 
1660 /*
1661  * We use a similar collision resolution algorithm as Python 2.5.4
1662  * but with a slightly tweaked implementation written from scratch.
1663  */
1664 
1665 #define	INIT_TBLSHIFT	3	/* initial table shift (2^3 = 8) */
1666 #define PERTURB_SHIFT	5	/* see Python 2.5.4 Objects/dictobject.c */
1667 
1668 static void tgrow(struct table *);
1669 static int tnamecmp(const void *, const void *);
1670 
1671 static void
tgrow(struct table * tp)1672 tgrow(struct table *tp)
1673 {
1674 	size_t i, j, osize, mask, perturb;
1675 	struct tbl *tblp, **pp;
1676 	struct tbl **ntblp, **otblp = tp->tbls;
1677 
1678 	if (tp->tshift > 29)
1679 		internal_errorf("hash table size limit reached");
1680 
1681 	/* calculate old size, new shift and new size */
1682 	osize = (size_t)1 << (tp->tshift++);
1683 	i = osize << 1;
1684 
1685 	ntblp = alloc2(i, sizeof(struct tbl *), tp->areap);
1686 	/* multiplication cannot overflow: alloc2 checked that */
1687 	memset(ntblp, 0, i * sizeof(struct tbl *));
1688 
1689 	/* table can get very full when reaching its size limit */
1690 	tp->nfree = (tp->tshift == 30) ? 0x3FFF0000UL :
1691 	    /* but otherwise, only 75% */
1692 	    ((i * 3) / 4);
1693 	tp->tbls = ntblp;
1694 	if (otblp == NULL)
1695 		return;
1696 
1697 	mask = i - 1;
1698 	for (i = 0; i < osize; i++)
1699 		if ((tblp = otblp[i]) != NULL) {
1700 			if ((tblp->flag & DEFINED)) {
1701 				/* search for free hash table slot */
1702 				j = perturb = tblp->ua.hval;
1703 				goto find_first_empty_slot;
1704  find_next_empty_slot:
1705 				j = (j << 2) + j + perturb + 1;
1706 				perturb >>= PERTURB_SHIFT;
1707  find_first_empty_slot:
1708 				pp = &ntblp[j & mask];
1709 				if (*pp != NULL)
1710 					goto find_next_empty_slot;
1711 				/* found an empty hash table slot */
1712 				*pp = tblp;
1713 				tp->nfree--;
1714 			} else if (!(tblp->flag & FINUSE)) {
1715 				afree(tblp, tp->areap);
1716 			}
1717 		}
1718 	afree(otblp, tp->areap);
1719 }
1720 
1721 void
ktinit(Area * ap,struct table * tp,uint8_t initshift)1722 ktinit(Area *ap, struct table *tp, uint8_t initshift)
1723 {
1724 	tp->areap = ap;
1725 	tp->tbls = NULL;
1726 	tp->tshift = ((initshift > INIT_TBLSHIFT) ?
1727 	    initshift : INIT_TBLSHIFT) - 1;
1728 	tgrow(tp);
1729 }
1730 
1731 /* table, name (key) to search for, hash(name), rv pointer to tbl ptr */
1732 struct tbl *
ktscan(struct table * tp,const char * name,uint32_t h,struct tbl *** ppp)1733 ktscan(struct table *tp, const char *name, uint32_t h, struct tbl ***ppp)
1734 {
1735 	size_t j, perturb, mask;
1736 	struct tbl **pp, *p;
1737 
1738 	mask = ((size_t)1 << (tp->tshift)) - 1;
1739 	/* search for hash table slot matching name */
1740 	j = perturb = h;
1741 	goto find_first_slot;
1742  find_next_slot:
1743 	j = (j << 2) + j + perturb + 1;
1744 	perturb >>= PERTURB_SHIFT;
1745  find_first_slot:
1746 	pp = &tp->tbls[j & mask];
1747 	if ((p = *pp) != NULL && (p->ua.hval != h || !(p->flag & DEFINED) ||
1748 	    strcmp(p->name, name)))
1749 		goto find_next_slot;
1750 	/* p == NULL if not found, correct found entry otherwise */
1751 	if (ppp)
1752 		*ppp = pp;
1753 	return (p);
1754 }
1755 
1756 /* table, name (key) to enter, hash(n) */
1757 struct tbl *
ktenter(struct table * tp,const char * n,uint32_t h)1758 ktenter(struct table *tp, const char *n, uint32_t h)
1759 {
1760 	struct tbl **pp, *p;
1761 	size_t len;
1762 
1763  Search:
1764 	if ((p = ktscan(tp, n, h, &pp)))
1765 		return (p);
1766 
1767 	if (tp->nfree == 0) {
1768 		/* too full */
1769 		tgrow(tp);
1770 		goto Search;
1771 	}
1772 
1773 	/* create new tbl entry */
1774 	len = strlen(n);
1775 	checkoktoadd(len, offsetof(struct tbl, name[0]) + 1);
1776 	p = alloc(offsetof(struct tbl, name[0]) + ++len, tp->areap);
1777 	p->flag = 0;
1778 	p->type = 0;
1779 	p->areap = tp->areap;
1780 	p->ua.hval = h;
1781 	p->u2.field = 0;
1782 	p->u.array = NULL;
1783 	memcpy(p->name, n, len);
1784 
1785 	/* enter in tp->tbls */
1786 	tp->nfree--;
1787 	*pp = p;
1788 	return (p);
1789 }
1790 
1791 void
ktwalk(struct tstate * ts,struct table * tp)1792 ktwalk(struct tstate *ts, struct table *tp)
1793 {
1794 	ts->left = (size_t)1 << (tp->tshift);
1795 	ts->next = tp->tbls;
1796 }
1797 
1798 struct tbl *
ktnext(struct tstate * ts)1799 ktnext(struct tstate *ts)
1800 {
1801 	while (--ts->left >= 0) {
1802 		struct tbl *p = *ts->next++;
1803 		if (p != NULL && (p->flag & DEFINED))
1804 			return (p);
1805 	}
1806 	return (NULL);
1807 }
1808 
1809 static int
tnamecmp(const void * p1,const void * p2)1810 tnamecmp(const void *p1, const void *p2)
1811 {
1812 	const struct tbl *a = *((const struct tbl * const *)p1);
1813 	const struct tbl *b = *((const struct tbl * const *)p2);
1814 
1815 	return (strcmp(a->name, b->name));
1816 }
1817 
1818 struct tbl **
ktsort(struct table * tp)1819 ktsort(struct table *tp)
1820 {
1821 	size_t i;
1822 	struct tbl **p, **sp, **dp;
1823 
1824 	/*
1825 	 * since the table is never entirely full, no need to reserve
1826 	 * additional space for the trailing NULL appended below
1827 	 */
1828 	i = (size_t)1 << (tp->tshift);
1829 	p = alloc2(i, sizeof(struct tbl *), ATEMP);
1830 	sp = tp->tbls;		/* source */
1831 	dp = p;			/* dest */
1832 	while (i--)
1833 		if ((*dp = *sp++) != NULL && (((*dp)->flag & DEFINED) ||
1834 		    ((*dp)->flag & ARRAY)))
1835 			dp++;
1836 	qsort(p, (i = dp - p), sizeof(struct tbl *), tnamecmp);
1837 	p[i] = NULL;
1838 	return (p);
1839 }
1840 
1841 #ifdef SIGWINCH
1842 static void
x_sigwinch(int sig MKSH_A_UNUSED)1843 x_sigwinch(int sig MKSH_A_UNUSED)
1844 {
1845 	/* this runs inside interrupt context, with errno saved */
1846 
1847 	got_winch = 1;
1848 }
1849 #endif
1850 
1851 #ifdef DF
1852 void
DF(const char * fmt,...)1853 DF(const char *fmt, ...)
1854 {
1855 	va_list args;
1856 	struct timeval tv;
1857 	mirtime_mjd mjd;
1858 
1859 	mksh_lockfd(shl_dbg_fd);
1860 	mksh_TIME(tv);
1861 	timet2mjd(&mjd, tv.tv_sec);
1862 	shf_fprintf(shl_dbg, "[%02u:%02u:%02u (%u) %u.%06u] ",
1863 	    (unsigned)mjd.sec / 3600, ((unsigned)mjd.sec / 60) % 60,
1864 	    (unsigned)mjd.sec % 60, (unsigned)getpid(),
1865 	    (unsigned)tv.tv_sec, (unsigned)tv.tv_usec);
1866 	va_start(args, fmt);
1867 	shf_vfprintf(shl_dbg, fmt, args);
1868 	va_end(args);
1869 	shf_putc('\n', shl_dbg);
1870 	shf_flush(shl_dbg);
1871 	mksh_unlkfd(shl_dbg_fd);
1872 }
1873 #endif
1874 
1875 void
x_mkraw(int fd,mksh_ttyst * ocb,bool forread)1876 x_mkraw(int fd, mksh_ttyst *ocb, bool forread)
1877 {
1878 	mksh_ttyst cb;
1879 
1880 	if (ocb)
1881 		mksh_tcget(fd, ocb);
1882 	else
1883 		ocb = &tty_state;
1884 
1885 	cb = *ocb;
1886 	if (forread) {
1887 		cb.c_iflag &= ~(ISTRIP);
1888 		cb.c_lflag &= ~(ICANON) | ECHO;
1889 	} else {
1890 		cb.c_iflag &= ~(INLCR | ICRNL | ISTRIP);
1891 		cb.c_lflag &= ~(ISIG | ICANON | ECHO);
1892 	}
1893 #if defined(VLNEXT) && defined(_POSIX_VDISABLE)
1894 	/* OSF/1 processes lnext when ~icanon */
1895 	cb.c_cc[VLNEXT] = _POSIX_VDISABLE;
1896 #endif
1897 	/* SunOS 4.1.x & OSF/1 processes discard(flush) when ~icanon */
1898 #if defined(VDISCARD) && defined(_POSIX_VDISABLE)
1899 	cb.c_cc[VDISCARD] = _POSIX_VDISABLE;
1900 #endif
1901 	cb.c_cc[VTIME] = 0;
1902 	cb.c_cc[VMIN] = 1;
1903 
1904 	mksh_tcset(fd, &cb);
1905 }
1906