/contrib/wpa_supplicant/src/utils/eloop.h

https://gitlab.com/HowTheStoryEnds/freebsd11-psm-port · C Header · 359 lines · 52 code · 29 blank · 278 comment · 0 complexity · 95f97eb24ced19c321c7ccc3570f5c5d MD5 · raw file

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