1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2006 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* |
---|
8 | * Event handling (using libevent) |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | This program is free software; you can redistribute it and/or modify |
---|
13 | it under the terms of the GNU General Public License as published by |
---|
14 | the Free Software Foundation; either version 2 of the License, or |
---|
15 | (at your option) any later version. |
---|
16 | |
---|
17 | This program is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
20 | GNU General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License with |
---|
23 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
24 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
25 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
26 | */ |
---|
27 | |
---|
28 | #define BITLBEE_CORE |
---|
29 | #include <stdio.h> |
---|
30 | #include <stdlib.h> |
---|
31 | #include <string.h> |
---|
32 | #include <unistd.h> |
---|
33 | #include <sys/types.h> |
---|
34 | #include <sys/time.h> |
---|
35 | #include <event.h> |
---|
36 | #include "proxy.h" |
---|
37 | |
---|
38 | static void b_main_restart(); |
---|
39 | static guint id_next = 1; /* Next ID to be allocated to an event handler. */ |
---|
40 | static guint id_cur = 0; /* Event ID that we're currently handling. */ |
---|
41 | static guint id_dead; /* Set to 1 if b_event_remove removes id_cur. */ |
---|
42 | static GHashTable *id_hash; |
---|
43 | static int quitting = 0; /* Prepare to quit, stop handling events. */ |
---|
44 | |
---|
45 | /* Since libevent doesn't handle two event handlers for one fd-condition |
---|
46 | very well (which happens sometimes when BitlBee changes event handlers |
---|
47 | for a combination), let's buid some indexes so we can delete them here |
---|
48 | already, just in time. */ |
---|
49 | static GHashTable *read_hash; |
---|
50 | static GHashTable *write_hash; |
---|
51 | |
---|
52 | struct event_base *leh; |
---|
53 | struct event_base *old_leh; |
---|
54 | |
---|
55 | struct b_event_data { |
---|
56 | guint id; |
---|
57 | struct event evinfo; |
---|
58 | gint timeout; |
---|
59 | b_event_handler function; |
---|
60 | void *data; |
---|
61 | guint flags; |
---|
62 | }; |
---|
63 | |
---|
64 | void b_main_init() |
---|
65 | { |
---|
66 | if (leh != NULL) { |
---|
67 | /* Clean up the hash tables? */ |
---|
68 | |
---|
69 | b_main_restart(); |
---|
70 | old_leh = leh; |
---|
71 | } |
---|
72 | |
---|
73 | leh = event_init(); |
---|
74 | |
---|
75 | id_hash = g_hash_table_new(g_int_hash, g_int_equal); |
---|
76 | read_hash = g_hash_table_new(g_int_hash, g_int_equal); |
---|
77 | write_hash = g_hash_table_new(g_int_hash, g_int_equal); |
---|
78 | } |
---|
79 | |
---|
80 | void b_main_run() |
---|
81 | { |
---|
82 | /* This while loop is necessary to exit the event loop and start a |
---|
83 | different one (necessary for ForkDaemon mode). */ |
---|
84 | while (event_base_dispatch(leh) == 0 && !quitting) { |
---|
85 | if (old_leh != NULL) { |
---|
86 | /* For some reason this just isn't allowed... |
---|
87 | Possibly a bug in older versions, will see later. |
---|
88 | event_base_free( old_leh ); */ |
---|
89 | old_leh = NULL; |
---|
90 | } |
---|
91 | |
---|
92 | event_debug("New event loop.\n"); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | static void b_main_restart() |
---|
97 | { |
---|
98 | struct timeval tv; |
---|
99 | |
---|
100 | memset(&tv, 0, sizeof(struct timeval)); |
---|
101 | event_base_loopexit(leh, &tv); |
---|
102 | |
---|
103 | event_debug("b_main_restart()\n"); |
---|
104 | } |
---|
105 | |
---|
106 | void b_main_quit() |
---|
107 | { |
---|
108 | /* Tell b_main_run() that it shouldn't restart the loop. Also, |
---|
109 | libevent sometimes generates events before really quitting, |
---|
110 | we want to stop them. */ |
---|
111 | quitting = 1; |
---|
112 | |
---|
113 | b_main_restart(); |
---|
114 | } |
---|
115 | |
---|
116 | void b_main_iteration() |
---|
117 | { |
---|
118 | event_base_loop(leh, EVLOOP_NONBLOCK); |
---|
119 | event_debug("b_main_iteration()\n"); |
---|
120 | } |
---|
121 | |
---|
122 | static void b_event_passthrough(int fd, short event, void *data) |
---|
123 | { |
---|
124 | struct b_event_data *b_ev = data; |
---|
125 | b_input_condition cond = 0; |
---|
126 | gboolean st; |
---|
127 | |
---|
128 | if (fd >= 0) { |
---|
129 | if (event & EV_READ) { |
---|
130 | cond |= B_EV_IO_READ; |
---|
131 | } |
---|
132 | if (event & EV_WRITE) { |
---|
133 | cond |= B_EV_IO_WRITE; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | event_debug("b_event_passthrough( %d, %d, 0x%x ) (%d)\n", fd, event, (int) data, b_ev->id); |
---|
138 | |
---|
139 | /* Since the called function might cancel this handler already |
---|
140 | (which free()s b_ev), we have to remember the ID here. */ |
---|
141 | id_cur = b_ev->id; |
---|
142 | id_dead = 0; |
---|
143 | |
---|
144 | if (quitting) { |
---|
145 | b_event_remove(id_cur); |
---|
146 | return; |
---|
147 | } |
---|
148 | |
---|
149 | st = b_ev->function(b_ev->data, fd, cond); |
---|
150 | if (id_dead) { |
---|
151 | /* This event was killed already, don't touch it! */ |
---|
152 | return; |
---|
153 | } else if (!st && !(b_ev->flags & B_EV_FLAG_FORCE_REPEAT)) { |
---|
154 | event_debug("Handler returned FALSE: "); |
---|
155 | b_event_remove(id_cur); |
---|
156 | } else if (fd == -1) { |
---|
157 | /* fd == -1 means it was a timer. These can't be auto-repeated |
---|
158 | so it has to be recreated every time. */ |
---|
159 | struct timeval tv; |
---|
160 | |
---|
161 | tv.tv_sec = b_ev->timeout / 1000; |
---|
162 | tv.tv_usec = (b_ev->timeout % 1000) * 1000; |
---|
163 | |
---|
164 | evtimer_add(&b_ev->evinfo, &tv); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | gint b_input_add(gint fd, b_input_condition condition, b_event_handler function, gpointer data) |
---|
169 | { |
---|
170 | struct b_event_data *b_ev; |
---|
171 | |
---|
172 | event_debug("b_input_add( %d, %d, 0x%x, 0x%x ) ", fd, condition, function, data); |
---|
173 | |
---|
174 | if ((condition & B_EV_IO_READ && (b_ev = g_hash_table_lookup(read_hash, &fd))) || |
---|
175 | (condition & B_EV_IO_WRITE && (b_ev = g_hash_table_lookup(write_hash, &fd)))) { |
---|
176 | /* We'll stick with this libevent entry, but give it a new BitlBee id. */ |
---|
177 | g_hash_table_remove(id_hash, &b_ev->id); |
---|
178 | |
---|
179 | event_debug("(replacing old handler (id = %d)) = %d\n", b_ev->id, id_next); |
---|
180 | |
---|
181 | b_ev->id = id_next++; |
---|
182 | b_ev->function = function; |
---|
183 | b_ev->data = data; |
---|
184 | } else { |
---|
185 | GIOCondition out_cond; |
---|
186 | |
---|
187 | event_debug("(new) = %d\n", id_next); |
---|
188 | |
---|
189 | b_ev = g_new0(struct b_event_data, 1); |
---|
190 | b_ev->id = id_next++; |
---|
191 | b_ev->function = function; |
---|
192 | b_ev->data = data; |
---|
193 | |
---|
194 | out_cond = EV_PERSIST; |
---|
195 | if (condition & B_EV_IO_READ) { |
---|
196 | out_cond |= EV_READ; |
---|
197 | } |
---|
198 | if (condition & B_EV_IO_WRITE) { |
---|
199 | out_cond |= EV_WRITE; |
---|
200 | } |
---|
201 | |
---|
202 | event_set(&b_ev->evinfo, fd, out_cond, b_event_passthrough, b_ev); |
---|
203 | event_add(&b_ev->evinfo, NULL); |
---|
204 | |
---|
205 | if (out_cond & EV_READ) { |
---|
206 | g_hash_table_insert(read_hash, &b_ev->evinfo.ev_fd, b_ev); |
---|
207 | } |
---|
208 | if (out_cond & EV_WRITE) { |
---|
209 | g_hash_table_insert(write_hash, &b_ev->evinfo.ev_fd, b_ev); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | b_ev->flags = condition; |
---|
214 | g_hash_table_insert(id_hash, &b_ev->id, b_ev); |
---|
215 | return b_ev->id; |
---|
216 | } |
---|
217 | |
---|
218 | /* TODO: Persistence for timers! */ |
---|
219 | gint b_timeout_add(gint timeout, b_event_handler function, gpointer data) |
---|
220 | { |
---|
221 | struct b_event_data *b_ev = g_new0(struct b_event_data, 1); |
---|
222 | struct timeval tv; |
---|
223 | |
---|
224 | b_ev->id = id_next++; |
---|
225 | b_ev->timeout = timeout; |
---|
226 | b_ev->function = function; |
---|
227 | b_ev->data = data; |
---|
228 | |
---|
229 | tv.tv_sec = timeout / 1000; |
---|
230 | tv.tv_usec = (timeout % 1000) * 1000; |
---|
231 | |
---|
232 | evtimer_set(&b_ev->evinfo, b_event_passthrough, b_ev); |
---|
233 | evtimer_add(&b_ev->evinfo, &tv); |
---|
234 | |
---|
235 | event_debug("b_timeout_add( %d, 0x%x, 0x%x ) = %d\n", timeout, function, data, b_ev->id); |
---|
236 | |
---|
237 | g_hash_table_insert(id_hash, &b_ev->id, b_ev); |
---|
238 | |
---|
239 | return b_ev->id; |
---|
240 | } |
---|
241 | |
---|
242 | void b_event_remove(gint id) |
---|
243 | { |
---|
244 | struct b_event_data *b_ev = g_hash_table_lookup(id_hash, &id); |
---|
245 | |
---|
246 | event_debug("b_event_remove( %d )\n", id); |
---|
247 | if (b_ev) { |
---|
248 | if (id == id_cur) { |
---|
249 | id_dead = TRUE; |
---|
250 | } |
---|
251 | |
---|
252 | g_hash_table_remove(id_hash, &b_ev->id); |
---|
253 | if (b_ev->evinfo.ev_fd >= 0) { |
---|
254 | if (b_ev->evinfo.ev_events & EV_READ) { |
---|
255 | g_hash_table_remove(read_hash, &b_ev->evinfo.ev_fd); |
---|
256 | } |
---|
257 | if (b_ev->evinfo.ev_events & EV_WRITE) { |
---|
258 | g_hash_table_remove(write_hash, &b_ev->evinfo.ev_fd); |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | event_del(&b_ev->evinfo); |
---|
263 | g_free(b_ev); |
---|
264 | } else { |
---|
265 | event_debug("Already removed?\n"); |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | void closesocket(int fd) |
---|
270 | { |
---|
271 | struct b_event_data *b_ev; |
---|
272 | |
---|
273 | /* Since epoll() (the main reason we use libevent) automatically removes sockets from |
---|
274 | the epoll() list when a socket gets closed and some modules have a habit of |
---|
275 | closing sockets before removing event handlers, our and libevent's administration |
---|
276 | get a little bit messed up. So this little function will remove the handlers |
---|
277 | properly before closing a socket. */ |
---|
278 | |
---|
279 | if ((b_ev = g_hash_table_lookup(read_hash, &fd))) { |
---|
280 | event_debug("Warning: fd %d still had a read event handler when shutting down.\n", fd); |
---|
281 | b_event_remove(b_ev->id); |
---|
282 | } |
---|
283 | if ((b_ev = g_hash_table_lookup(write_hash, &fd))) { |
---|
284 | event_debug("Warning: fd %d still had a write event handler when shutting down.\n", fd); |
---|
285 | b_event_remove(b_ev->id); |
---|
286 | } |
---|
287 | |
---|
288 | close(fd); |
---|
289 | } |
---|