xref: /titanic_51/usr/src/lib/libshell/common/sh/fault.c (revision dbed73cbda2229fd1aa6dc5743993cae7f0a7ee9)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1982-2009 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                  David Korn <dgk@research.att.com>                   *
18 *                                                                      *
19 ***********************************************************************/
20 #pragma prototyped
21 /*
22  * Fault handling routines
23  *
24  *   David Korn
25  *   AT&T Labs
26  *
27  */
28 
29 #include	"defs.h"
30 #include	<fcin.h>
31 #include	"io.h"
32 #include	"history.h"
33 #include	"shlex.h"
34 #include	"variables.h"
35 #include	"jobs.h"
36 #include	"path.h"
37 #include	"builtins.h"
38 
39 #define abortsig(sig)	(sig==SIGABRT || sig==SIGBUS || sig==SIGILL || sig==SIGSEGV)
40 
41 static char	indone;
42 
43 #if !_std_malloc
44 #   include	<vmalloc.h>
45 #endif
46 #if  defined(VMFL) && (VMALLOC_VERSION>=20031205L)
47     /*
48      * This exception handler is called after vmalloc() unlocks the region
49      */
50     static int malloc_done(Vmalloc_t* vm, int type, Void_t* val, Vmdisc_t* dp)
51     {
52 	dp->exceptf = 0;
53 	sh_exit(SH_EXITSIG);
54 	return(0);
55     }
56 #endif
57 
58 /*
59  * Most signals caught or ignored by the shell come here
60 */
61 void	sh_fault(register int sig)
62 {
63 	register Shell_t	*shp = sh_getinterp();
64 	register int 		flag=0;
65 	register char		*trap;
66 	register struct checkpt	*pp = (struct checkpt*)shp->jmplist;
67 	int	action=0;
68 	/* reset handler */
69 	if(!(sig&SH_TRAP))
70 		signal(sig, sh_fault);
71 	sig &= ~SH_TRAP;
72 #ifdef SIGWINCH
73 	if(sig==SIGWINCH)
74 	{
75 		int rows=0, cols=0;
76 		int32_t v;
77 		astwinsize(2,&rows,&cols);
78 		if(v = cols)
79 			nv_putval(COLUMNS, (char*)&v, NV_INT32|NV_RDONLY);
80 		if(v = rows)
81 			nv_putval(LINES, (char*)&v, NV_INT32|NV_RDONLY);
82 		shp->winch++;
83 	}
84 #endif  /* SIGWINCH */
85 	if(shp->savesig)
86 	{
87 		/* critical region, save and process later */
88 		shp->savesig = sig;
89 		return;
90 	}
91 	trap = shp->st.trapcom[sig];
92 	if(sig==SIGALRM && shp->bltinfun==b_sleep)
93 	{
94 		if(trap && *trap)
95 		{
96 			shp->trapnote |= SH_SIGTRAP;
97 			shp->sigflag[sig] |= SH_SIGTRAP;
98 		}
99 		return;
100 	}
101 	if(shp->subshell && sig!=SIGINT && sig!=SIGQUIT && sig!=SIGWINCH && sig!=SIGCONT)
102 	{
103 		shp->exitval = SH_EXITSIG|sig;
104 		sh_subfork();
105 		shp->exitval = 0;
106 		return;
107 	}
108 	/* handle ignored signals */
109 	if(trap && *trap==0)
110 		return;
111 	flag = shp->sigflag[sig]&~SH_SIGOFF;
112 	if(!trap)
113 	{
114 		if(sig==SIGINT && (shp->trapnote&SH_SIGIGNORE))
115 			return;
116 		if(flag&SH_SIGIGNORE)
117 			return;
118 		if(flag&SH_SIGDONE)
119 		{
120 			void *ptr=0;
121 			if((flag&SH_SIGINTERACTIVE) && sh_isstate(SH_INTERACTIVE) && !sh_isstate(SH_FORKED) && ! shp->subshell)
122 			{
123 				/* check for TERM signal between fork/exec */
124 				if(sig==SIGTERM && job.in_critical)
125 					shp->trapnote |= SH_SIGTERM;
126 				return;
127 			}
128 			shp->lastsig = sig;
129 			sigrelease(sig);
130 			if(pp->mode < SH_JMPFUN)
131 				pp->mode = SH_JMPFUN;
132 			else
133 				pp->mode = SH_JMPEXIT;
134 			if(sig==SIGABRT || (abortsig(sig) && (ptr = malloc(1))))
135 			{
136 				if(ptr)
137 					free(ptr);
138 				if(!shp->subshell)
139 					sh_done(shp,sig);
140 				sh_exit(SH_EXITSIG);
141 			}
142 			/* mark signal and continue */
143 			shp->trapnote |= SH_SIGSET;
144 			if(sig <= shp->sigmax)
145 				shp->sigflag[sig] |= SH_SIGSET;
146 #if  defined(VMFL) && (VMALLOC_VERSION>=20031205L)
147 			if(abortsig(sig))
148 			{
149 				/* abort inside malloc, process when malloc returns */
150 				/* VMFL defined when using vmalloc() */
151 				Vmdisc_t* dp = vmdisc(Vmregion,0);
152 				if(dp)
153 					dp->exceptf = malloc_done;
154 			}
155 #endif
156 			return;
157 		}
158 	}
159 	errno = 0;
160 	if(pp->mode==SH_JMPCMD)
161 		shp->lastsig = sig;
162 	if(trap)
163 	{
164 		/*
165 		 * propogate signal to foreground group
166 		 */
167 		if(sig==SIGHUP && job.curpgid)
168 			killpg(job.curpgid,SIGHUP);
169 		flag = SH_SIGTRAP;
170 	}
171 	else
172 	{
173 		shp->lastsig = sig;
174 		flag = SH_SIGSET;
175 #ifdef SIGTSTP
176 		if(sig==SIGTSTP)
177 		{
178 			shp->trapnote |= SH_SIGTSTP;
179 			if(pp->mode==SH_JMPCMD && sh_isstate(SH_STOPOK))
180 			{
181 				sigrelease(sig);
182 				sh_exit(SH_EXITSIG);
183 				flag = 0;
184 			}
185 		}
186 #endif /* SIGTSTP */
187 	}
188 #ifdef ERROR_NOTIFY
189 	if((error_info.flags&ERROR_NOTIFY) && shp->bltinfun)
190 		action = (*shp->bltinfun)(-sig,(char**)0,(void*)0);
191 	if(action>0)
192 		return;
193 #endif
194 	if(shp->bltinfun && shp->bltindata.notify)
195 	{
196 		shp->bltindata.sigset = 1;
197 		return;
198 	}
199 	shp->trapnote |= flag;
200 	if(sig <= shp->sigmax)
201 		shp->sigflag[sig] |= flag;
202 	if(pp->mode==SH_JMPCMD && sh_isstate(SH_STOPOK))
203 	{
204 		if(action<0)
205 			return;
206 		sigrelease(sig);
207 		sh_exit(SH_EXITSIG);
208 	}
209 }
210 
211 /*
212  * initialize signal handling
213  */
214 void sh_siginit(void *ptr)
215 {
216 	Shell_t	*shp = (Shell_t*)ptr;
217 	register int sig, n;
218 	register const struct shtable2	*tp = shtab_signals;
219 	sig_begin();
220 	/* find the largest signal number in the table */
221 #if defined(SIGRTMIN) && defined(SIGRTMAX)
222 	if ((n = SIGRTMIN) > 0 && (sig = SIGRTMAX) > n && sig < SH_TRAP)
223 	{
224 		shp->sigruntime[SH_SIGRTMIN] = n;
225 		shp->sigruntime[SH_SIGRTMAX] = sig;
226 	}
227 #endif /* SIGRTMIN && SIGRTMAX */
228 	n = SIGTERM;
229 	while(*tp->sh_name)
230 	{
231 		sig = (tp->sh_number&((1<<SH_SIGBITS)-1));
232 		if (!(sig-- & SH_TRAP))
233 		{
234 			if ((tp->sh_number>>SH_SIGBITS) & SH_SIGRUNTIME)
235 				sig = shp->sigruntime[sig];
236 			if(sig>n && sig<SH_TRAP)
237 				n = sig;
238 		}
239 		tp++;
240 	}
241 	shp->sigmax = n++;
242 	shp->st.trapcom = (char**)calloc(n,sizeof(char*));
243 	shp->sigflag = (unsigned char*)calloc(n,1);
244 	shp->sigmsg = (char**)calloc(n,sizeof(char*));
245 	for(tp=shtab_signals; sig=tp->sh_number; tp++)
246 	{
247 		n = (sig>>SH_SIGBITS);
248 		if((sig &= ((1<<SH_SIGBITS)-1)) > (shp->sigmax+1))
249 			continue;
250 		sig--;
251 		if(n&SH_SIGRUNTIME)
252 			sig = shp->sigruntime[sig];
253 		if(sig>=0)
254 		{
255 			shp->sigflag[sig] = n;
256 			if(*tp->sh_name)
257 				shp->sigmsg[sig] = (char*)tp->sh_value;
258 		}
259 	}
260 }
261 
262 /*
263  * Turn on trap handler for signal <sig>
264  */
265 void	sh_sigtrap(register int sig)
266 {
267 	register int flag;
268 	void (*fun)(int);
269 	sh.st.otrapcom = 0;
270 	if(sig==0)
271 		sh_sigdone();
272 	else if(!((flag=sh.sigflag[sig])&(SH_SIGFAULT|SH_SIGOFF)))
273 	{
274 		/* don't set signal if already set or off by parent */
275 		if((fun=signal(sig,sh_fault))==SIG_IGN)
276 		{
277 			signal(sig,SIG_IGN);
278 			flag |= SH_SIGOFF;
279 		}
280 		else
281 		{
282 			flag |= SH_SIGFAULT;
283 			if(sig==SIGALRM && fun!=SIG_DFL && fun!=sh_fault)
284 				signal(sig,fun);
285 		}
286 		flag &= ~(SH_SIGSET|SH_SIGTRAP);
287 		sh.sigflag[sig] = flag;
288 	}
289 }
290 
291 /*
292  * set signal handler so sh_done is called for all caught signals
293  */
294 void	sh_sigdone(void)
295 {
296 	register int 	flag, sig = sh.sigmax;
297 	sh.sigflag[0] |= SH_SIGFAULT;
298 	for(sig=sh.sigmax; sig>0; sig--)
299 	{
300 		flag = sh.sigflag[sig];
301 		if((flag&(SH_SIGDONE|SH_SIGIGNORE|SH_SIGINTERACTIVE)) && !(flag&(SH_SIGFAULT|SH_SIGOFF)))
302 			sh_sigtrap(sig);
303 	}
304 }
305 
306 /*
307  * Restore to default signals
308  * Free the trap strings if mode is non-zero
309  * If mode>1 then ignored traps cause signal to be ignored
310  */
311 void	sh_sigreset(register int mode)
312 {
313 	register char	*trap;
314 	register int 	flag, sig=sh.st.trapmax;
315 	while(sig-- > 0)
316 	{
317 		if(trap=sh.st.trapcom[sig])
318 		{
319 			flag  = sh.sigflag[sig]&~(SH_SIGTRAP|SH_SIGSET);
320 			if(*trap)
321 			{
322 				if(mode)
323 					free(trap);
324 				sh.st.trapcom[sig] = 0;
325 			}
326 			else if(sig && mode>1)
327 			{
328 				if(sig!=SIGCHLD)
329 					signal(sig,SIG_IGN);
330 				flag &= ~SH_SIGFAULT;
331 				flag |= SH_SIGOFF;
332 			}
333 			sh.sigflag[sig] = flag;
334 		}
335 	}
336 	for(sig=SH_DEBUGTRAP-1;sig>=0;sig--)
337 	{
338 		if(trap=sh.st.trap[sig])
339 		{
340 			if(mode)
341 				free(trap);
342 			sh.st.trap[sig] = 0;
343 		}
344 
345 	}
346 	sh.st.trapcom[0] = 0;
347 	if(mode)
348 		sh.st.trapmax = 0;
349 	sh.trapnote=0;
350 }
351 
352 /*
353  * free up trap if set and restore signal handler if modified
354  */
355 void	sh_sigclear(register int sig)
356 {
357 	register int flag = sh.sigflag[sig];
358 	register char *trap;
359 	sh.st.otrapcom=0;
360 	if(!(flag&SH_SIGFAULT))
361 		return;
362 	flag &= ~(SH_SIGTRAP|SH_SIGSET);
363 	if(trap=sh.st.trapcom[sig])
364 	{
365 		free(trap);
366 		sh.st.trapcom[sig]=0;
367 	}
368 	sh.sigflag[sig] = flag;
369 }
370 
371 /*
372  * check for traps
373  */
374 
375 void	sh_chktrap(void)
376 {
377 	register int 	sig=sh.st.trapmax;
378 	register char *trap;
379 	if(!(sh.trapnote&~SH_SIGIGNORE))
380 		sig=0;
381 	sh.trapnote &= ~SH_SIGTRAP;
382 	/* execute errexit trap first */
383 	if(sh_isstate(SH_ERREXIT) && sh.exitval)
384 	{
385 		int	sav_trapnote = sh.trapnote;
386 		sh.trapnote &= ~SH_SIGSET;
387 		if(sh.st.trap[SH_ERRTRAP])
388 		{
389 			trap = sh.st.trap[SH_ERRTRAP];
390 			sh.st.trap[SH_ERRTRAP] = 0;
391 			sh_trap(trap,0);
392 			sh.st.trap[SH_ERRTRAP] = trap;
393 		}
394 		sh.trapnote = sav_trapnote;
395 		if(sh_isoption(SH_ERREXIT))
396 		{
397 			struct checkpt	*pp = (struct checkpt*)sh.jmplist;
398 			pp->mode = SH_JMPEXIT;
399 			sh_exit(sh.exitval);
400 		}
401 	}
402 	if(sh.sigflag[SIGALRM]&SH_SIGALRM)
403 		sh_timetraps();
404 #ifdef SHOPT_BGX
405 	if((sh.sigflag[SIGCHLD]&SH_SIGTRAP) && sh.st.trapcom[SIGCHLD])
406 		job_chldtrap(&sh,sh.st.trapcom[SIGCHLD],1);
407 	while(--sig>=0 && sig!=SIGCHLD)
408 #else
409 	while(--sig>=0)
410 #endif /* SHOPT_BGX */
411 	{
412 		if(sh.sigflag[sig]&SH_SIGTRAP)
413 		{
414 			sh.sigflag[sig] &= ~SH_SIGTRAP;
415 			if(trap=sh.st.trapcom[sig])
416 			{
417 				Sfio_t *fp;
418 				if(sig==SIGPIPE && (fp=sfpool((Sfio_t*)0,sh.outpool,SF_WRITE)) && sferror(fp))
419 					sfclose(fp);
420  				sh.oldexit = SH_EXITSIG|sig;
421  				sh_trap(trap,0);
422  			}
423 		}
424 	}
425 }
426 
427 
428 /*
429  * parse and execute the given trap string, stream or tree depending on mode
430  * mode==0 for string, mode==1 for stream, mode==2 for parse tree
431  */
432 int sh_trap(const char *trap, int mode)
433 {
434 	Shell_t	*shp = sh_getinterp();
435 	int	jmpval, savxit = shp->exitval;
436 	int	was_history = sh_isstate(SH_HISTORY);
437 	int	was_verbose = sh_isstate(SH_VERBOSE);
438 	int	staktop = staktell();
439 	char	*savptr = stakfreeze(0);
440 	struct	checkpt buff;
441 	Fcin_t	savefc;
442 	fcsave(&savefc);
443 	sh_offstate(SH_HISTORY);
444 	sh_offstate(SH_VERBOSE);
445 	shp->intrap++;
446 	sh_pushcontext(&buff,SH_JMPTRAP);
447 	jmpval = sigsetjmp(buff.buff,0);
448 	if(jmpval == 0)
449 	{
450 		if(mode==2)
451 			sh_exec((Shnode_t*)trap,sh_isstate(SH_ERREXIT));
452 		else
453 		{
454 			Sfio_t *sp;
455 			if(mode)
456 				sp = (Sfio_t*)trap;
457 			else
458 				sp = sfopen(NIL(Sfio_t*),trap,"s");
459 			sh_eval(sp,0);
460 		}
461 	}
462 	else if(indone)
463 	{
464 		if(jmpval==SH_JMPSCRIPT)
465 			indone=0;
466 		else
467 		{
468 			if(jmpval==SH_JMPEXIT)
469 				savxit = shp->exitval;
470 			jmpval=SH_JMPTRAP;
471 		}
472 	}
473 	sh_popcontext(&buff);
474 	shp->intrap--;
475 	sfsync(shp->outpool);
476 	if(!shp->indebug && jmpval!=SH_JMPEXIT && jmpval!=SH_JMPFUN)
477 		shp->exitval=savxit;
478 	stakset(savptr,staktop);
479 	fcrestore(&savefc);
480 	if(was_history)
481 		sh_onstate(SH_HISTORY);
482 	if(was_verbose)
483 		sh_onstate(SH_VERBOSE);
484 	exitset();
485 	if(jmpval>SH_JMPTRAP && (((struct checkpt*)shp->jmpbuffer)->prev || ((struct checkpt*)shp->jmpbuffer)->mode==SH_JMPSCRIPT))
486 		siglongjmp(*shp->jmplist,jmpval);
487 	return(shp->exitval);
488 }
489 
490 /*
491  * exit the current scope and jump to an earlier one based on pp->mode
492  */
493 void sh_exit(register int xno)
494 {
495 	Shell_t	*shp = &sh;
496 	register struct checkpt	*pp = (struct checkpt*)shp->jmplist;
497 	register int		sig=0;
498 	register Sfio_t*	pool;
499 	shp->exitval=xno;
500 	if(xno==SH_EXITSIG)
501 		shp->exitval |= (sig=shp->lastsig);
502 #ifdef SIGTSTP
503 	if(shp->trapnote&SH_SIGTSTP)
504 	{
505 		/* ^Z detected by the shell */
506 		shp->trapnote = 0;
507 		shp->sigflag[SIGTSTP] = 0;
508 		if(!shp->subshell && sh_isstate(SH_MONITOR) && !sh_isstate(SH_STOPOK))
509 			return;
510 		if(sh_isstate(SH_TIMING))
511 			return;
512 		/* Handles ^Z for shell builtins, subshells, and functs */
513 		shp->lastsig = 0;
514 		sh_onstate(SH_MONITOR);
515 		sh_offstate(SH_STOPOK);
516 		shp->trapnote = 0;
517 		if(!shp->subshell && (sig=sh_fork(0,NIL(int*))))
518 		{
519 			job.curpgid = 0;
520 			job.parent = (pid_t)-1;
521 			job_wait(sig);
522 			job.parent = 0;
523 			shp->sigflag[SIGTSTP] = 0;
524 			/* wait for child to stop */
525 			shp->exitval = (SH_EXITSIG|SIGTSTP);
526 			/* return to prompt mode */
527 			pp->mode = SH_JMPERREXIT;
528 		}
529 		else
530 		{
531 			if(shp->subshell)
532 				sh_subfork();
533 			/* child process, put to sleep */
534 			sh_offstate(SH_STOPOK);
535 			sh_offstate(SH_MONITOR);
536 			shp->sigflag[SIGTSTP] = 0;
537 			/* stop child job */
538 			killpg(job.curpgid,SIGTSTP);
539 			/* child resumes */
540 			job_clear();
541 			shp->forked = 1;
542 			shp->exitval = (xno&SH_EXITMASK);
543 			return;
544 		}
545 	}
546 #endif /* SIGTSTP */
547 	/* unlock output pool */
548 	sh_offstate(SH_NOTRACK);
549 	if(!(pool=sfpool(NIL(Sfio_t*),shp->outpool,SF_WRITE)))
550 		pool = shp->outpool; /* can't happen? */
551 	sfclrlock(pool);
552 #ifdef SIGPIPE
553 	if(shp->lastsig==SIGPIPE)
554 		sfpurge(pool);
555 #endif /* SIGPIPE */
556 	sfclrlock(sfstdin);
557 	if(!pp)
558 		sh_done(shp,sig);
559 	shp->prefix = 0;
560 #if SHOPT_TYPEDEF
561 	shp->mktype = 0;
562 #endif /* SHOPT_TYPEDEF*/
563 	if(pp->mode == SH_JMPSCRIPT && !pp->prev)
564 		sh_done(shp,sig);
565 	if(pp->mode)
566 		siglongjmp(pp->buff,pp->mode);
567 }
568 
569 static void array_notify(Namval_t *np, void *data)
570 {
571 	Namarr_t	*ap = nv_arrayptr(np);
572 	NOT_USED(data);
573 	if(ap && ap->fun)
574 		(*ap->fun)(np, 0, NV_AFREE);
575 }
576 
577 /*
578  * This is the exit routine for the shell
579  */
580 
581 void sh_done(void *ptr, register int sig)
582 {
583 	Shell_t	*shp = (Shell_t*)ptr;
584 	register char *t;
585 	register int savxit = shp->exitval;
586 	shp->trapnote = 0;
587 	indone=1;
588 	if(sig)
589 		savxit = SH_EXITSIG|sig;
590 	if(shp->userinit)
591 		(*shp->userinit)(shp, -1);
592 	if(t=shp->st.trapcom[0])
593 	{
594 		shp->st.trapcom[0]=0; /*should free but not long */
595 		shp->oldexit = savxit;
596 		sh_trap(t,0);
597 		savxit = shp->exitval;
598 	}
599 	else
600 	{
601 		/* avoid recursive call for set -e */
602 		sh_offstate(SH_ERREXIT);
603 		sh_chktrap();
604 	}
605 	nv_scan(shp->var_tree,array_notify,(void*)0,NV_ARRAY,NV_ARRAY);
606 	sh_freeup(shp);
607 #if SHOPT_ACCT
608 	sh_accend();
609 #endif	/* SHOPT_ACCT */
610 #if SHOPT_VSH || SHOPT_ESH
611 	if(sh_isoption(SH_EMACS)||sh_isoption(SH_VI)||sh_isoption(SH_GMACS))
612 		tty_cooked(-1);
613 #endif
614 #ifdef JOBS
615 	if((sh_isoption(SH_INTERACTIVE) && shp->login_sh) || (!sh_isoption(SH_INTERACTIVE) && (sig==SIGHUP)))
616 		job_walk(sfstderr,job_terminate,SIGHUP,NIL(char**));
617 #endif	/* JOBS */
618 	job_close(shp);
619 	if(nv_search("VMTRACE", shp->var_tree,0))
620 		strmatch((char*)0,(char*)0);
621 	sfsync((Sfio_t*)sfstdin);
622 	sfsync((Sfio_t*)shp->outpool);
623 	sfsync((Sfio_t*)sfstdout);
624 	if(savxit&SH_EXITSIG)
625 		sig = savxit&SH_EXITMASK;
626 	if(sig)
627 	{
628 		/* generate fault termination code */
629 		signal(sig,SIG_DFL);
630 		sigrelease(sig);
631 		kill(getpid(),sig);
632 		pause();
633 	}
634 #if SHOPT_KIA
635 	if(sh_isoption(SH_NOEXEC))
636 		kiaclose((Lex_t*)shp->lex_context);
637 #endif /* SHOPT_KIA */
638 	exit(savxit&SH_EXITMASK);
639 }
640 
641