+ * Race near adns_beforeselect which could cause infinite timeout fixed
[adns] / regress / hcommon.c.m4
CommitLineData
bb7ba677 1m4_dnl hcommon.c
2m4_dnl (part of complex test harness, not of the library)
3m4_dnl - routines used for both record and playback
4
d942707d 5m4_dnl This file is
6m4_dnl Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
7m4_dnl
8m4_dnl It is part of adns, which is
9m4_dnl Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
10m4_dnl Copyright (C) 1999 Tony Finch <dot@dotat.at>
bb7ba677 11m4_dnl
12m4_dnl This program is free software; you can redistribute it and/or modify
13m4_dnl it under the terms of the GNU General Public License as published by
14m4_dnl the Free Software Foundation; either version 2, or (at your option)
15m4_dnl any later version.
16m4_dnl
17m4_dnl This program is distributed in the hope that it will be useful,
18m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
19m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20m4_dnl GNU General Public License for more details.
21m4_dnl
22m4_dnl You should have received a copy of the GNU General Public License
23m4_dnl along with this program; if not, write to the Free Software Foundation,
24m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26m4_include(hmacros.i4)
a4f094dd 27
7d251914 28#include <fcntl.h>
29#include <string.h>
30#include <errno.h>
914a5ff5 31#include <stdlib.h>
7d251914 32
33#include <sys/socket.h>
34#include <netinet/in.h>
35#include <arpa/inet.h>
36
a4f094dd 37#include "harness.h"
38#include "internal.h"
39
7d251914 40vbuf vb;
41FILE *Toutputfile= 0;
98b6d5b4 42struct timeval currenttime;
7d251914 43
44const struct Terrno Terrnos[]= {
45 { "EAGAIN", EAGAIN },
46 { "EINPROGRESS", EINPROGRESS },
47 { "EINTR", EINTR },
48 { "EINVAL", EINVAL },
49 { "EMSGSIZE", EMSGSIZE },
50 { "ENOBUFS", ENOBUFS },
51 { "ENOENT", ENOENT },
52 { "ENOPROTOOPT", ENOPROTOOPT },
53 { "ENOSPC", ENOSPC },
54 { "EWOULDBLOCK", EWOULDBLOCK },
a00dbcb2 55 { "EHOSTUNREACH", EHOSTUNREACH },
56 { "ECONNREFUSED", ECONNREFUSED },
57 { "EPIPE", EPIPE },
7d251914 58 { 0, 0 }
59};
a4f094dd 60
70a778e5 61static vbuf vbw;
7d251914 62
98b6d5b4 63int Hgettimeofday(struct timeval *tv, struct timezone *tz) {
64 Tmust("gettimeofday","tz",!tz);
65 *tv= currenttime;
66 return 0;
67}
68
70a778e5 69int Hwritev(int fd, const struct iovec *vector, size_t count) {
70 size_t i;
71
72 vbw.used= 0;
73 for (i=0; i<count; i++, vector++) {
74 if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem();
75 }
76 return Hwrite(fd,vbw.buf,vbw.used);
77}
7d251914 78
bb7ba677 79m4_define(`hm_syscall', `
80 hm_create_proto_q
81void Q$1(hm_args_massage($3,void)) {
82
83 vb.used= 0;
84 Tvba("$1");
85 m4_define(`hm_na',`')
86 m4_define(`hm_arg_nullptr',`')
87 m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);')
88 m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);')
8b1171e6 89 m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);')
bb7ba677 90 m4_define(`hm_arg_timeval_in_rel_null', `
91 if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec);
92 else Tvba(" $'`1=null");')
93 m4_define(`hm_arg_must', `')
94 m4_define(`hm_arg_socktype', `
95 Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");')
96 m4_define(`hm_arg_ign', `')
97 m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);')
98 m4_define(`hm_arg_fcntl_cmd_arg', `
99 if ($'`1 == F_SETFL) {
73c4c523 100 Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
101 } else if ($'`1 == F_GETFL) {
bb7ba677 102 Tvba(" $'`1=F_GETFL");
73c4c523 103 } else {
104 Tmust("$'`1","F_GETFL/F_SETFL",0);
bb7ba677 105 }')
70a778e5 106 m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
107 m4_define(`hm_arg_bytes_in', `')
bb7ba677 108 m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
109 m4_define(`hm_arg_addr_out', `Tvbf(" *$'`2=%d",$'`2);')
110 $3
7d251914 111
70a778e5 112 hm_create_nothing
113 m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
114 $3
115
7d251914 116 Q_vb();
117}
bb7ba677 118')
7d251914 119
bb7ba677 120m4_include(`hsyscalls.i4')
7d251914 121
122
123void Tvbaddr(const struct sockaddr *addr, int len) {
124 const struct sockaddr_in *ai= (const struct sockaddr_in*)addr;
125
126 assert(len==sizeof(struct sockaddr_in));
127 assert(ai->sin_family==AF_INET);
bb7ba677 128 Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port));
7d251914 129}
130
131void Tvbbytes(const void *buf, int len) {
132 const byte *bp;
133 int i;
134
68000374 135 if (!len) { Tvba("\n ."); return; }
7d251914 136 for (i=0, bp=buf; i<len; i++, bp++) {
137 if (!(i&31)) Tvba("\n ");
138 else if (!(i&3)) Tvba(" ");
139 Tvbf("%02x",*bp);
140 }
70a778e5 141 Tvba(".");
7d251914 142}
143
144void Tvbfdset(int max, const fd_set *fds) {
145 int i;
146 const char *comma= "";
147
bb7ba677 148 Tvba("[");
7d251914 149 for (i=0; i<max; i++) {
150 if (!FD_ISSET(i,fds)) continue;
151 Tvba(comma);
152 Tvbf("%d",i);
153 comma= ",";
154 }
155 Tvba("]");
156}
157
8b1171e6 158static void Tvbpollevents(int events) {
159 const char *delim= "";
160
161 events &= (POLLIN|POLLOUT|POLLPRI);
162 if (!events) { Tvba("0"); return; }
163 if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
164 if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
165 if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
166}
167
168void Tvbpollfds(const struct pollfd *fds, int nfds) {
169 const char *comma= "";
170
171 Tvba("[");
172 while (nfds>0) {
173 Tvba(comma);
174 Tvbf("{fd=%d, events=",fds->fd);
175 Tvbpollevents(fds->events);
176 Tvba(", revents=");
177 Tvbpollevents(fds->revents);
178 Tvba("}");
179 comma= ", ";
180 nfds--; fds++;
181 }
182 Tvba("]");
183}
184
7d251914 185void Tvberrno(int e) {
186 const struct Terrno *te;
a4f094dd 187
7d251914 188 for (te= Terrnos; te->n && te->v != e; te++);
43bb811f 189 assert(te->n);
190 Tvba(te->n);
a4f094dd 191}
192
7d251914 193void Tvba(const char *str) {
194 if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
195}
a4f094dd 196
7d251914 197void Tvbvf(const char *fmt, va_list al) {
198 char buf[1000];
199 buf[sizeof(buf)-2]= '\t';
200 vsnprintf(buf,sizeof(buf),fmt,al);
201 assert(buf[sizeof(buf)-2] == '\t');
a4f094dd 202
7d251914 203 Tvba(buf);
204}
205
206void Tvbf(const char *fmt, ...) {
207 va_list al;
208 va_start(al,fmt);
209 Tvbvf(fmt,al);
210 va_end(al);
211}
212
213
bb7ba677 214void Tmust(const char *call, const char *arg, int cond) {
215 if (cond) return;
216 fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
217 exit(-1);
218}
219
7d251914 220void Tfailed(const char *why) {
221 fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
222 exit(-1);
223}
224
225void Tnomem(void) {
226 Tfailed("unable to malloc/realloc");
227}
228
229void Toutputerr(void) {
230 Tfailed("write error on test harness output");
231}
914a5ff5 232
233struct malloced {
234 struct malloced *next, *back;
235 size_t sz;
236 unsigned long count;
237 struct { double d; long ul; void *p; void (*fp)(void); } data;
238};
239
240static unsigned long malloccount, mallocfailat;
241static struct { struct malloced *head, *tail; } mallocedlist;
242
243#define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head)
244
245void *Hmalloc(size_t sz) {
246 struct malloced *newnode;
247 const char *mfavar;
248 char *ep;
249
250 assert(sz);
251
252 newnode= malloc(MALLOCHSZ + sz); if (!newnode) Tnomem();
253
254 LIST_LINK_TAIL(mallocedlist,newnode);
255 newnode->sz= sz;
256 newnode->count= ++malloccount;
257 if (!mallocfailat) {
258 mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT");
259 if (mfavar) {
260 mallocfailat= strtoul(mfavar,&ep,10);
261 if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value");
262 } else {
263 mallocfailat= ~0UL;
264 }
265 }
266 assert(newnode->count != mallocfailat);
267 memset(&newnode->data,0xc7,sz);
268 return &newnode->data;
269}
270
271void Hfree(void *ptr) {
272 struct malloced *oldnode;
273
274 if (!ptr) return;
275
276 oldnode= (void*)((char*)ptr - MALLOCHSZ);
277 LIST_UNLINK(mallocedlist,oldnode);
278 memset(&oldnode->data,0x38,oldnode->sz);
279 free(oldnode);
280}
281
282void *Hrealloc(void *op, size_t nsz) {
283 struct malloced *oldnode;
284 void *np;
285 size_t osz;
286
287 if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; }
288 np= Hmalloc(nsz);
289 memcpy(np,op, osz>nsz ? nsz : osz);
290 Hfree(op);
291 return np;
292}
293
294void Hexit(int rv) {
295 struct malloced *loopnode;
296
297 Tshutdown();
298 adns__vbuf_free(&vb);
299 adns__vbuf_free(&vbw);
300 if (mallocedlist.head) {
301 fprintf(stderr,"adns test harness: memory leaked:");
302 for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next)
303 fprintf(stderr," %lu(%lu)",loopnode->count,(unsigned long)loopnode->sz);
304 putc('\n',stderr);
305 if (ferror(stderr)) exit(-1);
306 }
307 exit(rv);
308}