From b41978c1b227dab6337cdaa34002e51d1b708d5a Mon Sep 17 00:00:00 2001 From: Greg Hudson Date: Mon, 12 Sep 2011 16:08:04 +0000 Subject: [PATCH] Update verto sources to 2011-09-10 versions Also update verto-k5ev.c to match changes to verto-libev.c. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@25172 dc483132-0cff-0310-8789-dd5450dbe970 --- src/util/k5ev/verto-k5ev.c | 15 +++++- src/util/verto/verto-module.h | 2 + src/util/verto/verto.c | 87 ++++++++++++++++++++++++++--------- src/util/verto/verto.h | 20 ++++++-- 4 files changed, 98 insertions(+), 26 deletions(-) diff --git a/src/util/k5ev/verto-k5ev.c b/src/util/k5ev/verto-k5ev.c index fce4de8d5..134192fac 100644 --- a/src/util/k5ev/verto-k5ev.c +++ b/src/util/k5ev/verto-k5ev.c @@ -69,6 +69,12 @@ k5ev_ctx_break(void *ctx) ev_break(ctx, EVBREAK_ONE); } +static void +k5ev_ctx_reinitialize(void *ctx) +{ + ev_loop_fork(ctx); +} + static void libev_callback(EV_P_ ev_watcher *w, int revents) { @@ -127,14 +133,19 @@ k5ev_ctx_del(void *ctx, const verto_ev *ev, void *evpriv) switch (verto_get_type(ev)) { case VERTO_EV_TYPE_IO: ev_io_stop(ctx, evpriv); + break; case VERTO_EV_TYPE_TIMEOUT: ev_timer_stop(ctx, evpriv); + break; case VERTO_EV_TYPE_IDLE: ev_idle_stop(ctx, evpriv); + break; case VERTO_EV_TYPE_SIGNAL: ev_signal_stop(ctx, evpriv); + break; case VERTO_EV_TYPE_CHILD: ev_child_stop(ctx, evpriv); + break; default: break; } @@ -152,13 +163,13 @@ VERTO_MODULE(k5ev, NULL, VERTO_EV_TYPE_CHILD); verto_ctx * -verto_new_k5ev() +verto_new_k5ev(void) { return verto_convert_k5ev(ev_loop_new(EVFLAG_AUTO)); } verto_ctx * -verto_default_k5ev() +verto_default_k5ev(void) { return verto_convert_k5ev(ev_default_loop(EVFLAG_AUTO)); } diff --git a/src/util/verto/verto-module.h b/src/util/verto/verto-module.h index 89d6e5611..05a920f9e 100644 --- a/src/util/verto/verto-module.h +++ b/src/util/verto/verto-module.h @@ -37,6 +37,7 @@ name ## _ctx_run, \ name ## _ctx_run_once, \ name ## _ctx_break, \ + name ## _ctx_reinitialize, \ name ## _ctx_add, \ name ## _ctx_del \ }; \ @@ -65,6 +66,7 @@ typedef struct { void (*ctx_run)(void *ctx); void (*ctx_run_once)(void *ctx); void (*ctx_break)(void *ctx); + void (*ctx_reinitialize)(void *ctx); void *(*ctx_add)(void *ctx, const verto_ev *ev, verto_ev_flag *flags); void (*ctx_del)(void *ctx, const verto_ev *ev, void *evpriv); } verto_ctx_funcs; diff --git a/src/util/verto/verto.c b/src/util/verto/verto.c index f59e8bae2..98cc30351 100644 --- a/src/util/verto/verto.c +++ b/src/util/verto/verto.c @@ -46,7 +46,8 @@ #ifdef WIN32 #define pdlmsuffix ".dll" #define pdlmtype HMODULE -#define pdlopenl(filename) LoadLibraryEx(filename, NULL, DONT_RESOLVE_DLL_REFERENCES) +#define pdlopenl(filename) LoadLibraryEx(filename, NULL, \ + DONT_RESOLVE_DLL_REFERENCES) #define pdlclose(module) FreeLibrary((pdlmtype) module) #define pdlsym(mod, sym) ((void *) GetProcAddress(mod, sym)) @@ -57,7 +58,8 @@ pdlreopen(const char *filename, pdlmtype module) return LoadLibrary(filename); } -static char *pdlerror() { +static char * +pdlerror(void) { char *amsg; LPTSTR msg; @@ -191,8 +193,7 @@ _vasprintf(char **strp, const char *fmt, va_list ap) { size = vsnprintf(NULL, 0, fmt, apc); va_end(apc); - *strp = malloc(size + 1); - if (!size) + if (size <= 0 || !(*strp = malloc(size + 1))) return -1; return vsnprintf(*strp, size + 1, fmt, ap); @@ -254,15 +255,22 @@ do_load_dir(const char *dirname, const char *prefix, const char *suffix, int reqsym, verto_ev_type reqtypes, pdlmtype *dll, const verto_module **module) { + DIR *dir; + struct dirent *ent = NULL; + *module = NULL; - DIR *dir = opendir(dirname); + dir = opendir(dirname); if (!dir) return 0; - struct dirent *ent = NULL; + while ((ent = readdir(dir))) { - size_t flen = strlen(ent->d_name); - size_t slen = strlen(suffix); + char *tmp = NULL; + int success; + size_t flen, slen; + + flen = strlen(ent->d_name); + slen = strlen(suffix); if (!strcmp(".", ent->d_name) || !strcmp("..", ent->d_name)) continue; @@ -271,11 +279,10 @@ do_load_dir(const char *dirname, const char *prefix, const char *suffix, if (flen < slen || strcmp(ent->d_name + flen - slen, suffix)) continue; - char *tmp = NULL; if (_asprintf(&tmp, "%s/%s", dirname, ent->d_name) < 0) continue; - int success = do_load_file(tmp, reqsym, reqtypes, dll, module); + success = do_load_file(tmp, reqsym, reqtypes, dll, module); free(tmp); if (success) break; @@ -394,10 +401,12 @@ make_ev(verto_ctx *ctx, verto_callback *callback, static void push_ev(verto_ctx *ctx, verto_ev *ev) { + verto_ev *tmp; + if (!ctx || !ev) return; - verto_ev *tmp = ctx->events; + tmp = ctx->events; ctx->events = ev; ctx->events->next = tmp; } @@ -543,8 +552,36 @@ verto_break(verto_ctx *ctx) ctx->funcs.ctx_break(ctx->modpriv); } -#define doadd(set, type) \ - verto_ev *ev = make_ev(ctx, callback, type, flags); \ +void +verto_reinitialize(verto_ctx *ctx) +{ + verto_ev *tmp, *next; + if (!ctx) + return; + + /* Delete all events, but keep around the forkable ev structs */ + for (tmp = ctx->events; tmp; tmp = next) { + next = ctx->events->next; + + if (tmp->flags & VERTO_EV_FLAG_REINITIABLE) + ctx->funcs.ctx_del(ctx->modpriv, tmp, tmp->modpriv); + else + verto_del(tmp); + } + + /* Reinit the loop */ + ctx->funcs.ctx_reinitialize(ctx->modpriv); + + /* Recreate events that were marked forkable */ + for (tmp = ctx->events; tmp; tmp = tmp->next) { + tmp->actual = tmp->flags; + tmp->modpriv = ctx->funcs.ctx_add(ctx, tmp, &tmp->actual); + assert(tmp->modpriv); + } +} + +#define doadd(ev, set, type) \ + ev = make_ev(ctx, callback, type, flags); \ if (ev) { \ set; \ ev->actual = ev->flags; \ @@ -561,29 +598,36 @@ verto_ev * verto_add_io(verto_ctx *ctx, verto_ev_flag flags, verto_callback *callback, int fd) { + verto_ev *ev; + if (fd < 0 || !(flags & (VERTO_EV_FLAG_IO_READ | VERTO_EV_FLAG_IO_WRITE))) return NULL; - doadd(ev->option.fd = fd, VERTO_EV_TYPE_IO); + + doadd(ev, ev->option.fd = fd, VERTO_EV_TYPE_IO); } verto_ev * verto_add_timeout(verto_ctx *ctx, verto_ev_flag flags, verto_callback *callback, time_t interval) { - doadd(ev->option.interval = interval, VERTO_EV_TYPE_TIMEOUT); + verto_ev *ev; + doadd(ev, ev->option.interval = interval, VERTO_EV_TYPE_TIMEOUT); } verto_ev * verto_add_idle(verto_ctx *ctx, verto_ev_flag flags, verto_callback *callback) { - doadd(, VERTO_EV_TYPE_IDLE); + verto_ev *ev; + doadd(ev,, VERTO_EV_TYPE_IDLE); } verto_ev * verto_add_signal(verto_ctx *ctx, verto_ev_flag flags, verto_callback *callback, int signal) { + verto_ev *ev; + if (signal < 0) return NULL; #ifndef WIN32 @@ -595,13 +639,15 @@ verto_add_signal(verto_ctx *ctx, verto_ev_flag flags, if (!(flags & VERTO_EV_FLAG_PERSIST)) return NULL; } - doadd(ev->option.signal = signal, VERTO_EV_TYPE_SIGNAL); + doadd(ev, ev->option.signal = signal, VERTO_EV_TYPE_SIGNAL); } verto_ev * verto_add_child(verto_ctx *ctx, verto_ev_flag flags, verto_callback *callback, verto_proc proc) { + verto_ev *ev; + if (flags & VERTO_EV_FLAG_PERSIST) /* persist makes no sense */ return NULL; #ifdef WIN32 @@ -610,19 +656,18 @@ verto_add_child(verto_ctx *ctx, verto_ev_flag flags, if (proc < 1) #endif return NULL; - doadd(ev->option.child.proc = proc, VERTO_EV_TYPE_CHILD); + doadd(ev, ev->option.child.proc = proc, VERTO_EV_TYPE_CHILD); } -int +void verto_set_private(verto_ev *ev, void *priv, verto_callback *free) { if (!ev) - return 0; + return; if (ev->onfree && free) ev->onfree(ev->ctx, ev); ev->priv = priv; ev->onfree = free; - return 1; } void * diff --git a/src/util/verto/verto.h b/src/util/verto/verto.h index 452638de8..d7ab47956 100644 --- a/src/util/verto/verto.h +++ b/src/util/verto/verto.h @@ -59,7 +59,8 @@ typedef enum { VERTO_EV_FLAG_PRIORITY_HIGH = 1 << 3, VERTO_EV_FLAG_IO_READ = 1 << 4, VERTO_EV_FLAG_IO_WRITE = 1 << 5, - _VERTO_EV_FLAG_MAX = VERTO_EV_FLAG_IO_WRITE + VERTO_EV_FLAG_REINITIABLE = 1 << 6, + _VERTO_EV_FLAG_MAX = VERTO_EV_FLAG_REINITIABLE } verto_ev_flag; typedef void (verto_callback)(verto_ctx *ctx, verto_ev *ev); @@ -209,6 +210,20 @@ verto_run_once(verto_ctx *ctx); void verto_break(verto_ctx *ctx); +/** + * Re-initializes the verto_ctx. + * + * This function deletes all events, except those which have set the + * VERTO_EV_FLAG_REINITIABLE flag. If you fork(), you MUST call this in the + * child process after the fork! + * + * @see verto_new() + * @see verto_default() + * @param ctx The verto_ctx to re-initialize. + */ +void +verto_reinitialize(verto_ctx *ctx); + /** * Adds a callback executed when a file descriptor is ready to be read/written. * @@ -345,9 +360,8 @@ verto_add_child(verto_ctx *ctx, verto_ev_flag flags, * @param ev The verto_ev * @param priv The private value to store * @param free The callback used to free the data or NULL - * @return 1 on success or 0 on failure */ -int +void verto_set_private(verto_ev *ev, void *priv, verto_callback *free); /** -- 2.26.2