source: lib/events_libevent.c @ c3599e0

Last change on this file since c3599e0 was 5ebff60, checked in by dequis <dx@…>, at 2015-02-20T22:50:54Z

Reindent everything to K&R style with tabs

Used uncrustify, with the configuration file in ./doc/uncrustify.cfg

Commit author set to "Indent <please@…>" so that it's easier to
skip while doing git blame.

  • Property mode set to 100644
File size: 7.6 KB
RevLine 
[5ebff60]1/********************************************************************\
[13cc96c]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]38static void b_main_restart();
[939370c]39static guint id_next = 1; /* Next ID to be allocated to an event handler. */
40static guint id_cur = 0; /* Event ID that we're currently handling. */
41static guint id_dead; /* Set to 1 if b_event_remove removes id_cur. */
[13cc96c]42static GHashTable *id_hash;
[939370c]43static 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. */
49static GHashTable *read_hash;
50static GHashTable *write_hash;
51
[b6a2373]52struct event_base *leh;
53struct event_base *old_leh;
54
[5ebff60]55struct b_event_data {
[13cc96c]56        guint id;
57        struct event evinfo;
[19ac9c5]58        gint timeout;
[13cc96c]59        b_event_handler function;
60        void *data;
[c5c18c1]61        guint flags;
[13cc96c]62};
63
64void b_main_init()
65{
[5ebff60]66        if (leh != NULL) {
[b6a2373]67                /* Clean up the hash tables? */
[5ebff60]68
[b6a2373]69                b_main_restart();
70                old_leh = leh;
71        }
[5ebff60]72
[b6a2373]73        leh = event_init();
[5ebff60]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);
[13cc96c]78}
79
80void b_main_run()
81{
[b6a2373]82        /* This while loop is necessary to exit the event loop and start a
83           different one (necessary for ForkDaemon mode). */
[5ebff60]84        while (event_base_dispatch(leh) == 0 && !quitting) {
85                if (old_leh != NULL) {
[b6a2373]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                }
[5ebff60]91
92                event_debug("New event loop.\n");
[b6a2373]93        }
[13cc96c]94}
95
[b6a2373]96static void b_main_restart()
[13cc96c]97{
98        struct timeval tv;
[5ebff60]99
100        memset(&tv, 0, sizeof(struct timeval));
101        event_base_loopexit(leh, &tv);
102
103        event_debug("b_main_restart()\n");
[b6a2373]104}
105
106void 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,
[cdca30b]110           we want to stop them. */
111        quitting = 1;
[5ebff60]112
[b6a2373]113        b_main_restart();
[13cc96c]114}
115
[5ebff60]116static void b_event_passthrough(int fd, short event, void *data)
[13cc96c]117{
118        struct b_event_data *b_ev = data;
119        b_input_condition cond = 0;
[939370c]120        gboolean st;
[5ebff60]121
122        if (fd >= 0) {
123                if (event & EV_READ) {
[e046390]124                        cond |= B_EV_IO_READ;
[5ebff60]125                }
126                if (event & EV_WRITE) {
[e046390]127                        cond |= B_EV_IO_WRITE;
[5ebff60]128                }
[09f8cd1]129        }
[5ebff60]130
131        event_debug("b_event_passthrough( %d, %d, 0x%x ) (%d)\n", fd, event, (int) data, b_ev->id);
132
[fc2ee0f]133        /* Since the called function might cancel this handler already
[04026d4]134           (which free()s b_ev), we have to remember the ID here. */
[939370c]135        id_cur = b_ev->id;
136        id_dead = 0;
[5ebff60]137
138        if (quitting) {
139                b_event_remove(id_cur);
[cdca30b]140                return;
141        }
[5ebff60]142
143        st = b_ev->function(b_ev->data, fd, cond);
144        if (id_dead) {
[939370c]145                /* This event was killed already, don't touch it! */
146                return;
[5ebff60]147        } else if (!st && !(b_ev->flags & B_EV_FLAG_FORCE_REPEAT)) {
148                event_debug("Handler returned FALSE: ");
149                b_event_remove(id_cur);
150        } else if (fd == -1) {
[939370c]151                /* fd == -1 means it was a timer. These can't be auto-repeated
152                   so it has to be recreated every time. */
[19ac9c5]153                struct timeval tv;
[5ebff60]154
[19ac9c5]155                tv.tv_sec = b_ev->timeout / 1000;
[5ebff60]156                tv.tv_usec = (b_ev->timeout % 1000) * 1000;
157
158                evtimer_add(&b_ev->evinfo, &tv);
[19ac9c5]159        }
[13cc96c]160}
161
[5ebff60]162gint b_input_add(gint fd, b_input_condition condition, b_event_handler function, gpointer data)
[13cc96c]163{
[782d988]164        struct b_event_data *b_ev;
[5ebff60]165
166        event_debug("b_input_add( %d, %d, 0x%x, 0x%x ) ", fd, condition, function, data);
167
168        if ((condition & B_EV_IO_READ  && (b_ev = g_hash_table_lookup(read_hash,  &fd))) ||
169            (condition & B_EV_IO_WRITE && (b_ev = g_hash_table_lookup(write_hash, &fd)))) {
[782d988]170                /* We'll stick with this libevent entry, but give it a new BitlBee id. */
[5ebff60]171                g_hash_table_remove(id_hash, &b_ev->id);
172
173                event_debug("(replacing old handler (id = %d)) = %d\n", b_ev->id, id_next);
174
[782d988]175                b_ev->id = id_next++;
176                b_ev->function = function;
177                b_ev->data = data;
[5ebff60]178        } else {
[782d988]179                GIOCondition out_cond;
[5ebff60]180
181                event_debug("(new) = %d\n", id_next);
182
183                b_ev = g_new0(struct b_event_data, 1);
[782d988]184                b_ev->id = id_next++;
185                b_ev->function = function;
186                b_ev->data = data;
[5ebff60]187
[782d988]188                out_cond = EV_PERSIST;
[5ebff60]189                if (condition & B_EV_IO_READ) {
[782d988]190                        out_cond |= EV_READ;
[5ebff60]191                }
192                if (condition & B_EV_IO_WRITE) {
[782d988]193                        out_cond |= EV_WRITE;
[5ebff60]194                }
195
196                event_set(&b_ev->evinfo, fd, out_cond, b_event_passthrough, b_ev);
197                event_add(&b_ev->evinfo, NULL);
198
199                if (out_cond & EV_READ) {
200                        g_hash_table_insert(read_hash, &b_ev->evinfo.ev_fd, b_ev);
201                }
202                if (out_cond & EV_WRITE) {
203                        g_hash_table_insert(write_hash, &b_ev->evinfo.ev_fd, b_ev);
204                }
[782d988]205        }
[5ebff60]206
[c5c18c1]207        b_ev->flags = condition;
[5ebff60]208        g_hash_table_insert(id_hash, &b_ev->id, b_ev);
[13cc96c]209        return b_ev->id;
210}
211
[09f8cd1]212/* TODO: Persistence for timers! */
[5ebff60]213gint b_timeout_add(gint timeout, b_event_handler function, gpointer data)
[13cc96c]214{
[5ebff60]215        struct b_event_data *b_ev = g_new0(struct b_event_data, 1);
[13cc96c]216        struct timeval tv;
[5ebff60]217
[09f8cd1]218        b_ev->id = id_next++;
[19ac9c5]219        b_ev->timeout = timeout;
[13cc96c]220        b_ev->function = function;
221        b_ev->data = data;
[5ebff60]222
[13cc96c]223        tv.tv_sec = timeout / 1000;
[5ebff60]224        tv.tv_usec = (timeout % 1000) * 1000;
225
226        evtimer_set(&b_ev->evinfo, b_event_passthrough, b_ev);
227        evtimer_add(&b_ev->evinfo, &tv);
228
229        event_debug("b_timeout_add( %d, 0x%x, 0x%x ) = %d\n", timeout, function, data, b_ev->id);
230
231        g_hash_table_insert(id_hash, &b_ev->id, b_ev);
232
[13cc96c]233        return b_ev->id;
234}
235
[5ebff60]236void b_event_remove(gint id)
[13cc96c]237{
[5ebff60]238        struct b_event_data *b_ev = g_hash_table_lookup(id_hash, &id);
239
240        event_debug("b_event_remove( %d )\n", id);
241        if (b_ev) {
242                if (id == id_cur) {
[939370c]243                        id_dead = TRUE;
[782d988]244                }
[5ebff60]245
246                g_hash_table_remove(id_hash, &b_ev->id);
247                if (b_ev->evinfo.ev_fd >= 0) {
248                        if (b_ev->evinfo.ev_events & EV_READ) {
249                                g_hash_table_remove(read_hash, &b_ev->evinfo.ev_fd);
250                        }
251                        if (b_ev->evinfo.ev_events & EV_WRITE) {
252                                g_hash_table_remove(write_hash, &b_ev->evinfo.ev_fd);
253                        }
254                }
255
256                event_del(&b_ev->evinfo);
257                g_free(b_ev);
258        } else {
259                event_debug("Already removed?\n");
[b642f381]260        }
[13cc96c]261}
262
[5ebff60]263void closesocket(int fd)
[3f199fc]264{
265        struct b_event_data *b_ev;
[5ebff60]266
[3f199fc]267        /* Since epoll() (the main reason we use libevent) automatically removes sockets from
268           the epoll() list when a socket gets closed and some modules have a habit of
269           closing sockets before removing event handlers, our and libevent's administration
270           get a little bit messed up. So this little function will remove the handlers
271           properly before closing a socket. */
[5ebff60]272
273        if ((b_ev = g_hash_table_lookup(read_hash, &fd))) {
274                event_debug("Warning: fd %d still had a read event handler when shutting down.\n", fd);
275                b_event_remove(b_ev->id);
[5330e3d]276        }
[5ebff60]277        if ((b_ev = g_hash_table_lookup(write_hash, &fd))) {
278                event_debug("Warning: fd %d still had a write event handler when shutting down.\n", fd);
279                b_event_remove(b_ev->id);
[df70eafa]280        }
[5ebff60]281
282        close(fd);
[3f199fc]283}
Note: See TracBrowser for help on using the repository browser.