summaryrefslogtreecommitdiffstats
path: root/source3/lib/poll_funcs/poll_funcs_tevent.c
blob: 565cdaf1cfc57921b002a6f4451642f91d6766f4 (plain)
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
/*
 * Unix SMB/CIFS implementation.
 * Copyright (C) Volker Lendecke 2013,2014
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "poll_funcs_tevent.h"
#include "tevent.h"
#include "system/select.h"
#include "dlinklist.h"

/*
 * A poll_watch is asked for by the engine using this library via
 * funcs->watch_new(). It represents interest in "fd" becoming readable or
 * writable.
 */

struct poll_watch {
	struct poll_funcs_state *state;
	unsigned slot; 		/* index into state->watches[] */
	int fd;
	int events;
	void (*callback)(struct poll_watch *w, int fd, short events,
			 void *private_data);
	void *private_data;
};

struct poll_funcs_state {
	/*
	 * "watches" is the array of all watches that we have handed out via
	 * funcs->watch_new(). The "watches" array can contain NULL pointers.
	 */
	unsigned num_watches;
	struct poll_watch **watches;

	/*
	 * "contexts is the array of tevent_contexts that serve
	 * "watches". "contexts" can contain NULL pointers.
	 */
	unsigned num_contexts;
	struct poll_funcs_tevent_context **contexts;
};

struct poll_funcs_tevent_context {
	struct poll_funcs_tevent_handle *handles;
	struct poll_funcs_state *state;
	unsigned slot;		/* index into state->contexts[] */
	struct tevent_context *ev;
	struct tevent_fd **fdes; /* same indexes as state->watches[] */
};

/*
 * poll_funcs_tevent_register() hands out a struct poll_funcs_tevent_handle as
 * a void *. poll_funcs_tevent_register allows tevent_contexts to be
 * registered multiple times, and we can't add a tevent_fd for the same fd's
 * multiple times. So we have to share one poll_funcs_tevent_context.
 */
struct poll_funcs_tevent_handle {
	struct poll_funcs_tevent_handle *prev, *next;
	struct poll_funcs_tevent_context *ctx;
};

static uint16_t poll_events_to_tevent(short events)
{
	uint16_t ret = 0;

	if (events & POLLIN) {
		ret |= TEVENT_FD_READ;
	}
	if (events & POLLOUT) {
		ret |= TEVENT_FD_WRITE;
	}
	return ret;
}

static short tevent_to_poll_events(uint16_t flags)
{
	short ret = 0;

	if (flags & TEVENT_FD_READ) {
		ret |= POLLIN;
	}
	if (flags & TEVENT_FD_WRITE) {
		ret |= POLLOUT;
	}
	return ret;
}

/*
 * Find or create a free slot in state->watches[]
 */
static bool poll_funcs_watch_find_slot(struct poll_funcs_state *state,
				       unsigned *slot)
{
	struct poll_watch **watches;
	unsigned i;

	for (i=0; i<state->num_watches; i++) {
		if (state->watches[i] == NULL) {
			*slot = i;
			return true;
		}
	}

	watches = talloc_realloc(state, state->watches, struct poll_watch *,
				 state->num_watches + 1);
	if (watches == NULL) {
		return false;
	}
	watches[state->num_watches] = NULL;
	state->watches = watches;

	for (i=0; i<state->num_contexts; i++) {
		struct tevent_fd **fdes;
		struct poll_funcs_tevent_context *c = state->contexts[i];
		if (c == NULL) {
			continue;
		}
		fdes = talloc_realloc(c, c->fdes, struct tevent_fd *,
				      state->num_watches + 1);
		if (fdes == NULL) {
			return false;
		}
		c->fdes = fdes;

		fdes[state->num_watches] = NULL;
	}

	*slot = state->num_watches;
	state->num_watches += 1;

	return true;
}

static void poll_funcs_fde_handler(struct tevent_context *ev,
				   struct tevent_fd *fde, uint16_t flags,
				   void *private_data);
static int poll_watch_destructor(struct poll_watch *w);

