[13cc96c] | 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; |
---|
[6f10697] | 24 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 25 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[13cc96c] | 26 | */ |
---|
| 27 | |
---|
| 28 | #define BITLBEE_CORE |
---|
| 29 | #include <stdio.h> |
---|
| 30 | #include <stdlib.h> |
---|
| 31 | #include <string.h> |
---|
[3f199fc] | 32 | #include <unistd.h> |
---|
[13cc96c] | 33 | #include <sys/types.h> |
---|
| 34 | #include <sys/time.h> |
---|
| 35 | #include <event.h> |
---|
[9ff5737] | 36 | #include "proxy.h" |
---|
[13cc96c] | 37 | |
---|
[b6a2373] | 38 | static void b_main_restart(); |
---|
[939370c] | 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. */ |
---|
[13cc96c] | 42 | static GHashTable *id_hash; |
---|
[939370c] | 43 | static int quitting = 0; /* Prepare to quit, stop handling events. */ |
---|
[13cc96c] | 44 | |
---|
[782d988] | 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 | |
---|
[b6a2373] | 52 | struct event_base *leh; |
---|
| 53 | struct event_base *old_leh; |
---|
| 54 | |
---|
[13cc96c] | 55 | struct b_event_data |
---|
| 56 | { |
---|
| 57 | guint id; |
---|
| 58 | struct event evinfo; |
---|
[19ac9c5] | 59 | gint timeout; |
---|
[13cc96c] | 60 | b_event_handler function; |
---|
| 61 | void *data; |
---|
[c5c18c1] | 62 | guint flags; |
---|
[13cc96c] | 63 | }; |
---|
| 64 | |
---|
| 65 | void b_main_init() |
---|
| 66 | { |
---|
[b6a2373] | 67 | if( leh != NULL ) |
---|
| 68 | { |
---|
| 69 | /* Clean up the hash tables? */ |
---|
| 70 | |
---|
| 71 | b_main_restart(); |
---|
| 72 | old_leh = leh; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | leh = event_init(); |
---|
[13cc96c] | 76 | |
---|
| 77 | id_hash = g_hash_table_new( g_int_hash, g_int_equal ); |
---|
[782d988] | 78 | read_hash = g_hash_table_new( g_int_hash, g_int_equal ); |
---|
| 79 | write_hash = g_hash_table_new( g_int_hash, g_int_equal ); |
---|
[13cc96c] | 80 | } |
---|
| 81 | |
---|
| 82 | void b_main_run() |
---|
| 83 | { |
---|
[b6a2373] | 84 | /* This while loop is necessary to exit the event loop and start a |
---|
| 85 | different one (necessary for ForkDaemon mode). */ |
---|
| 86 | while( event_base_dispatch( leh ) == 0 && !quitting ) |
---|
| 87 | { |
---|
| 88 | if( old_leh != NULL ) |
---|
| 89 | { |
---|
| 90 | /* For some reason this just isn't allowed... |
---|
| 91 | Possibly a bug in older versions, will see later. |
---|
| 92 | event_base_free( old_leh ); */ |
---|
| 93 | old_leh = NULL; |
---|
| 94 | } |
---|
| 95 | |
---|
[9ff5737] | 96 | event_debug( "New event loop.\n" ); |
---|
[b6a2373] | 97 | } |
---|
[13cc96c] | 98 | } |
---|
| 99 | |
---|
[b6a2373] | 100 | static void b_main_restart() |
---|
[13cc96c] | 101 | { |
---|
| 102 | struct timeval tv; |
---|
| 103 | |
---|
[b6a2373] | 104 | memset( &tv, 0, sizeof( struct timeval ) ); |
---|
| 105 | event_base_loopexit( leh, &tv ); |
---|
| 106 | |
---|
[9ff5737] | 107 | event_debug( "b_main_restart()\n" ); |
---|
[b6a2373] | 108 | } |
---|
| 109 | |
---|
| 110 | void b_main_quit() |
---|
| 111 | { |
---|
| 112 | /* Tell b_main_run() that it shouldn't restart the loop. Also, |
---|
| 113 | libevent sometimes generates events before really quitting, |
---|
[cdca30b] | 114 | we want to stop them. */ |
---|
| 115 | quitting = 1; |
---|
| 116 | |
---|
[b6a2373] | 117 | b_main_restart(); |
---|
[13cc96c] | 118 | } |
---|
| 119 | |
---|
| 120 | static void b_event_passthrough( int fd, short event, void *data ) |
---|
| 121 | { |
---|
| 122 | struct b_event_data *b_ev = data; |
---|
| 123 | b_input_condition cond = 0; |
---|
[939370c] | 124 | gboolean st; |
---|
[09f8cd1] | 125 | |
---|
| 126 | if( fd >= 0 ) |
---|
| 127 | { |
---|
| 128 | if( event & EV_READ ) |
---|
[e046390] | 129 | cond |= B_EV_IO_READ; |
---|
[09f8cd1] | 130 | if( event & EV_WRITE ) |
---|
[e046390] | 131 | cond |= B_EV_IO_WRITE; |
---|
[09f8cd1] | 132 | } |
---|
| 133 | |
---|
[b642f381] | 134 | event_debug( "b_event_passthrough( %d, %d, 0x%x ) (%d)\n", fd, event, (int) data, b_ev->id ); |
---|
| 135 | |
---|
[fc2ee0f] | 136 | /* Since the called function might cancel this handler already |
---|
[04026d4] | 137 | (which free()s b_ev), we have to remember the ID here. */ |
---|
[939370c] | 138 | id_cur = b_ev->id; |
---|
| 139 | id_dead = 0; |
---|
[fc2ee0f] | 140 | |
---|
[cdca30b] | 141 | if( quitting ) |
---|
| 142 | { |
---|
[939370c] | 143 | b_event_remove( id_cur ); |
---|
[cdca30b] | 144 | return; |
---|
| 145 | } |
---|
| 146 | |
---|
[939370c] | 147 | st = b_ev->function( b_ev->data, fd, cond ); |
---|
| 148 | if( id_dead ) |
---|
| 149 | { |
---|
| 150 | /* This event was killed already, don't touch it! */ |
---|
| 151 | return; |
---|
| 152 | } |
---|
[c5c18c1] | 153 | else if( !st && !( b_ev->flags & B_EV_FLAG_FORCE_REPEAT ) ) |
---|
[b642f381] | 154 | { |
---|
| 155 | event_debug( "Handler returned FALSE: " ); |
---|
[939370c] | 156 | b_event_remove( id_cur ); |
---|
[b642f381] | 157 | } |
---|
[19ac9c5] | 158 | else if( fd == -1 ) |
---|
| 159 | { |
---|
[939370c] | 160 | /* fd == -1 means it was a timer. These can't be auto-repeated |
---|
| 161 | so it has to be recreated every time. */ |
---|
[19ac9c5] | 162 | struct timeval tv; |
---|
| 163 | |
---|
| 164 | tv.tv_sec = b_ev->timeout / 1000; |
---|
| 165 | tv.tv_usec = ( b_ev->timeout % 1000 ) * 1000; |
---|
| 166 | |
---|
| 167 | evtimer_add( &b_ev->evinfo, &tv ); |
---|
| 168 | } |
---|
[13cc96c] | 169 | } |
---|
| 170 | |
---|
[b642f381] | 171 | gint b_input_add( gint fd, b_input_condition condition, b_event_handler function, gpointer data ) |
---|
[13cc96c] | 172 | { |
---|
[782d988] | 173 | struct b_event_data *b_ev; |
---|
[13cc96c] | 174 | |
---|
[782d988] | 175 | event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) ", fd, condition, function, data ); |
---|
[13cc96c] | 176 | |
---|
[e046390] | 177 | if( ( condition & B_EV_IO_READ && ( b_ev = g_hash_table_lookup( read_hash, &fd ) ) ) || |
---|
| 178 | ( condition & B_EV_IO_WRITE && ( b_ev = g_hash_table_lookup( write_hash, &fd ) ) ) ) |
---|
[782d988] | 179 | { |
---|
| 180 | /* We'll stick with this libevent entry, but give it a new BitlBee id. */ |
---|
| 181 | g_hash_table_remove( id_hash, &b_ev->id ); |
---|
| 182 | |
---|
| 183 | event_debug( "(replacing old handler (id = %d)) = %d\n", b_ev->id, id_next ); |
---|
| 184 | |
---|
| 185 | b_ev->id = id_next++; |
---|
| 186 | b_ev->function = function; |
---|
| 187 | b_ev->data = data; |
---|
| 188 | } |
---|
| 189 | else |
---|
| 190 | { |
---|
| 191 | GIOCondition out_cond; |
---|
| 192 | |
---|
| 193 | event_debug( "(new) = %d\n", id_next ); |
---|
| 194 | |
---|
| 195 | b_ev = g_new0( struct b_event_data, 1 ); |
---|
| 196 | b_ev->id = id_next++; |
---|
| 197 | b_ev->function = function; |
---|
| 198 | b_ev->data = data; |
---|
| 199 | |
---|
| 200 | out_cond = EV_PERSIST; |
---|
[e046390] | 201 | if( condition & B_EV_IO_READ ) |
---|
[782d988] | 202 | out_cond |= EV_READ; |
---|
[e046390] | 203 | if( condition & B_EV_IO_WRITE ) |
---|
[782d988] | 204 | out_cond |= EV_WRITE; |
---|
| 205 | |
---|
| 206 | event_set( &b_ev->evinfo, fd, out_cond, b_event_passthrough, b_ev ); |
---|
| 207 | event_add( &b_ev->evinfo, NULL ); |
---|
| 208 | |
---|
| 209 | if( out_cond & EV_READ ) |
---|
| 210 | g_hash_table_insert( read_hash, &b_ev->evinfo.ev_fd, b_ev ); |
---|
| 211 | if( out_cond & EV_WRITE ) |
---|
| 212 | g_hash_table_insert( write_hash, &b_ev->evinfo.ev_fd, b_ev ); |
---|
| 213 | } |
---|
[b642f381] | 214 | |
---|
[c5c18c1] | 215 | b_ev->flags = condition; |
---|
[13cc96c] | 216 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
| 217 | return b_ev->id; |
---|
| 218 | } |
---|
| 219 | |
---|
[09f8cd1] | 220 | /* TODO: Persistence for timers! */ |
---|
[13cc96c] | 221 | gint b_timeout_add( gint timeout, b_event_handler function, gpointer data ) |
---|
| 222 | { |
---|
| 223 | struct b_event_data *b_ev = g_new0( struct b_event_data, 1 ); |
---|
| 224 | struct timeval tv; |
---|
| 225 | |
---|
[09f8cd1] | 226 | b_ev->id = id_next++; |
---|
[19ac9c5] | 227 | b_ev->timeout = timeout; |
---|
[13cc96c] | 228 | b_ev->function = function; |
---|
| 229 | b_ev->data = data; |
---|
| 230 | |
---|
| 231 | tv.tv_sec = timeout / 1000; |
---|
| 232 | tv.tv_usec = ( timeout % 1000 ) * 1000; |
---|
| 233 | |
---|
| 234 | evtimer_set( &b_ev->evinfo, b_event_passthrough, b_ev ); |
---|
[09f8cd1] | 235 | evtimer_add( &b_ev->evinfo, &tv ); |
---|
[13cc96c] | 236 | |
---|
[782d988] | 237 | event_debug( "b_timeout_add( %d, 0x%x, 0x%x ) = %d\n", timeout, function, data, b_ev->id ); |
---|
[b642f381] | 238 | |
---|
[13cc96c] | 239 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
| 240 | |
---|
| 241 | return b_ev->id; |
---|
| 242 | } |
---|
| 243 | |
---|
[b642f381] | 244 | void b_event_remove( gint id ) |
---|
[13cc96c] | 245 | { |
---|
[b642f381] | 246 | struct b_event_data *b_ev = g_hash_table_lookup( id_hash, &id ); |
---|
[13cc96c] | 247 | |
---|
[b642f381] | 248 | event_debug( "b_event_remove( %d )\n", id ); |
---|
[13cc96c] | 249 | if( b_ev ) |
---|
| 250 | { |
---|
[939370c] | 251 | if( id == id_cur ) |
---|
| 252 | id_dead = TRUE; |
---|
| 253 | |
---|
[09f8cd1] | 254 | g_hash_table_remove( id_hash, &b_ev->id ); |
---|
[782d988] | 255 | if( b_ev->evinfo.ev_fd >= 0 ) |
---|
| 256 | { |
---|
| 257 | if( b_ev->evinfo.ev_events & EV_READ ) |
---|
| 258 | g_hash_table_remove( read_hash, &b_ev->evinfo.ev_fd ); |
---|
| 259 | if( b_ev->evinfo.ev_events & EV_WRITE ) |
---|
| 260 | g_hash_table_remove( write_hash, &b_ev->evinfo.ev_fd ); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | event_del( &b_ev->evinfo ); |
---|
[13cc96c] | 264 | g_free( b_ev ); |
---|
| 265 | } |
---|
[b642f381] | 266 | else |
---|
| 267 | { |
---|
[782d988] | 268 | event_debug( "Already removed?\n" ); |
---|
[b642f381] | 269 | } |
---|
[13cc96c] | 270 | } |
---|
| 271 | |
---|
[3f199fc] | 272 | void closesocket( int fd ) |
---|
| 273 | { |
---|
| 274 | struct b_event_data *b_ev; |
---|
| 275 | |
---|
| 276 | /* Since epoll() (the main reason we use libevent) automatically removes sockets from |
---|
| 277 | the epoll() list when a socket gets closed and some modules have a habit of |
---|
| 278 | closing sockets before removing event handlers, our and libevent's administration |
---|
| 279 | get a little bit messed up. So this little function will remove the handlers |
---|
| 280 | properly before closing a socket. */ |
---|
| 281 | |
---|
[b8b8c6c] | 282 | if( ( b_ev = g_hash_table_lookup( read_hash, &fd ) ) ) |
---|
[df70eafa] | 283 | { |
---|
[5330e3d] | 284 | event_debug( "Warning: fd %d still had a read event handler when shutting down.\n", fd ); |
---|
| 285 | b_event_remove( b_ev->id ); |
---|
| 286 | } |
---|
[b8b8c6c] | 287 | if( ( b_ev = g_hash_table_lookup( write_hash, &fd ) ) ) |
---|
[5330e3d] | 288 | { |
---|
| 289 | event_debug( "Warning: fd %d still had a write event handler when shutting down.\n", fd ); |
---|
[3f199fc] | 290 | b_event_remove( b_ev->id ); |
---|
[df70eafa] | 291 | } |
---|
[3f199fc] | 292 | |
---|
| 293 | close( fd ); |
---|
| 294 | } |
---|