From 0e4bda4c55af57f7fb662b53ce6e06f58e12fd6c Mon Sep 17 00:00:00 2001 From: Wouter Wijngaards Date: Thu, 24 Jan 2008 09:14:07 +0000 Subject: [PATCH] cancel(). git-svn-id: file:///svn/unbound/trunk@891 be551aaa-1e26-0410-a405-d3ace91eadb9 --- doc/Changelog | 6 ++++++ libunbound/unbound.c | 27 +++++++++++++++++++++++++-- testcode/asynclook.c | 34 +++++++++++++++++++++++++--------- 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/doc/Changelog b/doc/Changelog index 3a243bc58..9ee6cb6e9 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,3 +1,9 @@ +24 January 2008: Wouter + - tested the cancel() function. + - asynclook -c (cancel) feature. + - fix fail to allocate context actions. + - make pipe nonblocking at start. + 23 January 2008: Wouter - removed debug prints from if-auto, verb-algo enables some. - libunbound QUIT setup, remove memory leaks, when using threads diff --git a/libunbound/unbound.c b/libunbound/unbound.c index 1bec20d17..148c22737 100644 --- a/libunbound/unbound.c +++ b/libunbound/unbound.c @@ -52,6 +52,7 @@ #include "util/module.h" #include "util/regional.h" #include "util/log.h" +#include "util/net_help.h" #include "services/modstack.h" #include "services/localzone.h" #include "services/cache/infra.h" @@ -82,18 +83,40 @@ ub_val_ctx_create() errno = e; return NULL; } + if(!fd_set_nonblock(ctx->rrpipe[0]) || + !fd_set_nonblock(ctx->rrpipe[1]) || + !fd_set_nonblock(ctx->qqpipe[0]) || + !fd_set_nonblock(ctx->qqpipe[1])) { + int e = errno; + close(ctx->rrpipe[0]); + close(ctx->rrpipe[1]); + close(ctx->qqpipe[0]); + close(ctx->qqpipe[1]); + free(ctx); + errno = e; + return NULL; + } lock_basic_init(&ctx->qqpipe_lock); lock_basic_init(&ctx->rrpipe_lock); lock_basic_init(&ctx->cfglock); ctx->env = (struct module_env*)calloc(1, sizeof(*ctx->env)); if(!ctx->env) { - ub_val_ctx_delete(ctx); + close(ctx->rrpipe[0]); + close(ctx->rrpipe[1]); + close(ctx->qqpipe[0]); + close(ctx->qqpipe[1]); + free(ctx); errno = ENOMEM; return NULL; } ctx->env->cfg = config_create_forlib(); if(!ctx->env->cfg) { - ub_val_ctx_delete(ctx); + close(ctx->rrpipe[0]); + close(ctx->rrpipe[1]); + close(ctx->qqpipe[0]); + close(ctx->qqpipe[1]); + free(ctx->env); + free(ctx); errno = ENOMEM; return NULL; } diff --git a/testcode/asynclook.c b/testcode/asynclook.c index 70b8f2dd5..53714275a 100644 --- a/testcode/asynclook.c +++ b/testcode/asynclook.c @@ -48,7 +48,7 @@ */ struct lookinfo { /** name to look up */ - char* qname; + char* name; /** tracking number that can be used to cancel the query */ int async_id; /** error code from libunbound */ @@ -65,8 +65,10 @@ void usage(char* argv[]) { printf("usage: %s name ...\n", argv[0]); printf("names are looked up at the same time, asynchronously.\n"); + printf("options only in this order, before any domain names.\n"); printf("-d : enable debug output\n"); printf("-t : use a resolver thread instead of forking a process\n"); + printf("-c : cancel the requests\n"); exit(1); } @@ -75,7 +77,7 @@ void lookup_is_done(void* mydata, int err, struct ub_val_result* result) { /* cast mydata back to the correct type */ struct lookinfo* info = (struct lookinfo*)mydata; - fprintf(stderr, "name %s resolved\n", info->qname); + fprintf(stderr, "name %s resolved\n", info->name); info->err = err; info->result = result; /* one less to wait for */ @@ -87,7 +89,7 @@ int main(int argc, char** argv) { struct ub_val_ctx* ctx; struct lookinfo* lookups; - int i, r; + int i, r, cancel=0; if(argc == 1) { usage(argv); } @@ -112,6 +114,11 @@ int main(int argc, char** argv) argc--; argv++; } + if(argc > 0 && strcmp(argv[0], "-c") == 0) { + cancel=1; + argc--; + argv++; + } /* allocate array for results. */ lookups = (struct lookinfo*)calloc((size_t)argc, @@ -125,16 +132,23 @@ int main(int argc, char** argv) num_wait = argc; for(i=0; ihavedata) - printf("%s: %s\n", lookups[i].qname, + printf("%s: %s\n", lookups[i].name, inet_ntop(AF_INET, lookups[i].result->data[0], buf, (socklen_t)sizeof(buf))); else { /* there is no data, why that? */ if(lookups[i].result->rcode == 0 /*noerror*/ || lookups[i].result->nxdomain) - printf("%s: no data %s\n", lookups[i].qname, + printf("%s: no data %s\n", lookups[i].name, lookups[i].result->nxdomain?"(no such host)": "(no IP4 address)"); else /* some error (from the server) */ - printf("%s: DNS error %d\n", lookups[i].qname, + printf("%s: DNS error %d\n", lookups[i].name, lookups[i].result->rcode); } } -- 2.47.2