3 * sigcompat - BSD compat signals via POSIX
6 * void (*signal(int "sig", void (*"handler")(int)))(int);
7 * int sigsetmask(int "mask");
8 * int sigblock(int "mask");
9 * int sigpause(int "mask");
10 * int sigvec(int "signo", struct sigvec *"sv", struct sigvec *"osv");
13 * These implement the old BSD routines via the POSIX equivalents.
14 * This module can be used to provide the missing routines, or if
15 * 'FORCE_POSIX_SIGNALS' is defined, force use of these.
17 * Note that signal() is identical to my Signal() routine except
18 * for checking for recursion. Within libsig, signal() just
22 * This package assumes POSIX signal handling is available and
23 * NOT implemeneted using these routines. To be safe, we check
24 * for recursion and abort(3) if detected.
26 * Sadly, on some systems, sigset_t is an array, and we cannot
27 * test for this via #if sizeof(sigset_t) ..., so unless
28 * 'SIGSET_T_INT' is defined, we have to assume the worst and use
29 * memcpy(3) to handle args and return values.
32 * These routines originate from BSD, and are derrived from the
33 * NetBSD 1.1 implementation. They have been seriously hacked to
34 * make them portable to other systems.
37 * Simon J. Gerraty <sjg@crufty.net>
40 * @(#)Copyright (c) 1994, Simon J. Gerraty.
42 * This is free software. It comes with NO WARRANTY.
43 * Permission to use, modify and distribute this source code
44 * is granted subject to the following conditions.
45 * 1/ that the above copyright notice and this notice
46 * are preserved in all copies and that due credit be given
48 * 2/ that any changes to this code are clearly commented
49 * as such so that the author does not get blamed for bugs
52 * Please send copies of changes and bug-fixes to:
57 * Copyright (c) 1989 The Regents of the University of California.
58 * All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
63 * 1. Redistributions of source code must retain the above copyright
64 * notice, this list of conditions and the following disclaimer.
65 * 2. Redistributions in binary form must reproduce the above copyright
66 * notice, this list of conditions and the following disclaimer in the
67 * documentation and/or other materials provided with the distribution.
68 * 3. All advertising materials mentioning features or use of this software
69 * must display the following acknowledgement:
70 * This product includes software developed by the University of
71 * California, Berkeley and its contributors.
72 * 4. Neither the name of the University nor the names of its contributors
73 * may be used to endorse or promote products derived from this software
74 * without specific prior written permission.
76 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
77 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
79 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
80 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
81 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
82 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
83 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
84 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
85 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
96 #if defined(sun) && !(defined(__svr4__) || defined(__SVR4))
100 # define NO_SIGCOMPAT
103 #if !defined(NO_SIGCOMPAT) && (defined(HAVE_SIGACTION) || defined(SA_NOCLDSTOP))
105 #if defined(LIBC_SCCS) && !defined(lint)
106 /*static char *sccsid = "from: @(#)sigcompat.c 5.3 (Berkeley) 2/24/91";*/
107 static char *rcsid
= "$Id: sigcompat.c,v 1.24 2022/09/26 17:38:10 sjg Exp $";
108 #endif /* LIBC_SCCS and not lint */
113 #include <sys/param.h>
114 #include <sys/cdefs.h>
118 # define ASSERT assert
128 # define SA_RESTART 2
131 # define SV_INTERRUPT SA_RESTART
135 # if defined(__hpux__) || defined(__hpux)
141 /* I just hate HPsUX */
142 #if (defined(__HPUX_VERSION) && __HPUX_VERSION > 9) || defined(__hpux)
143 # define PAUSE_MASK_T int
145 # define PAUSE_MASK_T MASK_T
149 # define SIG_HDLR void
152 #ifdef FORCE_POSIX_SIGNALS
153 #if !(defined(libsig) || defined(libsjg))
155 * This little block is almost identical to Signal(),
156 * and make this module standalone.
157 * We don't use it in libsig by default, as some apps might use both
158 * and expect _SignalFlags to be used by both.
162 # define SIGNAL_FLAGS 0 /* no auto-restart */
164 int _signalFlags
= SIGNAL_FLAGS
;
166 SIG_HDLR(*signal(int sig
, SIG_HDLR(*handler
)(int)))(int)
168 _DBUG(static int depth_signal
= 0);
169 struct sigaction act
, oact
;
172 _DBUG(++depth_signal
);
173 ASSERT(depth_signal
< 2);
174 act
.sa_handler
= handler
;
175 sigemptyset(&act
.sa_mask
);
176 act
.sa_flags
= _signalFlags
;
177 n
= sigaction(sig
, &act
, &oact
);
178 _DBUG(--depth_signal
);
181 return (oact
.sa_handler
);
184 SIG_HDLR(*signal(int sig
, SIG_HDLR(*handler
)(int)))(int)
186 extern SIG_HDLR(*Signal(int, void (*)(int)))(int);
187 _DBUG(static int depth_signal
= 0);
188 SIG_HDLR(*old
) __P((int));
190 _DBUG(++depth_signal
);
191 ASSERT(depth_signal
< 2);
192 old
= Signal(sig
, handler
);
193 _DBUG(--depth_signal
);
200 * on some systems, sigset_t is an array...
201 * it would be nicer if we could do
202 * #if sizeof(sigset_t) > sizeof(MASK_T)
205 # define ss2m(ss) (MASK_T) *(ss)
206 # define m2ss(ss, m) *ss = (sigset_t) *(m)
207 #elif !defined(HAVE_SIGSETMASK) || defined(FORCE_POSIX_SIGNALS)
211 MASK_T ma
[(sizeof(sigset_t
) / sizeof(MASK_T
)) + 1];
213 memcpy((char *) ma
, (char *) ss
, sizeof(sigset_t
));
218 m2ss(sigset_t
*ss
, MASK_T
*m
)
220 if (sizeof(sigset_t
) > sizeof(MASK_T
))
221 memset((char *) ss
, 0, sizeof(sigset_t
));
223 memcpy((char *) ss
, (char *) m
, sizeof(MASK_T
));
227 #if !defined(HAVE_SIGSETMASK) || defined(FORCE_POSIX_SIGNALS)
229 sigsetmask(MASK_T mask
)
231 _DBUG(static int depth_sigsetmask
= 0);
235 _DBUG(++depth_sigsetmask
);
236 ASSERT(depth_sigsetmask
< 2);
238 n
= sigprocmask(SIG_SETMASK
, (sigset_t
*) & m
, (sigset_t
*) & omask
);
239 _DBUG(--depth_sigsetmask
);
248 sigblock(MASK_T mask
)
250 _DBUG(static int depth_sigblock
= 0);
254 _DBUG(++depth_sigblock
);
255 ASSERT(depth_sigblock
< 2);
258 n
= sigprocmask(SIG_BLOCK
, (sigset_t
*) ((mask
) ? &m
: 0), (sigset_t
*) & omask
);
259 _DBUG(--depth_sigblock
);
265 #undef sigpause /* Linux at least */
268 sigpause(PAUSE_MASK_T mask
)
270 _DBUG(static int depth_sigpause
= 0);
274 _DBUG(++depth_sigpause
);
275 ASSERT(depth_sigpause
< 2);
278 _DBUG(--depth_sigpause
);
283 #if defined(HAVE_SIGVEC) && defined(FORCE_POSIX_SIGNALS)
285 sigvec(int signo
, struct sigvec
*sv
, struct sigvec
*osv
)
287 _DBUG(static int depth_sigvec
= 0);
291 _DBUG(++depth_sigvec
);
292 ASSERT(depth_sigvec
< 2);
295 nsv
.sv_flags
^= SV_INTERRUPT
; /* !SA_INTERRUPT */
297 ret
= sigaction(signo
, sv
? (struct sigaction
*) & nsv
: NULL
,
298 (struct sigaction
*) osv
);
299 _DBUG(--depth_sigvec
);
301 osv
->sv_flags
^= SV_INTERRUPT
; /* !SA_INTERRUPT */
308 # define sigmask(n) ((unsigned int)1 << (((n) - 1) & (32 - 1)))
312 main(int argc
, char *argv
[])
316 printf("expect: old=0,old=2\n");
318 signal(SIGQUIT
, SIG_IGN
);
319 old
= sigblock(sigmask(SIGINT
));
320 printf("old=%d,", old
);
321 old
= sigsetmask(sigmask(SIGALRM
));
322 printf("old=%d\n", old
);