summaryrefslogtreecommitdiff
path: root/lib/tevent/tevent.c
blob: ea16fa1e071417fc61bdd25b22b8f62481bce6ad (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
/* 
   Unix SMB/CIFS implementation.
   main select loop and event handling
   Copyright (C) Andrew Tridgell 2003
   
   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/>.
*/

/*
  PLEASE READ THIS BEFORE MODIFYING!

  This module is a general abstraction for the main select loop and
  event handling. Do not ever put any localised hacks in here, instead
  register one of the possible event types and implement that event
  somewhere else.

  There are 2 types of event handling that are handled in this module:

  1) a file descriptor becoming readable or writeable. This is mostly
     used for network sockets, but can be used for any type of file
     descriptor. You may only register one handler for each file
     descriptor/io combination or you will get unpredictable results
     (this means that you can have a handler for read events, and a
     separate handler for write events, but not two handlers that are
     both handling read events)

  2) a timed event. You can register an event that happens at a
     specific time.  You can register as many of these as you
     like. They are single shot - add a new timed event in the event
     handler to get another event.

  To setup a set of events you first need to create a event_context
  structure using the function tevent_context_init(); This returns a
  'struct tevent_context' that you use in all subsequent calls.

  After that you can add/remove events that you are interested in
  using tevent_add_*() and talloc_free()

  Finally, you call tevent_loop_wait_once() to block waiting for one of the
  events to occor or tevent_loop_wait() which will loop
  forever.

*/
#include "replace.h"
#include "system/filesys.h"
#include "tevent.h"
#include "tevent_internal.h"
#include "tevent_util.h"

struct tevent_ops_list {
	struct tevent_ops_list *next, *prev;
	const char *name;
	const struct tevent_ops *ops;
};

/* list of registered event backends */
static struct tevent_ops_list *tevent_backends = NULL;
static char *tevent_default_backend = NULL;

/*
  register an events backend
*/
bool tevent_register_backend(const char *name, const struct tevent_ops *ops)
{
	struct tevent_ops_list *e;

	for (e = tevent_backends; e != NULL; e = e->next) {
		if (0 == strcmp(e->name, name)) {
			/* already registered, skip it */
			return true;
		}
	}

	e = talloc(talloc_autofree_context(), struct tevent_ops_list);
	if (e == NULL) return false;

	e->name = name;
	e->ops = ops;
	DLIST_ADD(tevent_backends, e);

	return true;
}

/*
  set the default event backend
 */
void tevent_set_default_backend(const char *backend)
{
	talloc_free(tevent_default_backend);
	tevent_default_backend = talloc_strdup(talloc_autofree_context(),
					       backend);
}

/*
  initialise backends if not already done
*/
static void tevent_backend_init(void)
{
	tevent_select_init();
	tevent_standard_init();
#ifdef HAVE_EPOLL
	tevent_epoll_init();
#endif
#ifdef HAVE_LINUX_AIO
	tevent_aio_init();
#endif
}

/*
  list available backends
*/
const char **tevent_backend_list(TALLOC_CTX *mem_ctx)
{
	const char **list = NULL;
	struct tevent_ops_list *e;

	tevent_backend_init();

	for (e=tevent_backends;e;e=e->next) {
		list = ev_str_list_add(list, e->name);
	}

	talloc_steal(mem_ctx, list);

	return list;
}

int tevent_common_context_destructor(struct tevent_context *ev)
{
	struct tevent_fd *fd, *fn;
	struct tevent_timer *te, *tn;
	struct tevent_signal *se, *sn;

	if (ev->pipe_fde) {
		talloc_free(ev->pipe_fde);
		ev->pipe_fde = NULL;
	}

	for (fd = ev->fd_events; fd; fd = fn) {
		fn = fd->next;
		fd->event_ctx = NULL;
		DLIST_REMOVE(ev->fd_events, fd);
	}

	for (te = ev->timer_events; te; te = tn) {
		tn = te->next;
		te->event_ctx = NULL;
		DLIST_REMOVE(ev->timer_events, te);
	}

	for (se = ev->signal_events; se; se = sn) {
		sn = se->next;
		se->event_ctx = NULL;
		DLIST_REMOVE(ev->signal_events, se);
	}

	return 0;
}

/*
  create a event_context structure for a specific implemementation.
  This must be the first events call, and all subsequent calls pass
  this event_context as the first element. Event handlers also
  receive this as their first argument.

  This function is for allowing third-party-applications to hook in gluecode
  to their own event loop code, so that they can make async usage of our client libs

  NOTE: use tevent_context_init() inside of samba!
*/
static struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
						      const struct tevent_ops *ops)
{
	struct tevent_context *ev;
	int ret;

	ev = talloc_zero(mem_ctx, struct tevent_context);
	if (!ev) return NULL;

	talloc_set_destructor(ev, tevent_common_context_destructor);

	ev->ops = ops;

	ret = ev->ops->context_init(ev);
	if (ret != 0) {
		talloc_free(ev);
		return NULL;
	}

	return ev;
}

