Imported Upstream version 3.0
[debian/gnuradio] / usrp / host / lib / usrp_local_sighandler.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 /*
24  * This is actually the same as gr_local_signhandler, but with a different name.
25  * We don't have a common library to put this in, so...
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <usrp_local_sighandler.h>
33 #include <stdexcept>
34 #include <stdio.h>
35
36 usrp_local_sighandler::usrp_local_sighandler (int signum,
37                                               void (*new_handler)(int))
38   : d_signum (signum)
39 {
40 #ifdef HAVE_SIGACTION
41   struct sigaction new_action;
42   memset (&new_action, 0, sizeof (new_action));
43
44   new_action.sa_handler = new_handler;
45   sigemptyset (&new_action.sa_mask);
46   new_action.sa_flags = 0;
47
48   if (sigaction (d_signum, &new_action, &d_old_action) < 0){
49     perror ("sigaction (install new)");
50     throw std::runtime_error ("sigaction");
51   }
52 #endif
53 }
54
55 usrp_local_sighandler::~usrp_local_sighandler ()
56 {
57 #ifdef HAVE_SIGACTION
58   if (sigaction (d_signum, &d_old_action, 0) < 0){
59     perror ("sigaction (restore old)");
60     throw std::runtime_error ("sigaction");
61   }
62 #endif
63 }
64
65 void
66 usrp_local_sighandler::throw_signal(int signum) throw(usrp_signal)
67 {
68   throw usrp_signal (signum);
69 }
70
71 /*
72  * Semi-hideous way to may a signal number into a signal name
73  */
74
75 #define SIGNAME(x) case x: return #x
76
77 std::string
78 usrp_signal::name () const
79 {
80   char tmp[128];
81
82   switch (signal ()){
83 #ifdef SIGHUP
84     SIGNAME (SIGHUP);
85 #endif
86 #ifdef SIGINT
87     SIGNAME (SIGINT);
88 #endif
89 #ifdef SIGQUIT
90     SIGNAME (SIGQUIT);
91 #endif
92 #ifdef SIGILL
93     SIGNAME (SIGILL);
94 #endif
95 #ifdef SIGTRAP
96     SIGNAME (SIGTRAP);
97 #endif
98 #ifdef SIGABRT
99     SIGNAME (SIGABRT);
100 #endif
101 #ifdef SIGBUS
102     SIGNAME (SIGBUS);
103 #endif
104 #ifdef SIGFPE
105     SIGNAME (SIGFPE);
106 #endif
107 #ifdef SIGKILL
108     SIGNAME (SIGKILL);
109 #endif
110 #ifdef SIGUSR1
111     SIGNAME (SIGUSR1);
112 #endif
113 #ifdef SIGSEGV
114     SIGNAME (SIGSEGV);
115 #endif
116 #ifdef SIGUSR2
117     SIGNAME (SIGUSR2);
118 #endif
119 #ifdef SIGPIPE
120     SIGNAME (SIGPIPE);
121 #endif
122 #ifdef SIGALRM
123     SIGNAME (SIGALRM);
124 #endif
125 #ifdef SIGTERM
126     SIGNAME (SIGTERM);
127 #endif
128 #ifdef SIGSTKFLT
129     SIGNAME (SIGSTKFLT);
130 #endif
131 #ifdef SIGCHLD
132     SIGNAME (SIGCHLD);
133 #endif
134 #ifdef SIGCONT
135     SIGNAME (SIGCONT);
136 #endif
137 #ifdef SIGSTOP
138     SIGNAME (SIGSTOP);
139 #endif
140 #ifdef SIGTSTP
141     SIGNAME (SIGTSTP);
142 #endif
143 #ifdef SIGTTIN
144     SIGNAME (SIGTTIN);
145 #endif
146 #ifdef SIGTTOU
147     SIGNAME (SIGTTOU);
148 #endif
149 #ifdef SIGURG
150     SIGNAME (SIGURG);
151 #endif
152 #ifdef SIGXCPU
153     SIGNAME (SIGXCPU);
154 #endif
155 #ifdef SIGXFSZ
156     SIGNAME (SIGXFSZ);
157 #endif
158 #ifdef SIGVTALRM
159     SIGNAME (SIGVTALRM);
160 #endif
161 #ifdef SIGPROF
162     SIGNAME (SIGPROF);
163 #endif
164 #ifdef SIGWINCH
165     SIGNAME (SIGWINCH);
166 #endif
167 #ifdef SIGIO
168     SIGNAME (SIGIO);
169 #endif
170 #ifdef SIGPWR
171     SIGNAME (SIGPWR);
172 #endif
173 #ifdef SIGSYS
174     SIGNAME (SIGSYS);
175 #endif
176   default:
177 #if defined (HAVE_SNPRINTF)
178 #if defined (SIGRTMIN) && defined (SIGRTMAX) 
179     if (signal () >= SIGRTMIN && signal () <= SIGRTMAX){
180       snprintf (tmp, sizeof (tmp), "SIGRTMIN + %d", signal ());
181       return tmp;
182     }
183 #endif
184     snprintf (tmp, sizeof (tmp), "SIGNAL %d", signal ());
185     return tmp;
186 #else
187     return "Unknown signal";
188 #endif
189   }
190 }