|
|
@@ -29,20 +29,9 @@ |
|
|
|
#include "btpd.h" |
|
|
|
#include "http.h" |
|
|
|
|
|
|
|
struct child { |
|
|
|
pid_t pid; |
|
|
|
void *arg; |
|
|
|
void (*cb)(pid_t, void *); |
|
|
|
BTPDQ_ENTRY(child) entry; |
|
|
|
}; |
|
|
|
|
|
|
|
BTPDQ_HEAD(child_tq, child); |
|
|
|
|
|
|
|
static uint8_t m_peer_id[20]; |
|
|
|
static struct event m_sigint; |
|
|
|
static struct event m_sigterm; |
|
|
|
static struct event m_sigchld; |
|
|
|
static struct child_tq m_kids = BTPDQ_HEAD_INITIALIZER(m_kids); |
|
|
|
static unsigned m_ntorrents; |
|
|
|
static struct torrent_tq m_torrents = BTPDQ_HEAD_INITIALIZER(m_torrents); |
|
|
|
|
|
|
@@ -68,35 +57,6 @@ signal_cb(int signal, short type, void *arg) |
|
|
|
btpd_shutdown(); |
|
|
|
} |
|
|
|
|
|
|
|
void |
|
|
|
btpd_add_child(pid_t pid, void (*cb)(pid_t, void *), void *arg) |
|
|
|
{ |
|
|
|
struct child *kid = btpd_calloc(1, sizeof(*kid)); |
|
|
|
kid->pid = pid; |
|
|
|
kid->arg = arg; |
|
|
|
kid->cb = cb; |
|
|
|
BTPDQ_INSERT_TAIL(&m_kids, kid, entry); |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
child_cb(int signal, short type, void *arg) |
|
|
|
{ |
|
|
|
int status; |
|
|
|
pid_t pid; |
|
|
|
|
|
|
|
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
|
|
|
if (WIFEXITED(status) || WIFSIGNALED(status)) { |
|
|
|
struct child *kid = BTPDQ_FIRST(&m_kids); |
|
|
|
while (kid != NULL && kid->pid != pid) |
|
|
|
kid = BTPDQ_NEXT(kid, entry); |
|
|
|
assert(kid != NULL); |
|
|
|
BTPDQ_REMOVE(&m_kids, kid, entry); |
|
|
|
kid->cb(kid->pid, kid->arg); |
|
|
|
free(kid); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void |
|
|
|
btpd_add_torrent(struct torrent *tp) |
|
|
|
{ |
|
|
@@ -271,6 +231,4 @@ btpd_init(void) |
|
|
|
signal_add(&m_sigint, NULL); |
|
|
|
signal_set(&m_sigterm, SIGTERM, signal_cb, NULL); |
|
|
|
signal_add(&m_sigterm, NULL); |
|
|
|
signal_set(&m_sigchld, SIGCHLD, child_cb, NULL); |
|
|
|
signal_add(&m_sigchld, NULL); |
|
|
|
} |