BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file BUnixSignal.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Object for catching unix signals.
32 */
33  
34 #ifndef BADVPN_SYSTEM_BUNIXSIGNAL_H
35 #define BADVPN_SYSTEM_BUNIXSIGNAL_H
36  
37 #if (defined(BADVPN_USE_SIGNALFD) + defined(BADVPN_USE_KEVENT) + defined(BADVPN_USE_SELFPIPE)) != 1
38 #error Unknown signal backend or too many signal backends
39 #endif
40  
41 #include <unistd.h>
42 #include <signal.h>
43  
44 #include <misc/debug.h>
45 #include <system/BReactor.h>
46 #include <base/DebugObject.h>
47  
48 struct BUnixSignal_s;
49  
50 /**
51 * Handler function called when a signal is received.
52 *
53 * @param user as in {@link BUnixSignal_Init}
54 * @param signo signal number. Will be one of the signals provided to {@link signals}.
55 */
56 typedef void (*BUnixSignal_handler) (void *user, int signo);
57  
58 #ifdef BADVPN_USE_KEVENT
59 struct BUnixSignal_kevent_entry {
60 struct BUnixSignal_s *parent;
61 int signo;
62 BReactorKEvent kevent;
63 };
64 #endif
65  
66 #ifdef BADVPN_USE_SELFPIPE
67 struct BUnixSignal_selfpipe_entry {
68 struct BUnixSignal_s *parent;
69 int signo;
70 int pipefds[2];
71 BFileDescriptor pipe_read_bfd;
72 };
73 #endif
74  
75 /**
76 * Object for catching unix signals.
77 */
78 typedef struct BUnixSignal_s {
79 BReactor *reactor;
80 sigset_t signals;
81 BUnixSignal_handler handler;
82 void *user;
83  
84 #ifdef BADVPN_USE_SIGNALFD
85 int signalfd_fd;
86 BFileDescriptor signalfd_bfd;
87 #endif
88  
89 #ifdef BADVPN_USE_KEVENT
90 struct BUnixSignal_kevent_entry *entries;
91 int num_entries;
92 #endif
93  
94 #ifdef BADVPN_USE_SELFPIPE
95 struct BUnixSignal_selfpipe_entry *entries;
96 int num_entries;
97 #endif
98  
99 DebugObject d_obj;
100 } BUnixSignal;
101  
102 /**
103 * Initializes the object.
104 * {@link BLog_Init} must have been done.
105 *
106 * WARNING: for every signal number there should be at most one {@link BUnixSignal}
107 * object handling it (or anything else that could interfere).
108 *
109 * This blocks the signal using pthread_sigmask() and sets up signalfd() for receiving
110 * signals.
111 *
112 * @param o the object
113 * @param reactor reactor we live in
114 * @param signals signals to handle. See man 3 sigsetops.
115 * @param handler handler function to call when a signal is received
116 * @param user value passed to callback function
117 * @return 1 on success, 0 on failure
118 */
119 int BUnixSignal_Init (BUnixSignal *o, BReactor *reactor, sigset_t signals, BUnixSignal_handler handler, void *user) WARN_UNUSED;
120  
121 /**
122 * Frees the object.
123 *
124 * @param o the object
125 * @param unblock whether to unblock the signals using pthread_sigmask(). Not unblocking it
126 * can be used while the program is exiting gracefully to prevent the
127 * signals from being handled handled according to its default disposition
128 * after this function is called. Must be 0 or 1.
129 */
130 void BUnixSignal_Free (BUnixSignal *o, int unblock);
131  
132 #endif