File: dnsthread.c

package info (click to toggle)
spiped 1.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,328 kB
  • sloc: ansic: 11,951; sh: 1,081; makefile: 629; perl: 121
file content (468 lines) | stat: -rw-r--r-- 10,759 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include <sys/socket.h>

#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "events.h"
#include "noeintr.h"
#include "sock.h"
#include "warnp.h"

#include "dnsthread.h"

/* Thread management structure. */
struct dnsthread_internal {
	/* Threading glue. */
	pthread_t thr;		/* Thread ID. */
	pthread_mutex_t	mtx;	/* Controls access to this structure. */
	pthread_cond_t cv;	/* Thread sleeps on this. */

	/* State management. */
	int state;		/* THREAD_* as below. */
	int wakeupsock[2];	/* Writes to [0], reads from [1]. */

	/* Address resolution variables. */
	char * addr;		/* Address to be resolved. */
	struct sock_addr ** sas;	/* Results. */
	int res_errno;		/* Errno to be passed back on failure. */

	/* Callback to occur when resolution is complete. */
	int (* callback)(void *, struct sock_addr **);	/* Callback. */
	void * cookie;		/* Cookie. */
};

/* Cookie used by dnsthread_resolve. */
struct resolve_cookie {
	int (* callback)(void *, struct sock_addr **);
	void * cookie;
	DNSTHREAD T;
};

/*
 * Thread states.  _resolveone moves the thread from SLEEPING to HASWORK;
 * workthread moves the thread from HASWORK to SLEEPING; and _kill moves the
 * thread from either state to SUICIDE.
 */
#define	THREAD_SLEEPING 0
#define THREAD_HASWORK 1
#define THREAD_SUICIDE 2

/* Callback functions used below. */
static int callback_resolveone(void * cookie);
static int callback_resolve(void *, struct sock_addr **);

/* Address resolution thread. */
static void *
workthread(void * cookie)
{
	struct dnsthread_internal * T = cookie;
	char * addr;
	struct sock_addr ** sas;
	int res_errno = 0;
	int rc;
	uint8_t zero = 0;

	/* Grab the mutex. */
	if ((rc = pthread_mutex_lock(&T->mtx)) != 0) {
		warn0("pthread_mutex_lock: %s", strerror(rc));
		exit(1);
	}

	/* Infinite loop doing work until told to suicide. */
	do {
		/*
		 * Sleep on the condition variable as long as we're in the
		 * SLEEPING state.
		 */
		while (T->state == THREAD_SLEEPING) {
			/* Sleep until we're woken up. */
			if ((rc = pthread_cond_wait(&T->cv, &T->mtx)) != 0) {
				warn0("pthread_cond_wait: %s", strerror(rc));
				exit(1);
			}
		}

		/* If we need to kill ourself, stop looping. */
		if (T->state == THREAD_SUICIDE)
			break;

		/* Grab the work. */
		addr = T->addr;

		/* Release the mutex. */
		if ((rc = pthread_mutex_unlock(&T->mtx)) != 0) {
			warn0("pthread_mutex_unlock: %s", strerror(rc));
			exit(1);
		}

		/* Perform the address resolution. */
		if ((sas = sock_resolve(addr)) == NULL)
			res_errno = errno;

		/* Grab the mutex again. */
		if ((rc = pthread_mutex_lock(&T->mtx)) != 0) {
			warn0("pthread_mutex_lock: %s", strerror(rc));
			exit(1);
		}

		/* Write the answer back. */
		T->sas = sas;
		T->res_errno = res_errno;

		/* Send a completion message. */
		if (noeintr_write(T->wakeupsock[0], &zero, 1) != 1) {
			warnp("Error writing to wakeup socket");
			exit(1);
		}

		/* Return to sleeping, unless we were instructed to die. */
		if (T->state != THREAD_SUICIDE)
			T->state = THREAD_SLEEPING;
	} while (1);

	/* Close the socket pair. */
	if (close(T->wakeupsock[1]))
		warnp("close");
	if (close(T->wakeupsock[0]))
		warnp("close");

	/* Destroy the condition variable. */
	if ((rc = pthread_cond_destroy(&T->cv)) != 0) {
		warn0("pthread_cond_destroy: %s", strerror(rc));
		exit(1);
	}

	/* Release the mutex. */
	if ((rc = pthread_mutex_unlock(&T->mtx)) != 0) {
		warn0("pthread_mutex_unlock: %s", strerror(rc));
		exit(1);
	}

	/* Destroy the mutex. */
	if ((rc = pthread_mutex_destroy(&T->mtx)) != 0) {
		warn0("pthread_mutex_destroy: %s", strerror(rc));
		exit(1);
	}

	/* Free the control structure. */
	free(T);

	/* Successful thread termination. */
	return (NULL);
}

