File: ircsig.c

package info (click to toggle)
epic5 3.0.3-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 5,328 kB
  • sloc: ansic: 75,810; makefile: 648; ruby: 227; python: 215; sh: 78; perl: 13
file content (143 lines) | stat: -rw-r--r-- 3,930 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * ircsig.c: has a `my_signal()' that uses sigaction().
 *
 * Copyright (c) 1993-1996 Matthew R. Green.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
 * written by matthew green, 1993.
 *
 * i stole bits of this from w. richard stevens' `advanced programming
 * in the unix environment' -mrg
 */

#include "irc.h"
#include "irc_std.h"

int	block_signal (int sig_no)
{
	sigset_t set;

	sigemptyset(&set);
	sigaddset(&set, sig_no);
	return sigprocmask(SIG_BLOCK, &set, NULL);
}

int	unblock_signal (int sig_no)
{
	sigset_t set;

	sigemptyset(&set);
	sigaddset(&set, sig_no);
	return sigprocmask(SIG_UNBLOCK, &set, NULL);
}

/* array of signal handlers containing mostly NULL */
sigfunc *		signal_handlers[NSIG];
volatile sig_atomic_t	signals_caught[NSIG];

/* grand unified signal handler, which sets flags for scriptable signals
 * - pegasus
 */
static void	signal_handler (int sig_no)
{
	signals_caught[0] = 1;
	signals_caught[sig_no]++;
	if (signal_handlers[sig_no])
		signal_handlers[sig_no](sig_no);
}

static sigfunc *reset_one_signal (int sig_no, sigfunc *sig_handler)
{
	struct sigaction sa, osa;

	if (sig_no < 0)
		return NULL;			/* Signal not implemented */

	signal_handlers[sig_no] = NULL;

	sa.sa_handler = sig_handler;
	sigemptyset(&sa.sa_mask);
	sigaddset(&sa.sa_mask, sig_no);

	/* this is ugly, but the `correct' way.  i hate c. -mrg */
	sa.sa_flags = 0;
#if defined(SA_RESTART) || defined(SA_INTERRUPT)
	if (SIGALRM == sig_no || SIGINT == sig_no)
	{
# if defined(SA_INTERRUPT)
		sa.sa_flags |= SA_INTERRUPT;
# endif /* SA_INTERRUPT */
	}
	else
	{
# if defined(SA_RESTART)
		sa.sa_flags |= SA_RESTART;
# endif /* SA_RESTART */
	}
#endif /* SA_RESTART || SA_INTERRUPT */

	if (0 > sigaction(sig_no, &sa, &osa))
		return SIG_ERR;

	return osa.sa_handler;
}


/* hook_all_signals needs to be called in main() before my_signal()
 * if any signal hooks fail, it returns SIG_ERR, otherwise it returns
 * NULL. - pegasus
 */
sigfunc *	init_signals (void)
{
	int sig_no;
	sigfunc *error = NULL;

	memset((void *)&signals_caught, 0, NSIG * sizeof(int));

	for (sig_no = 1; sig_no < NSIG; sig_no++)
	{
		if ((reset_one_signal(sig_no, signal_handler)) == SIG_ERR)
			error = SIG_ERR;
	}
	return error;
}

sigfunc *	my_signal (int sig_no, sigfunc *sig_handler)
{
	sigfunc	*old;

	old = signal_handlers[sig_no];
	if (sig_handler == SIG_IGN || sig_handler == SIG_DFL)
		reset_one_signal(sig_no, sig_handler);
	else
	{
		reset_one_signal(sig_no, signal_handler);
		signal_handlers[sig_no] = (sigfunc *)sig_handler;
	}

	return old;
}