From 33e4dfd65ff3c2c06e3774da831ebfd0b94c7263 Mon Sep 17 00:00:00 2001 From: Richard Nyberg Date: Fri, 24 Jun 2005 11:32:43 +0000 Subject: [PATCH] Separate the child struct from worker specific data. --- btpd/btpd.h | 1 + btpd/tracker_req.c | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/btpd/btpd.h b/btpd/btpd.h index 438fae6..a82b3fe 100644 --- a/btpd/btpd.h +++ b/btpd/btpd.h @@ -26,6 +26,7 @@ struct child { pid_t pid; + void *data; void (*child_done)(struct child *child); TAILQ_ENTRY(child) entry; }; diff --git a/btpd/tracker_req.c b/btpd/tracker_req.c index c8ef9e6..b84a2da 100644 --- a/btpd/tracker_req.c +++ b/btpd/tracker_req.c @@ -24,7 +24,6 @@ #define REQ_SIZE (getpagesize() * 2) struct tracker_req { - struct child child; enum tr_event tr_event; uint8_t info_hash[20]; struct io_buffer *res; @@ -66,7 +65,7 @@ out: static void tracker_done(struct child *child) { - struct tracker_req *req = (struct tracker_req *)child; + struct tracker_req *req = child->data; int failed = 0; char *buf; const char *peers; @@ -260,20 +259,19 @@ void tracker_req(struct torrent *tp, enum tr_event tr_event) { struct tracker_req *req; + struct child *child; btpd_log(BTPD_L_TRACKER, "request for %s, event: %s.\n", tp->relpath, event2str(tr_event)); - req = (struct tracker_req *)btpd_calloc(1, sizeof(*req)); + req = (struct tracker_req *)btpd_calloc(1, sizeof(*req) + sizeof(*child)); req->res = mmap(NULL, REQ_SIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0); - if (req->res == MAP_FAILED) { - free(req); + if (req->res == MAP_FAILED) btpd_err("Failed mmap: %s\n", strerror(errno)); - } req->res->buf_len = REQ_SIZE - sizeof(*req->res); req->res->buf_off = 0; @@ -284,12 +282,15 @@ tracker_req(struct torrent *tp, enum tr_event tr_event) fflush(NULL); - req->child.child_done = tracker_done; - TAILQ_INSERT_TAIL(&btpd.kids, &req->child, entry); - req->child.pid = fork(); - if (req->child.pid < 0) { + child = (struct child *)(req + 1); + child->data = req; + child->child_done = tracker_done; + TAILQ_INSERT_TAIL(&btpd.kids, child, entry); + + child->pid = fork(); + if (child->pid < 0) { btpd_err("Couldn't fork (%s).\n", strerror(errno)); - } else if (req->child.pid == 0) { // Child + } else if (child->pid == 0) { // Child int nfiles = getdtablesize(); for (int i = 0; i < nfiles; i++) close(i);