/**
 * dnsthread_spawn(void):
 * Spawn a thread for performing address resolution.  Return a token which can
 * be passed to dnsthread_resolveone() and dnsthread_kill().
 */
DNSTHREAD
dnsthread_spawn(void)
{
	struct dnsthread_internal * T;
	int rc;

	/* Allocate a thread management structure. */
	if ((T = malloc(sizeof(struct dnsthread_internal))) == NULL)
		goto err0;

	/* Create and lock a mutex. */
	if ((rc = pthread_mutex_init(&T->mtx, NULL)) != 0) {
		warn0("pthread_mutex_init: %s", strerror(rc));
		goto err1;
	}
	if ((rc = pthread_mutex_lock(&T->mtx)) != 0) {
		warn0("pthread_mutex_lock: %s", strerror(rc));
		goto err2;
	}

	/* Create state-changed condition variable. */
	if ((rc = pthread_cond_init(&T->cv, NULL)) != 0) {
		warn0("pthread_cond_init: %s", strerror(rc));
		goto err3;
	}

	/* Create wakeup socketpair. */
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, T->wakeupsock)) {
		warnp("socketpair");
		goto err4;
	}

	/* The thread starts out sleeping. */
	T->state = THREAD_SLEEPING;

	/* Create the thread. */
	if ((rc = pthread_create(&T->thr, NULL, workthread, T)) != 0) {
		warn0("pthread_create: %s", strerror(rc));
		goto err5;
	}

	/* Unlock the mutex. */
	if ((rc = pthread_mutex_unlock(&T->mtx)) != 0) {
		warn0("pthread_mutex_unlock: %s", strerror(rc));
		goto err0;
	}

	/* Success! */
	return (T);

err5:
	if (close(T->wakeupsock[1]))
		warnp("close");
	if (close(T->wakeupsock[0]))
		warnp("close");
err4:
	pthread_cond_destroy(&T->cv);
err3:
	pthread_mutex_unlock(&T->mtx);
err2:
	pthread_mutex_destroy(&T->mtx);
err1:
	free(T);
err0:
	/* Failure! */
	return (NULL);
}

/**
 * dnsthread_resolveone(T, addr, callback, cookie):
 * Using the thread for which ${T} was returned by dnsthread_spawn(), resolve
 * the address ${addr}, which must be in one of the forms accepted by
 * sock_resolve().  If ${T} is already resolving an address, do not
 * resolve this address and instead return with errno == EALREADY.  Upon
 * completion, invoke ${callback}(${cookie}, sas), where ${sas} is a
 * NULL-terminated array of pointers to sock_addr structures or NULL on
 * resolution failure.
 */
int
dnsthread_resolveone(DNSTHREAD T, const char * addr,
    int (* callback)(void *, struct sock_addr **), void * cookie)
{
	int err = 0;
	int rc;

	/* Grab the mutex. */
	if ((rc = pthread_mutex_lock(&T->mtx)) != 0) {
		warn0("pthread_mutex_lock: %s", strerror(rc));
		goto err0;
	}

	/* If the resolver is already busy, fail. */
	if (T->state == THREAD_HASWORK) {
		err = EALREADY;
		goto ealready;
	}

	/* Duplicate the address to be resolved. */
	if ((T->addr = strdup(addr)) == NULL)
		goto err1;

	/* Remember what callback we'll need to do eventually. */
	T->callback = callback;
	T->cookie = cookie;

	/* There is now work for the thread to do. */
	T->state = THREAD_HASWORK;

	/* Wake up the worker thread. */
	if ((rc = pthread_cond_signal(&T->cv)) != 0) {
		warn0("pthread_cond_signal: %s", strerror(rc));
		goto err1;
	}

	/* We want a callback when the worker thread pokes us. */
	if (events_network_register(callback_resolveone, T, T->wakeupsock[1],
	    EVENTS_NETWORK_OP_READ)) {
		warnp("Error registering wakeup listener");
		goto err1;
	}

ealready:
	/* Release the mutex. */
	if ((rc = pthread_mutex_unlock(&T->mtx)) != 0) {
		warn0("pthread_mutex_unlock: %s", strerror(rc));
		goto err0;
	}

	/* If err was set earlier, store the value in errno. */
	if (err)
		errno = err;

	/* Success! */
	return (0);

err1:
	pthread_mutex_unlock(&T->mtx);
err0:
	/* Failure! */
	return (-1);
}

