2 Unix SMB/CIFS implementation.
3 main select loop and event handling
4 Copyright (C) Andrew Tridgell 2003
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 PLEASE READ THIS BEFORE MODIFYING!
24 This module is a general abstraction for the main select loop and
25 event handling. Do not ever put any localised hacks in here, instead
26 register one of the possible event types and implement that event
29 There are 2 types of event handling that are handled in this module:
31 1) a file descriptor becoming readable or writeable. This is mostly
32 used for network sockets, but can be used for any type of file
33 descriptor. You may only register one handler for each file
34 descriptor/io combination or you will get unpredictable results
35 (this means that you can have a handler for read events, and a
36 separate handler for write events, but not two handlers that are
37 both handling read events)
39 2) a timed event. You can register an event that happens at a
40 specific time. You can register as many of these as you
41 like. They are single shot - add a new timed event in the event
42 handler to get another event.
44 To setup a set of events you first need to create a event_context
45 structure using the function event_context_init(); This returns a
46 'struct event_context' that you use in all subsequent calls.
48 After that you can add/remove events that you are interested in
49 using event_add_*() and talloc_free()
51 Finally, you call event_loop_wait_once() to block waiting for one of the
52 events to occor or event_loop_wait() which will loop
58 #include "lib/events/events.h"
59 #include "lib/events/events_internal.h"
60 #include "lib/util/dlinklist.h"
63 struct event_ops_list {
64 struct event_ops_list *next, *prev;
66 const struct event_ops *ops;
69 /* list of registered event backends */
70 static struct event_ops_list *event_backends;
73 register an events backend
75 NTSTATUS event_register_backend(const char *name, const struct event_ops *ops)
77 struct event_ops_list *e;
78 e = talloc(talloc_autofree_context(), struct event_ops_list);
79 NT_STATUS_HAVE_NO_MEMORY(e);
82 DLIST_ADD(event_backends, e);
87 initialise backends if not already done
89 static void event_backend_init(void)
91 init_module_fn static_init[] = STATIC_LIBEVENTS_MODULES;
92 init_module_fn *shared_init;
93 if (event_backends) return;
94 shared_init = load_samba_modules(NULL, "LIBEVENTS");
95 run_init_functions(static_init);
96 run_init_functions(shared_init);
100 list available backends
102 const char **event_backend_list(TALLOC_CTX *mem_ctx)
104 const char **list = NULL;
105 struct event_ops_list *e;
107 event_backend_init();
109 for (e=event_backends;e;e=e->next) {
110 list = str_list_add(list, e->name);
113 talloc_steal(mem_ctx, list);
119 create a event_context structure for a specific implemementation.
120 This must be the first events call, and all subsequent calls pass
121 this event_context as the first element. Event handlers also
122 receive this as their first argument.
124 This function is for allowing third-party-applications to hook in gluecode
125 to their own event loop code, so that they can make async usage of our client libs
127 NOTE: use event_context_init() inside of samba!
129 static struct event_context *event_context_init_ops(TALLOC_CTX *mem_ctx,
130 const struct event_ops *ops)
132 struct event_context *ev;
135 ev = talloc_zero(mem_ctx, struct event_context);
136 if (!ev) return NULL;
140 ret = ev->ops->context_init(ev);
150 create a event_context structure. This must be the first events
151 call, and all subsequent calls pass this event_context as the first
152 element. Event handlers also receive this as their first argument.
154 struct event_context *event_context_init_byname(TALLOC_CTX *mem_ctx, const char *name)
156 struct event_ops_list *e;
158 event_backend_init();
162 name = lp_parm_string(-1, "event", "backend");
169 for (e=event_backends;e;e=e->next) {
170 if (strcmp(name, e->name) == 0) {
171 return event_context_init_ops(mem_ctx, e->ops);
179 create a event_context structure. This must be the first events
180 call, and all subsequent calls pass this event_context as the first
181 element. Event handlers also receive this as their first argument.
183 struct event_context *event_context_init(TALLOC_CTX *mem_ctx)
185 return event_context_init_byname(mem_ctx, NULL);
190 return NULL on failure (memory allocation error)
192 struct fd_event *event_add_fd(struct event_context *ev, TALLOC_CTX *mem_ctx,
193 int fd, uint16_t flags, event_fd_handler_t handler,
196 return ev->ops->add_fd(ev, mem_ctx, fd, flags, handler, private_data);
202 struct aio_event *event_add_aio(struct event_context *ev,
205 event_aio_handler_t handler,
208 if (ev->ops->add_aio == NULL) return NULL;
209 return ev->ops->add_aio(ev, mem_ctx, iocb, handler, private_data);
213 return the fd event flags
215 uint16_t event_get_fd_flags(struct fd_event *fde)
218 return fde->event_ctx->ops->get_fd_flags(fde);
222 set the fd event flags
224 void event_set_fd_flags(struct fd_event *fde, uint16_t flags)
227 fde->event_ctx->ops->set_fd_flags(fde, flags);
232 return NULL on failure
234 struct timed_event *event_add_timed(struct event_context *ev, TALLOC_CTX *mem_ctx,
235 struct timeval next_event,
236 event_timed_handler_t handler,
239 return ev->ops->add_timed(ev, mem_ctx, next_event, handler, private_data);
245 sa_flags are flags to sigaction(2)
247 return NULL on failure
249 struct signal_event *event_add_signal(struct event_context *ev, TALLOC_CTX *mem_ctx,
252 event_signal_handler_t handler,
255 return ev->ops->add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data);
259 do a single event loop using the events defined in ev
261 _PUBLIC_ int event_loop_once(struct event_context *ev)
263 return ev->ops->loop_once(ev);
267 return on failure or (with 0) if all fd events are removed
269 int event_loop_wait(struct event_context *ev)
271 return ev->ops->loop_wait(ev);
275 find an event context that is a parent of the given memory context,
276 or create a new event context as a child of the given context if
279 This should be used in preference to event_context_init() in places
280 where you would prefer to use the existing event context if possible
281 (which is most situations)
283 struct event_context *event_context_find(TALLOC_CTX *mem_ctx)
285 struct event_context *ev = talloc_find_parent_bytype(mem_ctx, struct event_context);
287 ev = event_context_init(mem_ctx);