1 /*
2 * ircsig.c: has a `my_signal()' that uses sigaction().
3 *
4 * Copyright (c) 1993-2016 Matthew R. Green.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /*
32 * written by matthew green, 1993.
33 *
34 * i stole bits of this from w. richard stevens' `advanced programming
35 * in the unix environment' -mrg
36 */
37
38 #include "irc.h"
39 IRCII_RCSID("@(#)$eterna: ircsig.c,v 1.20 2016/12/04 21:40:49 gkm Exp $");
40
41 sigfunc *
my_signal(int sig_no,sigfunc * sig_handler,int misc_flags)42 my_signal(int sig_no, sigfunc *sig_handler, int misc_flags)
43 {
44 /*
45 * misc_flags is unused currently. it's planned to be used
46 * to use some of the doovier bits of sigaction(), if at
47 * some point we need them, -mrg
48 */
49
50 struct sigaction sa, osa;
51
52 /*
53 * have to (void *) this because of strict type checking
54 * on some systems -glen
55 */
56 sa.sa_handler = (void (*)(int)) sig_handler;
57
58 sigemptyset(&sa.sa_mask);
59 sigaddset(&sa.sa_mask, sig_no);
60
61 /* this is ugly, but the `correct' way. i hate c. -mrg */
62 sa.sa_flags = 0;
63 #if defined(SA_RESTART) || defined(SA_INTERRUPT)
64 if (SIGALRM == sig_no)
65 {
66 # if defined(SA_INTERRUPT)
67 sa.sa_flags |= SA_INTERRUPT;
68 # endif /* SA_INTERRUPT */
69 }
70 else
71 {
72 # if defined(SA_RESTART)
73 sa.sa_flags |= SA_RESTART;
74 # endif /* SA_RESTART */
75 }
76 #endif /* SA_RESTART || SA_INTERRUPT */
77
78 if (0 > sigaction(sig_no, &sa, &osa))
79 return ((sigfunc *)SIG_ERR);
80
81 return ((sigfunc *)osa.sa_handler);
82 }
83