diff options
author | joerg <joerg> | 2009-01-31 23:25:37 +0000 |
---|---|---|
committer | joerg <joerg> | 2009-01-31 23:25:37 +0000 |
commit | 49e7c6cd4d0ff78e5b76f8bfff938f2a5d87c745 (patch) | |
tree | 5344e926c09eebb1ffcb0f4c826c194b7f792490 /pkgtools/pbulk | |
parent | e928b836aeeca71925c7e5bdf7c859fbd70cf3aa (diff) | |
download | pkgsrc-49e7c6cd4d0ff78e5b76f8bfff938f2a5d87c745.tar.gz |
pbulk-0.37:
- replace libevent dependency with internal event loop to make the pbulk
core standalone
- open success and error log with O_APPEND, which allows e.g. cleaning
up the error build during build without breaking it if done with care.
Diffstat (limited to 'pkgtools/pbulk')
-rw-r--r-- | pkgtools/pbulk/Makefile | 6 | ||||
-rw-r--r-- | pkgtools/pbulk/files/pbulk/Makefile.inc | 9 | ||||
-rw-r--r-- | pkgtools/pbulk/files/pbulk/lib/event.c | 235 | ||||
-rw-r--r-- | pkgtools/pbulk/files/pbulk/lib/pbulk.h | 30 | ||||
-rw-r--r-- | pkgtools/pbulk/files/pbulk/pbuild/jobs.c | 10 | ||||
-rw-r--r-- | pkgtools/pbulk/files/pbulk/pbuild/master.c | 19 | ||||
-rw-r--r-- | pkgtools/pbulk/files/pbulk/presolve/Makefile | 4 | ||||
-rw-r--r-- | pkgtools/pbulk/files/pbulk/pscan/master.c | 19 |
8 files changed, 276 insertions, 56 deletions
diff --git a/pkgtools/pbulk/Makefile b/pkgtools/pbulk/Makefile index 274b38c66d1..3548e650330 100644 --- a/pkgtools/pbulk/Makefile +++ b/pkgtools/pbulk/Makefile @@ -1,7 +1,6 @@ -# $NetBSD: Makefile,v 1.49 2008/10/16 21:52:17 wiz Exp $ +# $NetBSD: Makefile,v 1.50 2009/01/31 23:25:37 joerg Exp $ -DISTNAME= pbulk-0.36 -PKGREVISION= 1 +DISTNAME= pbulk-0.37 CATEGORIES= pkgtools MASTER_SITES= # empty DISTFILES= # empty @@ -76,5 +75,4 @@ MAKE_ENV+= LDADD=${LDADD:Q} CPPFLAGS+= -D_GNU_SOURCE .endif -.include "../../devel/libevent/buildlink3.mk" .include "../../mk/bsd.pkg.mk" diff --git a/pkgtools/pbulk/files/pbulk/Makefile.inc b/pkgtools/pbulk/files/pbulk/Makefile.inc index 659db47e590..e11b6d7528c 100644 --- a/pkgtools/pbulk/files/pbulk/Makefile.inc +++ b/pkgtools/pbulk/files/pbulk/Makefile.inc @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.inc,v 1.3 2007/07/17 18:11:37 tnn Exp $ +# $NetBSD: Makefile.inc,v 1.4 2009/01/31 23:25:38 joerg Exp $ BINDIR?= ${PREFIX}/bin @@ -11,10 +11,3 @@ LDADD+= -L${.OBJDIR}/../lib -lpbulk .endif LDADD+= -lnbcompat - -.if !defined(NO_LIBEVENT_NEEDED) -CPPFLAGS+= -.if !defined(LIBPBULK_ONLY) -LDADD+= -levent -.endif -.endif diff --git a/pkgtools/pbulk/files/pbulk/lib/event.c b/pkgtools/pbulk/files/pbulk/lib/event.c index 23d15b27507..192a0616167 100644 --- a/pkgtools/pbulk/files/pbulk/lib/event.c +++ b/pkgtools/pbulk/files/pbulk/lib/event.c @@ -1,7 +1,7 @@ -/* $NetBSD: event.c,v 1.3 2007/06/25 21:38:44 joerg Exp $ */ +/* $NetBSD: event.c,v 1.4 2009/01/31 23:25:38 joerg Exp $ */ /*- - * Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>. + * Copyright (c) 2007, 2009 Joerg Sonnenberger <joerg@NetBSD.org>. * All rights reserved. * * This code was developed as part of Google's Summer of Code 2007 program. @@ -38,11 +38,13 @@ #ifdef __sun #include <sys/filio.h> #endif +#include <nbcompat/poll.h> #include <nbcompat/time.h> -#include <event.h> #include <nbcompat/stdlib.h> #include <nbcompat/unistd.h> +#include <signal.h> + #include "pbulk.h" struct deferred_read_arg { @@ -70,7 +72,7 @@ struct deferred_write_arg { }; static void -deferred_read_handler(int fd, short event, void *arg) +deferred_read_handler(int fd, void *arg) { struct deferred_read_arg *data = arg; ssize_t received; @@ -88,7 +90,7 @@ deferred_read_handler(int fd, short event, void *arg) free(data); return; } - event_add(&data->ev, NULL); + event_add(&data->ev, data->fd, 0, 0, deferred_read_handler, data); } void @@ -105,13 +107,11 @@ deferred_read(int fd, void *buf, size_t buf_len, void *arg, data->buf = buf; data->remaining = buf_len; - event_set(&data->ev, data->fd, EV_READ, - deferred_read_handler, data); - event_add(&data->ev, NULL); + event_add(&data->ev, data->fd, 0, 0, deferred_read_handler, data); } static void -deferred_write_handler(int fd, short event, void *arg) +deferred_write_handler(int fd, void *arg) { struct deferred_write_arg *data = arg; ssize_t sent; @@ -129,7 +129,7 @@ deferred_write_handler(int fd, short event, void *arg) free(data); return; } - event_add(&data->ev, NULL); + event_add(&data->ev, data->fd, 1, 0, deferred_write_handler, data); } void @@ -146,9 +146,7 @@ deferred_write(int fd, const void *buf, size_t buf_len, void *arg, data->buf = buf; data->remaining = buf_len; - event_set(&data->ev, data->fd, EV_WRITE, - deferred_write_handler, data); - event_add(&data->ev, NULL); + event_add(&data->ev, data->fd, 1, 0, deferred_write_handler, data); } int @@ -161,3 +159,214 @@ set_nonblocking(int fd) return -1; return 0; } + +static size_t active_events; +static LIST_HEAD(, event) all_events; +static LIST_HEAD(, signal_event) all_signals; +static struct pollfd *poll_list; +static size_t poll_allocated; + +static int signal_pipe[2]; +static struct event signal_event; + +static void signal_read_handler(int, void *); + +void +event_init(void) +{ + LIST_INIT(&all_events); + LIST_INIT(&all_signals); + + if (pipe(signal_pipe)) + err(1, "can't create signal pipe"); + + event_add(&signal_event, signal_pipe[0], 0, 1, signal_read_handler, + NULL); + set_nonblocking(signal_pipe[0]); + set_nonblocking(signal_pipe[1]); +} + +void +event_add(struct event *ev, int fd, int do_write, int is_persistent, + void (*handler)(int, void *), void *arg) +{ + ev->ev_fd = fd; + ev->ev_write = do_write; + ev->ev_persistent = is_persistent; + ev->ev_handler = handler; + ev->ev_arg = arg; + LIST_INSERT_HEAD(&all_events, ev, ev_link); + ++active_events; +} + +void +event_del(struct event *ev) +{ + LIST_REMOVE(ev, ev_link); + --active_events; +} + +static void +mask_all(void) +{ + sigset_t mask; + + sigfillset(&mask); + + sigprocmask(SIG_SETMASK, &mask, NULL); +} + +static void +unmask_all(void) +{ + sigset_t mask; + + sigemptyset(&mask); + + sigprocmask(SIG_SETMASK, &mask, NULL); +} + +static void +signal_read_handler(int fd, void *arg) +{ + struct signal_event *sig, *next; + int got_something; + char buf[256]; + + for (;;) { + got_something = 0; + + while (read(signal_pipe[0], buf, sizeof(buf)) > 0) + got_something = 1; + if (got_something == 0) + break; + + for (sig = LIST_FIRST(&all_signals); + sig && (next = LIST_NEXT(sig, sig_link), 1); + sig = next) { + if (sig->sig_received) { + sig->sig_received = 0; + (*sig->sig_handler)(sig); + } + } + } +} + +static void +signal_handler(int sig_id) +{ + struct signal_event *sig; + char buf = 0; + int old_errno; + + LIST_FOREACH(sig, &all_signals, sig_link) { + if (sig->sig_id == sig_id) + sig->sig_received = 1; + } + + old_errno = errno; + write(signal_pipe[1], &buf, 1); + errno = old_errno; +} + +void +signal_add(struct signal_event *sig, int sigtype, + void (*handler)(struct signal_event *)) +{ + sig->sig_id = sigtype; + sig->sig_received = 0; + sig->sig_handler = handler; + + mask_all(); + LIST_INSERT_HEAD(&all_signals, sig, sig_link); + signal(sigtype, signal_handler); + unmask_all(); +} + +void +signal_del(struct signal_event *sig) +{ + mask_all(); + LIST_REMOVE(sig, sig_link); + /* XXX Unset signal handler */ + unmask_all(); +} + +static struct timeval exit_time; + +void +event_dispatch(void) +{ + struct timeval now; + struct event *ev, *next; + struct pollfd *iter, *last_iter; + int ret, timeout; + +loop: + if (active_events > poll_allocated) { + if (poll_allocated == 0) + poll_allocated = 512; + while (active_events > poll_allocated) + poll_allocated <<= 1; + poll_list = xrealloc(poll_list, + sizeof(struct pollfd) * poll_allocated); + } + + iter = poll_list; + LIST_FOREACH(ev, &all_events, ev_link) { + iter->fd = ev->ev_fd; + if (ev->ev_write) + iter->events = POLLOUT; + else + iter->events = POLLIN; + ++iter; + } + last_iter = iter; + + if (exit_time.tv_sec || exit_time.tv_usec) { + gettimeofday(&now, NULL); + timeout = (exit_time.tv_sec - now.tv_sec) * 1000; + timeout += (exit_time.tv_usec - now.tv_usec + 999) / 1000; + if (timeout < 0) + timeout = 0; + } else + timeout = -1; + + ret = poll(poll_list, active_events, timeout); + if (ret < 0 && errno != EINTR) + return; + if (ret == 0 && timeout == 0) + return; + if (ret > 0) { + iter = poll_list; + for (ev = LIST_FIRST(&all_events); + iter < last_iter && ev && (next = LIST_NEXT(ev, ev_link), 1); + ev = next, ++iter) { + if (iter->revents) { + (*ev->ev_handler)(ev->ev_fd, ev->ev_arg); + if (!ev->ev_persistent) { + --active_events; + LIST_REMOVE(ev, ev_link); + } + --ret; + } + } + } + goto loop; +} + +void +event_loopexit(struct timeval *tv) +{ + gettimeofday(&exit_time, NULL); + exit_time.tv_sec += tv->tv_sec; + exit_time.tv_usec += tv->tv_usec; + while (exit_time.tv_usec >= 1000000) { + exit_time.tv_usec -= 1000000; + ++exit_time.tv_sec; + } + while (exit_time.tv_usec < 0) { + exit_time.tv_usec += 1000000; + --exit_time.tv_sec; + } +} diff --git a/pkgtools/pbulk/files/pbulk/lib/pbulk.h b/pkgtools/pbulk/files/pbulk/lib/pbulk.h index 842281f9748..5290848448c 100644 --- a/pkgtools/pbulk/files/pbulk/lib/pbulk.h +++ b/pkgtools/pbulk/files/pbulk/lib/pbulk.h @@ -1,7 +1,7 @@ -/* $NetBSD: pbulk.h,v 1.2 2007/06/25 21:38:44 joerg Exp $ */ +/* $NetBSD: pbulk.h,v 1.3 2009/01/31 23:25:38 joerg Exp $ */ /*- - * Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>. + * Copyright (c) 2007, 2009 Joerg Sonnenberger <joerg@NetBSD.org>. * All rights reserved. * * This code was developed as part of Google's Summer of Code 2007 program. @@ -32,8 +32,25 @@ */ #include <netinet/in.h> +#include <nbcompat/queue.h> #include <nbcompat/unistd.h> +struct event { + LIST_ENTRY(event) ev_link; + int ev_fd; + int ev_write; + int ev_persistent; + void (*ev_handler)(int, void *); + void *ev_arg; +}; + +struct signal_event { + LIST_ENTRY(signal_event) sig_link; + int sig_id; + volatile int sig_received; + void (*sig_handler)(struct signal_event *); +}; + #if defined(__GNUC__) && __GNUC__ >= 2 char *xasprintf(const char *, ...) __attribute__((__format__(__printf__, 1, 2))); @@ -41,6 +58,15 @@ char *xasprintf(const char *, ...) char *xasprintf(const char *, ...); #endif +void event_init(void); +void event_add(struct event *, int, int, int, + void (*)(int, void *), void *); +void event_del(struct event *); +void signal_add(struct signal_event *, int, + void (*)(struct signal_event *)); +void signal_del(struct signal_event *); +void event_dispatch(void); +void event_loopexit(struct timeval *tv); int set_nonblocking(int); void deferred_read(int fd, void *, size_t, void *, diff --git a/pkgtools/pbulk/files/pbulk/pbuild/jobs.c b/pkgtools/pbulk/files/pbulk/pbuild/jobs.c index cd3b036fd38..6d09eb82be1 100644 --- a/pkgtools/pbulk/files/pbulk/pbuild/jobs.c +++ b/pkgtools/pbulk/files/pbulk/pbuild/jobs.c @@ -1,7 +1,7 @@ -/* $NetBSD: jobs.c,v 1.9 2008/09/14 18:59:02 joerg Exp $ */ +/* $NetBSD: jobs.c,v 1.10 2009/01/31 23:25:38 joerg Exp $ */ /*- - * Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>. + * Copyright (c) 2007, 2009 Joerg Sonnenberger <joerg@NetBSD.org>. * All rights reserved. * * This code was developed as part of Google's Summer of Code 2007 program. @@ -187,10 +187,12 @@ init_jobs(const char *scan_output, const char *success_file, const char *error_f if ((fd = open(scan_output, O_RDONLY, 0)) == -1) err(1, "Cannot open input"); - if ((log_success = open(success_file, O_RDWR | O_CREAT, 0666)) == -1) + log_success = open(success_file, O_RDWR | O_CREAT | O_APPEND, 0666); + if (log_success == -1) err(1, "Cannot open log file for successful builds"); - if ((log_failed = open(error_file, O_RDWR | O_CREAT, 0666)) == -1) + log_failed = open(error_file, O_RDWR | O_CREAT | O_APPEND, 0666); + if (log_failed == -1) err(1, "Cannot open log file for failed builds"); input = read_from_file(fd); diff --git a/pkgtools/pbulk/files/pbulk/pbuild/master.c b/pkgtools/pbulk/files/pbulk/pbuild/master.c index 08ab55e65b6..f602e2a98ae 100644 --- a/pkgtools/pbulk/files/pbulk/pbuild/master.c +++ b/pkgtools/pbulk/files/pbulk/pbuild/master.c @@ -1,7 +1,7 @@ -/* $NetBSD: master.c,v 1.7 2008/01/27 14:01:23 joerg Exp $ */ +/* $NetBSD: master.c,v 1.8 2009/01/31 23:25:38 joerg Exp $ */ /*- - * Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>. + * Copyright (c) 2007, 2009 Joerg Sonnenberger <joerg@NetBSD.org>. * All rights reserved. * * This code was developed as part of Google's Summer of Code 2007 program. @@ -43,7 +43,6 @@ #include <nbcompat/time.h> #include <sys/wait.h> #include <nbcompat/err.h> -#include <event.h> #include <signal.h> #include <nbcompat/stdlib.h> #include <nbcompat/stdio.h> @@ -58,7 +57,7 @@ static int clients_started; static LIST_HEAD(, build_peer) active_peers, inactive_peers, unassigned_peers; static struct event listen_event; static int listen_event_socket; -static struct event child_event; +static struct signal_event child_event; static pid_t child_pid; struct build_peer { @@ -243,7 +242,7 @@ recv_command(struct build_peer *peer) } static void -listen_handler(int sock, short event, void *arg) +listen_handler(int sock, void *arg) { struct build_peer *peer; struct sockaddr_in src; @@ -269,7 +268,7 @@ listen_handler(int sock, short event, void *arg) } static void -child_handler(int dummy, short event, void *arg) +child_handler(struct signal_event *ev) { struct build_peer *peer; int status; @@ -283,7 +282,7 @@ child_handler(int dummy, short event, void *arg) err(1, "Start script failed"); clients_started = 1; - signal_del(&child_event); + signal_del(ev); if ((peer = LIST_FIRST(&inactive_peers)) != NULL) { LIST_REMOVE(peer, peer_link); @@ -318,13 +317,11 @@ master_mode(const char *master_port, const char *start_script) if (listen(fd, 5) == -1) err(1, "Could not listen on socket"); - event_set(&listen_event, fd, EV_READ | EV_PERSIST, listen_handler, NULL); - event_add(&listen_event, NULL); + event_add(&listen_event, fd, 0, 1, listen_handler, NULL); listen_event_socket = fd; if (start_script) { - signal_set(&child_event, SIGCHLD, child_handler, NULL); - signal_add(&child_event, NULL); + signal_add(&child_event, SIGCHLD, child_handler); if ((child_pid = vfork()) == 0) { execlp(start_script, start_script, (char *)NULL); diff --git a/pkgtools/pbulk/files/pbulk/presolve/Makefile b/pkgtools/pbulk/files/pbulk/presolve/Makefile index b88c0439672..83bc57f21f5 100644 --- a/pkgtools/pbulk/files/pbulk/presolve/Makefile +++ b/pkgtools/pbulk/files/pbulk/presolve/Makefile @@ -1,8 +1,6 @@ -# $NetBSD: Makefile,v 1.1.1.1 2007/06/19 19:49:57 joerg Exp $ +# $NetBSD: Makefile,v 1.2 2009/01/31 23:25:38 joerg Exp $ PROG= pbulk-resolve SRCS= presolve.c -NO_LIBEVENT_NEEDED= # defined - .include <bsd.prog.mk> diff --git a/pkgtools/pbulk/files/pbulk/pscan/master.c b/pkgtools/pbulk/files/pbulk/pscan/master.c index b68b7eb5fd4..94221ee527c 100644 --- a/pkgtools/pbulk/files/pbulk/pscan/master.c +++ b/pkgtools/pbulk/files/pbulk/pscan/master.c @@ -1,7 +1,7 @@ -/* $NetBSD: master.c,v 1.6 2008/01/27 14:01:23 joerg Exp $ */ +/* $NetBSD: master.c,v 1.7 2009/01/31 23:25:38 joerg Exp $ */ /*- - * Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>. + * Copyright (c) 2007, 2009 Joerg Sonnenberger <joerg@NetBSD.org>. * All rights reserved. * * This code was developed as part of Google's Summer of Code 2007 program. @@ -43,7 +43,6 @@ #include <nbcompat/time.h> #include <sys/wait.h> #include <nbcompat/err.h> -#include <event.h> #include <signal.h> #include <nbcompat/stdlib.h> #include <nbcompat/stdio.h> @@ -58,7 +57,7 @@ static int clients_started; static LIST_HEAD(, scan_peer) active_peers, inactive_peers; static struct event listen_event; static int listen_event_socket; -static struct event child_event; +static struct signal_event child_event; static pid_t child_pid; struct scan_peer { @@ -195,7 +194,7 @@ assign_job(struct scan_peer *peer) } static void -listen_handler(int sock, short event, void *arg) +listen_handler(int sock, void *arg) { struct scan_peer *peer; struct sockaddr_in src; @@ -219,7 +218,7 @@ listen_handler(int sock, short event, void *arg) } static void -child_handler(int dummy, short event, void *arg) +child_handler(struct signal_event *ev) { struct scan_peer *peer; int status; @@ -233,7 +232,7 @@ child_handler(int dummy, short event, void *arg) err(1, "Start script failed"); clients_started = 1; - signal_del(&child_event); + signal_del(ev); while ((peer = LIST_FIRST(&inactive_peers)) != NULL) { LIST_REMOVE(peer, peer_link); @@ -269,13 +268,11 @@ master_mode(const char *master_port, const char *start_script) if (listen(fd, 5) == -1) err(1, "Could not listen on socket"); - event_set(&listen_event, fd, EV_READ | EV_PERSIST, listen_handler, NULL); - event_add(&listen_event, NULL); + event_add(&listen_event, fd, 0, 1, listen_handler, NULL); listen_event_socket = fd; if (start_script) { - signal_set(&child_event, SIGCHLD, child_handler, NULL); - signal_add(&child_event, NULL); + signal_add(&child_event, SIGCHLD, child_handler); if ((child_pid = vfork()) == 0) { execlp(start_script, start_script, (char *)NULL); |