chiark / gitweb /
get resolver destruction right
[chiark-tcl.git] / adns / adns.c
index e768cd6bf283853c9d74d29fe446410a105d6ca6..d57c49bf1ba3e2efee7f11add33135a8e450e671 100644 (file)
@@ -37,6 +37,8 @@
  *         -quotefail-cname
  *         -cname-loose
  *         -cname-forbid
+ *         -reverse
+ *         -reverse-any ZONE-A-LIKE
  *
  * adns new-resolver [RES-OPTIONS...]                         => RESOLVER
  *        options:
@@ -46,8 +48,6 @@
  *         -noenv|-debug|-logpid
  *         -checkc-entex
  *         -checkc-freq
- *         -reverse
- *         -reverse-any ZONE-A-LIKE
  *         -config CONFIG-STRING
  *
  * adns set-default-resolver RESOLVER
  *        cancels outstanding queries
  */
 
+#define _GNU_SOURCE
+
+#include <stdio.h>
+
 #include "tables.h"
 #include "hbytes.h"
 
@@ -125,7 +129,7 @@ static int oifn_reverse_any(Tcl_Interp *ip, const OptionInfo *oi,
 
 static void optparse_blank(OptionParse *op) {
   memset(op,0,sizeof(*op));
-  op->errfile= 0;
+  op->errfile= stderr;
   op->errcallback= 0;
   op->config_string= 0;
 }
@@ -170,6 +174,7 @@ struct Resolver {
   int maxfd;
   fd_set handling[3];
   ScriptToInvoke errcallback;
+  Tcl_Obj *errstring_accum;
 };
 
 /* The default resolver is recorded using Tcl_SetAssocData with key
@@ -220,9 +225,36 @@ static const OptionInfo resolver_optioninfos[]= {
   { 0 }
 };
 
+static void adnslogfn_flushmessage(Resolver *res) {
+  scriptinv_invoke(&res->errcallback, 1, &res->errstring_accum);
+  Tcl_SetObjLength(res->errstring_accum, 0);
+}
+
 static void adnslogfn_callback(adns_state ads, void *logfndata,
                               const char *fmt, va_list al) {
-  abort(); /* fixme implement adns_logcallbackfn */
+  Resolver *res= logfndata;
+  int l, newline;
+  char *str;
+
+  l= vasprintf(&str,fmt,al);
+  if (l<0) {
+    posixerr(res->interp,errno,"construct adns log callback string");
+    Tcl_BackgroundError(res->interp);
+  }
+
+  if (l==0) { free(str); return; }
+  if ((newline= l>0 && str[l-1]=='\n')) l--;
+
+  if (!res->errstring_accum) {
+    res->errstring_accum= Tcl_NewStringObj(str,l);
+    Tcl_IncrRefCount(res->errstring_accum);
+  } else {
+    Tcl_AppendToObj(res->errstring_accum,str,l);
+  }
+  free(str);
+
+  if (newline)
+    adnslogfn_flushmessage(res);
 }
 
 static Resolver *default_resolver(Tcl_Interp *ip) {
@@ -231,11 +263,20 @@ static Resolver *default_resolver(Tcl_Interp *ip) {
 
 static void destroy_resolver(Tcl_Interp *ip, Resolver *res) {
   void *query_v;
+  int logstring_len;
+  char *rstr;
   adns_query aqu;
  
   if (res == default_resolver(ip))
     Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES);
  
+  if (res->errstring_accum) {
+    rstr= Tcl_GetStringFromObj(res->errstring_accum, &logstring_len);
+    assert(rstr);
+    if (logstring_len)
+      adnslogfn_flushmessage(res);
+  }
+
   if (res->ads) {
     /* although adns would throw these away for us, we need to
      * destroy our own data too and only adns has a list of them */
@@ -246,9 +287,11 @@ static void destroy_resolver(Tcl_Interp *ip, Resolver *res) {
       asynch_query_dispose(ip, query_v);
     }
     adns_finish(res->ads);
+    res->ads= 0;
   }
   asynch_cancelhandlers(res);
-  TFREE(res);
+  scriptinv_cancel(&res->errcallback);
+  Tcl_EventuallyFree(res, Tcl_Free);
 }
 
 static void destroy_resolver_idtabcb(Tcl_Interp *ip, void *resolver_v) {
@@ -259,8 +302,8 @@ static void destroy_resolver_defcb(ClientData resolver_v, Tcl_Interp *ip) {
 }
 
 int do_adns_destroy_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
-  destroy_resolver(ip,res_v);
   tabledataid_disposing(ip,res_v,&adnstcl_resolvers);
+  destroy_resolver(ip,res_v);
   return TCL_OK;
 }
 
@@ -275,20 +318,20 @@ static int create_resolver(Tcl_Interp *ip, const OptionParse *op,
   res->ads= 0;
   res->timertoken= 0;
   res->maxfd= 0;
-  scriptinv_init(&res->errcallback);
   for (i=0; i<3; i++) FD_ZERO(&res->handling[i]);
+  scriptinv_init(&res->errcallback);
+  res->errstring_accum= 0;
+
+  if (op->errcallback)
+    scriptinv_set(&res->errcallback, ip, op->errcallback, 0);
 
   ec= adns_init_logfn(&res->ads,
                      op->aflags | adns_if_noautosys,
                      op->config_string,
                      op->errcallback ? adnslogfn_callback : 0,
-                     op->errcallback ? (void*)op->errcallback
-                     : (void*)op->errfile);
+                     op->errcallback ? (void*)res : (void*)op->errfile);
   if (ec) { rc= posixerr(ip,ec,"create adns resolver"); goto x_rc; }
 
-  if (op->errcallback)
-    scriptinv_set(&res->errcallback, ip, op->errcallback, 0);
-
   *res_r= res;
   return TCL_OK;
 
@@ -388,33 +431,36 @@ static const OptionInfo query_optioninfos[]= {
 static int query_submit(Tcl_Interp *ip,
                        const AdnsTclRRTypeInfo *type, const char *domain,
                        int queryopts_objc, Tcl_Obj *const *queryopts_objv,
-                       adns_query *aqu_r, void *context, OptionParse *op) {
+                       adns_query *aqu_r, void *context, Resolver **res_r) {
   struct sockaddr sa;
   static const int aftry[]= { AF_INET, AF_INET6 };
+  OptionParse op;
   OptionParse res_op;
   int rc, r, ec;
   adns_state ads;
   
-  op->aflags= adns_qf_owner;
-  op->sflags= 0;
-  op->resolver= 0;
-  op->reverseany= 0;
-  rc= parse_options(ip, queryopts_objc,queryopts_objv, query_optioninfos,op);
+  op.aflags= adns_qf_owner;
+  op.sflags= 0;
+  op.resolver= 0;
+  op.reverseany= 0;
+  rc= parse_options(ip, queryopts_objc,queryopts_objv, query_optioninfos,&op);
   if (rc) return rc;
 
-  if (!op->resolver) {
-    op->resolver= default_resolver(ip);
-    if (!op->resolver) {
+  if (!op.resolver) {
+    op.resolver= default_resolver(ip);
+    if (!op.resolver) {
       optparse_blank(&res_op);
-      rc= create_resolver(ip, &res_op, &op->resolver);
+      rc= create_resolver(ip, &res_op, &op.resolver);
       if (rc) return rc;
 
       Tcl_SetAssocData(ip, ASSOC_DEFAULTRES,
-                      destroy_resolver_defcb, op->resolver);
+                      destroy_resolver_defcb, op.resolver);
     }
   }
+
+  *res_r= op.resolver;
   
-  if (op->reverseany || (op->sflags & oisf_reverse)) {
+  if (op.reverseany || (op.sflags & oisf_reverse)) {
     const int *af;
     for (af=aftry; af < af + sizeof(af)/sizeof(*af); af++) {
       memset(&sa,0,sizeof(sa));
@@ -426,17 +472,17 @@ static int query_submit(Tcl_Interp *ip,
   af_found:;
   }
 
-  ads= op->resolver->ads;
+  ads= op.resolver->ads;
 
-  if (op->reverseany) {
-    ec= adns_submit_reverse_any(ads, &sa, op->reverseany,
-                               type->number, op->aflags, context, aqu_r);
-  } else if (op->sflags & oisf_reverse) {
+  if (op.reverseany) {
+    ec= adns_submit_reverse_any(ads, &sa, op.reverseany,
+                               type->number, op.aflags, context, aqu_r);
+  } else if (op.sflags & oisf_reverse) {
     ec= adns_submit_reverse(ads, &sa,
-                           type->number, op->aflags, context, aqu_r);
+                           type->number, op.aflags, context, aqu_r);
   } else {
     ec= adns_submit(ads, domain,
-                   type->number, op->aflags, context, aqu_r);
+                   type->number, op.aflags, context, aqu_r);
   }
   if (ec)
     return posixerr(ip,ec,"submit adns query");
@@ -491,14 +537,12 @@ static int synch(Tcl_Interp *ip, const AdnsTclRRTypeInfo *rrtype,
                 const char *domain,
                 int objc, Tcl_Obj *const *objv, adns_answer **answer_r) {
   adns_query aqu;
-  OptionParse op;
   Resolver *res;
   int rc, ec;
   
-  rc= query_submit(ip,rrtype,domain,objc,objv,&aqu,0,&op);
+  rc= query_submit(ip,rrtype,domain,objc,objv,&aqu,0,&res);
   if (rc) return rc;
 
-  res= op.resolver;
   ec= adns_wait(res->ads,&aqu,answer_r,0);
   assert(!ec);
 
@@ -595,9 +639,15 @@ static void asynch_sethandlers_generic(Resolver *res,
        if (FD_ISSET(fd, &want[0])) mask |= TCL_READABLE;
        if (FD_ISSET(fd, &want[1])) mask |= TCL_WRITABLE;
        if (FD_ISSET(fd, &want[2])) mask |= TCL_EXCEPTION;
-       if (mask) Tcl_CreateFileHandler(fd,mask,asynch_filehandler,res);
-       else Tcl_DeleteFileHandler(fd);
+       if (mask) {
+         Tcl_CreateFileHandler(fd,mask,asynch_filehandler,res);
+         FD_SET(fd, &res->handling[i]);
+       } else {
+         Tcl_DeleteFileHandler(fd);
+         FD_CLR(fd, &res->handling[i]);
+       }
       }
+  res->maxfd= maxfd;
 
   Tcl_DeleteTimerHandler(res->timertoken);
 
@@ -637,7 +687,14 @@ static void asynch_check_now(Resolver *res) {
   int ec;
   Tcl_Obj *results[RESULTLIST_LLEN];
 
+  Tcl_Preserve(res);
+  
   for (;;) {
+    if (!res->ads) { /* oh, it has been destroyed! */
+      Tcl_Release(res);
+      return;
+    }
+
     aqu= 0;
     ec= adns_check(res->ads, &aqu, &answer, &query_v);
     if (ec==ESRCH || ec==EAGAIN) break;
@@ -658,6 +715,8 @@ static void asynch_check_now(Resolver *res) {
   }
 
   asynch_sethandlers(res);
+
+  Tcl_Release(res);
 }
     
 int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
@@ -667,8 +726,7 @@ int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
                   int objc, Tcl_Obj *const *objv, void **result) {
   Query *query;
   int rc;
-  Resolver *res=0;
-  OptionParse op;
+  Resolver *res= 0;
   
   query= TALLOC(sizeof(*query));
   query->ix= -1;
@@ -678,10 +736,10 @@ int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
   scriptinv_init(&query->on_fail);
   query->xargs= 0;
 
-  rc= query_submit(ip,rrtype,domain,objc,objv,&query->aqu,query,&op);
+  rc= query_submit(ip,rrtype,domain,objc,objv,&query->aqu,query,&query->res);
   if (rc) goto x_rc;
 
-  res= op.resolver;
+  res= query->res;
 
   rc= scriptinv_set(&query->on_yes, ip,on_yes, xargs);  if (rc) goto x_rc;
   rc= scriptinv_set(&query->on_no,  ip,on_no,  xargs);  if (rc) goto x_rc;