1 /* 2 * Event loop 3 * Copyright (c) 2002-2006, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 * 8 * This file defines an event loop interface that supports processing events 9 * from registered timeouts (i.e., do something after N seconds), sockets 10 * (e.g., a new packet available for reading), and signals. eloop.c is an 11 * implementation of this interface using select() and sockets. This is 12 * suitable for most UNIX/POSIX systems. When porting to other operating 13 * systems, it may be necessary to replace that implementation with OS specific 14 * mechanisms. 15 */ 16 17 #ifndef ELOOP_H 18 #define ELOOP_H 19 20 /** 21 * ELOOP_ALL_CTX - eloop_cancel_timeout() magic number to match all timeouts 22 */ 23 #define ELOOP_ALL_CTX (void *) -1 24 25 /** 26 * eloop_event_type - eloop socket event type for eloop_register_sock() 27 * @EVENT_TYPE_READ: Socket has data available for reading 28 * @EVENT_TYPE_WRITE: Socket has room for new data to be written 29 * @EVENT_TYPE_EXCEPTION: An exception has been reported 30 */ 31 typedef enum { 32 EVENT_TYPE_READ = 0, 33 EVENT_TYPE_WRITE, 34 EVENT_TYPE_EXCEPTION 35 } eloop_event_type; 36 37 /** 38 * eloop_sock_handler - eloop socket event callback type 39 * @sock: File descriptor number for the socket 40 * @eloop_ctx: Registered callback context data (eloop_data) 41 * @sock_ctx: Registered callback context data (user_data) 42 */ 43 typedef void (*eloop_sock_handler)(int sock, void *eloop_ctx, void *sock_ctx); 44 45 /** 46 * eloop_event_handler - eloop generic event callback type 47 * @eloop_ctx: Registered callback context data (eloop_data) 48 * @sock_ctx: Registered callback context data (user_data) 49 */ 50 typedef void (*eloop_event_handler)(void *eloop_data, void *user_ctx); 51 52 /** 53 * eloop_timeout_handler - eloop timeout event callback type 54 * @eloop_ctx: Registered callback context data (eloop_data) 55 * @sock_ctx: Registered callback context data (user_data) 56 */ 57 typedef void (*eloop_timeout_handler)(void *eloop_data, void *user_ctx); 58 59 /** 60 * eloop_signal_handler - eloop signal event callback type 61 * @sig: Signal number 62 * @signal_ctx: Registered callback context data (user_data from 63 * eloop_register_signal(), eloop_register_signal_terminate(), or 64 * eloop_register_signal_reconfig() call) 65 */ 66 typedef void (*eloop_signal_handler)(int sig, void *signal_ctx); 67 68 /** 69 * eloop_init() - Initialize global event loop data 70 * Returns: 0 on success, -1 on failure 71 * 72 * This function must be called before any other eloop_* function. 73 */ 74 int eloop_init(void); 75 76 /** 77 * eloop_register_read_sock - Register handler for read events 78 * @sock: File descriptor number for the socket 79 * @handler: Callback function to be called when data is available for reading 80 * @eloop_data: Callback context data (eloop_ctx) 81 * @user_data: Callback context data (sock_ctx) 82 * Returns: 0 on success, -1 on failure 83 * 84 * Register a read socket notifier for the given file descriptor. The handler 85 * function will be called whenever data is available for reading from the 86 * socket. The handler function is responsible for clearing the event after 87 * having processed it in order to avoid eloop from calling the handler again 88 * for the same event. 89 */ 90 int eloop_register_read_sock(int sock, eloop_sock_handler handler, 91 void *eloop_data, void *user_data); 92 93 /** 94 * eloop_unregister_read_sock - Unregister handler for read events 95 * @sock: File descriptor number for the socket 96 * 97 * Unregister a read socket notifier that was previously registered with 98 * eloop_register_read_sock(). 99 */ 100 void eloop_unregister_read_sock(int sock); 101 102 /** 103 * eloop_register_sock - Register handler for socket events 104 * @sock: File descriptor number for the socket 105 * @type: Type of event to wait for 106 * @handler: Callback function to be called when the event is triggered 107 * @eloop_data: Callback context data (eloop_ctx) 108 * @user_data: Callback context data (sock_ctx) 109 * Returns: 0 on success, -1 on failure 110 * 111 * Register an event notifier for the given socket's file descriptor. The 112 * handler function will be called whenever the that event is triggered for the 113 * socket. The handler function is responsible for clearing the event after 114 * having processed it in order to avoid eloop from calling the handler again 115 * for the same event. 116 */ 117 int eloop_register_sock(int sock, eloop_event_type type, 118 eloop_sock_handler handler, 119 void *eloop_data, void *user_data); 120 121 /** 122 * eloop_unregister_sock - Unregister handler for socket events 123 * @sock: File descriptor number for the socket 124 * @type: Type of event for which sock was registered 125 * 126 * Unregister a socket event notifier that was previously registered with 127 * eloop_register_sock(). 128 */ 129 void eloop_unregister_sock(int sock, eloop_event_type type); 130 131 /** 132 * eloop_register_event - Register handler for generic events 133 * @event: Event to wait (eloop implementation specific) 134 * @event_size: Size of event data 135 * @handler: Callback function to be called when event is triggered 136 * @eloop_data: Callback context data (eloop_data) 137 * @user_data: Callback context data (user_data) 138 * Returns: 0 on success, -1 on failure 139 * 140 * Register an event handler for the given event. This function is used to 141 * register eloop implementation specific events which are mainly targeted for 142 * operating system specific code (driver interface and l2_packet) since the 143 * portable code will not be able to use such an OS-specific call. The handler 144 * function will be called whenever the event is triggered. The handler 145 * function is responsible for clearing the event after having processed it in 146 * order to avoid eloop from calling the handler again for the same event. 147 * 148 * In case of Windows implementation (eloop_win.c), event pointer is of HANDLE 149 * type, i.e., void*. The callers are likely to have 'HANDLE h' type variable, 150 * and they would call this function with eloop_register_event(h, sizeof(h), 151 * ...). 152 */ 153 int eloop_register_event(void *event, size_t event_size, 154 eloop_event_handler handler, 155 void *eloop_data, void *user_data); 156 157 /** 158 * eloop_unregister_event - Unregister handler for a generic event 159 * @event: Event to cancel (eloop implementation specific) 160 * @event_size: Size of event data 161 * 162 * Unregister a generic event notifier that was previously registered with 163 * eloop_register_event(). 164 */ 165 void eloop_unregister_event(void *event, size_t event_size); 166 167 /** 168 * eloop_register_timeout - Register timeout 169 * @secs: Number of seconds to the timeout 170 * @usecs: Number of microseconds to the timeout 171 * @handler: Callback function to be called when timeout occurs 172 * @eloop_data: Callback context data (eloop_ctx) 173 * @user_data: Callback context data (sock_ctx) 174 * Returns: 0 on success, -1 on failure 175 * 176 * Register a timeout that will cause the handler function to be called after 177 * given time. 178 */ 179 int eloop_register_timeout(unsigned int secs, unsigned int usecs, 180 eloop_timeout_handler handler, 181 void *eloop_data, void *user_data); 182 183 /** 184 * eloop_cancel_timeout - Cancel timeouts 185 * @handler: Matching callback function 186 * @eloop_data: Matching eloop_data or %ELOOP_ALL_CTX to match all 187 * @user_data: Matching user_data or %ELOOP_ALL_CTX to match all 188 * Returns: Number of cancelled timeouts 189 * 190 * Cancel matching <handler,eloop_data,user_data> timeouts registered with 191 * eloop_register_timeout(). ELOOP_ALL_CTX can be used as a wildcard for 192 * cancelling all timeouts regardless of eloop_data/user_data. 193 */ 194 int eloop_cancel_timeout(eloop_timeout_handler handler, 195 void *eloop_data, void *user_data); 196 197 /** 198 * eloop_is_timeout_registered - Check if a timeout is already registered 199 * @handler: Matching callback function 200 * @eloop_data: Matching eloop_data 201 * @user_data: Matching user_data 202 * Returns: 1 if the timeout is registered, 0 if the timeout is not registered 203 * 204 * Determine if a matching <handler,eloop_data,user_data> timeout is registered 205 * with eloop_register_timeout(). 206 */ 207 int eloop_is_timeout_registered(eloop_timeout_handler handler, 208 void *eloop_data, void *user_data); 209 210 /** 211 * eloop_register_signal - Register handler for signals 212 * @sig: Signal number (e.g., SIGHUP) 213 * @handler: Callback function to be called when the signal is received 214 * @user_data: Callback context data (signal_ctx) 215 * Returns: 0 on success, -1 on failure 216 * 217 * Register a callback function that will be called when a signal is received. 218 * The callback function is actually called only after the system signal 219 * handler has returned. This means that the normal limits for sighandlers 220 * (i.e., only "safe functions" allowed) do not apply for the registered 221 * callback. 222 */ 223 int eloop_register_signal(int sig, eloop_signal_handler handler, 224 void *user_data); 225 226 /** 227 * eloop_register_signal_terminate - Register handler for terminate signals 228 * @handler: Callback function to be called when the signal is received 229 * @user_data: Callback context data (signal_ctx) 230 * Returns: 0 on success, -1 on failure 231 * 232 * Register a callback function that will be called when a process termination 233 * signal is received. The callback function is actually called only after the 234 * system signal handler has returned. This means that the normal limits for 235 * sighandlers (i.e., only "safe functions" allowed) do not apply for the 236 * registered callback. 237 * 238 * This function is a more portable version of eloop_register_signal() since 239 * the knowledge of exact details of the signals is hidden in eloop 240 * implementation. In case of operating systems using signal(), this function 241 * registers handlers for SIGINT and SIGTERM. 242 */ 243 int eloop_register_signal_terminate(eloop_signal_handler handler, 244 void *user_data); 245 246 /** 247 * eloop_register_signal_reconfig - Register handler for reconfig signals 248 * @handler: Callback function to be called when the signal is received 249 * @user_data: Callback context data (signal_ctx) 250 * Returns: 0 on success, -1 on failure 251 * 252 * Register a callback function that will be called when a reconfiguration / 253 * hangup signal is received. The callback function is actually called only 254 * after the system signal handler has returned. This means that the normal 255 * limits for sighandlers (i.e., only "safe functions" allowed) do not apply 256 * for the registered callback. 257 * 258 * This function is a more portable version of eloop_register_signal() since 259 * the knowledge of exact details of the signals is hidden in eloop 260 * implementation. In case of operating systems using signal(), this function 261 * registers a handler for SIGHUP. 262 */ 263 int eloop_register_signal_reconfig(eloop_signal_handler handler, 264 void *user_data); 265 266 /** 267 * eloop_run - Start the event loop 268 * 269 * Start the event loop and continue running as long as there are any 270 * registered event handlers. This function is run after event loop has been 271 * initialized with event_init() and one or more events have been registered. 272 */ 273 void eloop_run(void); 274 275 /** 276 * eloop_terminate - Terminate event loop 277 * 278 * Terminate event loop even if there are registered events. This can be used 279 * to request the program to be terminated cleanly. 280 */ 281 void eloop_terminate(void); 282 283 /** 284 * eloop_destroy - Free any resources allocated for the event loop 285 * 286 * After calling eloop_destroy(), other eloop_* functions must not be called 287 * before re-running eloop_init(). 288 */ 289 void eloop_destroy(void); 290 291 /** 292 * eloop_terminated - Check whether event loop has been terminated 293 * Returns: 1 = event loop terminate, 0 = event loop still running 294 * 295 * This function can be used to check whether eloop_terminate() has been called 296 * to request termination of the event loop. This is normally used to abort 297 * operations that may still be queued to be run when eloop_terminate() was 298 * called. 299 */ 300 int eloop_terminated(void); 301 302 /** 303 * eloop_wait_for_read_sock - Wait for a single reader 304 * @sock: File descriptor number for the socket 305 * 306 * Do a blocking wait for a single read socket. 307 */ 308 void eloop_wait_for_read_sock(int sock); 309 310 #endif /* ELOOP_H */ 311