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