Browse Source

Use the new functions btpd_ev_(add|del) instead of calling event_(add|del)

directly. Failure to add or delete an event is treated as a fatal error.
master
Richard Nyberg 18 years ago
parent
commit
ed56df9e56
9 changed files with 45 additions and 28 deletions
  1. +3
    -3
      btpd/btpd.c
  2. +3
    -1
      btpd/btpd.h
  3. +3
    -3
      btpd/cli_if.c
  4. +3
    -3
      btpd/content.c
  5. +6
    -6
      btpd/net.c
  6. +5
    -5
      btpd/peer.c
  7. +5
    -5
      btpd/tracker_req.c
  8. +2
    -2
      btpd/upload.c
  9. +15
    -0
      btpd/util.c

+ 3
- 3
btpd/btpd.c View File

@@ -169,7 +169,7 @@ td_init(void)
btpd_err("Couldn't create mutex (%s).\n", strerror(err)); btpd_err("Couldn't create mutex (%s).\n", strerror(err));


event_set(&m_td_ev, m_td_rd, EV_READ|EV_PERSIST, td_cb, NULL); event_set(&m_td_ev, m_td_rd, EV_READ|EV_PERSIST, td_cb, NULL);
event_add(&m_td_ev, NULL);
btpd_ev_add(&m_td_ev, NULL);
} }


void ipc_init(void); void ipc_init(void);
@@ -193,7 +193,7 @@ btpd_init(void)
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);


signal_set(&m_sigint, SIGINT, signal_cb, NULL); signal_set(&m_sigint, SIGINT, signal_cb, NULL);
signal_add(&m_sigint, NULL);
btpd_ev_add(&m_sigint, NULL);
signal_set(&m_sigterm, SIGTERM, signal_cb, NULL); signal_set(&m_sigterm, SIGTERM, signal_cb, NULL);
signal_add(&m_sigterm, NULL);
btpd_ev_add(&m_sigterm, NULL);
} }

+ 3
- 1
btpd/btpd.h View File

@@ -42,12 +42,14 @@
void btpd_init(void); void btpd_init(void);


void btpd_log(uint32_t type, const char *fmt, ...); void btpd_log(uint32_t type, const char *fmt, ...);

void btpd_err(const char *fmt, ...); void btpd_err(const char *fmt, ...);


void *btpd_malloc(size_t size); void *btpd_malloc(size_t size);
void *btpd_calloc(size_t nmemb, size_t size); void *btpd_calloc(size_t nmemb, size_t size);


void btpd_ev_add(struct event *ev, struct timeval *tv);
void btpd_ev_del(struct event *ev);

void btpd_shutdown(int grace_seconds); void btpd_shutdown(int grace_seconds);
int btpd_is_stopping(void); int btpd_is_stopping(void);




+ 3
- 3
btpd/cli_if.c View File

@@ -221,7 +221,7 @@ cli_read_cb(int sd, short type, void *arg)
goto error; goto error;


free(msg); free(msg);
event_add(&cli->read, NULL);
btpd_ev_add(&cli->read, NULL);
return; return;


error: error:
@@ -249,7 +249,7 @@ client_connection_cb(int sd, short type, void *arg)
struct cli *cli = btpd_calloc(1, sizeof(*cli)); struct cli *cli = btpd_calloc(1, sizeof(*cli));
cli->sd = nsd; cli->sd = nsd;
event_set(&cli->read, cli->sd, EV_READ, cli_read_cb, cli); event_set(&cli->read, cli->sd, EV_READ, cli_read_cb, cli);
event_add(&cli->read, NULL);
btpd_ev_add(&cli->read, NULL);
} }


void void
@@ -281,5 +281,5 @@ ipc_init(void)


event_set(&m_cli_incoming, sd, EV_READ | EV_PERSIST, event_set(&m_cli_incoming, sd, EV_READ | EV_PERSIST,
client_connection_cb, NULL); client_connection_cb, NULL);
event_add(&m_cli_incoming, NULL);
btpd_ev_add(&m_cli_incoming, NULL);
} }