/*
  create a event_context structure. This must be the first events
  call, and all subsequent calls pass this event_context as the first
  element. Event handlers also receive this as their first argument.
*/
struct tevent_context *tevent_context_init_byname(TALLOC_CTX *mem_ctx,
						  const char *name)
{
	struct tevent_ops_list *e;

	tevent_backend_init();

	if (name == NULL) {
		name = tevent_default_backend;
	}
	if (name == NULL) {
		name = "standard";
	}

	for (e=tevent_backends;e;e=e->next) {
		if (strcmp(name, e->name) == 0) {
			return tevent_context_init_ops(mem_ctx, e->ops);
		}
	}
	return NULL;
}


/*
  create a event_context structure. This must be the first events
  call, and all subsequent calls pass this event_context as the first
  element. Event handlers also receive this as their first argument.
*/
struct tevent_context *tevent_context_init(TALLOC_CTX *mem_ctx)
{
	return tevent_context_init_byname(mem_ctx, NULL);
}

/*
  add a fd based event
  return NULL on failure (memory allocation error)

  if flags contains TEVENT_FD_AUTOCLOSE then the fd will be closed when
  the returned fd_event context is freed
*/
struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
				 TALLOC_CTX *mem_ctx,
				 int fd,
				 uint16_t flags,
				 tevent_fd_handler_t handler,
				 void *private_data,
				 const char *handler_name,
				 const char *location)
{
	return ev->ops->add_fd(ev, mem_ctx, fd, flags, handler, private_data,
			       handler_name, location);
}

/*
  add a disk aio event
*/
struct tevent_aio *_tevent_add_aio(struct tevent_context *ev,
				   TALLOC_CTX *mem_ctx,
				   struct iocb *iocb,
				   tevent_aio_handler_t handler,
				   void *private_data,
				   const char *handler_name,
				   const char *location)
{
	if (ev->ops->add_aio == NULL) return NULL;
	return ev->ops->add_aio(ev, mem_ctx, iocb, handler, private_data,
				handler_name, location);
}

/*
  set a close function on the fd event
*/
void tevent_fd_set_close_fn(struct tevent_fd *fde,
			    tevent_fd_close_fn_t close_fn)
{
	if (!fde) return;
	if (!fde->event_ctx) return;
	fde->event_ctx->ops->set_fd_close_fn(fde, close_fn);
}

static void tevent_fd_auto_close_fn(struct tevent_context *ev,
				    struct tevent_fd *fde,
				    int fd,
				    void *private_data)
{
	close(fd);
}

void tevent_fd_set_auto_close(struct tevent_fd *fde)
{
	tevent_fd_set_close_fn(fde, tevent_fd_auto_close_fn);
}

/*
  return the fd event flags
*/
uint16_t tevent_fd_get_flags(struct tevent_fd *fde)
{
	if (!fde) return 0;
	if (!fde->event_ctx) return 0;
	return fde->event_ctx->ops->get_fd_flags(fde);
}

/*
  set the fd event flags
*/
void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags)
{
	if (!fde) return;
	if (!fde->event_ctx) return;
	fde->event_ctx->ops->set_fd_flags(fde, flags);
}

/*
  add a timer event
  return NULL on failure
*/
struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
				       TALLOC_CTX *mem_ctx,
				       struct timeval next_event,
				       tevent_timer_handler_t handler,
				       void *private_data,
				       const char *handler_name,
				       const char *location)
{
	return ev->ops->add_timer(ev, mem_ctx, next_event, handler, private_data,
				  handler_name, location);
}

/*
  add a signal event

  sa_flags are flags to sigaction(2)

  return NULL on failure
*/
struct tevent_signal *_tevent_add_signal(struct tevent_context *ev,
					 TALLOC_CTX *mem_ctx,
					 int signum,
					 int sa_flags,
					 tevent_signal_handler_t handler,
					 void *private_data,
					 const char *handler_name,
					 const char *location)
{
	return ev->ops->add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data,
				   handler_name, location);
}

/*
  do a single event loop using the events defined in ev 
*/
int tevent_loop_once(struct tevent_context *ev)
{
	return ev->ops->loop_once(ev);
}

/*
  return on failure or (with 0) if all fd events are removed
*/
int tevent_loop_wait(struct tevent_context *ev)
{
	return ev->ops->loop_wait(ev);
}