Imported Upstream version 3.2.2
[debian/gnuradio] / usrp / host / lib / legacy / 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 3, 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 #include <string.h>
36
37 usrp_local_sighandler::usrp_local_sighandler (int signum,
38                                               void (*new_handler)(int))
39   : d_signum (signum)
40 {
41 #ifdef HAVE_SIGACTION
42   struct sigaction new_action;
43   memset (&new_action, 0, sizeof (new_action));
44
45   new_action.sa_handler = new_handler;
46   sigemptyset (&new_action.sa_mask);
47   new_action.sa_flags = 0;
48
49   if (sigaction (d_signum, &new_action, &d_old_action) < 0){
50     perror ("sigaction (install new)");
51     throw std::runtime_error ("sigaction");
52   }
53 #endif
54 }
55
56 usrp_local_sighandler::~usrp_local_sighandler ()
57 {
58 #ifdef HAVE_SIGACTION
59   if (sigaction (d_signum, &d_old_action, 0) < 0){
60     perror ("sigaction (restore old)");
61     throw std::runtime_error ("sigaction");
62   }
63 #endif
64 }
65
66 void
67 usrp_local_sighandler::throw_signal(int signum) throw(usrp_signal)
68 {
69   throw usrp_signal (signum);
70 }
71
72 /*
73  * Semi-hideous way to may a signal number into a signal name
74  */
75
76 #define SIGNAME(x) case x: return #x
77
78 std::string
79 usrp_signal::name () const
80 {
81   char tmp[128];
82
83   switch (signal ()){
84 #ifdef SIGHUP
85     SIGNAME (SIGHUP);
86 #endif
87 #ifdef SIGINT
88     SIGNAME (SIGINT);
89 #endif
90 #ifdef SIGQUIT
91     SIGNAME (SIGQUIT);
92 #endif
93 #ifdef SIGILL
94     SIGNAME (SIGILL);
95 #endif
96 #ifdef SIGTRAP
97     SIGNAME (SIGTRAP);
98 #endif
99 #ifdef SIGABRT
100     SIGNAME (SIGABRT);
101 #endif
102 #ifdef SIGBUS
103     SIGNAME (SIGBUS);
104 #endif
105 #ifdef SIGFPE
106     SIGNAME (SIGFPE);
107 #endif
108 #ifdef SIGKILL
109     SIGNAME (SIGKILL);
110 #endif
111 #ifdef SIGUSR1
112     SIGNAME (SIGUSR1);
113 #endif
114 #ifdef SIGSEGV
115     SIGNAME (SIGSEGV);
116 #endif
117 #ifdef SIGUSR2
118     SIGNAME (SIGUSR2);
119 #endif
120 #ifdef SIGPIPE
121     SIGNAME (SIGPIPE);
122 #endif
123 #ifdef SIGALRM
124     SIGNAME (SIGALRM);
125 #endif
126 #ifdef SIGTERM
127     SIGNAME (SIGTERM);
128 #endif
129 #ifdef SIGSTKFLT
130     SIGNAME (SIGSTKFLT);
131 #endif
132 #ifdef SIGCHLD
133     SIGNAME (SIGCHLD);
134 #endif
135 #ifdef SIGCONT
136     SIGNAME (SIGCONT);
137 #endif
138 #ifdef SIGSTOP
139     SIGNAME (SIGSTOP);
140 #endif
141 #ifdef SIGTSTP
142     SIGNAME (SIGTSTP);
143 #endif
144 #ifdef SIGTTIN
145     SIGNAME (SIGTTIN);
146 #endif
147 #ifdef SIGTTOU
148     SIGNAME (SIGTTOU);
149 #endif
150 #ifdef SIGURG
151     SIGNAME (SIGURG);
152 #endif
153 #ifdef SIGXCPU
154     SIGNAME (SIGXCPU);
155 #endif
156 #ifdef SIGXFSZ
157     SIGNAME (SIGXFSZ);
158 #endif
159 #ifdef SIGVTALRM
160     SIGNAME (SIGVTALRM);
161 #endif
162 #ifdef SIGPROF
163     SIGNAME (SIGPROF);
164 #endif
165 #ifdef SIGWINCH
166     SIGNAME (SIGWINCH);
167 #endif
168 #ifdef SIGIO
169     SIGNAME (SIGIO);
170 #endif
171 #ifdef SIGPWR
172     SIGNAME (SIGPWR);
173 #endif
174 #ifdef SIGSYS
175     SIGNAME (SIGSYS);
176 #endif
177   default:
178 #if defined (HAVE_SNPRINTF)
179 #if defined (SIGRTMIN) && defined (SIGRTMAX) 
180     if (signal () >= SIGRTMIN && signal () <= SIGRTMAX){
181       snprintf (tmp, sizeof (tmp), "SIGRTMIN + %d", signal ());
182       return tmp;
183     }
184 #endif
185     snprintf (tmp, sizeof (tmp), "SIGNAL %d", signal ());
186     return tmp;
187 #else
188     return "Unknown signal";
189 #endif
190   }
191 }