1 /* Quagga signal handling functions.
2 * Copyright (C) 2004 Paul Jakma,
4 * This file is part of Quagga.
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #ifdef HAVE_UCONTEXT_H
29 /* get REG_EIP from ucontext.h */
32 #endif /* __USE_GNU */
33 #endif /* GNU_LINUX */
35 #endif /* HAVE_UCONTEXT_H */
36 #endif /* SA_SIGINFO */
39 /* master signals descriptor struct */
40 struct quagga_sigevent_master_t
44 struct quagga_signal_t
*signals
;
47 volatile sig_atomic_t caught
;
50 /* Generic signal handler
51 * Schedules signal event thread
54 quagga_signal_handler (int signo
)
57 struct quagga_signal_t
*sig
;
59 for (i
= 0; i
< sigmaster
.sigc
; i
++)
61 sig
= &(sigmaster
.signals
[i
]);
63 if (sig
->signal
== signo
)
70 /* check if signals have been caught and run appropriate handlers */
72 quagga_sigevent_process (void)
74 struct quagga_signal_t
*sig
;
76 #ifdef SIGEVENT_BLOCK_SIGNALS
77 /* shouldnt need to block signals, but potentially may be needed */
78 sigset_t newmask
, oldmask
;
81 * Block most signals, but be careful not to defer SIGTRAP because
82 * doing so breaks gdb, at least on NetBSD 2.0. Avoid asking to
83 * block SIGKILL, just because we shouldn't be able to do so.
85 sigfillset (&newmask
);
86 sigdelset (&newmask
, SIGTRAP
);
87 sigdelset (&newmask
, SIGKILL
);
89 if ( (sigprocmask (SIG_BLOCK
, &newmask
, &oldmask
)) < 0)
91 zlog_err ("quagga_signal_timer: couldnt block signals!");
94 #endif /* SIGEVENT_BLOCK_SIGNALS */
96 if (sigmaster
.caught
> 0)
99 /* must not read or set sigmaster.caught after here,
100 * race condition with per-sig caught flags if one does
103 for (i
= 0; i
< sigmaster
.sigc
; i
++)
105 sig
= &(sigmaster
.signals
[i
]);
116 #ifdef SIGEVENT_BLOCK_SIGNALS
117 if ( sigprocmask (SIG_UNBLOCK
, &oldmask
, NULL
) < 0 );
119 #endif /* SIGEVENT_BLOCK_SIGNALS */
124 #ifdef SIGEVENT_SCHEDULE_THREAD
125 /* timer thread to check signals. Shouldnt be needed */
127 quagga_signal_timer (struct thread
*t
)
129 struct quagga_sigevent_master_t
*sigm
;
130 struct quagga_signal_t
*sig
;
133 sigm
= THREAD_ARG (t
);
135 thread_add_timer(sigm
->t
->master
, quagga_signal_timer
, &sigmaster
, QUAGGA_SIGNAL_TIMER_INTERVAL
,
137 return quagga_sigevent_process ();
139 #endif /* SIGEVENT_SCHEDULE_THREAD */
141 /* Initialization of signal handles. */
142 /* Signal wrapper. */
144 signal_set (int signo
)
147 struct sigaction sig
;
148 struct sigaction osig
;
150 sig
.sa_handler
= &quagga_signal_handler
;
151 sigfillset (&sig
.sa_mask
);
153 if (signo
== SIGALRM
) {
155 sig
.sa_flags
|= SA_INTERRUPT
; /* SunOS */
159 sig
.sa_flags
|= SA_RESTART
;
160 #endif /* SA_RESTART */
163 ret
= sigaction (signo
, &sig
, &osig
);
172 /* XXX This function should be enhanced to support more platforms
173 (it currently works only on Linux/x86). */
175 program_counter(void *context
)
177 #ifdef HAVE_UCONTEXT_H
179 /* these are from GNU libc, rather than Linux, strictly speaking */
180 # if defined(REG_EIP)
181 # define REG_INDEX REG_EIP
182 # elif defined(REG_RIP)
183 # define REG_INDEX REG_RIP
184 # elif defined(__powerpc__)
185 # define REG_INDEX 32
187 #elif defined(SUNOS_5) /* !GNU_LINUX */
188 # define REG_INDEX REG_PC
189 #endif /* GNU_LINUX */
192 # ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS
193 # define REGS gregs[REG_INDEX]
194 # elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_UC_REGS)
195 # define REGS uc_regs->gregs[REG_INDEX]
196 # endif /* HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS */
197 #endif /* REG_INDEX */
201 return (void *)(((ucontext_t
*)context
)->uc_mcontext
.REGS
);
202 #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_REGS__NIP)
203 /* older Linux / struct pt_regs ? */
205 return (void *)(((ucontext_t
*)context
)->uc_mcontext
.regs
->nip
);
208 #endif /* HAVE_UCONTEXT_H */
212 #endif /* SA_SIGINFO */
214 static void __attribute__ ((noreturn
))
215 exit_handler(int signo
217 , siginfo_t
*siginfo
, void *context
221 zlog_signal(signo
, "exiting..."
223 , siginfo
, program_counter(context
)
229 static void __attribute__ ((noreturn
))
230 core_handler(int signo
232 , siginfo_t
*siginfo
, void *context
236 /* make sure we don't hang in here. default for SIGALRM is terminate.
237 * - if we're in backtrace for more than a second, abort. */
238 struct sigaction sa_default
= { .sa_handler
= SIG_DFL
};
239 sigaction (SIGALRM
, &sa_default
, NULL
);
242 sigemptyset (&sigset
);
243 sigaddset (&sigset
, SIGALRM
);
244 sigprocmask (SIG_UNBLOCK
, &sigset
, NULL
);
248 zlog_signal(signo
, "aborting..."
250 , siginfo
, program_counter(context
)
253 /* dump memory stats on core */
254 log_memstats_stderr ("core_handler");
259 trap_default_signals(void)
261 static const int core_signals
[] = {
280 static const int exit_signals
[] = {
297 static const int ignore_signals
[] = {
300 static const struct {
303 void (*handler
)(int signo
305 , siginfo_t
*info
, void *context
309 { core_signals
, array_size(core_signals
), core_handler
},
310 { exit_signals
, array_size(exit_signals
), exit_handler
},
311 { ignore_signals
, array_size(ignore_signals
), NULL
},
315 for (i
= 0; i
< array_size(sigmap
); i
++)
319 for (j
= 0; j
< sigmap
[i
].nsigs
; j
++)
321 struct sigaction oact
;
322 if ((sigaction(sigmap
[i
].sigs
[j
],NULL
,&oact
) == 0) &&
323 (oact
.sa_handler
== SIG_DFL
))
325 struct sigaction act
;
326 sigfillset (&act
.sa_mask
);
327 if (sigmap
[i
].handler
== NULL
)
329 act
.sa_handler
= SIG_IGN
;
335 /* Request extra arguments to signal handler. */
336 act
.sa_sigaction
= sigmap
[i
].handler
;
337 act
.sa_flags
= SA_SIGINFO
;
339 act
.sa_handler
= sigmap
[i
].handler
;
343 /* don't try to print backtraces recursively */
344 if (sigmap
[i
].handler
== core_handler
)
345 act
.sa_flags
|= SA_RESETHAND
;
348 if (sigaction(sigmap
[i
].sigs
[j
],&act
,NULL
) < 0)
349 zlog_warn("Unable to set signal handler for signal %d: %s",
350 sigmap
[i
].sigs
[j
],safe_strerror(errno
));
358 signal_init (struct thread_master
*m
, int sigc
,
359 struct quagga_signal_t signals
[])
363 struct quagga_signal_t
*sig
;
365 /* First establish some default handlers that can be overridden by
367 trap_default_signals();
372 if ( signal_set (sig
->signal
) < 0 )
377 sigmaster
.sigc
= sigc
;
378 sigmaster
.signals
= signals
;
380 #ifdef SIGEVENT_SCHEDULE_THREAD
382 thread_add_timer(m
, quagga_signal_timer
, &sigmaster
, QUAGGA_SIGNAL_TIMER_INTERVAL
,
384 #endif /* SIGEVENT_SCHEDULE_THREAD */