diff options
Diffstat (limited to 'src/util/verto/verto.c')
-rw-r--r-- | src/util/verto/verto.c | 87 |
1 files changed, 66 insertions, 21 deletions
diff --git a/src/util/verto/verto.c b/src/util/verto/verto.c index f59e8bae2c..98cc30351a 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 * |