10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52256Sna195498 * Common Development and Distribution License (the "License").
62256Sna195498 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21527Schin
22527Schin /*
23*9461SArindam.Sarkar@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24527Schin * Use is subject to license terms.
25527Schin */
26527Schin
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate * UNIX shell
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include "defs.h"
360Sstevel@tonic-gate #include <sys/procset.h>
370Sstevel@tonic-gate #include <siginfo.h>
380Sstevel@tonic-gate #include <ucontext.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate #include <string.h>
410Sstevel@tonic-gate
42*9461SArindam.Sarkar@Sun.COM extern void hupforegnd(void);
43*9461SArindam.Sarkar@Sun.COM
44*9461SArindam.Sarkar@Sun.COM /* previous signal handler for signal 0 */
45*9461SArindam.Sarkar@Sun.COM static void (*psig0_func)() = SIG_ERR;
460Sstevel@tonic-gate static char sigsegv_stack[SIGSTKSZ];
470Sstevel@tonic-gate
480Sstevel@tonic-gate static void sigsegv(int sig, siginfo_t *sip, ucontext_t *uap);
490Sstevel@tonic-gate static void fault();
500Sstevel@tonic-gate static BOOL sleeping = 0;
510Sstevel@tonic-gate static unsigned char *trapcom[MAXTRAP]; /* array of actions, one per signal */
520Sstevel@tonic-gate static BOOL trapflg[MAXTRAP] =
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 0,
550Sstevel@tonic-gate 0, /* hangup */
560Sstevel@tonic-gate 0, /* interrupt */
570Sstevel@tonic-gate 0, /* quit */
580Sstevel@tonic-gate 0, /* illegal instr */
590Sstevel@tonic-gate 0, /* trace trap */
600Sstevel@tonic-gate 0, /* IOT */
610Sstevel@tonic-gate 0, /* EMT */
620Sstevel@tonic-gate 0, /* float pt. exp */
630Sstevel@tonic-gate 0, /* kill */
640Sstevel@tonic-gate 0, /* bus error */
650Sstevel@tonic-gate 0, /* memory faults */
660Sstevel@tonic-gate 0, /* bad sys call */
670Sstevel@tonic-gate 0, /* bad pipe call */
680Sstevel@tonic-gate 0, /* alarm */
690Sstevel@tonic-gate 0, /* software termination */
700Sstevel@tonic-gate 0, /* unassigned */
710Sstevel@tonic-gate 0, /* unassigned */
720Sstevel@tonic-gate 0, /* death of child */
730Sstevel@tonic-gate 0, /* power fail */
740Sstevel@tonic-gate 0, /* window size change */
750Sstevel@tonic-gate 0, /* urgent IO condition */
760Sstevel@tonic-gate 0, /* pollable event occured */
770Sstevel@tonic-gate 0, /* stopped by signal */
780Sstevel@tonic-gate 0, /* stopped by user */
790Sstevel@tonic-gate 0, /* continued */
800Sstevel@tonic-gate 0, /* stopped by tty input */
810Sstevel@tonic-gate 0, /* stopped by tty output */
820Sstevel@tonic-gate 0, /* virtual timer expired */
830Sstevel@tonic-gate 0, /* profiling timer expired */
840Sstevel@tonic-gate 0, /* exceeded cpu limit */
850Sstevel@tonic-gate 0, /* exceeded file size limit */
860Sstevel@tonic-gate 0, /* process's lwps are blocked */
870Sstevel@tonic-gate 0, /* special signal used by thread library */
880Sstevel@tonic-gate 0, /* check point freeze */
890Sstevel@tonic-gate 0, /* check point thaw */
900Sstevel@tonic-gate };
910Sstevel@tonic-gate
920Sstevel@tonic-gate static void (*(
930Sstevel@tonic-gate sigval[MAXTRAP]))() =
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 0,
960Sstevel@tonic-gate done, /* hangup */
970Sstevel@tonic-gate fault, /* interrupt */
980Sstevel@tonic-gate fault, /* quit */
990Sstevel@tonic-gate done, /* illegal instr */
1000Sstevel@tonic-gate done, /* trace trap */
1010Sstevel@tonic-gate done, /* IOT */
1020Sstevel@tonic-gate done, /* EMT */
1030Sstevel@tonic-gate done, /* floating pt. exp */
1040Sstevel@tonic-gate 0, /* kill */
1050Sstevel@tonic-gate done, /* bus error */
1060Sstevel@tonic-gate sigsegv, /* memory faults */
1070Sstevel@tonic-gate done, /* bad sys call */
1080Sstevel@tonic-gate done, /* bad pipe call */
1090Sstevel@tonic-gate done, /* alarm */
1100Sstevel@tonic-gate fault, /* software termination */
1110Sstevel@tonic-gate done, /* unassigned */
1120Sstevel@tonic-gate done, /* unassigned */
1130Sstevel@tonic-gate 0, /* death of child */
1140Sstevel@tonic-gate done, /* power fail */
1150Sstevel@tonic-gate 0, /* window size change */
1160Sstevel@tonic-gate done, /* urgent IO condition */
1170Sstevel@tonic-gate done, /* pollable event occured */
1180Sstevel@tonic-gate 0, /* uncatchable stop */
1190Sstevel@tonic-gate 0, /* foreground stop */
1200Sstevel@tonic-gate 0, /* stopped process continued */
1210Sstevel@tonic-gate 0, /* background tty read */
1220Sstevel@tonic-gate 0, /* background tty write */
1230Sstevel@tonic-gate done, /* virtual timer expired */
1240Sstevel@tonic-gate done, /* profiling timer expired */
1250Sstevel@tonic-gate done, /* exceeded cpu limit */
1260Sstevel@tonic-gate done, /* exceeded file size limit */
1270Sstevel@tonic-gate 0, /* process's lwps are blocked */
1280Sstevel@tonic-gate 0, /* special signal used by thread library */
1290Sstevel@tonic-gate 0, /* check point freeze */
1300Sstevel@tonic-gate 0, /* check point thaw */
1310Sstevel@tonic-gate };
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate static int
ignoring(int i)134527Schin ignoring(int i)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate struct sigaction act;
1370Sstevel@tonic-gate if (trapflg[i] & SIGIGN)
1380Sstevel@tonic-gate return (1);
1390Sstevel@tonic-gate sigaction(i, 0, &act);
1400Sstevel@tonic-gate if (act.sa_handler == SIG_IGN) {
1410Sstevel@tonic-gate trapflg[i] |= SIGIGN;
1420Sstevel@tonic-gate return (1);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate return (0);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate static void
clrsig(i)1480Sstevel@tonic-gate clrsig(i)
1490Sstevel@tonic-gate int i;
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate if (trapcom[i] != 0) {
1520Sstevel@tonic-gate free(trapcom[i]);
1530Sstevel@tonic-gate trapcom[i] = 0;
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate if (trapflg[i] & SIGMOD) {
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate * If the signal has been set to SIGIGN and we are now
1600Sstevel@tonic-gate * clearing the disposition of the signal (restoring it
1610Sstevel@tonic-gate * back to its default value) then we need to clear this
1620Sstevel@tonic-gate * bit as well
1630Sstevel@tonic-gate *
1640Sstevel@tonic-gate */
1650Sstevel@tonic-gate if (trapflg[i] & SIGIGN)
1660Sstevel@tonic-gate trapflg[i] &= ~SIGIGN;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate trapflg[i] &= ~SIGMOD;
1690Sstevel@tonic-gate handle(i, sigval[i]);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate void
done(sig)1740Sstevel@tonic-gate done(sig)
1750Sstevel@tonic-gate {
176527Schin unsigned char *t;
1770Sstevel@tonic-gate int savxit;
1780Sstevel@tonic-gate
179*9461SArindam.Sarkar@Sun.COM if (t = trapcom[0]) {
1800Sstevel@tonic-gate trapcom[0] = 0;
1810Sstevel@tonic-gate /* Save exit value so trap handler will not change its val */
1820Sstevel@tonic-gate savxit = exitval;
1830Sstevel@tonic-gate execexp(t, 0);
1840Sstevel@tonic-gate exitval = savxit; /* Restore exit value */
1850Sstevel@tonic-gate free(t);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate else
1880Sstevel@tonic-gate chktrap();
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate rmtemp(0);
1910Sstevel@tonic-gate rmfunctmp();
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate #ifdef ACCT
1940Sstevel@tonic-gate doacct();
1950Sstevel@tonic-gate #endif
1960Sstevel@tonic-gate if (flags & subsh) {
1970Sstevel@tonic-gate /* in a subshell, need to wait on foreground job */
1980Sstevel@tonic-gate collect_fg_job();
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate (void) endjobs(0);
2020Sstevel@tonic-gate if (sig) {
2030Sstevel@tonic-gate sigset_t set;
204*9461SArindam.Sarkar@Sun.COM
205*9461SArindam.Sarkar@Sun.COM /*
206*9461SArindam.Sarkar@Sun.COM * If the signal is SIGHUP, then it should be delivered
207*9461SArindam.Sarkar@Sun.COM * to the process group leader of the foreground job.
208*9461SArindam.Sarkar@Sun.COM */
209*9461SArindam.Sarkar@Sun.COM if (sig == SIGHUP)
210*9461SArindam.Sarkar@Sun.COM hupforegnd();
211*9461SArindam.Sarkar@Sun.COM
2120Sstevel@tonic-gate sigemptyset(&set);
2130Sstevel@tonic-gate sigaddset(&set, sig);
2140Sstevel@tonic-gate sigprocmask(SIG_UNBLOCK, &set, 0);
2150Sstevel@tonic-gate handle(sig, SIG_DFL);
2160Sstevel@tonic-gate kill(mypid, sig);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate exit(exitval);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate static void
fault(int sig)222527Schin fault(int sig)
2230Sstevel@tonic-gate {
224527Schin int flag;
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate switch (sig) {
2270Sstevel@tonic-gate case SIGALRM:
2280Sstevel@tonic-gate if (sleeping)
2290Sstevel@tonic-gate return;
2300Sstevel@tonic-gate break;
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (trapcom[sig])
2340Sstevel@tonic-gate flag = TRAPSET;
2350Sstevel@tonic-gate else if (flags & subsh)
2360Sstevel@tonic-gate done(sig);
2370Sstevel@tonic-gate else
2380Sstevel@tonic-gate flag = SIGSET;
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate trapnote |= flag;
2410Sstevel@tonic-gate trapflg[sig] |= flag;
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate int
handle(sig,func)2450Sstevel@tonic-gate handle(sig, func)
2460Sstevel@tonic-gate int sig;
2470Sstevel@tonic-gate void (*func)();
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate int ret;
2500Sstevel@tonic-gate struct sigaction act, oact;
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate if (func == SIG_IGN && (trapflg[sig] & SIGIGN))
2530Sstevel@tonic-gate return (0);
254*9461SArindam.Sarkar@Sun.COM
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate * Ensure that sigaction is only called with valid signal numbers,
2570Sstevel@tonic-gate * we can get random values back for oact.sa_handler if the signal
2580Sstevel@tonic-gate * number is invalid
2590Sstevel@tonic-gate *
2600Sstevel@tonic-gate */
2610Sstevel@tonic-gate if (sig > MINTRAP && sig < MAXTRAP) {
2620Sstevel@tonic-gate sigemptyset(&act.sa_mask);
2630Sstevel@tonic-gate act.sa_flags = (sig == SIGSEGV) ? (SA_ONSTACK | SA_SIGINFO) : 0;
2640Sstevel@tonic-gate act.sa_handler = func;
2650Sstevel@tonic-gate sigaction(sig, &act, &oact);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate if (func == SIG_IGN)
2690Sstevel@tonic-gate trapflg[sig] |= SIGIGN;
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate /*
2720Sstevel@tonic-gate * Special case for signal zero, we can not obtain the previos
2730Sstevel@tonic-gate * action by calling sigaction, instead we save it in the variable
2740Sstevel@tonic-gate * psig0_func, so we can test it next time through this code
2750Sstevel@tonic-gate *
2760Sstevel@tonic-gate */
2770Sstevel@tonic-gate if (sig == 0) {
2780Sstevel@tonic-gate ret = (psig0_func != func);
2790Sstevel@tonic-gate psig0_func = func;
2800Sstevel@tonic-gate } else {
2810Sstevel@tonic-gate ret = (func != oact.sa_handler);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate return (ret);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate void
stdsigs()2880Sstevel@tonic-gate stdsigs()
2890Sstevel@tonic-gate {
290527Schin int i;
2910Sstevel@tonic-gate stack_t ss;
2920Sstevel@tonic-gate int rtmin = (int)SIGRTMIN;
2930Sstevel@tonic-gate int rtmax = (int)SIGRTMAX;
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate ss.ss_size = SIGSTKSZ;
2960Sstevel@tonic-gate ss.ss_sp = sigsegv_stack;
2970Sstevel@tonic-gate ss.ss_flags = 0;
2982256Sna195498 if (sigaltstack(&ss, NULL) == -1) {
2992256Sna195498 error("sigaltstack(2) failed");
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate for (i = 1; i < MAXTRAP; i++) {
3030Sstevel@tonic-gate if (i == rtmin) {
3040Sstevel@tonic-gate i = rtmax;
3050Sstevel@tonic-gate continue;
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate if (sigval[i] == 0)
3080Sstevel@tonic-gate continue;
3090Sstevel@tonic-gate if (i != SIGSEGV && ignoring(i))
3100Sstevel@tonic-gate continue;
3110Sstevel@tonic-gate handle(i, sigval[i]);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate * handle all the realtime signals
3160Sstevel@tonic-gate *
3170Sstevel@tonic-gate */
3180Sstevel@tonic-gate for (i = rtmin; i <= rtmax; i++) {
3190Sstevel@tonic-gate handle(i, done);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate void
oldsigs()3240Sstevel@tonic-gate oldsigs()
3250Sstevel@tonic-gate {
326527Schin int i;
327527Schin unsigned char *t;
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate i = MAXTRAP;
330*9461SArindam.Sarkar@Sun.COM while (i--) {
3310Sstevel@tonic-gate t = trapcom[i];
3320Sstevel@tonic-gate if (t == 0 || *t)
3330Sstevel@tonic-gate clrsig(i);
3340Sstevel@tonic-gate trapflg[i] = 0;
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate trapnote = 0;
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate /*
3400Sstevel@tonic-gate * check for traps
3410Sstevel@tonic-gate */
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate void
chktrap()3440Sstevel@tonic-gate chktrap()
3450Sstevel@tonic-gate {
346527Schin int i = MAXTRAP;
347527Schin unsigned char *t;
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate trapnote &= ~TRAPSET;
350*9461SArindam.Sarkar@Sun.COM while (--i) {
351*9461SArindam.Sarkar@Sun.COM if (trapflg[i] & TRAPSET) {
3520Sstevel@tonic-gate trapflg[i] &= ~TRAPSET;
353*9461SArindam.Sarkar@Sun.COM if (t = trapcom[i]) {
3540Sstevel@tonic-gate int savxit = exitval;
355*9461SArindam.Sarkar@Sun.COM
3560Sstevel@tonic-gate execexp(t, 0);
3570Sstevel@tonic-gate exitval = savxit;
3580Sstevel@tonic-gate exitset();
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate
364527Schin void
systrap(int argc,char ** argv)365527Schin systrap(int argc, char **argv)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate int sig;
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate if (argc == 1) {
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate * print out the current action associated with each signal
3720Sstevel@tonic-gate * handled by the shell
3730Sstevel@tonic-gate *
3740Sstevel@tonic-gate */
3750Sstevel@tonic-gate for (sig = 0; sig < MAXTRAP; sig++) {
3760Sstevel@tonic-gate if (trapcom[sig]) {
3770Sstevel@tonic-gate prn_buff(sig);
3780Sstevel@tonic-gate prs_buff(colon);
3790Sstevel@tonic-gate prs_buff(trapcom[sig]);
3800Sstevel@tonic-gate prc_buff(NL);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate } else {
3840Sstevel@tonic-gate /*
3850Sstevel@tonic-gate * set the action for the list of signals
3860Sstevel@tonic-gate *
3870Sstevel@tonic-gate */
3880Sstevel@tonic-gate char *cmd = *argv, *a1 = *(argv+1);
3890Sstevel@tonic-gate BOOL noa1;
3900Sstevel@tonic-gate noa1 = (str2sig(a1, &sig) == 0);
3910Sstevel@tonic-gate if (noa1 == 0)
3920Sstevel@tonic-gate ++argv;
3930Sstevel@tonic-gate while (*++argv) {
3940Sstevel@tonic-gate if (str2sig(*argv, &sig) < 0 ||
3950Sstevel@tonic-gate sig >= MAXTRAP || sig < MINTRAP ||
3960Sstevel@tonic-gate sig == SIGSEGV) {
3970Sstevel@tonic-gate failure(cmd, badtrap);
3980Sstevel@tonic-gate } else if (noa1) {
3990Sstevel@tonic-gate /*
4000Sstevel@tonic-gate * no action specifed so reset the siganl
4010Sstevel@tonic-gate * to its default disposition
4020Sstevel@tonic-gate *
4030Sstevel@tonic-gate */
4040Sstevel@tonic-gate clrsig(sig);
4050Sstevel@tonic-gate } else if (*a1) {
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate * set the action associated with the signal
4080Sstevel@tonic-gate * to a1
4090Sstevel@tonic-gate *
4100Sstevel@tonic-gate */
4110Sstevel@tonic-gate if (trapflg[sig] & SIGMOD || sig == 0 ||
4120Sstevel@tonic-gate !ignoring(sig)) {
4130Sstevel@tonic-gate handle(sig, fault);
4140Sstevel@tonic-gate trapflg[sig] |= SIGMOD;
4150Sstevel@tonic-gate replace(&trapcom[sig], a1);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate } else if (handle(sig, SIG_IGN)) {
4180Sstevel@tonic-gate /*
4190Sstevel@tonic-gate * set the action associated with the signal
4200Sstevel@tonic-gate * to SIG_IGN
4210Sstevel@tonic-gate *
4220Sstevel@tonic-gate */
4230Sstevel@tonic-gate trapflg[sig] |= SIGMOD;
4240Sstevel@tonic-gate replace(&trapcom[sig], a1);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate
430527Schin void
sh_sleep(unsigned int ticks)431527Schin sh_sleep(unsigned int ticks)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate sigset_t set, oset;
4340Sstevel@tonic-gate struct sigaction act, oact;
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate * add SIGALRM to mask
4390Sstevel@tonic-gate */
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate sigemptyset(&set);
4420Sstevel@tonic-gate sigaddset(&set, SIGALRM);
4430Sstevel@tonic-gate sigprocmask(SIG_BLOCK, &set, &oset);
4440Sstevel@tonic-gate
4450Sstevel@tonic-gate /*
4460Sstevel@tonic-gate * catch SIGALRM
4470Sstevel@tonic-gate */
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate sigemptyset(&act.sa_mask);
4500Sstevel@tonic-gate act.sa_flags = 0;
4510Sstevel@tonic-gate act.sa_handler = fault;
4520Sstevel@tonic-gate sigaction(SIGALRM, &act, &oact);
4530Sstevel@tonic-gate
4540Sstevel@tonic-gate /*
4550Sstevel@tonic-gate * start alarm and wait for signal
4560Sstevel@tonic-gate */
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate alarm(ticks);
4590Sstevel@tonic-gate sleeping = 1;
4600Sstevel@tonic-gate sigsuspend(&oset);
4610Sstevel@tonic-gate sleeping = 0;
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate /*
4640Sstevel@tonic-gate * reset alarm, catcher and mask
4650Sstevel@tonic-gate */
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate alarm(0);
4680Sstevel@tonic-gate sigaction(SIGALRM, &oact, NULL);
4690Sstevel@tonic-gate sigprocmask(SIG_SETMASK, &oset, 0);
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate void
sigsegv(int sig,siginfo_t * sip,ucontext_t * uap)4740Sstevel@tonic-gate sigsegv(int sig, siginfo_t *sip, ucontext_t *uap)
4750Sstevel@tonic-gate {
4760Sstevel@tonic-gate if (sip == (siginfo_t *)NULL) {
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate * This should never happen, but if it does this is all we
4790Sstevel@tonic-gate * can do. It can only happen if sigaction(2) for SIGSEGV
4800Sstevel@tonic-gate * has been called without SA_SIGINFO being set.
4810Sstevel@tonic-gate *
4820Sstevel@tonic-gate */
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate exit(ERROR);
4850Sstevel@tonic-gate } else {
4860Sstevel@tonic-gate if (sip->si_code <= 0) {
4870Sstevel@tonic-gate /*
4880Sstevel@tonic-gate * If we are here then SIGSEGV must have been sent to
4890Sstevel@tonic-gate * us from a user process NOT as a result of an
4900Sstevel@tonic-gate * internal error within the shell eg
4910Sstevel@tonic-gate * kill -SEGV $$
4920Sstevel@tonic-gate * will bring us here. So do the normal thing.
4930Sstevel@tonic-gate *
4940Sstevel@tonic-gate */
4950Sstevel@tonic-gate fault(sig);
4960Sstevel@tonic-gate } else {
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate * If we are here then there must have been an internal
4990Sstevel@tonic-gate * error within the shell to generate SIGSEGV eg
5000Sstevel@tonic-gate * the stack is full and we cannot call any more
5010Sstevel@tonic-gate * functions (Remeber this signal handler is running
5020Sstevel@tonic-gate * on an alternate stack). So we just exit cleanly
5030Sstevel@tonic-gate * with an error status (no core file).
5040Sstevel@tonic-gate */
5050Sstevel@tonic-gate exit(ERROR);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate }
509