|
|
1.1 root 1: #include "sh.h"
2: #include <signal.h>
3:
4: /*
5: * Interrupt flags.
6: */
7: int intflag = 0;
8: int realint = 0;
9: int norecur = 0;
10:
11: /*
12: * Trap storage area.
13: */
14: typedef struct {
15: int t_cnt; /* Signal received */
16: int (*t_set)(); /* Current action */
17: int (*t_def)(); /* Default action */
18: char *t_act; /* Action to take */
19: } TRAP;
20:
21: TRAP trap[NSIG+1];
22:
23: /*
24: * Set default traps for various contexts.
25: */
26: dflttrp(context)
27: register int context;
28: {
29: extern int sigintr();
30: register int sig;
31:
32: switch (context) {
33: case IRDY: /* Trap initialisation */
34: for (sig=0; sig<=NSIG; sig+=1)
35: deftrp(sig, signal(sig, SIG_IGN));
36: if (iflag) {
37: signal(SIGINT, sigintr);
38: signal(SIGTERM, SIG_IGN);
39: }
40: #ifndef DEBUG
41: signal(SIGQUIT, SIG_IGN);
42: #endif
43: break;
44: case IBACK: /* Background initialisation */
45: deftrp(SIGINT, SIG_IGN);
46: deftrp(SIGQUIT, SIG_IGN);
47: break;
48: case IFORK: /* Sub-shell initialization */
49: signal(SIGINT, trap[SIGINT].t_set);
50: signal(SIGTERM, trap[SIGTERM].t_set);
51: break;
52: case ICMD: /* Command execution initialisation */
53: signal(SIGQUIT, trap[SIGQUIT].t_set);
54: break;
55: }
56: }
57:
58: deftrp(sig, def)
59: register int sig;
60: register int (*def)();
61: {
62: signal(sig, def);
63: trap[sig].t_cnt = 0;
64: trap[sig].t_set = def;
65: trap[sig].t_def = def;
66: sfree(trap[sig].t_act);
67: trap[sig].t_act = NULL;
68: }
69:
70:
71: /*
72: * Show the contents of traps.
73: */
74: telltrp()
75: {
76: register int sig;
77: register int (*act)();
78: register char *what;
79:
80: if ((what=trap[0].t_act) != NULL)
81: prints("0=exit: %s\n", what);
82: for (sig=1; sig<=NSIG; sig+=1) {
83: act = trap[sig].t_set;
84: if (act==SIG_IGN)
85: what = "ignored";
86: else if (act==sigintr)
87: what = trap[sig].t_act;
88: else
89: what = NULL;
90: if (what)
91: prints("%d=%s: %s\n", sig, signame[sig], what);
92: }
93: return (0);
94: }
95:
96: /*
97: * Set a trap.
98: */
99: setstrp(sig, actp)
100: register int sig;
101: register char *actp;
102: {
103: int spc;
104:
105: if (sig < 0 || sig > NSIG) {
106: printe("Bad trap: %d", sig);
107: return (1);
108: }
109: spc = (iflag && (sig==SIGINT||sig==SIGTERM)) || sig==SIGQUIT;
110: sfree(trap[sig].t_act);
111: trap[sig].t_act = NULL;
112: if (actp==NULL) {
113: if (! spc)
114: signal(sig, trap[sig].t_def);
115: trap[sig].t_set = trap[sig].t_def;
116: } else if (*actp=='\0') {
117: if (! spc)
118: signal(sig, SIG_IGN);
119: trap[sig].t_set = SIG_IGN;
120: } else if (trap[sig].t_def == SIG_IGN) {
121: ;
122: } else {
123: trap[sig].t_act = duplstr(actp, 1);
124: if (! spc)
125: signal(sig, sigintr);
126: trap[sig].t_set = sigintr;
127: }
128: return (0);
129: }
130:
131: /*
132: * Execute a trap action.
133: */
134: /*
135: * Interrupt routine.
136: */
137: sigintr(sig)
138: register int sig;
139: {
140: signal(sig, sigintr);
141: intflag += 1;
142: trap[sig].t_cnt += 1;
143: if (sig==SIGINT && iflag) {
144: noeflag += 1;
145: realint += 1;
146: }
147: }
148:
149: /*
150: * Recover from an interrupt.
151: */
152: recover(context)
153: {
154: register int sig;
155: register char *actp;
156:
157: if ( ! intflag || norecur)
158: return (1);
159: norecur++;
160: realint = realint && iflag; /* iflag may have been cleared */
161: switch (context) {
162: case IPROF:
163: exit(0377);
164: case IRDY:
165: case ICMD:
166: break;
167: case ILEX:
168: case IEVAL:
169: norecur = 0;
170: return ( ! realint);
171: }
172: for (sig = 0; sig <= NSIG; sig += 1) {
173: if (trap[sig].t_cnt) {
174: trap[sig].t_cnt = 0;
175: if (actp = trap[sig].t_act) {
176: #if 0
177: trap[sig].t_act = NULL;
178: session(SARGS, actp);
179: trap[sig].t_act = actp;
180: setstrp(sig, NULL);
181: #else
182: session(SARGS, actp);
183: #endif
184: } else if (sig==SIGINT) {
185: if (! realint)
186: kill(getpid(), SIGINT);
187: }
188: }
189: }
190: intflag = 0;
191: norecur = 0;
192: if (realint) {
193: noeflag = 0;
194: realint = 0;
195: reset(RINT);
196: NOTREACHED;
197: }
198: return (1);
199: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.