xref: /freebsd/contrib/netbsd-tests/lib/libpthread/h_resolv.c (revision d131218534977f1b2ed590380e70d59a3b20b333)
157718be8SEnji Cooper /* $NetBSD: h_resolv.c,v 1.2 2010/11/03 16:10:22 christos Exp $ */
257718be8SEnji Cooper 
357718be8SEnji Cooper /*-
457718be8SEnji Cooper  * Copyright (c) 2004, 2008 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper  * by Christos Zoulas.
957718be8SEnji Cooper  *
1057718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper  * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper  * are met:
1357718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper  *
1957718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper  */
3157718be8SEnji Cooper 
3257718be8SEnji Cooper #include <sys/cdefs.h>
3357718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008\
3457718be8SEnji Cooper  The NetBSD Foundation, inc. All rights reserved.");
3557718be8SEnji Cooper __RCSID("$NetBSD: h_resolv.c,v 1.2 2010/11/03 16:10:22 christos Exp $");
3657718be8SEnji Cooper 
3757718be8SEnji Cooper #include <pthread.h>
3857718be8SEnji Cooper #include <stdio.h>
3957718be8SEnji Cooper #include <netdb.h>
4057718be8SEnji Cooper #include <stdlib.h>
4157718be8SEnji Cooper #include <unistd.h>
4257718be8SEnji Cooper #include <err.h>
4357718be8SEnji Cooper #include <string.h>
4457718be8SEnji Cooper #include <stringlist.h>
4557718be8SEnji Cooper 
4657718be8SEnji Cooper #define NTHREADS    10
4757718be8SEnji Cooper #define NHOSTS        100
4857718be8SEnji Cooper #define WS        " \t\n\r"
4957718be8SEnji Cooper 
5057718be8SEnji Cooper static StringList *hosts = NULL;
5157718be8SEnji Cooper static int debug = 0;
5257718be8SEnji Cooper static int *ask = NULL;
5357718be8SEnji Cooper static int *got = NULL;
5457718be8SEnji Cooper 
5557718be8SEnji Cooper static void usage(void)  __attribute__((__noreturn__));
5657718be8SEnji Cooper static void load(const char *);
5757718be8SEnji Cooper static void resolvone(int);
5857718be8SEnji Cooper static void *resolvloop(void *);
5957718be8SEnji Cooper static void run(int *);
6057718be8SEnji Cooper 
6157718be8SEnji Cooper static pthread_mutex_t stats = PTHREAD_MUTEX_INITIALIZER;
6257718be8SEnji Cooper 
6357718be8SEnji Cooper static void
usage(void)6457718be8SEnji Cooper usage(void)
6557718be8SEnji Cooper {
6657718be8SEnji Cooper 	(void)fprintf(stderr,
6757718be8SEnji Cooper 		"Usage: %s [-d] [-h <nhosts>] [-n <nthreads>] <file> ...\n",
6857718be8SEnji Cooper 		getprogname());
6957718be8SEnji Cooper 	exit(1);
7057718be8SEnji Cooper }
7157718be8SEnji Cooper 
7257718be8SEnji Cooper static void
load(const char * fname)7357718be8SEnji Cooper load(const char *fname)
7457718be8SEnji Cooper {
7557718be8SEnji Cooper 	FILE *fp;
76*d1312185SJohn Baldwin 	size_t linecap;
7757718be8SEnji Cooper 	char *line;
7857718be8SEnji Cooper 
7957718be8SEnji Cooper 	if ((fp = fopen(fname, "r")) == NULL)
8057718be8SEnji Cooper 		err(1, "Cannot open `%s'", fname);
81*d1312185SJohn Baldwin 	line = NULL;
82*d1312185SJohn Baldwin 	linecap = 0;
83*d1312185SJohn Baldwin 	while (getline(&line, &linecap, fp) >= 0) {
8457718be8SEnji Cooper 		char *ptr;
85*d1312185SJohn Baldwin 
8657718be8SEnji Cooper 		for (ptr = strtok(line, WS); ptr; ptr = strtok(NULL, WS))
8757718be8SEnji Cooper 			sl_add(hosts, strdup(ptr));
8857718be8SEnji Cooper 	}
8957718be8SEnji Cooper 
9057718be8SEnji Cooper 	(void)fclose(fp);
9157718be8SEnji Cooper }
9257718be8SEnji Cooper 
9357718be8SEnji Cooper static void
resolvone(int n)9457718be8SEnji Cooper resolvone(int n)
9557718be8SEnji Cooper {
9657718be8SEnji Cooper 	char buf[1024];
9757718be8SEnji Cooper 	pthread_t self = pthread_self();
9857718be8SEnji Cooper 	size_t i = (random() & 0x0fffffff) % hosts->sl_cur;
9957718be8SEnji Cooper 	char *host = hosts->sl_str[i];
10057718be8SEnji Cooper 	struct addrinfo *res;
10157718be8SEnji Cooper 	int error, len;
10257718be8SEnji Cooper 	if (debug) {
10357718be8SEnji Cooper 		len = snprintf(buf, sizeof(buf), "%p: %d resolving %s %d\n",
10457718be8SEnji Cooper 			self, n, host, (int)i);
10557718be8SEnji Cooper 		(void)write(STDOUT_FILENO, buf, len);
10657718be8SEnji Cooper 	}
10757718be8SEnji Cooper 	error = getaddrinfo(host, NULL, NULL, &res);
10857718be8SEnji Cooper 	if (debug) {
10957718be8SEnji Cooper 		len = snprintf(buf, sizeof(buf), "%p: host %s %s\n",
11057718be8SEnji Cooper 			self, host, error ? "not found" : "ok");
11157718be8SEnji Cooper 		(void)write(STDOUT_FILENO, buf, len);
11257718be8SEnji Cooper 	}
11357718be8SEnji Cooper 	pthread_mutex_lock(&stats);
11457718be8SEnji Cooper 	ask[i]++;
11557718be8SEnji Cooper 	got[i] += error == 0;
11657718be8SEnji Cooper 	pthread_mutex_unlock(&stats);
11757718be8SEnji Cooper 	if (error == 0)
11857718be8SEnji Cooper 		freeaddrinfo(res);
11957718be8SEnji Cooper }
12057718be8SEnji Cooper 
12157718be8SEnji Cooper static void *
resolvloop(void * p)12257718be8SEnji Cooper resolvloop(void *p)
12357718be8SEnji Cooper {
12457718be8SEnji Cooper 	int *nhosts = (int *)p;
12557718be8SEnji Cooper 	if (*nhosts == 0)
12657718be8SEnji Cooper 		return NULL;
12757718be8SEnji Cooper 	do
12857718be8SEnji Cooper 		resolvone(*nhosts);
12957718be8SEnji Cooper 	while (--(*nhosts));
13057718be8SEnji Cooper 	return NULL;
13157718be8SEnji Cooper }
13257718be8SEnji Cooper 
13357718be8SEnji Cooper static void
run(int * nhosts)13457718be8SEnji Cooper run(int *nhosts)
13557718be8SEnji Cooper {
13657718be8SEnji Cooper 	pthread_t self = pthread_self();
13757718be8SEnji Cooper 	if (pthread_create(&self, NULL, resolvloop, nhosts) != 0)
13857718be8SEnji Cooper 		err(1, "pthread_create");
13957718be8SEnji Cooper }
14057718be8SEnji Cooper 
14157718be8SEnji Cooper int
main(int argc,char * argv[])14257718be8SEnji Cooper main(int argc, char *argv[])
14357718be8SEnji Cooper {
14457718be8SEnji Cooper 	int nthreads = NTHREADS;
14557718be8SEnji Cooper 	int nhosts = NHOSTS;
14657718be8SEnji Cooper 	int i, c, done, *nleft;
14757718be8SEnji Cooper 	hosts = sl_init();
14857718be8SEnji Cooper 
14957718be8SEnji Cooper 	srandom(1234);
15057718be8SEnji Cooper 
15157718be8SEnji Cooper 	while ((c = getopt(argc, argv, "dh:n:")) != -1)
15257718be8SEnji Cooper 		switch (c) {
15357718be8SEnji Cooper 		case 'd':
15457718be8SEnji Cooper 			debug++;
15557718be8SEnji Cooper 			break;
15657718be8SEnji Cooper 		case 'h':
15757718be8SEnji Cooper 			nhosts = atoi(optarg);
15857718be8SEnji Cooper 			break;
15957718be8SEnji Cooper 		case 'n':
16057718be8SEnji Cooper 			nthreads = atoi(optarg);
16157718be8SEnji Cooper 			break;
16257718be8SEnji Cooper 		default:
16357718be8SEnji Cooper 			usage();
16457718be8SEnji Cooper 		}
16557718be8SEnji Cooper 
16657718be8SEnji Cooper 	for (i = optind; i < argc; i++)
16757718be8SEnji Cooper 		load(argv[i]);
16857718be8SEnji Cooper 
16957718be8SEnji Cooper 	if (hosts->sl_cur == 0)
17057718be8SEnji Cooper 		usage();
17157718be8SEnji Cooper 
17257718be8SEnji Cooper 	if ((nleft = malloc(nthreads * sizeof(int))) == NULL)
17357718be8SEnji Cooper 		err(1, "malloc");
17457718be8SEnji Cooper 	if ((ask = calloc(hosts->sl_cur, sizeof(int))) == NULL)
17557718be8SEnji Cooper 		err(1, "calloc");
17657718be8SEnji Cooper 	if ((got = calloc(hosts->sl_cur, sizeof(int))) == NULL)
17757718be8SEnji Cooper 		err(1, "calloc");
17857718be8SEnji Cooper 
17957718be8SEnji Cooper 
18057718be8SEnji Cooper 	for (i = 0; i < nthreads; i++) {
18157718be8SEnji Cooper 		nleft[i] = nhosts;
18257718be8SEnji Cooper 		run(&nleft[i]);
18357718be8SEnji Cooper 	}
18457718be8SEnji Cooper 
18557718be8SEnji Cooper 	for (done = 0; !done;) {
18657718be8SEnji Cooper 		done = 1;
18757718be8SEnji Cooper 		for (i = 0; i < nthreads; i++) {
18857718be8SEnji Cooper 			if (nleft[i] != 0) {
18957718be8SEnji Cooper 				done = 0;
19057718be8SEnji Cooper 				break;
19157718be8SEnji Cooper 			}
19257718be8SEnji Cooper 		}
19357718be8SEnji Cooper 		sleep(1);
19457718be8SEnji Cooper 	}
19557718be8SEnji Cooper 	c = 0;
19657718be8SEnji Cooper 	for (i = 0; i < (int)hosts->sl_cur; i++) {
19757718be8SEnji Cooper 		if (ask[i] != got[i] && got[i] != 0) {
19857718be8SEnji Cooper 			warnx("Error: host %s ask %d got %d\n",
19957718be8SEnji Cooper 				hosts->sl_str[i], ask[i], got[i]);
20057718be8SEnji Cooper 			c++;
20157718be8SEnji Cooper 		}
20257718be8SEnji Cooper 	}
20357718be8SEnji Cooper 	free(nleft);
20457718be8SEnji Cooper 	free(ask);
20557718be8SEnji Cooper 	free(got);
20657718be8SEnji Cooper 	sl_free(hosts, 1);
20757718be8SEnji Cooper 	return c;
20857718be8SEnji Cooper }
209