xref: /minix3/tests/lib/libpthread/h_resolv.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /* $NetBSD: h_resolv.c,v 1.2 2010/11/03 16:10:22 christos Exp $ */
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc  * Copyright (c) 2004, 2008 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8*11be35a1SLionel Sambuc  * by Christos Zoulas.
9*11be35a1SLionel Sambuc  *
10*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
12*11be35a1SLionel Sambuc  * are met:
13*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*11be35a1SLionel Sambuc  *
19*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30*11be35a1SLionel Sambuc  */
31*11be35a1SLionel Sambuc 
32*11be35a1SLionel Sambuc #include <sys/cdefs.h>
33*11be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008\
34*11be35a1SLionel Sambuc  The NetBSD Foundation, inc. All rights reserved.");
35*11be35a1SLionel Sambuc __RCSID("$NetBSD: h_resolv.c,v 1.2 2010/11/03 16:10:22 christos Exp $");
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include <pthread.h>
38*11be35a1SLionel Sambuc #include <stdio.h>
39*11be35a1SLionel Sambuc #include <netdb.h>
40*11be35a1SLionel Sambuc #include <stdlib.h>
41*11be35a1SLionel Sambuc #include <unistd.h>
42*11be35a1SLionel Sambuc #include <err.h>
43*11be35a1SLionel Sambuc #include <string.h>
44*11be35a1SLionel Sambuc #include <stringlist.h>
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc #define NTHREADS    10
47*11be35a1SLionel Sambuc #define NHOSTS        100
48*11be35a1SLionel Sambuc #define WS        " \t\n\r"
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc static StringList *hosts = NULL;
51*11be35a1SLionel Sambuc static int debug = 0;
52*11be35a1SLionel Sambuc static int *ask = NULL;
53*11be35a1SLionel Sambuc static int *got = NULL;
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc static void usage(void)  __attribute__((__noreturn__));
56*11be35a1SLionel Sambuc static void load(const char *);
57*11be35a1SLionel Sambuc static void resolvone(int);
58*11be35a1SLionel Sambuc static void *resolvloop(void *);
59*11be35a1SLionel Sambuc static void run(int *);
60*11be35a1SLionel Sambuc 
61*11be35a1SLionel Sambuc static pthread_mutex_t stats = PTHREAD_MUTEX_INITIALIZER;
62*11be35a1SLionel Sambuc 
63*11be35a1SLionel Sambuc static void
usage(void)64*11be35a1SLionel Sambuc usage(void)
65*11be35a1SLionel Sambuc {
66*11be35a1SLionel Sambuc 	(void)fprintf(stderr,
67*11be35a1SLionel Sambuc 		"Usage: %s [-d] [-h <nhosts>] [-n <nthreads>] <file> ...\n",
68*11be35a1SLionel Sambuc 		getprogname());
69*11be35a1SLionel Sambuc 	exit(1);
70*11be35a1SLionel Sambuc }
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc static void
load(const char * fname)73*11be35a1SLionel Sambuc load(const char *fname)
74*11be35a1SLionel Sambuc {
75*11be35a1SLionel Sambuc 	FILE *fp;
76*11be35a1SLionel Sambuc 	size_t len;
77*11be35a1SLionel Sambuc 	char *line;
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc 	if ((fp = fopen(fname, "r")) == NULL)
80*11be35a1SLionel Sambuc 		err(1, "Cannot open `%s'", fname);
81*11be35a1SLionel Sambuc 	while ((line = fgetln(fp, &len)) != NULL) {
82*11be35a1SLionel Sambuc 		char c = line[len];
83*11be35a1SLionel Sambuc 		char *ptr;
84*11be35a1SLionel Sambuc 		line[len] = '\0';
85*11be35a1SLionel Sambuc 		for (ptr = strtok(line, WS); ptr; ptr = strtok(NULL, WS))
86*11be35a1SLionel Sambuc 			sl_add(hosts, strdup(ptr));
87*11be35a1SLionel Sambuc 		line[len] = c;
88*11be35a1SLionel Sambuc 	}
89*11be35a1SLionel Sambuc 
90*11be35a1SLionel Sambuc 	(void)fclose(fp);
91*11be35a1SLionel Sambuc }
92*11be35a1SLionel Sambuc 
93*11be35a1SLionel Sambuc static void
resolvone(int n)94*11be35a1SLionel Sambuc resolvone(int n)
95*11be35a1SLionel Sambuc {
96*11be35a1SLionel Sambuc 	char buf[1024];
97*11be35a1SLionel Sambuc 	pthread_t self = pthread_self();
98*11be35a1SLionel Sambuc 	size_t i = (random() & 0x0fffffff) % hosts->sl_cur;
99*11be35a1SLionel Sambuc 	char *host = hosts->sl_str[i];
100*11be35a1SLionel Sambuc 	struct addrinfo *res;
101*11be35a1SLionel Sambuc 	int error, len;
102*11be35a1SLionel Sambuc 	if (debug) {
103*11be35a1SLionel Sambuc 		len = snprintf(buf, sizeof(buf), "%p: %d resolving %s %d\n",
104*11be35a1SLionel Sambuc 			self, n, host, (int)i);
105*11be35a1SLionel Sambuc 		(void)write(STDOUT_FILENO, buf, len);
106*11be35a1SLionel Sambuc 	}
107*11be35a1SLionel Sambuc 	error = getaddrinfo(host, NULL, NULL, &res);
108*11be35a1SLionel Sambuc 	if (debug) {
109*11be35a1SLionel Sambuc 		len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
110*11be35a1SLionel Sambuc 			self, host, error ? "not found" : "ok");
111*11be35a1SLionel Sambuc 		(void)write(STDOUT_FILENO, buf, len);
112*11be35a1SLionel Sambuc 	}
113*11be35a1SLionel Sambuc 	pthread_mutex_lock(&stats);
114*11be35a1SLionel Sambuc 	ask[i]++;
115*11be35a1SLionel Sambuc 	got[i] += error == 0;
116*11be35a1SLionel Sambuc 	pthread_mutex_unlock(&stats);
117*11be35a1SLionel Sambuc 	if (error == 0)
118*11be35a1SLionel Sambuc 		freeaddrinfo(res);
119*11be35a1SLionel Sambuc }
120*11be35a1SLionel Sambuc 
121*11be35a1SLionel Sambuc static void *
resolvloop(void * p)122*11be35a1SLionel Sambuc resolvloop(void *p)
123*11be35a1SLionel Sambuc {
124*11be35a1SLionel Sambuc 	int *nhosts = (int *)p;
125*11be35a1SLionel Sambuc 	if (*nhosts == 0)
126*11be35a1SLionel Sambuc 		return NULL;
127*11be35a1SLionel Sambuc 	do
128*11be35a1SLionel Sambuc 		resolvone(*nhosts);
129*11be35a1SLionel Sambuc 	while (--(*nhosts));
130*11be35a1SLionel Sambuc 	return NULL;
131*11be35a1SLionel Sambuc }
132*11be35a1SLionel Sambuc 
133*11be35a1SLionel Sambuc static void
run(int * nhosts)134*11be35a1SLionel Sambuc run(int *nhosts)
135*11be35a1SLionel Sambuc {
136*11be35a1SLionel Sambuc 	pthread_t self = pthread_self();
137*11be35a1SLionel Sambuc 	if (pthread_create(&self, NULL, resolvloop, nhosts) != 0)
138*11be35a1SLionel Sambuc 		err(1, "pthread_create");
139*11be35a1SLionel Sambuc }
140*11be35a1SLionel Sambuc 
141*11be35a1SLionel Sambuc int
main(int argc,char * argv[])142*11be35a1SLionel Sambuc main(int argc, char *argv[])
143*11be35a1SLionel Sambuc {
144*11be35a1SLionel Sambuc 	int nthreads = NTHREADS;
145*11be35a1SLionel Sambuc 	int nhosts = NHOSTS;
146*11be35a1SLionel Sambuc 	int i, c, done, *nleft;
147*11be35a1SLionel Sambuc 	hosts = sl_init();
148*11be35a1SLionel Sambuc 
149*11be35a1SLionel Sambuc 	srandom(1234);
150*11be35a1SLionel Sambuc 
151*11be35a1SLionel Sambuc 	while ((c = getopt(argc, argv, "dh:n:")) != -1)
152*11be35a1SLionel Sambuc 		switch (c) {
153*11be35a1SLionel Sambuc 		case 'd':
154*11be35a1SLionel Sambuc 			debug++;
155*11be35a1SLionel Sambuc 			break;
156*11be35a1SLionel Sambuc 		case 'h':
157*11be35a1SLionel Sambuc 			nhosts = atoi(optarg);
158*11be35a1SLionel Sambuc 			break;
159*11be35a1SLionel Sambuc 		case 'n':
160*11be35a1SLionel Sambuc 			nthreads = atoi(optarg);
161*11be35a1SLionel Sambuc 			break;
162*11be35a1SLionel Sambuc 		default:
163*11be35a1SLionel Sambuc 			usage();
164*11be35a1SLionel Sambuc 		}
165*11be35a1SLionel Sambuc 
166*11be35a1SLionel Sambuc 	for (i = optind; i < argc; i++)
167*11be35a1SLionel Sambuc 		load(argv[i]);
168*11be35a1SLionel Sambuc 
169*11be35a1SLionel Sambuc 	if (hosts->sl_cur == 0)
170*11be35a1SLionel Sambuc 		usage();
171*11be35a1SLionel Sambuc 
172*11be35a1SLionel Sambuc 	if ((nleft = malloc(nthreads * sizeof(int))) == NULL)
173*11be35a1SLionel Sambuc 		err(1, "malloc");
174*11be35a1SLionel Sambuc 	if ((ask = calloc(hosts->sl_cur, sizeof(int))) == NULL)
175*11be35a1SLionel Sambuc 		err(1, "calloc");
176*11be35a1SLionel Sambuc 	if ((got = calloc(hosts->sl_cur, sizeof(int))) == NULL)
177*11be35a1SLionel Sambuc 		err(1, "calloc");
178*11be35a1SLionel Sambuc 
179*11be35a1SLionel Sambuc 
180*11be35a1SLionel Sambuc 	for (i = 0; i < nthreads; i++) {
181*11be35a1SLionel Sambuc 		nleft[i] = nhosts;
182*11be35a1SLionel Sambuc 		run(&nleft[i]);
183*11be35a1SLionel Sambuc 	}
184*11be35a1SLionel Sambuc 
185*11be35a1SLionel Sambuc 	for (done = 0; !done;) {
186*11be35a1SLionel Sambuc 		done = 1;
187*11be35a1SLionel Sambuc 		for (i = 0; i < nthreads; i++) {
188*11be35a1SLionel Sambuc 			if (nleft[i] != 0) {
189*11be35a1SLionel Sambuc 				done = 0;
190*11be35a1SLionel Sambuc 				break;
191*11be35a1SLionel Sambuc 			}
192*11be35a1SLionel Sambuc 		}
193*11be35a1SLionel Sambuc 		sleep(1);
194*11be35a1SLionel Sambuc 	}
195*11be35a1SLionel Sambuc 	c = 0;
196*11be35a1SLionel Sambuc 	for (i = 0; i < (int)hosts->sl_cur; i++) {
197*11be35a1SLionel Sambuc 		if (ask[i] != got[i] && got[i] != 0) {
198*11be35a1SLionel Sambuc 			warnx("Error: host %s ask %d got %d\n",
199*11be35a1SLionel Sambuc 				hosts->sl_str[i], ask[i], got[i]);
200*11be35a1SLionel Sambuc 			c++;
201*11be35a1SLionel Sambuc 		}
202*11be35a1SLionel Sambuc 	}
203*11be35a1SLionel Sambuc 	free(nleft);
204*11be35a1SLionel Sambuc 	free(ask);
205*11be35a1SLionel Sambuc 	free(got);
206*11be35a1SLionel Sambuc 	sl_free(hosts, 1);
207*11be35a1SLionel Sambuc 	return c;
208*11be35a1SLionel Sambuc }
209