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