BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file NCDRfkillMonitor.c
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  
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34  
35 #include <misc/debug.h>
36 #include <misc/nonblocking.h>
37 #include <base/BLog.h>
38  
39 #include "NCDRfkillMonitor.h"
40  
41 #include <generated/blog_channel_NCDRfkillMonitor.h>
42  
43 #define RFKILL_DEVICE_NODE "/dev/rfkill"
44  
45 static void rfkill_fd_handler (NCDRfkillMonitor *o, int events);
46  
47 void rfkill_fd_handler (NCDRfkillMonitor *o, int events)
48 {
49 DebugObject_Access(&o->d_obj);
50  
51 // read from netlink fd
52 struct rfkill_event event;
53 int len = read(o->rfkill_fd, &event, sizeof(event));
54 if (len < 0) {
55 BLog(BLOG_ERROR, "read failed");
56 return;
57 }
58 if (len != sizeof(event)) {
59 BLog(BLOG_ERROR, "read returned wrong length");
60 return;
61 }
62  
63 // call handler
64 o->handler(o->user, event);
65 return;
66 }
67  
68 int NCDRfkillMonitor_Init (NCDRfkillMonitor *o, BReactor *reactor, NCDRfkillMonitor_handler handler, void *user)
69 {
70 // init arguments
71 o->reactor = reactor;
72 o->handler = handler;
73 o->user = user;
74  
75 // open rfkill
76 if ((o->rfkill_fd = open(RFKILL_DEVICE_NODE, O_RDONLY)) < 0) {
77 BLog(BLOG_ERROR, "open failed");
78 goto fail0;
79 }
80  
81 // set fd non-blocking
82 if (!badvpn_set_nonblocking(o->rfkill_fd)) {
83 BLog(BLOG_ERROR, "badvpn_set_nonblocking failed");
84 goto fail1;
85 }
86  
87 // init BFileDescriptor
88 BFileDescriptor_Init(&o->bfd, o->rfkill_fd, (BFileDescriptor_handler)rfkill_fd_handler, o);
89 if (!BReactor_AddFileDescriptor(o->reactor, &o->bfd)) {
90 BLog(BLOG_ERROR, "BReactor_AddFileDescriptor failed");
91 goto fail1;
92 }
93 BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
94  
95 DebugObject_Init(&o->d_obj);
96 return 1;
97  
98 fail1:
99 if (close(o->rfkill_fd) < 0) {
100 BLog(BLOG_ERROR, "close failed");
101 }
102 fail0:
103 return 0;
104 }
105  
106 void NCDRfkillMonitor_Free (NCDRfkillMonitor *o)
107 {
108 DebugObject_Free(&o->d_obj);
109  
110 // free BFileDescriptor
111 BReactor_RemoveFileDescriptor(o->reactor, &o->bfd);
112  
113 // close rfkill
114 if (close(o->rfkill_fd) < 0) {
115 BLog(BLOG_ERROR, "close failed");
116 }
117 }