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