BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BThreadSignal.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 <errno.h> |
||
31 | #include <unistd.h> |
||
32 | |||
33 | #include <misc/debug.h> |
||
34 | #include <misc/nonblocking.h> |
||
35 | #include <base/BLog.h> |
||
36 | |||
37 | #include "BThreadSignal.h" |
||
38 | |||
39 | #include <generated/blog_channel_BThreadSignal.h> |
||
40 | |||
41 | static void bfd_handler (void *user, int events) |
||
42 | { |
||
43 | BThreadSignal *o = user; |
||
44 | DebugObject_Access(&o->d_obj); |
||
45 | |||
46 | char byte; |
||
47 | ssize_t res = read(o->pipe[0], &byte, sizeof(byte)); |
||
48 | |||
49 | if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | if (res < 0) { |
||
54 | BLog(BLOG_ERROR, "read failed"); |
||
55 | return; |
||
56 | } |
||
57 | |||
58 | if (res != sizeof(byte)) { |
||
59 | BLog(BLOG_ERROR, "bad read"); |
||
60 | return; |
||
61 | } |
||
62 | |||
63 | o->handler(o); |
||
64 | } |
||
65 | |||
66 | int BThreadSignal_Init (BThreadSignal *o, BReactor *reactor, BThreadSignal_handler handler) |
||
67 | { |
||
68 | o->reactor = reactor; |
||
69 | o->handler = handler; |
||
70 | |||
71 | if (pipe(o->pipe) < 0) { |
||
72 | BLog(BLOG_ERROR, "pipe failed"); |
||
73 | goto fail0; |
||
74 | } |
||
75 | |||
76 | if (!badvpn_set_nonblocking(o->pipe[0]) || !badvpn_set_nonblocking(o->pipe[1])) { |
||
77 | BLog(BLOG_ERROR, "badvpn_set_nonblocking failed"); |
||
78 | goto fail1; |
||
79 | } |
||
80 | |||
81 | BFileDescriptor_Init(&o->bfd, o->pipe[0], bfd_handler, o); |
||
82 | |||
83 | if (!BReactor_AddFileDescriptor(o->reactor, &o->bfd)) { |
||
84 | BLog(BLOG_ERROR, "BReactor_AddFileDescriptor failed"); |
||
85 | goto fail1; |
||
86 | } |
||
87 | |||
88 | BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ); |
||
89 | |||
90 | DebugObject_Init(&o->d_obj); |
||
91 | return 1; |
||
92 | |||
93 | fail1: |
||
94 | if (close(o->pipe[1]) < 0) { |
||
95 | BLog(BLOG_ERROR, "close failed"); |
||
96 | } |
||
97 | if (close(o->pipe[0]) < 0) { |
||
98 | BLog(BLOG_ERROR, "close failed"); |
||
99 | } |
||
100 | fail0: |
||
101 | return 0; |
||
102 | } |
||
103 | |||
104 | void BThreadSignal_Free (BThreadSignal *o) |
||
105 | { |
||
106 | DebugObject_Free(&o->d_obj); |
||
107 | |||
108 | BReactor_RemoveFileDescriptor(o->reactor, &o->bfd); |
||
109 | |||
110 | if (close(o->pipe[1]) < 0) { |
||
111 | BLog(BLOG_ERROR, "close failed"); |
||
112 | } |
||
113 | if (close(o->pipe[0]) < 0) { |
||
114 | BLog(BLOG_ERROR, "close failed"); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | int BThreadSignal_Thread_Signal (BThreadSignal *o) |
||
119 | { |
||
120 | DebugObject_Access(&o->d_obj); |
||
121 | |||
122 | char byte = 0; |
||
123 | ssize_t res = write(o->pipe[1], &byte, sizeof(byte)); |
||
124 | |||
125 | if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
||
126 | return 0; |
||
127 | } |
||
128 | |||
129 | if (res < 0) { |
||
130 | BLog(BLOG_ERROR, "write failed"); |
||
131 | } else if (res != sizeof(byte)) { |
||
132 | BLog(BLOG_ERROR, "bad write"); |
||
133 | } |
||
134 | |||
135 | return 1; |
||
136 | } |