Correct type of various printf arguments: ptrdiff_t != int
[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
3d5cde09 6m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
d942707d 7m4_dnl
8m4_dnl It is part of adns, which is
3d5cde09 9m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
bef232ae 10m4_dnl Copyright (C) 1999-2000 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 <string.h>
29#include <errno.h>
914a5ff5 30#include <stdlib.h>
4e25ff4e 31#include <stdarg.h>
7d251914 32
bbede9a3 33#include <sys/types.h>
7d251914 34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37
bbede9a3 38#include <unistd.h>
39#include <fcntl.h>
40
a4f094dd 41#include "harness.h"
42#include "internal.h"
43
7d251914 44vbuf vb;
45FILE *Toutputfile= 0;
98b6d5b4 46struct timeval currenttime;
7d251914 47
48const struct Terrno Terrnos[]= {
a80db454 49 { "EBADF", EBADF },
7d251914 50 { "EAGAIN", EAGAIN },
51 { "EINPROGRESS", EINPROGRESS },
52 { "EINTR", EINTR },
53 { "EINVAL", EINVAL },
54 { "EMSGSIZE", EMSGSIZE },
55 { "ENOBUFS", ENOBUFS },
56 { "ENOENT", ENOENT },
57 { "ENOPROTOOPT", ENOPROTOOPT },
58 { "ENOSPC", ENOSPC },
59 { "EWOULDBLOCK", EWOULDBLOCK },
a00dbcb2 60 { "EHOSTUNREACH", EHOSTUNREACH },
fb901bf5 61 { "ECONNRESET", ECONNRESET },
a00dbcb2 62 { "ECONNREFUSED", ECONNREFUSED },
63 { "EPIPE", EPIPE },
87dd6345 64 { "ENOTSOCK", ENOTSOCK },
7d251914 65 { 0, 0 }
66};
a4f094dd 67
70a778e5 68static vbuf vbw;
7d251914 69
98b6d5b4 70int Hgettimeofday(struct timeval *tv, struct timezone *tz) {
87dd6345 71 Tensurerecordfile();
98b6d5b4 72 Tmust("gettimeofday","tz",!tz);
73 *tv= currenttime;
74 return 0;
75}
76
70a778e5 77int Hwritev(int fd, const struct iovec *vector, size_t count) {
78 size_t i;
79
80 vbw.used= 0;
81 for (i=0; i<count; i++, vector++) {
82 if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem();
83 }
84 return Hwrite(fd,vbw.buf,vbw.used);
85}
7d251914 86
bb7ba677 87m4_define(`hm_syscall', `
88 hm_create_proto_q
89void Q$1(hm_args_massage($3,void)) {
90
91 vb.used= 0;
92 Tvba("$1");
93 m4_define(`hm_na',`')
94 m4_define(`hm_arg_nullptr',`')
95 m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);')
96 m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);')
8b1171e6 97 m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);')
bb7ba677 98 m4_define(`hm_arg_timeval_in_rel_null', `
99 if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec);
100 else Tvba(" $'`1=null");')
101 m4_define(`hm_arg_must', `')
102 m4_define(`hm_arg_socktype', `
103 Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");')
104 m4_define(`hm_arg_ign', `')
105 m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);')
106 m4_define(`hm_arg_fcntl_cmd_arg', `
107 if ($'`1 == F_SETFL) {
73c4c523 108 Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
109 } else if ($'`1 == F_GETFL) {
bb7ba677 110 Tvba(" $'`1=F_GETFL");
73c4c523 111 } else {
112 Tmust("$'`1","F_GETFL/F_SETFL",0);
bb7ba677 113 }')
70a778e5 114 m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
115 m4_define(`hm_arg_bytes_in', `')
bb7ba677 116 m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
117 m4_define(`hm_arg_addr_out', `Tvbf(" *$'`2=%d",$'`2);')
118 $3
7d251914 119
70a778e5 120 hm_create_nothing
121 m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
122 $3
123
7d251914 124 Q_vb();
125}
bb7ba677 126')
7d251914 127
0ebff22d 128m4_define(`hm_specsyscall', `')
7d251914 129
0ebff22d 130m4_include(`hsyscalls.i4')
7d251914 131
132void Tvbaddr(const struct sockaddr *addr, int len) {
133 const struct sockaddr_in *ai= (const struct sockaddr_in*)addr;
134
135 assert(len==sizeof(struct sockaddr_in));
136 assert(ai->sin_family==AF_INET);
bb7ba677 137 Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port));
7d251914 138}
139
140void Tvbbytes(const void *buf, int len) {
141 const byte *bp;
142 int i;
143
68000374 144 if (!len) { Tvba("\n ."); return; }
7d251914 145 for (i=0, bp=buf; i<len; i++, bp++) {
146 if (!(i&31)) Tvba("\n ");
147 else if (!(i&3)) Tvba(" ");
148 Tvbf("%02x",*bp);
149 }
70a778e5 150 Tvba(".");
7d251914 151}
152
153void Tvbfdset(int max, const fd_set *fds) {
154 int i;
155 const char *comma= "";
156
bb7ba677 157 Tvba("[");
7d251914 158 for (i=0; i<max; i++) {
159 if (!FD_ISSET(i,fds)) continue;
160 Tvba(comma);
161 Tvbf("%d",i);
162 comma= ",";
163 }
164 Tvba("]");
165}
166
8b1171e6 167static void Tvbpollevents(int events) {
168 const char *delim= "";
169
170 events &= (POLLIN|POLLOUT|POLLPRI);
171 if (!events) { Tvba("0"); return; }
172 if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
173 if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
174 if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
175}
176
177void Tvbpollfds(const struct pollfd *fds, int nfds) {
178 const char *comma= "";
179
180 Tvba("[");
181 while (nfds>0) {
182 Tvba(comma);
183 Tvbf("{fd=%d, events=",fds->fd);
184 Tvbpollevents(fds->events);
185 Tvba(", revents=");
186 Tvbpollevents(fds->revents);
187 Tvba("}");
188 comma= ", ";
189 nfds--; fds++;
190 }
191 Tvba("]");
192}
193
7d251914 194void Tvberrno(int e) {
195 const struct Terrno *te;
a4f094dd 196
7d251914 197 for (te= Terrnos; te->n && te->v != e; te++);
43bb811f 198 assert(te->n);
199 Tvba(te->n);
a4f094dd 200}
201
7d251914 202void Tvba(const char *str) {
203 if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
204}
a4f094dd 205
7d251914 206void Tvbvf(const char *fmt, va_list al) {
207 char buf[1000];
208 buf[sizeof(buf)-2]= '\t';
209 vsnprintf(buf,sizeof(buf),fmt,al);
210 assert(buf[sizeof(buf)-2] == '\t');
a4f094dd 211
7d251914 212 Tvba(buf);
213}
214
215void Tvbf(const char *fmt, ...) {
216 va_list al;
217 va_start(al,fmt);
218 Tvbvf(fmt,al);
219 va_end(al);
220}
221
222
bb7ba677 223void Tmust(const char *call, const char *arg, int cond) {
224 if (cond) return;
225 fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
226 exit(-1);
227}
228
7d251914 229void Tfailed(const char *why) {
230 fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
231 exit(-1);
232}
233
234void Tnomem(void) {
235 Tfailed("unable to malloc/realloc");
236}
237
238void Toutputerr(void) {
239 Tfailed("write error on test harness output");
240}
914a5ff5 241
242struct malloced {
243 struct malloced *next, *back;
244 size_t sz;
245 unsigned long count;
246 struct { double d; long ul; void *p; void (*fp)(void); } data;
247};
248
249static unsigned long malloccount, mallocfailat;
250static struct { struct malloced *head, *tail; } mallocedlist;
251
252#define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head)
253
254void *Hmalloc(size_t sz) {
255 struct malloced *newnode;
256 const char *mfavar;
257 char *ep;
258
259 assert(sz);
260
261 newnode= malloc(MALLOCHSZ + sz); if (!newnode) Tnomem();
262
263 LIST_LINK_TAIL(mallocedlist,newnode);
264 newnode->sz= sz;
265 newnode->count= ++malloccount;
266 if (!mallocfailat) {
267 mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT");
268 if (mfavar) {
269 mallocfailat= strtoul(mfavar,&ep,10);
270 if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value");
271 } else {
272 mallocfailat= ~0UL;
273 }
274 }
275 assert(newnode->count != mallocfailat);
276 memset(&newnode->data,0xc7,sz);
277 return &newnode->data;
278}
279
280void Hfree(void *ptr) {
281 struct malloced *oldnode;
282
283 if (!ptr) return;
284
285 oldnode= (void*)((char*)ptr - MALLOCHSZ);
286 LIST_UNLINK(mallocedlist,oldnode);
287 memset(&oldnode->data,0x38,oldnode->sz);
288 free(oldnode);
289}
290
291void *Hrealloc(void *op, size_t nsz) {
292 struct malloced *oldnode;
293 void *np;
294 size_t osz;
295
296 if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; }
297 np= Hmalloc(nsz);
298 memcpy(np,op, osz>nsz ? nsz : osz);
299 Hfree(op);
300 return np;
301}
302
303void Hexit(int rv) {
304 struct malloced *loopnode;
305
306 Tshutdown();
307 adns__vbuf_free(&vb);
308 adns__vbuf_free(&vbw);
309 if (mallocedlist.head) {
310 fprintf(stderr,"adns test harness: memory leaked:");
311 for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next)
312 fprintf(stderr," %lu(%lu)",loopnode->count,(unsigned long)loopnode->sz);
313 putc('\n',stderr);
314 if (ferror(stderr)) exit(-1);
315 }
316 exit(rv);
317}