chiark / gitweb /
regress: fuzzraw: Improve sync lost msg
[adns.git] / regress / hfuzzraw.c.m4
index 82293553e55dfe61213779141fda75042fbd4843..0ed71a2c326232cba12bb4e72fdac6b58ea575ec 100644 (file)
@@ -46,36 +46,14 @@ static vbuf fdtab;
 #define FDF_OPEN     001u
 #define FDF_NONBLOCK 002u
 
-static FILE *Tinputfile;
-static int stdout_enable;
+static FILE *Tinputfile, *traceout;
+static int traceprint;
 
-static void Tflushstdout( void) {
-  if (fflush(stdout)) Toutputerr();
+static void Tflushtrace( void) {
+  if (fflush(traceout)) Toutputerr();
 }
 
-void Q_vb(void) {
-  if (!adns__vbuf_append(&vb,"",1)) Tnomem();
-  if (fprintf(stdout," %s\n",vb.buf) == EOF) Toutputerr();
-  Tflushstdout();
-}
-
-static void Pformat(const char *what) {
-  fprintf(stderr,"adns test harness: format error in raw log input file: %s\n",what);
-  exit(-1);
-}
-
-extern void Tshutdown(void) {
-  int c= fgetc(Tinputfile);
-  if (c!=EOF) Pformat("unwanted additional syscall reply data");
-  if (ferror(Tinputfile)) Tfailed("read test log input (at end)");
-}
-
-static void Pcheckinput(void) {
-  if (ferror(Tinputfile)) Tfailed("read test log input file");
-  if (feof(Tinputfile)) Pformat("eof at syscall reply");
-}
-
-void Tensurerecordfile(void) {
+void Tensuresetup(void) {
   static int done;
 
   if (done) return;
@@ -93,15 +71,40 @@ void Tensurerecordfile(void) {
     if (!adns__vbuf_append(&fdtab,&fdfstd,1)) Tnomem();
   }
 
-  const char *proutstr= getenv("ADNS_TEST_FUZZRAW_STDOUT_ENABLE");
-  if (proutstr) stdout_enable= atoi(proutstr);
+  const char *traceprintstr= getenv("ADNS_TEST_FUZZRAW_TRACEPRINT");
+  if (traceprintstr) {
+    traceprint= atoi(traceprintstr);
+    traceout= fdopen(2,"w");  if (!traceout) Tfailed("fdopen for traceout");
+  }
+}
+
+void Q_vb(void) {
+  if (!traceprint) return; /* hcommon.c.m4 can call Q_vb directly */
+  if (!adns__vbuf_append(&vb,"",1)) Tnomem();
+  if (fprintf(traceout," %s\n",vb.buf) == EOF) Toutputerr();
+  Tflushtrace();
+}
+
+static void Pformat(const char *what) {
+  fprintf(stderr,"adns test harness: format error in raw log input file: %s\n",what);
+  exit(-1);
 }
 
+extern void Tshutdown(void) {
+  int c= fgetc(Tinputfile);
+  if (c!=EOF) Pformat("unwanted additional syscall reply data");
+  if (ferror(Tinputfile)) Tfailed("read test log input (at end)");
+}
+
+static void Pcheckinput(void) {
+  if (ferror(Tinputfile)) Tfailed("read test log input file");
+  if (feof(Tinputfile)) Pformat("eof at syscall reply");
+}
 
 static void P_read_dump(const unsigned char *p0, size_t count, ssize_t d) {
-  fputs(" | ",stdout);
+  fputs(" | ",traceout);
   while (count) {
-    fprintf(stdout,"%02x", *p0);
+    fprintf(traceout,"%02x", *p0);
     p0 += d;
     count--;
   }
@@ -112,14 +115,14 @@ static void P_read(void *p, size_t sz, const char *what) {
   ssize_t got = fread(p,1,sz,Tinputfile);
   Pcheckinput();
   assert(got==sz);
-  if (stdout_enable && sz) {
-    fprintf(stdout,"%8lx %8s:",pos,what);
+  if (traceprint>1 && sz) {
+    fprintf(traceout,"%8lx %8s:",pos,what);
     P_read_dump(p, sz, +1);
     if (sz<=16) {
       P_read_dump((const unsigned char *)p+sz-1, sz, -1);
     }
-    fputs(" |\n",stdout);
-    Tflushstdout();
+    fputs(" |\n",traceout);
+    Tflushtrace();
   }
 }
 
@@ -196,6 +199,13 @@ static void Psync(const char *exp, char *got, size_t sz, const char *what) {
   if (memcmp(exp,got,sz)) Pformat(what);
 }
 #endif
+m4_define(`syscall_sync',`
+#ifdef FUZZRAW_SYNC
+  hm_fr_syscall_ident($'`1)
+  static char sync_got[sizeof(sync_expect)];
+  Psync(sync_expect, sync_got, sizeof(sync_got), "sync lost: program did $1");
+#endif
+')
 
 #ifdef HAVE_POLL
 static void Ppollfds(struct pollfd *fds, int nfds, int *r_io) {
@@ -213,10 +223,7 @@ static void Ppollfds(struct pollfd *fds, int nfds, int *r_io) {
 static int P_succfail(void) {
   int e;
   P_READ(e);
-  if (e<0 && -e<Tnerrnos) {
-    errno= Terrnos[-e].v;
-    return -1;
-  } else if (e>0 && e<=255) {
+  if (e>0) {
     errno= e;
     return -1;
   } else if (e) {
@@ -238,18 +245,14 @@ int H$1(hm_args_massage($3,void)) {
  hm_create_hqcall_init($1)
  $3
 
- Tensurerecordfile();
+ Tensuresetup();
 
- if (stdout_enable) {
+ if (traceprint) {
    hm_create_hqcall_args
    Q$1(hm_args_massage($3));
  }
 
-#ifdef FUZZRAW_SYNC
-  hm_fr_syscall_ident($'`1)
-  static char sync_got[sizeof(sync_expect)];
-  Psync(sync_expect, sync_got, sizeof(sync_got), "sync lost: exp=$1");
-#endif
+  syscall_sync($'`1)
 
  m4_define(`hm_rv_succfail',`
   r= P_succfail();
@@ -330,6 +333,7 @@ m4_define(`hm_specsyscall', `')
 m4_include(`hsyscalls.i4')
 
 int Hclose(int fd) {
+  syscall_sync(close)
   P_fdf(fd);
   fdtab.buf[fd]= 0;
   return P_succfail();