2 * Unix SMB/CIFS implementation.
3 * Copyright (C) Volker Lendecke 2013,2014
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "poll_funcs_tevent.h"
21 #include "system/select.h"
22 #include "lib/util/dlinklist.h"
25 * A poll_watch is asked for by the engine using this library via
26 * funcs->watch_new(). It represents interest in "fd" becoming readable or
31 struct poll_funcs_state *state;
32 unsigned slot; /* index into state->watches[] */
35 void (*callback)(struct poll_watch *w, int fd, short events,
40 struct poll_funcs_state {
42 * "watches" is the array of all watches that we have handed out via
43 * funcs->watch_new(). The "watches" array can contain NULL pointers.
46 struct poll_watch **watches;
49 * "contexts is the array of tevent_contexts that serve
50 * "watches". "contexts" can contain NULL pointers.
52 unsigned num_contexts;
53 struct poll_funcs_tevent_context **contexts;
56 struct poll_funcs_tevent_context {
57 struct poll_funcs_tevent_handle *handles;
58 struct poll_funcs_state *state;
59 unsigned slot; /* index into state->contexts[] */
60 struct tevent_context *ev;
61 struct tevent_fd **fdes; /* same indexes as state->watches[] */
65 * poll_funcs_tevent_register() hands out a struct poll_funcs_tevent_handle as
66 * a void *. poll_funcs_tevent_register allows tevent_contexts to be
67 * registered multiple times, and we can't add a tevent_fd for the same fd's
68 * multiple times. So we have to share one poll_funcs_tevent_context.
70 struct poll_funcs_tevent_handle {
71 struct poll_funcs_tevent_handle *prev, *next;
72 struct poll_funcs_tevent_context *ctx;
75 static uint16_t poll_events_to_tevent(short events)
79 if (events & POLLIN) {
80 ret |= TEVENT_FD_READ;
82 if (events & POLLOUT) {
83 ret |= TEVENT_FD_WRITE;
88 static short tevent_to_poll_events(uint16_t flags)
92 if (flags & TEVENT_FD_READ) {
95 if (flags & TEVENT_FD_WRITE) {
102 * Find or create a free slot in state->watches[]
104 static bool poll_funcs_watch_find_slot(struct poll_funcs_state *state,
107 struct poll_watch **watches;
110 for (i=0; i<state->num_watches; i++) {
111 if (state->watches[i] == NULL) {
117 watches = talloc_realloc(state, state->watches, struct poll_watch *,
118 state->num_watches + 1);
119 if (watches == NULL) {
122 watches[state->num_watches] = NULL;
123 state->watches = watches;
125 for (i=0; i<state->num_contexts; i++) {
126 struct tevent_fd **fdes;
127 struct poll_funcs_tevent_context *c = state->contexts[i];
131 fdes = talloc_realloc(c, c->fdes, struct tevent_fd *,
132 state->num_watches + 1);
138 fdes[state->num_watches] = NULL;
141 *slot = state->num_watches;
142 state->num_watches += 1;
147 static void poll_funcs_fde_handler(struct tevent_context *ev,
148 struct tevent_fd *fde, uint16_t flags,
150 static int poll_watch_destructor(struct poll_watch *w);
152 static struct poll_watch *tevent_watch_new(
153 const struct poll_funcs *funcs, int fd, short events,
154 void (*callback)(struct poll_watch *w, int fd, short events,
158 struct poll_funcs_state *state = talloc_get_type_abort(
159 funcs->private_data, struct poll_funcs_state);
160 struct poll_watch *w;
163 if (!poll_funcs_watch_find_slot(state, &slot)) {
167 w = talloc(state->watches, struct poll_watch);
174 w->events = poll_events_to_tevent(events);
176 w->callback = callback;
177 w->private_data = private_data;
178 state->watches[slot] = w;
180 talloc_set_destructor(w, poll_watch_destructor);
182 for (i=0; i<state->num_contexts; i++) {
183 struct poll_funcs_tevent_context *c = state->contexts[i];
187 c->fdes[slot] = tevent_add_fd(c->ev, c->fdes, w->fd, w->events,
188 poll_funcs_fde_handler, w);
189 if (c->fdes[slot] == NULL) {
200 static int poll_watch_destructor(struct poll_watch *w)
202 struct poll_funcs_state *state = w->state;
203 unsigned slot = w->slot;
206 TALLOC_FREE(state->watches[slot]);
208 for (i=0; i<state->num_contexts; i++) {
209 struct poll_funcs_tevent_context *c = state->contexts[i];
213 TALLOC_FREE(c->fdes[slot]);
219 static void tevent_watch_update(struct poll_watch *w, short events)
221 struct poll_funcs_state *state = w->state;
222 unsigned slot = w->slot;
225 w->events = poll_events_to_tevent(events);
227 for (i=0; i<state->num_contexts; i++) {
228 struct poll_funcs_tevent_context *c = state->contexts[i];
232 tevent_fd_set_flags(c->fdes[slot], w->events);
236 static short tevent_watch_get_events(struct poll_watch *w)
238 return tevent_to_poll_events(w->events);
241 static void tevent_watch_free(struct poll_watch *w)
246 static struct poll_timeout *tevent_timeout_new(
247 const struct poll_funcs *funcs, const struct timeval *tv,
248 void (*callback)(struct poll_timeout *t, void *private_data),
251 /* not implemented yet */
255 static void tevent_timeout_update(struct poll_timeout *t,
256 const struct timespec *ts)
261 static void tevent_timeout_free(struct poll_timeout *t)
266 static int poll_funcs_state_destructor(struct poll_funcs_state *state);
268 struct poll_funcs *poll_funcs_init_tevent(TALLOC_CTX *mem_ctx)
270 struct poll_funcs *f;
271 struct poll_funcs_state *state;
273 f = talloc(mem_ctx, struct poll_funcs);
277 state = talloc_zero(f, struct poll_funcs_state);
282 talloc_set_destructor(state, poll_funcs_state_destructor);
284 f->watch_new = tevent_watch_new;
285 f->watch_update = tevent_watch_update;
286 f->watch_get_events = tevent_watch_get_events;
287 f->watch_free = tevent_watch_free;
288 f->timeout_new = tevent_timeout_new;
289 f->timeout_update = tevent_timeout_update;
290 f->timeout_free = tevent_timeout_free;
291 f->private_data = state;
295 static int poll_funcs_state_destructor(struct poll_funcs_state *state)
299 * Make sure the watches are cleared before the contexts. The watches
300 * have destructors attached to them that clean up the fde's
302 for (i=0; i<state->num_watches; i++) {
303 TALLOC_FREE(state->watches[i]);
309 * Find or create a free slot in state->contexts[]
311 static bool poll_funcs_context_slot_find(struct poll_funcs_state *state,
312 struct tevent_context *ev,
315 struct poll_funcs_tevent_context **contexts;
318 for (i=0; i<state->num_contexts; i++) {
319 struct poll_funcs_tevent_context *ctx = state->contexts[i];
321 if ((ctx == NULL) || (ctx->ev == ev)) {
327 contexts = talloc_realloc(state, state->contexts,
328 struct poll_funcs_tevent_context *,
329 state->num_contexts + 1);
330 if (contexts == NULL) {
333 state->contexts = contexts;
334 state->contexts[state->num_contexts] = NULL;
336 *slot = state->num_contexts;
337 state->num_contexts += 1;
342 static int poll_funcs_tevent_context_destructor(
343 struct poll_funcs_tevent_context *ctx);
345 static struct poll_funcs_tevent_context *poll_funcs_tevent_context_new(
346 TALLOC_CTX *mem_ctx, struct poll_funcs_state *state,
347 struct tevent_context *ev, unsigned slot)
349 struct poll_funcs_tevent_context *ctx;
352 ctx = talloc(mem_ctx, struct poll_funcs_tevent_context);
362 ctx->fdes = talloc_array(ctx, struct tevent_fd *, state->num_watches);
363 if (ctx->fdes == NULL) {
367 for (i=0; i<state->num_watches; i++) {
368 struct poll_watch *w = state->watches[i];
374 ctx->fdes[i] = tevent_add_fd(ev, ctx->fdes, w->fd, w->events,
375 poll_funcs_fde_handler, w);
376 if (ctx->fdes[i] == NULL) {
380 talloc_set_destructor(ctx, poll_funcs_tevent_context_destructor);
387 static int poll_funcs_tevent_context_destructor(
388 struct poll_funcs_tevent_context *ctx)
390 struct poll_funcs_tevent_handle *h;
392 ctx->state->contexts[ctx->slot] = NULL;
394 for (h = ctx->handles; h != NULL; h = h->next) {
401 static void poll_funcs_fde_handler(struct tevent_context *ev,
402 struct tevent_fd *fde, uint16_t flags,
405 struct poll_watch *w = talloc_get_type_abort(
406 private_data, struct poll_watch);
407 short events = tevent_to_poll_events(flags);
408 w->callback(w, w->fd, events, w->private_data);
411 static int poll_funcs_tevent_handle_destructor(
412 struct poll_funcs_tevent_handle *handle);
414 void *poll_funcs_tevent_register(TALLOC_CTX *mem_ctx, struct poll_funcs *f,
415 struct tevent_context *ev)
417 struct poll_funcs_state *state = talloc_get_type_abort(
418 f->private_data, struct poll_funcs_state);
419 struct poll_funcs_tevent_handle *handle;
422 handle = talloc(mem_ctx, struct poll_funcs_tevent_handle);
423 if (handle == NULL) {
427 if (!poll_funcs_context_slot_find(state, ev, &slot)) {
430 if (state->contexts[slot] == NULL) {
431 state->contexts[slot] = poll_funcs_tevent_context_new(
432 state->contexts, state, ev, slot);
433 if (state->contexts[slot] == NULL) {
438 handle->ctx = state->contexts[slot];
439 DLIST_ADD(handle->ctx->handles, handle);
440 talloc_set_destructor(handle, poll_funcs_tevent_handle_destructor);
447 static int poll_funcs_tevent_handle_destructor(
448 struct poll_funcs_tevent_handle *handle)
450 if (handle->ctx == NULL) {
453 if (handle->ctx->handles == NULL) {
457 DLIST_REMOVE(handle->ctx->handles, handle);
459 if (handle->ctx->handles == NULL) {
460 TALLOC_FREE(handle->ctx);