+ 3
- 3
btpd/content.c View File

@@ -191,7 +191,7 @@ cm_write_done(struct torrent *tp)
btpd_err("Error closing write stream for '%s' (%s).\n", btpd_err("Error closing write stream for '%s' (%s).\n",
torrent_name(tp), strerror(errno)); torrent_name(tp), strerror(errno));
cm->wrs = NULL; cm->wrs = NULL;
event_del(&cm->save_timer);
btpd_ev_del(&cm->save_timer);
cm_save(tp); cm_save(tp);
} }


@@ -231,7 +231,7 @@ static void
save_timer_cb(int fd, short type, void *arg) save_timer_cb(int fd, short type, void *arg)
{ {
struct torrent *tp = arg; struct torrent *tp = arg;
event_add(&tp->cm->save_timer, SAVE_INTERVAL);
btpd_ev_add(&tp->cm->save_timer, SAVE_INTERVAL);
cm_save(tp); cm_save(tp);
} }


@@ -258,7 +258,7 @@ cm_td_cb(void *arg)
if ((err = bts_open(&cm->wrs, &tp->meta, fd_cb_wr, tp)) != 0) if ((err = bts_open(&cm->wrs, &tp->meta, fd_cb_wr, tp)) != 0)
btpd_err("Couldn't open write stream for '%s' (%s).\n", btpd_err("Couldn't open write stream for '%s' (%s).\n",
torrent_name(tp), strerror(err)); torrent_name(tp), strerror(err));
event_add(&cm->save_timer, SAVE_INTERVAL);
btpd_ev_add(&cm->save_timer, SAVE_INTERVAL);
} }
torrent_on_cm_started(tp); torrent_on_cm_started(tp);
} }


+ 6
- 6
btpd/net.c View File

@@ -178,7 +178,7 @@ net_write(struct peer *p, unsigned long wmax)
nwritten = writev(p->sd, iov, niov); nwritten = writev(p->sd, iov, niov);
if (nwritten < 0) { if (nwritten < 0) {
if (errno == EAGAIN) { if (errno == EAGAIN) {
event_add(&p->out_ev, WRITE_TIMEOUT);
btpd_ev_add(&p->out_ev, WRITE_TIMEOUT);
return 0; return 0;
} else { } else {
btpd_log(BTPD_L_CONN, "write error: %s\n", strerror(errno)); btpd_log(BTPD_L_CONN, "write error: %s\n", strerror(errno));
@@ -218,7 +218,7 @@ net_write(struct peer *p, unsigned long wmax)
} }
} }
if (!BTPDQ_EMPTY(&p->outq)) if (!BTPDQ_EMPTY(&p->outq))
event_add(&p->out_ev, WRITE_TIMEOUT);
btpd_ev_add(&p->out_ev, WRITE_TIMEOUT);


return nwritten; return nwritten;
} }
@@ -458,7 +458,7 @@ net_read(struct peer *p, unsigned long rmax)
} }


out: out:
event_add(&p->in_ev, NULL);
btpd_ev_add(&p->in_ev, NULL);
return nread > 0 ? nread : 0; return nread > 0 ? nread : 0;
} }


@@ -575,7 +575,7 @@ net_bw_cb(int sd, short type, void *arg)
{ {
struct peer *p; struct peer *p;


evtimer_add(&m_bw_timer, (& (struct timeval) { 1, 0 }));
btpd_ev_add(&m_bw_timer, (& (struct timeval) { 1, 0 }));


compute_rates(); compute_rates();


@@ -671,8 +671,8 @@ net_init(void)


event_set(&m_net_incoming, sd, EV_READ | EV_PERSIST, event_set(&m_net_incoming, sd, EV_READ | EV_PERSIST,
net_connection_cb, NULL); net_connection_cb, NULL);
event_add(&m_net_incoming, NULL);
btpd_ev_add(&m_net_incoming, NULL);


evtimer_set(&m_bw_timer, net_bw_cb, NULL); evtimer_set(&m_bw_timer, net_bw_cb, NULL);
evtimer_add(&m_bw_timer, (& (struct timeval) { 1, 0 }));
btpd_ev_add(&m_bw_timer, (& (struct timeval) { 1, 0 }));
} }

