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