/* Callback for dnsthread_resolveone, from wakeup socket. */
static int
callback_resolveone(void * cookie)
{
	struct dnsthread_internal * T = cookie;
	struct sock_addr ** sas;
	uint8_t zero;
	int res_errno = 0;
	int (* callback)(void *, struct sock_addr **);
	void * cb_cookie;
	int rc;

	/* Drain the byte from the socketpair. */
	if (read(T->wakeupsock[1], &zero, 1) != 1) {
		warn0("Error reading from wakeup socket");
		goto err0;
	}

	/* Grab the mutex. */
	if ((rc = pthread_mutex_lock(&T->mtx)) != 0) {
		warn0("pthread_mutex_lock: %s", strerror(rc));
		goto err0;
	}

	/* Free the (strduped) address which was to be resolved. */
	free(T->addr);

	/* Grab the result. */
	sas = T->sas;
	res_errno = T->res_errno;

	/* Grab the callback. */
	callback = T->callback;
	cb_cookie = T->cookie;

	/* Release the mutex. */
	if ((rc = pthread_mutex_unlock(&T->mtx)) != 0) {
		warn0("pthread_mutex_unlock: %s", strerror(rc));
		goto err0;
	}

	/* Perform the callback. */
	if (sas == NULL)
		errno = res_errno;
	return ((callback)(cb_cookie, sas));

err0:
	/* Failure! */
	return (-1);
}

/**
 * dnsthread_kill(T):
 * Instruct an address resolution thread to die.  If the thread does not have
 * an address resolution operation currently pending, wait for the thread to
 * die before returning.
 */
int
dnsthread_kill(DNSTHREAD T)
{
	int rc;
	int ostate;
	pthread_t thr;

	/* Lock the control structure. */
	if ((rc = pthread_mutex_lock(&T->mtx)) != 0) {
		warn0("pthread_mutex_lock: %s", strerror(rc));
		goto err0;
	}

	/* Remember what state the thread is currently in. */
	ostate = T->state;

	/* Grab the thread ID. */
	thr = T->thr;

	/* Tell the thread to die, and wake it up. */
	T->state = THREAD_SUICIDE;
	if ((rc = pthread_cond_signal(&T->cv)) != 0) {
		warn0("pthread_cond_signal: %s", strerror(rc));
		goto err1;
	}

	/* Unlock the control structure. */
	if ((rc = pthread_mutex_unlock(&T->mtx)) != 0) {
		warn0("pthread_mutex_unlock: %s", strerror(rc));
		goto err0;
	}

	/* If the thread was sleeping, wait for it to wake up and die. */
	if (ostate == THREAD_SLEEPING) {
		if ((rc = pthread_join(thr, NULL)) != 0) {
			warn0("pthread_join: %s", strerror(rc));
			goto err0;
		}
	}

	/* Success! */
	return (0);

err1:
	pthread_mutex_unlock(&T->mtx);
err0:
	/* Failure! */
	return (-1);
}

/**
 * dnsthread_resolve(addr, callback, cookie):
 * Perform a non-blocking address resolution of ${addr}.  This function may
 * spawn a thread internally.
 */
int
dnsthread_resolve(const char * addr,
    int (* callback)(void *, struct sock_addr **), void * cookie)
{
	struct resolve_cookie * R;

	/* Bake a cookie. */
	if ((R = malloc(sizeof(struct resolve_cookie))) == NULL)
		goto err0;
	R->callback = callback;
	R->cookie = cookie;

	/* Spawn a thread. */
	if ((R->T = dnsthread_spawn()) == NULL)
		goto err1;

	/* Launch the request. */
	if (dnsthread_resolveone(R->T, addr, callback_resolve, R))
		goto err2;

	/* Success! */
	return (0);

err2:
	dnsthread_kill(R->T);
err1:
	free(R);
err0:
	/* Failure! */
	return (-1);
}

/* Callback for dnsthread_resolve, from dnsthread_resolveone. */
static int
callback_resolve(void * cookie, struct sock_addr ** sas)
{
	struct resolve_cookie * R = cookie;
	int rc;

	/* Invoke the upstream callback. */
	rc = (R->callback)(R->cookie, sas);

	/* Kill the resolver thread. */
	if (dnsthread_kill(R->T))
		rc = -1;

	/* Free our cookie. */
	free(R);

	/* Return upstream return code or failure. */
	return (rc);
}