+ 5
- 5
btpd/peer.c View File

@@ -31,8 +31,8 @@ peer_kill(struct peer *p)
BTPDQ_REMOVE(&net_bw_writeq, p, wq_entry); BTPDQ_REMOVE(&net_bw_writeq, p, wq_entry);


close(p->sd); close(p->sd);
event_del(&p->in_ev);
event_del(&p->out_ev);
btpd_ev_del(&p->in_ev);
btpd_ev_del(&p->out_ev);


nl = BTPDQ_FIRST(&p->outq); nl = BTPDQ_FIRST(&p->outq);
while (nl != NULL) { while (nl != NULL) {
@@ -66,7 +66,7 @@ peer_send(struct peer *p, struct net_buf *nb)


if (BTPDQ_EMPTY(&p->outq)) { if (BTPDQ_EMPTY(&p->outq)) {
assert(p->outq_off == 0); assert(p->outq_off == 0);
event_add(&p->out_ev, WRITE_TIMEOUT);
btpd_ev_add(&p->out_ev, WRITE_TIMEOUT);
} }
BTPDQ_INSERT_TAIL(&p->outq, nl, entry); BTPDQ_INSERT_TAIL(&p->outq, nl, entry);
} }
@@ -94,7 +94,7 @@ peer_unsend(struct peer *p, struct nb_link *nl)
BTPDQ_REMOVE(&net_bw_writeq, p, wq_entry); BTPDQ_REMOVE(&net_bw_writeq, p, wq_entry);
p->flags &= ~PF_ON_WRITEQ; p->flags &= ~PF_ON_WRITEQ;
} else } else
event_del(&p->out_ev);
btpd_ev_del(&p->out_ev);
} }
return 1; return 1;
} else } else
@@ -270,7 +270,7 @@ peer_create_common(int sd)


event_set(&p->out_ev, p->sd, EV_WRITE, net_write_cb, p); event_set(&p->out_ev, p->sd, EV_WRITE, net_write_cb, p);
event_set(&p->in_ev, p->sd, EV_READ, net_read_cb, p); event_set(&p->in_ev, p->sd, EV_READ, net_read_cb, p);
event_add(&p->in_ev, NULL);
btpd_ev_add(&p->in_ev, NULL);


BTPDQ_INSERT_TAIL(&net_unattached, p, p_entry); BTPDQ_INSERT_TAIL(&net_unattached, p, p_entry);
net_npeers++; net_npeers++;


+ 5
- 5
btpd/tracker_req.c View File

