1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23*0Sstevel@tonic-gate /* All Rights Reserved */
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
28*0Sstevel@tonic-gate * The Regents of the University of California
29*0Sstevel@tonic-gate * All Rights Reserved
30*0Sstevel@tonic-gate *
31*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
32*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
33*0Sstevel@tonic-gate * contributors.
34*0Sstevel@tonic-gate */
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley
40*0Sstevel@tonic-gate * mail program
41*0Sstevel@tonic-gate */
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate /*
44*0Sstevel@tonic-gate * This code is only compiled in if SIG_HOLD is not defined!
45*0Sstevel@tonic-gate *
46*0Sstevel@tonic-gate * Retrofit new signal interface to old signal primitives.
47*0Sstevel@tonic-gate * Supported routines:
48*0Sstevel@tonic-gate * sigsys(sig, func)
49*0Sstevel@tonic-gate * sigset(sig, func)
50*0Sstevel@tonic-gate * sighold(sig)
51*0Sstevel@tonic-gate * sigrelse(sig)
52*0Sstevel@tonic-gate * sigignore(sig)
53*0Sstevel@tonic-gate * sigpause(sig)
54*0Sstevel@tonic-gate * Also,
55*0Sstevel@tonic-gate * sigchild()
56*0Sstevel@tonic-gate * to set all held signals to ignored signals in the
57*0Sstevel@tonic-gate * child process after fork(2)
58*0Sstevel@tonic-gate */
59*0Sstevel@tonic-gate #include <signal.h>
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate #ifndef SIG_HOLD
62*0Sstevel@tonic-gate # include <errno.h>
63*0Sstevel@tonic-gate # include <setjmp.h>
64*0Sstevel@tonic-gate # include <stdio.h>
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate extern int errno;
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate typedef void (*sigtype)();
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate #define SIG_HOLD ((sigtype) 2)
71*0Sstevel@tonic-gate #ifndef SIG_ERR
72*0Sstevel@tonic-gate # define SIG_ERR ((sigtype) -1)
73*0Sstevel@tonic-gate #endif
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate sigtype sigdisp(), sighold(), sigignore();
76*0Sstevel@tonic-gate void _Sigtramp();
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate * The following helps us keep the extended signal semantics together.
80*0Sstevel@tonic-gate * We remember for each signal the address of the function we're
81*0Sstevel@tonic-gate * supposed to call. s_func is SIG_DFL / SIG_IGN if appropriate.
82*0Sstevel@tonic-gate */
83*0Sstevel@tonic-gate static struct sigtable {
84*0Sstevel@tonic-gate sigtype s_func; /* What to call */
85*0Sstevel@tonic-gate int s_flag; /* Signal flags; see below */
86*0Sstevel@tonic-gate } sigtable[NSIG + 1];
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate /*
89*0Sstevel@tonic-gate * Signal flag values.
90*0Sstevel@tonic-gate */
91*0Sstevel@tonic-gate #define SHELD 1 /* Signal is being held */
92*0Sstevel@tonic-gate #define SDEFER 2 /* Signal occured while held */
93*0Sstevel@tonic-gate #define SSET 4 /* s_func is believable */
94*0Sstevel@tonic-gate #define SPAUSE 8 /* are pausing, waiting for sig */
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate jmp_buf _pause; /* For doing sigpause() */
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate /*
99*0Sstevel@tonic-gate * Approximate sigsys() system call
100*0Sstevel@tonic-gate * This is almost useless since one only calls sigsys()
101*0Sstevel@tonic-gate * in the child of a vfork(). If you have vfork(), you have new signals
102*0Sstevel@tonic-gate * anyway. The real sigsys() does all the stuff needed to support
103*0Sstevel@tonic-gate * the real sigset() library. We don't bother here, assuming that
104*0Sstevel@tonic-gate * you are either ignoring or defaulting a signal in the child.
105*0Sstevel@tonic-gate */
106*0Sstevel@tonic-gate sigtype
sigsys(int sig,sigtype func)107*0Sstevel@tonic-gate sigsys(int sig, sigtype func)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate sigtype old;
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate old = sigdisp(sig);
112*0Sstevel@tonic-gate signal(sig, func);
113*0Sstevel@tonic-gate return(old);
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate /*
118*0Sstevel@tonic-gate * Set the (permanent) disposition of a signal.
119*0Sstevel@tonic-gate * If the signal is subsequently (or even now) held,
120*0Sstevel@tonic-gate * the action you set here can be enabled using sigrelse().
121*0Sstevel@tonic-gate */
122*0Sstevel@tonic-gate sigtype
sigset(int sig,sigtype func)123*0Sstevel@tonic-gate sigset(int sig, sigtype func)
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate sigtype old;
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate if (sig < 1 || sig > NSIG) {
128*0Sstevel@tonic-gate errno = EINVAL;
129*0Sstevel@tonic-gate return(SIG_ERR);
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate old = sigdisp(sig);
132*0Sstevel@tonic-gate /*
133*0Sstevel@tonic-gate * Does anyone actually call sigset with SIG_HOLD!?
134*0Sstevel@tonic-gate */
135*0Sstevel@tonic-gate if (func == SIG_HOLD) {
136*0Sstevel@tonic-gate sighold(sig);
137*0Sstevel@tonic-gate return(old);
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate sigtable[sig].s_flag |= SSET;
140*0Sstevel@tonic-gate sigtable[sig].s_func = func;
141*0Sstevel@tonic-gate if (func == SIG_DFL) {
142*0Sstevel@tonic-gate /*
143*0Sstevel@tonic-gate * If signal has been held, must retain
144*0Sstevel@tonic-gate * the catch so that we can note occurrance
145*0Sstevel@tonic-gate * of signal.
146*0Sstevel@tonic-gate */
147*0Sstevel@tonic-gate if ((sigtable[sig].s_flag & SHELD) == 0)
148*0Sstevel@tonic-gate signal(sig, SIG_DFL);
149*0Sstevel@tonic-gate else
150*0Sstevel@tonic-gate signal(sig, _Sigtramp);
151*0Sstevel@tonic-gate return(old);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate if (func == SIG_IGN) {
154*0Sstevel@tonic-gate /*
155*0Sstevel@tonic-gate * Clear pending signal
156*0Sstevel@tonic-gate */
157*0Sstevel@tonic-gate signal(sig, SIG_IGN);
158*0Sstevel@tonic-gate sigtable[sig].s_flag &= ~SDEFER;
159*0Sstevel@tonic-gate return(old);
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate signal(sig, _Sigtramp);
162*0Sstevel@tonic-gate return(old);
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate /*
166*0Sstevel@tonic-gate * Hold a signal.
167*0Sstevel@tonic-gate * This CAN be tricky if the signal's disposition is SIG_DFL.
168*0Sstevel@tonic-gate * In that case, we still catch the signal so we can note it
169*0Sstevel@tonic-gate */
170*0Sstevel@tonic-gate sigtype
sighold(int sig)171*0Sstevel@tonic-gate sighold(int sig)
172*0Sstevel@tonic-gate {
173*0Sstevel@tonic-gate sigtype old;
174*0Sstevel@tonic-gate
175*0Sstevel@tonic-gate if (sig < 1 || sig > NSIG) {
176*0Sstevel@tonic-gate errno = EINVAL;
177*0Sstevel@tonic-gate return(SIG_ERR);
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate old = sigdisp(sig);
180*0Sstevel@tonic-gate if (sigtable[sig].s_flag & SHELD)
181*0Sstevel@tonic-gate return(old);
182*0Sstevel@tonic-gate /*
183*0Sstevel@tonic-gate * When the default action is required, we have to
184*0Sstevel@tonic-gate * set up to catch the signal to note signal's occurrance.
185*0Sstevel@tonic-gate */
186*0Sstevel@tonic-gate if (old == SIG_DFL) {
187*0Sstevel@tonic-gate sigtable[sig].s_flag |= SSET;
188*0Sstevel@tonic-gate signal(sig, _Sigtramp);
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate sigtable[sig].s_flag |= SHELD;
191*0Sstevel@tonic-gate return(old);
192*0Sstevel@tonic-gate }
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate /*
195*0Sstevel@tonic-gate * Release a signal
196*0Sstevel@tonic-gate * If the signal occurred while we had it held, cause the signal.
197*0Sstevel@tonic-gate */
198*0Sstevel@tonic-gate sigtype
sigrelse(int sig)199*0Sstevel@tonic-gate sigrelse(int sig)
200*0Sstevel@tonic-gate {
201*0Sstevel@tonic-gate sigtype old;
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate if (sig < 1 || sig > NSIG) {
204*0Sstevel@tonic-gate errno = EINVAL;
205*0Sstevel@tonic-gate return(SIG_ERR);
206*0Sstevel@tonic-gate }
207*0Sstevel@tonic-gate old = sigdisp(sig);
208*0Sstevel@tonic-gate if ((sigtable[sig].s_flag & SHELD) == 0)
209*0Sstevel@tonic-gate return(old);
210*0Sstevel@tonic-gate sigtable[sig].s_flag &= ~SHELD;
211*0Sstevel@tonic-gate if (sigtable[sig].s_flag & SDEFER)
212*0Sstevel@tonic-gate _Sigtramp(sig);
213*0Sstevel@tonic-gate /*
214*0Sstevel@tonic-gate * If disposition was the default, then we can unset the
215*0Sstevel@tonic-gate * catch to _Sigtramp() and let the system do the work.
216*0Sstevel@tonic-gate */
217*0Sstevel@tonic-gate if (sigtable[sig].s_func == SIG_DFL)
218*0Sstevel@tonic-gate signal(sig, SIG_DFL);
219*0Sstevel@tonic-gate return(old);
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate /*
223*0Sstevel@tonic-gate * Ignore a signal.
224*0Sstevel@tonic-gate */
225*0Sstevel@tonic-gate sigtype
sigignore(int sig)226*0Sstevel@tonic-gate sigignore(int sig)
227*0Sstevel@tonic-gate {
228*0Sstevel@tonic-gate return(sigset(sig, SIG_IGN));
229*0Sstevel@tonic-gate }
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gate /*
232*0Sstevel@tonic-gate * Pause, waiting for sig to occur.
233*0Sstevel@tonic-gate * We assume LUSER called us with the signal held.
234*0Sstevel@tonic-gate * When we got the signal, mark the signal as having
235*0Sstevel@tonic-gate * occurred. It will actually cause something when
236*0Sstevel@tonic-gate * the signal is released.
237*0Sstevel@tonic-gate */
238*0Sstevel@tonic-gate int
sigpause(int sig)239*0Sstevel@tonic-gate sigpause(int sig)
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate if (sig < 1 || sig > NSIG) {
242*0Sstevel@tonic-gate errno = EINVAL;
243*0Sstevel@tonic-gate return;
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate sigtable[sig].s_flag |= SHELD|SPAUSE;
246*0Sstevel@tonic-gate if (setjmp(_pause) == 0)
247*0Sstevel@tonic-gate pause();
248*0Sstevel@tonic-gate sigtable[sig].s_flag &= ~SPAUSE;
249*0Sstevel@tonic-gate sigtable[sig].s_flag |= SDEFER;
250*0Sstevel@tonic-gate }
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate /*
253*0Sstevel@tonic-gate * In the child process after fork(2), set the disposition of all held
254*0Sstevel@tonic-gate * signals to SIG_IGN. This is a new procedure not in the real sigset()
255*0Sstevel@tonic-gate * package, provided for retrofitting purposes.
256*0Sstevel@tonic-gate */
257*0Sstevel@tonic-gate int
sigchild(void)258*0Sstevel@tonic-gate sigchild(void)
259*0Sstevel@tonic-gate {
260*0Sstevel@tonic-gate register int i;
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate for (i = 1; i <= NSIG; i++)
263*0Sstevel@tonic-gate if (sigtable[i].s_flag & SHELD)
264*0Sstevel@tonic-gate signal(i, SIG_IGN);
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gate
268*0Sstevel@tonic-gate /*
269*0Sstevel@tonic-gate * Return the current disposition of a signal
270*0Sstevel@tonic-gate * If we have not set this signal before, we have to
271*0Sstevel@tonic-gate * ask the system
272*0Sstevel@tonic-gate */
273*0Sstevel@tonic-gate sigtype
sigdisp(int sig)274*0Sstevel@tonic-gate sigdisp(int sig)
275*0Sstevel@tonic-gate {
276*0Sstevel@tonic-gate sigtype old;
277*0Sstevel@tonic-gate
278*0Sstevel@tonic-gate if (sig < 1 || sig > NSIG) {
279*0Sstevel@tonic-gate errno = EINVAL;
280*0Sstevel@tonic-gate return(SIG_ERR);
281*0Sstevel@tonic-gate }
282*0Sstevel@tonic-gate /*
283*0Sstevel@tonic-gate * If we have no knowledge of this signal,
284*0Sstevel@tonic-gate * ask the system, then save the result for later.
285*0Sstevel@tonic-gate */
286*0Sstevel@tonic-gate if ((sigtable[sig].s_flag & SSET) == 0) {
287*0Sstevel@tonic-gate old = signal(sig, SIG_IGN);
288*0Sstevel@tonic-gate sigtable[sig].s_func = old;
289*0Sstevel@tonic-gate sigtable[sig].s_flag |= SSET;
290*0Sstevel@tonic-gate signal(sig, old);
291*0Sstevel@tonic-gate return(old);
292*0Sstevel@tonic-gate }
293*0Sstevel@tonic-gate /*
294*0Sstevel@tonic-gate * If we have set this signal before, then sigset()
295*0Sstevel@tonic-gate * will have been careful to leave something meaningful
296*0Sstevel@tonic-gate * in s_func.
297*0Sstevel@tonic-gate */
298*0Sstevel@tonic-gate return(sigtable[sig].s_func);
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate
301*0Sstevel@tonic-gate /*
302*0Sstevel@tonic-gate * The following routine gets called for any signal
303*0Sstevel@tonic-gate * that is to be trapped to a user function.
304*0Sstevel@tonic-gate */
305*0Sstevel@tonic-gate void
_Sigtramp(int sig)306*0Sstevel@tonic-gate _Sigtramp(int sig)
307*0Sstevel@tonic-gate {
308*0Sstevel@tonic-gate sigtype old;
309*0Sstevel@tonic-gate
310*0Sstevel@tonic-gate if (sig < 1 || sig > NSIG) {
311*0Sstevel@tonic-gate errno = EINVAL;
312*0Sstevel@tonic-gate return;
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate
315*0Sstevel@tonic-gate top:
316*0Sstevel@tonic-gate old = signal(sig, SIG_IGN);
317*0Sstevel@tonic-gate /*
318*0Sstevel@tonic-gate * If signal being paused on, wakeup sigpause()
319*0Sstevel@tonic-gate */
320*0Sstevel@tonic-gate if (sigtable[sig].s_flag & SPAUSE)
321*0Sstevel@tonic-gate longjmp(_pause, 1);
322*0Sstevel@tonic-gate /*
323*0Sstevel@tonic-gate * If signal is being held, mark its table entry
324*0Sstevel@tonic-gate * so we can trigger it when signal is released.
325*0Sstevel@tonic-gate * Then just return.
326*0Sstevel@tonic-gate */
327*0Sstevel@tonic-gate if (sigtable[sig].s_flag & SHELD) {
328*0Sstevel@tonic-gate sigtable[sig].s_flag |= SDEFER;
329*0Sstevel@tonic-gate signal(sig, _Sigtramp);
330*0Sstevel@tonic-gate return;
331*0Sstevel@tonic-gate }
332*0Sstevel@tonic-gate /*
333*0Sstevel@tonic-gate * If the signal is being ignored, just return.
334*0Sstevel@tonic-gate * This would make SIGCONT more normal, but of course
335*0Sstevel@tonic-gate * any system with SIGCONT also has the new signal pkg, so...
336*0Sstevel@tonic-gate */
337*0Sstevel@tonic-gate if (sigtable[sig].s_func == SIG_IGN)
338*0Sstevel@tonic-gate return;
339*0Sstevel@tonic-gate /*
340*0Sstevel@tonic-gate * If the signal is SIG_DFL, then we probably got here
341*0Sstevel@tonic-gate * by holding the signal, having it happen, then releasing
342*0Sstevel@tonic-gate * the signal.
343*0Sstevel@tonic-gate */
344*0Sstevel@tonic-gate if (sigtable[sig].s_func == SIG_DFL) {
345*0Sstevel@tonic-gate signal(sig, SIG_DFL);
346*0Sstevel@tonic-gate kill(getpid(), sig);
347*0Sstevel@tonic-gate /* Will we get back here? */
348*0Sstevel@tonic-gate return;
349*0Sstevel@tonic-gate }
350*0Sstevel@tonic-gate /*
351*0Sstevel@tonic-gate * Looks like we should just cause the signal...
352*0Sstevel@tonic-gate * We hold the signal for the duration of the user's
353*0Sstevel@tonic-gate * code with the signal re-enabled. If the signal
354*0Sstevel@tonic-gate * happens again while in user code, we will recursively
355*0Sstevel@tonic-gate * trap here and mark that we had another occurance
356*0Sstevel@tonic-gate * and return to the user's trap code. When we return
357*0Sstevel@tonic-gate * from there, we can cause the signal again.
358*0Sstevel@tonic-gate */
359*0Sstevel@tonic-gate sigtable[sig].s_flag &= ~SDEFER;
360*0Sstevel@tonic-gate sigtable[sig].s_flag |= SHELD;
361*0Sstevel@tonic-gate signal(sig, _Sigtramp);
362*0Sstevel@tonic-gate (*sigtable[sig].s_func)(sig);
363*0Sstevel@tonic-gate /*
364*0Sstevel@tonic-gate * If the signal re-occurred while in the user's routine,
365*0Sstevel@tonic-gate * just go try it again...
366*0Sstevel@tonic-gate */
367*0Sstevel@tonic-gate sigtable[sig].s_flag &= ~SHELD;
368*0Sstevel@tonic-gate if (sigtable[sig].s_flag & SDEFER)
369*0Sstevel@tonic-gate goto top;
370*0Sstevel@tonic-gate }
371*0Sstevel@tonic-gate #endif
372