static struct poll_watch *tevent_watch_new(
	const struct poll_funcs *funcs, int fd, short events,
	void (*callback)(struct poll_watch *w, int fd, short events,
			 void *private_data),
	void *private_data)
{
	struct poll_funcs_state *state = talloc_get_type_abort(
		funcs->private_data, struct poll_funcs_state);
	struct poll_watch *w;
	unsigned i, slot;

	if (!poll_funcs_watch_find_slot(state, &slot)) {
		return NULL;
	}

	w = talloc(state->watches, struct poll_watch);
	if (w == NULL) {
		return NULL;
	}
	w->state = state;
	w->slot = slot;
	w->fd = fd;
	w->events = poll_events_to_tevent(events);
	w->fd = fd;
	w->callback = callback;
	w->private_data = private_data;
	state->watches[slot] = w;

	talloc_set_destructor(w, poll_watch_destructor);

	for (i=0; i<state->num_contexts; i++) {
		struct poll_funcs_tevent_context *c = state->contexts[i];
		if (c == NULL) {
			continue;
		}
		c->fdes[slot] = tevent_add_fd(c->ev, c->fdes, w->fd, w->events,
					      poll_funcs_fde_handler, w);
		if (c->fdes[slot] == NULL) {
			goto fail;
		}
	}
	return w;

fail:
	TALLOC_FREE(w);
	return NULL;
}

static int poll_watch_destructor(struct poll_watch *w)
{
	struct poll_funcs_state *state = w->state;
	unsigned slot = w->slot;
	unsigned i;

	TALLOC_FREE(state->watches[slot]);

	for (i=0; i<state->num_contexts; i++) {
		struct poll_funcs_tevent_context *c = state->contexts[i];
		if (c == NULL) {
			continue;
		}
		TALLOC_FREE(c->fdes[slot]);
	}

	return 0;
}

static void tevent_watch_update(struct poll_watch *w, short events)
{
	struct poll_funcs_state *state = w->state;
	unsigned slot = w->slot;
	unsigned i;

	w->events = poll_events_to_tevent(events);

	for (i=0; i<state->num_contexts; i++) {
		struct poll_funcs_tevent_context *c = state->contexts[i];
		if (c == NULL) {
			continue;
		}
		tevent_fd_set_flags(c->fdes[slot], w->events);
	}
}

static short tevent_watch_get_events(struct poll_watch *w)
{
	return tevent_to_poll_events(w->events);
}

static void tevent_watch_free(struct poll_watch *w)
{
	TALLOC_FREE(w);
}

static struct poll_timeout *tevent_timeout_new(
	const struct poll_funcs *funcs, const struct timeval *tv,
	void (*callback)(struct poll_timeout *t, void *private_data),
	void *private_data)
{
	/* not implemented yet */
	return NULL;
}

static void tevent_timeout_update(struct poll_timeout *t,
				  const struct timespec *ts)
{
	return;
}

static void tevent_timeout_free(struct poll_timeout *t)
{
	return;
}

static int poll_funcs_state_destructor(struct poll_funcs_state *state);

struct poll_funcs *poll_funcs_init_tevent(TALLOC_CTX *mem_ctx)
{
	struct poll_funcs *f;
	struct poll_funcs_state *state;

	f = talloc(mem_ctx, struct poll_funcs);
	if (f == NULL) {
		return NULL;
	}
	state = talloc_zero(f, struct poll_funcs_state);
	if (state == NULL) {
		TALLOC_FREE(f);
		return NULL;
	}
	talloc_set_destructor(state, poll_funcs_state_destructor);

	f->watch_new = tevent_watch_new;
	f->watch_update = tevent_watch_update;
	f->watch_get_events = tevent_watch_get_events;
	f->watch_free = tevent_watch_free;
	f->timeout_new = tevent_timeout_new;
	f->timeout_update = tevent_timeout_update;
	f->timeout_free = tevent_timeout_free;
	f->private_data = state;
	return f;
}

static int poll_funcs_state_destructor(struct poll_funcs_state *state)
{
	unsigned i;
	/*
	 * Make sure the watches are cleared before the contexts. The watches
	 * have destructors attached to them that clean up the fde's
	 */
	for (i=0; i<state->num_watches; i++) {
		TALLOC_FREE(state->watches[i]);
	}
	return 0;
}

/*
 * Find or create a free slot in state->contexts[]
 */