@@ -119,7 +119,7 @@ static void
tr_set_stopped(struct torrent *tp) tr_set_stopped(struct torrent *tp)
{ {
struct tracker *tr = tp->tr; struct tracker *tr = tp->tr;
event_del(&tr->timer);
btpd_ev_del(&tr->timer);
tr->ttype = TIMER_NONE; tr->ttype = TIMER_NONE;
if (tr->req != NULL) { if (tr->req != NULL) {
http_cancel(tr->req); http_cancel(tr->req);
@@ -139,14 +139,14 @@ http_cb(struct http *req, struct http_res *res, void *arg)
tr->event != TR_EV_STOPPED) == 0) { tr->event != TR_EV_STOPPED) == 0) {
tr->nerrors = 0; tr->nerrors = 0;
tr->ttype = TIMER_INTERVAL; tr->ttype = TIMER_INTERVAL;
event_add(&tr->timer, (& (struct timeval) { tr->interval, 0 }));
btpd_ev_add(&tr->timer, (& (struct timeval) { tr->interval, 0 }));
} else { } else {
if (res->res == HRES_FAIL) if (res->res == HRES_FAIL)
btpd_log(BTPD_L_BTPD, "Tracker request for '%s' failed (%s).\n", btpd_log(BTPD_L_BTPD, "Tracker request for '%s' failed (%s).\n",
torrent_name(tp), res->errmsg); torrent_name(tp), res->errmsg);
tr->nerrors++; tr->nerrors++;
tr->ttype = TIMER_RETRY; tr->ttype = TIMER_RETRY;
event_add(&tr->timer, RETRY_WAIT);
btpd_ev_add(&tr->timer, RETRY_WAIT);
} }
if (tr->event == TR_EV_STOPPED && (tr->nerrors == 0 || tr->nerrors >= 5)) if (tr->event == TR_EV_STOPPED && (tr->nerrors == 0 || tr->nerrors >= 5))
tr_set_stopped(tp); tr_set_stopped(tp);
@@ -188,7 +188,7 @@ tr_send(struct torrent *tp, enum tr_event event)
if (tr->ttype == TIMER_TIMEOUT) if (tr->ttype == TIMER_TIMEOUT)
http_cancel(tr->req); http_cancel(tr->req);
tr->ttype = TIMER_TIMEOUT; tr->ttype = TIMER_TIMEOUT;
event_add(&tr->timer, REQ_TIMEOUT);
btpd_ev_add(&tr->timer, REQ_TIMEOUT);


qc = (strchr(tp->meta.announce, '?') == NULL) ? '?' : '&'; qc = (strchr(tp->meta.announce, '?') == NULL) ? '?' : '&';


@@ -225,7 +225,7 @@ tr_kill(struct torrent *tp)
{ {
struct tracker *tr = tp->tr; struct tracker *tr = tp->tr;
tp->tr = NULL; tp->tr = NULL;
event_del(&tr->timer);
btpd_ev_del(&tr->timer);
if (tr->req != NULL) if (tr->req != NULL)
http_cancel(tr->req); http_cancel(tr->req);
free(tr); free(tr);


+ 2
- 2
btpd/upload.c View File

@@ -112,7 +112,7 @@ shuffle_optimists(void)
static void static void
choke_cb(int sd, short type, void *arg) choke_cb(int sd, short type, void *arg)
{ {
evtimer_add(&m_choke_timer, CHOKE_INTERVAL);
btpd_ev_add(&m_choke_timer, CHOKE_INTERVAL);
static int cb_count = 0; static int cb_count = 0;
cb_count++; cb_count++;
if (cb_count % 3 == 0) if (cb_count % 3 == 0)
@@ -193,5 +193,5 @@ ul_init(void)
} }


evtimer_set(&m_choke_timer, choke_cb, NULL); evtimer_set(&m_choke_timer, choke_cb, NULL);
evtimer_add(&m_choke_timer, CHOKE_INTERVAL);
btpd_ev_add(&m_choke_timer, CHOKE_INTERVAL);
} }

+ 15
- 0
btpd/util.c View File

@@ -1,5 +1,6 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <time.h> #include <time.h>


#include "btpd.h" #include "btpd.h"
@@ -22,6 +23,20 @@ btpd_calloc(size_t nmemb, size_t size)
return a; return a;
} }


void
btpd_ev_add(struct event *ev, struct timeval *tv)
{
if (event_add(ev, tv) != 0)
btpd_err("Failed to add event (%s).\n", strerror(errno));
}

void
btpd_ev_del(struct event *ev)
{
if (event_del(ev) != 0)
btpd_err("Failed to remove event (%s).\n", strerror(errno));
}

static const char * static const char *
logtype_str(uint32_t type) logtype_str(uint32_t type)
{ {


Loading…
Cancel
Save