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
17 * along with Quagga; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #ifdef HAVE_UCONTEXT_H
30 /* get REG_EIP from ucontext.h */
33 #endif /* __USE_GNU */
34 #endif /* GNU_LINUX */
36 #endif /* HAVE_UCONTEXT_H */
37 #endif /* SA_SIGINFO */
40 /* master signals descriptor struct */
41 struct quagga_sigevent_master_t
45 struct quagga_signal_t *signals;
48 volatile sig_atomic_t caught;
51 /* Generic signal handler
52 * Schedules signal event thread
55 quagga_signal_handler (int signo)
58 struct quagga_signal_t *sig;
60 for (i = 0; i < sigmaster.sigc; i++)
62 sig = &(sigmaster.signals[i]);
64 if (sig->signal == signo)
71 /* check if signals have been caught and run appropriate handlers */
73 quagga_sigevent_process (void)
75 struct quagga_signal_t *sig;
77 #ifdef SIGEVENT_BLOCK_SIGNALS
78 /* shouldnt need to block signals, but potentially may be needed */
79 sigset_t newmask, oldmask;
82 * Block most signals, but be careful not to defer SIGTRAP because
83 * doing so breaks gdb, at least on NetBSD 2.0. Avoid asking to
84 * block SIGKILL, just because we shouldn't be able to do so.
86 sigfillset (&newmask);
87 sigdelset (&newmask, SIGTRAP);
88 sigdelset (&newmask, SIGKILL);
90 if ( (sigprocmask (SIG_BLOCK, &newmask, &oldmask)) < 0)
92 zlog_err ("quagga_signal_timer: couldnt block signals!");
95 #endif /* SIGEVENT_BLOCK_SIGNALS */
97 if (sigmaster.caught > 0)
100 /* must not read or set sigmaster.caught after here,
101 * race condition with per-sig caught flags if one does
104 for (i = 0; i < sigmaster.sigc; i++)
106 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);
134 sigm->t = thread_add_timer (sigm->t->master, quagga_signal_timer, &sigmaster,
135 QUAGGA_SIGNAL_TIMER_INTERVAL);
136 return quagga_sigevent_process ();
138 #endif /* SIGEVENT_SCHEDULE_THREAD */
140 /* Initialization of signal handles. */
141 /* Signal wrapper. */
143 signal_set (int signo)
146 struct sigaction sig;
147 struct sigaction osig;
149 sig.sa_handler = &quagga_signal_handler;
150 sigfillset (&sig.sa_mask);
152 if (signo == SIGALRM) {
154 sig.sa_flags |= SA_INTERRUPT; /* SunOS */
158 sig.sa_flags |= SA_RESTART;
159 #endif /* SA_RESTART */
162 ret = sigaction (signo, &sig, &osig);
171 /* XXX This function should be enhanced to support more platforms
172 (it currently works only on Linux/x86). */
174 program_counter(void *context)
176 #ifdef HAVE_UCONTEXT_H
178 /* these are from GNU libc, rather than Linux, strictly speaking */
179 # if defined(REG_EIP)
180 # define REG_INDEX REG_EIP
181 # elif defined(REG_RIP)
182 # define REG_INDEX REG_RIP
183 # elif defined(__powerpc__)
184 # define REG_INDEX 32
186 #elif defined(SUNOS_5) /* !GNU_LINUX */
187 # define REG_INDEX REG_PC
188 #endif /* GNU_LINUX */
191 # ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS
192 # define REGS gregs[REG_INDEX]
193 # elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_UC_REGS)
194 # define REGS uc_regs->gregs[REG_INDEX]
195 # endif /* HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS */
196 #endif /* REG_INDEX */
200 return (void *)(((ucontext_t *)context)->uc_mcontext.REGS);
201 #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_REGS__NIP)
202 /* older Linux / struct pt_regs ? */
204 return (void *)(((ucontext_t *)context)->uc_mcontext.regs->nip);
207 #endif /* HAVE_UCONTEXT_H */
211 #endif /* SA_SIGINFO */
213 static void __attribute__ ((noreturn))
214 exit_handler(int signo
216 , siginfo_t *siginfo, void *context
220 zlog_signal(signo, "exiting..."
222 , siginfo, program_counter(context)
228 static void __attribute__ ((noreturn))
229 core_handler(int signo
231 , siginfo_t *siginfo, void *context
235 zlog_signal(signo, "aborting..."
237 , siginfo, program_counter(context)
240 /* dump memory stats on core */
241 log_memstats_stderr ("core_handler");
246 trap_default_signals(void)
248 static const int core_signals[] = {
267 static const int exit_signals[] = {
284 static const int ignore_signals[] = {
287 static const struct {
290 void (*handler)(int signo
292 , siginfo_t *info, void *context
296 { core_signals, array_size(core_signals), core_handler},
297 { exit_signals, array_size(exit_signals), exit_handler},
298 { ignore_signals, array_size(ignore_signals), NULL},
302 for (i = 0; i < array_size(sigmap); i++)
306 for (j = 0; j < sigmap[i].nsigs; j++)
308 struct sigaction oact;
309 if ((sigaction(sigmap[i].sigs[j],NULL,&oact) == 0) &&
310 (oact.sa_handler == SIG_DFL))
312 struct sigaction act;
313 sigfillset (&act.sa_mask);
314 if (sigmap[i].handler == NULL)
316 act.sa_handler = SIG_IGN;
322 /* Request extra arguments to signal handler. */
323 act.sa_sigaction = sigmap[i].handler;
324 act.sa_flags = SA_SIGINFO;
326 act.sa_handler = sigmap[i].handler;
330 if (sigaction(sigmap[i].sigs[j],&act,NULL) < 0)
331 zlog_warn("Unable to set signal handler for signal %d: %s",
332 sigmap[i].sigs[j],safe_strerror(errno));
340 signal_init (struct thread_master *m, int sigc,
341 struct quagga_signal_t signals[])
345 struct quagga_signal_t *sig;
347 /* First establish some default handlers that can be overridden by
349 trap_default_signals();
354 if ( signal_set (sig->signal) < 0 )
359 sigmaster.sigc = sigc;
360 sigmaster.signals = signals;
362 #ifdef SIGEVENT_SCHEDULE_THREAD
364 thread_add_timer (m, quagga_signal_timer, &sigmaster,
365 QUAGGA_SIGNAL_TIMER_INTERVAL);
366 #endif /* SIGEVENT_SCHEDULE_THREAD */