static bool poll_funcs_context_slot_find(struct poll_funcs_state *state,
					 struct tevent_context *ev,
					 unsigned *slot)
{
	struct poll_funcs_tevent_context **contexts;
	unsigned i;

	for (i=0; i<state->num_contexts; i++) {
		struct poll_funcs_tevent_context *ctx = state->contexts[i];

		if ((ctx == NULL) || (ctx->ev == ev)) {
			*slot = i;
			return true;
		}
	}

	contexts = talloc_realloc(state, state->contexts,
				  struct poll_funcs_tevent_context *,
				  state->num_contexts + 1);
	if (contexts == NULL) {
		return false;
	}
	state->contexts = contexts;
	state->contexts[state->num_contexts] = NULL;

	*slot = state->num_contexts;
	state->num_contexts += 1;

	return true;
}

static int poll_funcs_tevent_context_destructor(
	struct poll_funcs_tevent_context *ctx);

static struct poll_funcs_tevent_context *poll_funcs_tevent_context_new(
	TALLOC_CTX *mem_ctx, struct poll_funcs_state *state,
	struct tevent_context *ev, unsigned slot)
{
	struct poll_funcs_tevent_context *ctx;
	unsigned i;

	ctx = talloc(mem_ctx, struct poll_funcs_tevent_context);
	if (ctx == NULL) {
		return NULL;
	}

	ctx->handles = NULL;
	ctx->state = state;
	ctx->ev = ev;
	ctx->slot = slot;

	ctx->fdes = talloc_array(ctx, struct tevent_fd *, state->num_watches);
	if (ctx->fdes == NULL) {
		goto fail;
	}

	for (i=0; i<state->num_watches; i++) {
		struct poll_watch *w = state->watches[i];

		if (w == NULL) {
			ctx->fdes[i] = NULL;
			continue;
		}
		ctx->fdes[i] = tevent_add_fd(ev, ctx->fdes, w->fd, w->events,
					     poll_funcs_fde_handler, w);
		if (ctx->fdes[i] == NULL) {
			goto fail;
		}
	}
	talloc_set_destructor(ctx, poll_funcs_tevent_context_destructor);
	return ctx;
fail:
	TALLOC_FREE(ctx);
	return NULL;
}

static int poll_funcs_tevent_context_destructor(
	struct poll_funcs_tevent_context *ctx)
{
	struct poll_funcs_tevent_handle *h;

	ctx->state->contexts[ctx->slot] = NULL;

	for (h = ctx->handles; h != NULL; h = h->next) {
		h->ctx = NULL;
	}

	return 0;
}

static void poll_funcs_fde_handler(struct tevent_context *ev,
				   struct tevent_fd *fde, uint16_t flags,
				   void *private_data)
{
	struct poll_watch *w = talloc_get_type_abort(
		private_data, struct poll_watch);
	short events = tevent_to_poll_events(flags);
	w->callback(w, w->fd, events, w->private_data);
}

static int poll_funcs_tevent_handle_destructor(
	struct poll_funcs_tevent_handle *handle);

void *poll_funcs_tevent_register(TALLOC_CTX *mem_ctx, struct poll_funcs *f,
				 struct tevent_context *ev)
{
	struct poll_funcs_state *state = talloc_get_type_abort(
		f->private_data, struct poll_funcs_state);
	struct poll_funcs_tevent_handle *handle;
	unsigned slot;

	handle = talloc(mem_ctx, struct poll_funcs_tevent_handle);
	if (handle == NULL) {
		return NULL;
	}

	if (!poll_funcs_context_slot_find(state, ev, &slot)) {
		goto fail;
	}
	if (state->contexts[slot] == NULL) {
		state->contexts[slot] = poll_funcs_tevent_context_new(
			state->contexts, state, ev, slot);
		if (state->contexts[slot] == NULL) {
			goto fail;
		}
	}

	handle->ctx = state->contexts[slot];
	DLIST_ADD(handle->ctx->handles, handle);
	talloc_set_destructor(handle, poll_funcs_tevent_handle_destructor);
	return handle;
fail:
	TALLOC_FREE(handle);
	return NULL;
}

static int poll_funcs_tevent_handle_destructor(
	struct poll_funcs_tevent_handle *handle)
{
	if (handle->ctx == NULL) {
		return 0;
	}
	if (handle->ctx->handles == NULL) {
		abort();
	}

	DLIST_REMOVE(handle->ctx->handles, handle);

	if (handle->ctx->handles == NULL) {
		TALLOC_FREE(handle->ctx);
	}
	return 0;
}