eloop.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /** @file eloop.c
  2. * @brief Event loop.
  3. *
  4. * It's a class to organize main event loop. Class has unified interface to get
  5. * different types of events: signals, file descriptor events, scheduled time
  6. * events. User can register callbacks for interested events. Callback has
  7. * the same prototype for all types of events. Callback is called with
  8. * associated data. Assiciated data is user data, type of event and additional
  9. * data with information about things specific for current type. It's a number
  10. * of signal for signals, file descriptor and type of file event for file
  11. * descriptor events, event ID and pointer to special event object for scheduled
  12. * time events.
  13. */
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif /* HAVE_CONFIG_H */
  17. #include <stdlib.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include <time.h>
  28. #include <signal.h>
  29. #include <poll.h>
  30. #include <sys/signalfd.h>
  31. #include "faux/faux.h"
  32. #include "faux/str.h"
  33. #include "faux/net.h"
  34. #include "faux/sched.h"
  35. #include "faux/eloop.h"
  36. #include "private.h"
  37. #define TIMESPEC_TO_MILISECONDS(t) ((t.tv_sec * 1000) + (t.tv_nsec / 1000000l))
  38. #ifdef HAVE_SIGNALFD
  39. #define SIGNALFD_FLAGS (SFD_NONBLOCK | SFD_CLOEXEC)
  40. #else // Standard signals
  41. static void *faux_eloop_static_user_data = NULL;
  42. /** @brief Signal handler sends signal number to programm over pipe.
  43. *
  44. * Static service function. It's used for non-linux implementation on systems
  45. * that has no signalfd() function. The pipe pair is created. The write end is
  46. * used in signal handler to write signo to it. The read end of pipe is used
  47. * with poll()-like function to get signal number in main programm. It is
  48. * necessary to solve race problem with poll() function and signal handlers. See
  49. * manpage for select() and pselect().
  50. */
  51. static void faux_eloop_static_sighandler(int signo)
  52. {
  53. int pipe = -1;
  54. if (!faux_eloop_static_user_data)
  55. return;
  56. pipe = *((int *)faux_eloop_static_user_data);
  57. write(pipe, &signo, sizeof(signo));
  58. }
  59. #endif
  60. /** @brief Callback compare function for fd list.
  61. */
  62. static int faux_eloop_fd_compare(const void *first, const void *second)
  63. {
  64. const faux_eloop_fd_t *f = (const faux_eloop_fd_t *)first;
  65. const faux_eloop_fd_t *s = (const faux_eloop_fd_t *)second;
  66. return (f->fd - s->fd);
  67. }
  68. /** @brief Callback compare function for fd list to search by key.
  69. */
  70. static int faux_eloop_fd_kcompare(const void *key, const void *list_item)
  71. {
  72. int *f = (int *)key;
  73. const faux_eloop_fd_t *s = (const faux_eloop_fd_t *)list_item;
  74. return (*f - s->fd);
  75. }
  76. /** @brief Callback compare function for signal list.
  77. */
  78. static int faux_eloop_signal_compare(const void *first, const void *second)
  79. {
  80. const faux_eloop_signal_t *f = (const faux_eloop_signal_t *)first;
  81. const faux_eloop_signal_t *s = (const faux_eloop_signal_t *)second;
  82. return (f->signo - s->signo);
  83. }
  84. /** @brief Callback compare function for signal list to search by key.
  85. */
  86. static int faux_eloop_signal_kcompare(const void *key, const void *list_item)
  87. {
  88. int *f = (int *)key;
  89. const faux_eloop_signal_t *s = (const faux_eloop_signal_t *)list_item;
  90. return (*f - s->signo);
  91. }
  92. /** @brief Create new event loop object.
  93. *
  94. * Function gets default event callback as argument. It will be used for all
  95. * events if private callback for event is not specified.
  96. *
  97. * @param [in] default_event_cb Default event callback.
  98. * @return Allocated faux_eloop_t object or NULL on error.
  99. */
  100. faux_eloop_t *faux_eloop_new(faux_eloop_cb_fn default_event_cb)
  101. {
  102. faux_eloop_t *eloop = NULL;
  103. eloop = faux_zmalloc(sizeof(*eloop));
  104. assert(eloop);
  105. if (!eloop)
  106. return NULL;
  107. // Init
  108. eloop->working = BOOL_FALSE;
  109. eloop->default_event_cb = default_event_cb;
  110. // Sched
  111. eloop->sched = faux_sched_new();
  112. assert(eloop->sched);
  113. // FD
  114. eloop->fds = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  115. faux_eloop_fd_compare, faux_eloop_fd_kcompare, faux_free);
  116. assert(eloop->fds);
  117. eloop->pollfds = faux_pollfd_new();
  118. assert(eloop->pollfds);
  119. // Signal
  120. eloop->signals = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  121. faux_eloop_signal_compare, faux_eloop_signal_kcompare, faux_free);
  122. assert(eloop->signals);
  123. sigemptyset(&eloop->sig_set);
  124. sigfillset(&eloop->sig_mask);
  125. #ifdef HAVE_SIGNALFD
  126. eloop->signal_fd = -1;
  127. #endif
  128. return eloop;
  129. }
  130. /** @brief Free event loop object.
  131. *
  132. * @param [in] Event loop object.
  133. */
  134. void faux_eloop_free(faux_eloop_t *eloop)
  135. {
  136. if (!eloop)
  137. return;
  138. faux_list_free(eloop->signals);
  139. faux_pollfd_free(eloop->pollfds);
  140. faux_list_free(eloop->fds);
  141. faux_sched_free(eloop->sched);
  142. faux_free(eloop);
  143. }
  144. /** @brief Event loop function.
  145. *
  146. * Function blocks and waits for registered events. When event occurs the
  147. * correspondent callback will be called. Callback returns bool_t value. If
  148. * callback returns BOOL_FALSE then loop will break and unblock the programm.
  149. * On BOOL_TRUE the loop will wait for the next event.
  150. *
  151. * @param [in] eloop Allocated and initialized event loop object.
  152. * @returns BOOL_TRUE - success, BOOL_FALSE - error.
  153. */
  154. bool_t faux_eloop_loop(faux_eloop_t *eloop)
  155. {
  156. bool_t retval = BOOL_TRUE;
  157. bool_t stop = BOOL_FALSE;
  158. sigset_t blocked_signals;
  159. sigset_t orig_sig_set;
  160. #ifdef HAVE_PPOLL
  161. sigset_t *sigset_for_ppoll = NULL;
  162. #endif // HAVE_PPOLL
  163. #ifndef HAVE_SIGNALFD
  164. int signal_pipe[2];
  165. int fflags = 0;
  166. #endif // not HAVE_SIGNALFD
  167. // If event loop is active already and we try to start nested loop
  168. // then return.
  169. if (eloop->working)
  170. return BOOL_FALSE;
  171. eloop->working = BOOL_TRUE;
  172. // Block signals to prevent race conditions while loop and ppoll()
  173. // Catch signals while ppoll() only
  174. sigfillset(&blocked_signals);
  175. sigprocmask(SIG_SETMASK, &blocked_signals, &orig_sig_set);
  176. #ifdef HAVE_SIGNALFD
  177. // Create Linux-specific signal file descriptor. Wait for signals.
  178. eloop->signal_fd = signalfd(eloop->signal_fd, &eloop->sig_set,
  179. SIGNALFD_FLAGS);
  180. faux_pollfd_add(eloop->pollfds, eloop->signal_fd, POLLIN);
  181. #else // Standard signal processing
  182. #ifdef PPOLL
  183. sigset_for_ppoll = &eloop->sig_mask;
  184. #endif // HAVE_PPOLL
  185. // Create signal pipe pair to get signal number on pipe read end
  186. pipe(signal_pipe);
  187. fcntl(signal_pipe[0], F_SETFD, FD_CLOEXEC);
  188. fflags = fcntl(signal_pipe[0], F_GETFL);
  189. fcntl(signal_pipe[0], F_SETFL, fflags | O_NONBLOCK);
  190. fcntl(signal_pipe[1], F_SETFD, FD_CLOEXEC);
  191. fflags = fcntl(signal_pipe[1], F_GETFL);
  192. fcntl(signal_pipe[1], F_SETFL, fflags | O_NONBLOCK);
  193. faux_eloop_static_user_data = &signal_pipe[1];
  194. faux_pollfd_add(eloop->pollfds, signal_pipe[0], POLLIN);
  195. if (faux_list_len(eloop->signals) != 0) {
  196. faux_list_node_t *iter = faux_list_head(eloop->signals);
  197. faux_eloop_signal_t *sig = NULL;
  198. struct sigaction sig_act = {};
  199. sig_act.sa_flags = 0;
  200. sig_act.sa_mask = eloop->sig_set;
  201. sig_act.sa_handler = &faux_eloop_static_sighandler;
  202. while ((sig = (faux_eloop_signal_t *)faux_list_each(&iter)))
  203. sigaction(sig->signo, &sig_act, &sig->oldact);
  204. }
  205. #endif // HAVE_SIGNALFD
  206. // Main loop
  207. while (!stop) {
  208. int sn = 0;
  209. struct timespec *timeout = NULL;
  210. struct timespec next_interval = {};
  211. faux_pollfd_iterator_t pollfd_iter;
  212. struct pollfd *pollfd = NULL;
  213. // Find out next scheduled interval
  214. if (!faux_sched_next_interval(eloop->sched, &next_interval))
  215. timeout = NULL;
  216. else
  217. timeout = &next_interval;
  218. // Wait for events
  219. #ifdef HAVE_PPOLL
  220. sn = ppoll(faux_pollfd_vector(eloop->pollfds),
  221. faux_pollfd_len(eloop->pollfds), timeout, sigset_for_ppoll);
  222. #else // poll()
  223. sigprocmask(SIG_SETMASK, &eloop->sig_mask, NULL);
  224. sn = poll(faux_pollfd_vector(eloop->pollfds),
  225. faux_pollfd_len(eloop->pollfds),
  226. timeout ? TIMESPEC_TO_MILISECONDS(next_interval) : -1);
  227. sigprocmask(SIG_SETMASK, &blocked_signals, NULL);
  228. #endif // HAVE_PPOLL
  229. // Error or signal
  230. if (sn < 0) {
  231. // Let poll() read signal pipe or signalfd on next step
  232. if (EINTR == errno)
  233. continue;
  234. retval = BOOL_FALSE;
  235. break;
  236. }
  237. // Scheduled event
  238. if (0 == sn) {
  239. faux_ev_t *ev = NULL;
  240. // Some scheduled events
  241. while((ev = faux_sched_pop(eloop->sched))) {
  242. faux_eloop_info_sched_t info = {};
  243. bool_t r = BOOL_TRUE;
  244. int ev_id = faux_ev_id(ev);
  245. faux_eloop_context_t *context =
  246. (faux_eloop_context_t *)faux_ev_data(ev);
  247. faux_eloop_cb_fn event_cb = context->event_cb;
  248. void *user_data = context->user_data;
  249. if (!faux_ev_is_busy(ev)) {
  250. faux_ev_free(ev);
  251. ev = NULL;
  252. }
  253. if (!event_cb)
  254. event_cb = eloop->default_event_cb;
  255. if (!event_cb) // Callback is not defined
  256. continue;
  257. info.ev_id = ev_id;
  258. // Callback will get only rescheduled event object.
  259. // If event is not scheduled, callback will get NULL.
  260. info.ev = ev;
  261. // Execute callback
  262. r = event_cb(eloop, FAUX_ELOOP_SCHED, &info,
  263. user_data);
  264. // BOOL_FALSE return value means "break the loop"
  265. if (!r)
  266. stop = BOOL_TRUE;
  267. }
  268. continue;
  269. }
  270. // File descriptor
  271. faux_pollfd_init_iterator(eloop->pollfds, &pollfd_iter);
  272. while ((pollfd = faux_pollfd_each_active(eloop->pollfds, &pollfd_iter))) {
  273. int fd = pollfd->fd;
  274. faux_eloop_info_fd_t info = {};
  275. faux_eloop_cb_fn event_cb = NULL;
  276. faux_eloop_fd_t *entry = NULL;
  277. bool_t r = BOOL_TRUE;
  278. // Read special signal file descriptor
  279. #ifdef HAVE_SIGNALFD
  280. if (fd == eloop->signal_fd) {
  281. struct signalfd_siginfo signal_info = {};
  282. while (faux_read_block(fd, &signal_info,
  283. sizeof(signal_info)) == sizeof(signal_info)) {
  284. int signo = signal_info.ssi_signo;
  285. #else
  286. if (fd == signal_pipe[0]) {
  287. int tmp = 0;
  288. while (faux_read_block(fd, &tmp,
  289. sizeof(tmp)) == sizeof(tmp)) {
  290. int signo = tmp;
  291. #endif // HAVE_SIGNALFD
  292. faux_eloop_info_signal_t sinfo = {};
  293. faux_eloop_signal_t *sentry =
  294. (faux_eloop_signal_t *)faux_list_kfind(
  295. eloop->signals, &signo);
  296. if (!sentry) // Not registered signal. Drop it.
  297. continue;
  298. event_cb = sentry->context.event_cb;
  299. if (!event_cb)
  300. event_cb = eloop->default_event_cb;
  301. if (!event_cb) // Callback is not defined
  302. continue;
  303. sinfo.signo = signo;
  304. // Execute callback
  305. r = event_cb(eloop, FAUX_ELOOP_SIGNAL, &sinfo,
  306. sentry->context.user_data);
  307. // BOOL_FALSE return value means "break the loop"
  308. if (!r)
  309. stop = BOOL_TRUE;
  310. }
  311. continue; // Another fds are common, not signal
  312. }
  313. // File descriptor
  314. entry = (faux_eloop_fd_t *)faux_list_kfind(eloop->fds, &fd);
  315. assert(entry);
  316. if (!entry) // Something went wrong
  317. continue;
  318. event_cb = entry->context.event_cb;
  319. if (!event_cb)
  320. event_cb = eloop->default_event_cb;
  321. if (!event_cb) // Callback function is not defined for this event
  322. continue;
  323. info.fd = fd;
  324. info.revents = pollfd->revents;
  325. // Execute callback
  326. r = event_cb(eloop, FAUX_ELOOP_FD, &info, entry->context.user_data);
  327. // BOOL_FALSE return value means "break the loop"
  328. if (!r)
  329. stop = BOOL_TRUE;
  330. }
  331. } // Loop end
  332. #ifdef HAVE_SIGNALFD
  333. // Close signal file descriptor
  334. faux_pollfd_del_by_fd(eloop->pollfds, eloop->signal_fd);
  335. close(eloop->signal_fd);
  336. eloop->signal_fd = -1;
  337. #else // Standard signals. Restore signal handlers
  338. if (faux_list_len(eloop->signals) != 0) {
  339. faux_list_node_t *iter = faux_list_head(eloop->signals);
  340. faux_eloop_signal_t *sig = NULL;
  341. while ((sig = (faux_eloop_signal_t *)faux_list_each(&iter)))
  342. sigaction(sig->signo, &sig->oldact, NULL);
  343. }
  344. faux_pollfd_del_by_fd(eloop->pollfds, signal_pipe[0]);
  345. close(signal_pipe[0]);
  346. close(signal_pipe[1]);
  347. #endif
  348. // Unblock signals
  349. sigprocmask(SIG_SETMASK, &orig_sig_set, NULL);
  350. // Deactivate loop flag
  351. eloop->working = BOOL_FALSE;
  352. return retval;
  353. }
  354. /** @brief Registers file descriptor to wait for events.
  355. *
  356. * See poll() for explanation of possible file events ("events" argument).
  357. *
  358. * @param [in] eloop Allocated and initialized event loop object.
  359. * @param [in] fd File descriptor to wait on.
  360. * @param [in] events File events mask like POLLIN, POLLOUT.
  361. * @param [in] event_cb Callback for event.
  362. * @param [in] user_data User data to pass to callback.
  363. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  364. */
  365. bool_t faux_eloop_add_fd(faux_eloop_t *eloop, int fd, short events,
  366. faux_eloop_cb_fn event_cb, void *user_data)
  367. {
  368. faux_eloop_fd_t *entry = NULL;
  369. faux_list_node_t *new_node = NULL;
  370. assert(eloop);
  371. if (!eloop || (fd < 0))
  372. return BOOL_FALSE;
  373. entry = faux_zmalloc(sizeof(*entry));
  374. if (!entry)
  375. return BOOL_FALSE;
  376. entry->fd = fd;
  377. entry->events = events;
  378. entry->context.event_cb = event_cb;
  379. entry->context.user_data = user_data;
  380. if (!(new_node = faux_list_add(eloop->fds, entry))) {
  381. faux_free(entry);
  382. return BOOL_FALSE;
  383. }
  384. if (!faux_pollfd_add(eloop->pollfds, entry->fd, entry->events)) {
  385. faux_list_del(eloop->fds, new_node);
  386. faux_free(entry);
  387. return BOOL_FALSE;
  388. }
  389. return BOOL_TRUE;
  390. }
  391. /** @brief Registers additional event for specified fd.
  392. *
  393. * See poll() for explanation of possible file events ("events" argument).
  394. * Suppose some fd was added by faux_eloop_add_fd(). User have specified some
  395. * events like POLLIN. Now user wants to track POLLOUT event too. So it's not
  396. * necessary to remove fd by faux_eloop_del_fd() and then re-add it with new
  397. * event mask. User can include additional events by
  398. * faux_eloop_include_fd_event(). Specified event will be added to existent
  399. * event mask.
  400. *
  401. * @param [in] eloop Allocated and initialized event loop object.
  402. * @param [in] fd File descriptor to change event mask.
  403. * @param [in] events File event to include (like POLLIN, POLLOUT).
  404. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  405. */
  406. bool_t faux_eloop_include_fd_event(faux_eloop_t *eloop, int fd, short event)
  407. {
  408. faux_eloop_fd_t *entry = NULL;
  409. assert(eloop);
  410. if (!eloop)
  411. return BOOL_FALSE;
  412. assert(fd >= 0);
  413. if (fd < 0)
  414. return BOOL_FALSE;
  415. entry = (faux_eloop_fd_t *)faux_list_kfind(eloop->fds, &fd);
  416. if (!entry)
  417. return BOOL_FALSE;
  418. entry->events = entry->events | event;
  419. faux_pollfd_del_by_fd(eloop->pollfds, fd);
  420. faux_pollfd_add(eloop->pollfds, fd, entry->events);
  421. return BOOL_TRUE;
  422. }
  423. /** @brief Unregisters event for specified fd.
  424. *
  425. * See poll() for explanation of possible file events ("events" argument).
  426. * Suppose some fd was added by faux_eloop_add_fd(). User have specified some
  427. * events like POLLIN, POLLOUT. Now user doesn't wants to track one of the
  428. * events (POLLOUT for example). So it's not necessary to remove fd by
  429. * faux_eloop_del_fd() and then re-add it with new event mask. User can exclude
  430. * event by faux_eloop_include_fd_event(). Specified event will be excluded from
  431. * existent event mask.
  432. *
  433. * @param [in] eloop Allocated and initialized event loop object.
  434. * @param [in] fd File descriptor to change event mask.
  435. * @param [in] events File event to exclude (like POLLIN, POLLOUT).
  436. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  437. */
  438. bool_t faux_eloop_exclude_fd_event(faux_eloop_t *eloop, int fd, short event)
  439. {
  440. faux_eloop_fd_t *entry = NULL;
  441. assert(eloop);
  442. if (!eloop)
  443. return BOOL_FALSE;
  444. assert(fd >= 0);
  445. if (fd < 0)
  446. return BOOL_FALSE;
  447. entry = (faux_eloop_fd_t *)faux_list_kfind(eloop->fds, &fd);
  448. if (!entry)
  449. return BOOL_FALSE;
  450. entry->events = entry->events & (~event);
  451. faux_pollfd_del_by_fd(eloop->pollfds, fd);
  452. faux_pollfd_add(eloop->pollfds, fd, entry->events);
  453. return BOOL_TRUE;
  454. }
  455. /** @brief Unregisters file descriptor.
  456. *
  457. * @param [in] eloop Allocated and initialized event loop object.
  458. * @param [in] fd File descriptor to unregister.
  459. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  460. */
  461. bool_t faux_eloop_del_fd(faux_eloop_t *eloop, int fd)
  462. {
  463. if (!eloop || (fd < 0))
  464. return BOOL_FALSE;
  465. if (!faux_list_kdel(eloop->fds, &fd))
  466. return BOOL_FALSE;
  467. if (!faux_pollfd_del_by_fd(eloop->pollfds, fd))
  468. return BOOL_FALSE;
  469. return BOOL_TRUE;
  470. }
  471. /** @brief Unregisters all file descriptors.
  472. *
  473. * @param [in] eloop Allocated and initialized event loop object.
  474. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  475. */
  476. bool_t faux_eloop_del_fd_all(faux_eloop_t *eloop)
  477. {
  478. faux_list_node_t *iter = NULL;
  479. if (!eloop)
  480. return BOOL_FALSE;
  481. // "Del all" function is so complex because pollfd object
  482. // contains not user added fds only. It contains special fd for signals,
  483. // service pipe and may be something else. So del all fds one by one.
  484. while ((iter = faux_list_tail(eloop->fds))) {
  485. faux_eloop_fd_t *entry = NULL;
  486. entry = (faux_eloop_fd_t *)faux_list_data(iter);
  487. faux_eloop_del_fd(eloop, entry->fd);
  488. }
  489. return BOOL_TRUE;
  490. }
  491. /** @brief Registers signal to wait for.
  492. *
  493. * @param [in] eloop Allocated and initialized event loop object.
  494. * @param [in] signal Signal number to wait for.
  495. * @param [in] event_cb Callback for event.
  496. * @param [in] user_data User data to pass to callback.
  497. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  498. */
  499. bool_t faux_eloop_add_signal(faux_eloop_t *eloop, int signo,
  500. faux_eloop_cb_fn event_cb, void *user_data)
  501. {
  502. faux_eloop_signal_t *entry = NULL;
  503. if (!eloop || (signo < 0))
  504. return BOOL_FALSE;
  505. if (sigismember(&eloop->sig_set, signo) == 1)
  506. return BOOL_FALSE; // Already exists
  507. // Firstly try to add signal to sigset. Library function will validate
  508. // signal number value.
  509. if (sigaddset(&eloop->sig_set, signo) < 0)
  510. return BOOL_FALSE; // Invalid signal number
  511. sigdelset(&eloop->sig_mask, signo);
  512. entry = faux_zmalloc(sizeof(*entry));
  513. if (!entry) {
  514. sigdelset(&eloop->sig_set, signo);
  515. sigaddset(&eloop->sig_mask, signo);
  516. return BOOL_FALSE;
  517. }
  518. entry->signo = signo;
  519. entry->context.event_cb = event_cb;
  520. entry->context.user_data = user_data;
  521. if (!faux_list_add(eloop->signals, entry)) {
  522. faux_free(entry);
  523. sigdelset(&eloop->sig_set, signo);
  524. sigaddset(&eloop->sig_mask, signo);
  525. return BOOL_FALSE;
  526. }
  527. if (eloop->working) { // Add signal on the fly
  528. #ifdef HAVE_SIGNALFD
  529. // Reattach signalfd handler with updated sig_set
  530. eloop->signal_fd = signalfd(eloop->signal_fd, &eloop->sig_set,
  531. SIGNALFD_FLAGS);
  532. #else // Standard signals
  533. struct sigaction sig_act = {};
  534. sig_act.sa_flags = 0;
  535. sig_act.sa_mask = eloop->sig_set;
  536. sig_act.sa_handler = &faux_eloop_static_sighandler;
  537. sigaction(signo, &sig_act, &entry->oldact);
  538. #endif
  539. }
  540. return BOOL_TRUE;
  541. }
  542. /** @brief Unregisters signal to wait for.
  543. *
  544. * @param [in] eloop Allocated and initialized event loop object.
  545. * @param [in] signal Signal to unregister.
  546. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  547. */
  548. bool_t faux_eloop_del_signal(faux_eloop_t *eloop, int signo)
  549. {
  550. if (!eloop || (signo < 0))
  551. return BOOL_FALSE;
  552. if (sigismember(&eloop->sig_set, signo) != 1)
  553. return BOOL_FALSE; // Doesn't exist
  554. sigdelset(&eloop->sig_set, signo);
  555. sigaddset(&eloop->sig_mask, signo);
  556. if (eloop->working) { // Del signal on the fly
  557. #ifdef HAVE_SIGNALFD
  558. // Reattach signalfd handler with updated sig_set
  559. eloop->signal_fd = signalfd(eloop->signal_fd, &eloop->sig_set,
  560. SIGNALFD_FLAGS);
  561. #else // Standard signals
  562. faux_eloop_signal_t *sig = faux_list_kfind(eloop->signals, &signo);
  563. sigaction(signo, &sig->oldact, NULL);
  564. #endif
  565. }
  566. faux_list_kdel(eloop->signals, &signo);
  567. return BOOL_TRUE;
  568. }
  569. /** @brief Unregisters all signals to wait for.
  570. *
  571. * @param [in] eloop Allocated and initialized event loop object.
  572. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  573. */
  574. bool_t faux_eloop_del_signal_all(faux_eloop_t *eloop)
  575. {
  576. faux_list_node_t *iter = NULL;
  577. if (!eloop)
  578. return BOOL_FALSE;
  579. // "Del all" function is so complex because signals can be set now
  580. // and deletion is not only removing from list.
  581. // So del all signals one by one.
  582. while ((iter = faux_list_tail(eloop->signals))) {
  583. faux_eloop_signal_t *entry = NULL;
  584. entry = (faux_eloop_signal_t *)faux_list_data(iter);
  585. faux_eloop_del_signal(eloop, entry->signo);
  586. }
  587. return BOOL_TRUE;
  588. }
  589. /** @brief Service function to create new context for event.
  590. *
  591. * @param [in] event_cb Callback for event.
  592. * @param [in] data User data for event.
  593. * @return Allocated context structure or NULL on error.
  594. */
  595. static faux_eloop_context_t *faux_eloop_new_context(
  596. faux_eloop_cb_fn event_cb, void *data)
  597. {
  598. faux_eloop_context_t *context = NULL;
  599. context = faux_zmalloc(sizeof(*context));
  600. assert(context);
  601. if (!context)
  602. return NULL;
  603. context->event_cb = event_cb;
  604. context->user_data = data;
  605. return context;
  606. }
  607. /** @brief Registers scheduled time event. See faux_sched_once().
  608. *
  609. * @param [in] eloop Allocated and initialized event loop object.
  610. * @param [in] time See faux_sched_once().
  611. * @param [in] ev_id See faux_sched_once().
  612. * @param [in] event_cb See faux_sched_once().
  613. * @param [in] data See faux_sched_once().
  614. * @return Pointer to created faux_ev_t object or NULL on error.
  615. */
  616. faux_ev_t *faux_eloop_add_sched_once(faux_eloop_t *eloop, const struct timespec *time,
  617. int ev_id, faux_eloop_cb_fn event_cb, void *data)
  618. {
  619. faux_eloop_context_t *context = NULL;
  620. faux_ev_t *ev = NULL;
  621. assert(eloop);
  622. if (!eloop)
  623. return NULL;
  624. context = faux_eloop_new_context(event_cb, data);
  625. assert(context);
  626. if (!context)
  627. return NULL;
  628. if (!(ev = faux_sched_once(eloop->sched, time, ev_id, context))) {
  629. faux_free(context);
  630. return NULL;
  631. }
  632. faux_ev_set_free_data_cb(ev, faux_free);
  633. return ev;
  634. }
  635. /** @brief Registers scheduled time event. See faux_sched_once_delayed().
  636. *
  637. * @param [in] eloop Allocated and initialized event loop object.
  638. * @param [in] interval See faux_sched_once_delayed().
  639. * @param [in] ev_id See faux_sched_once_delayed().
  640. * @param [in] event_cb See faux_sched_once_delayed().
  641. * @param [in] data See faux_sched_once_delayed().
  642. * @return Pointer to created faux_ev_t object or NULL on error.
  643. */
  644. faux_ev_t *faux_eloop_add_sched_once_delayed(faux_eloop_t *eloop, const struct timespec *interval,
  645. int ev_id, faux_eloop_cb_fn event_cb, void *data)
  646. {
  647. faux_eloop_context_t *context = NULL;
  648. faux_ev_t *ev = NULL;
  649. assert(eloop);
  650. if (!eloop)
  651. return NULL;
  652. context = faux_eloop_new_context(event_cb, data);
  653. assert(context);
  654. if (!context)
  655. return NULL;
  656. if (!(ev = faux_sched_once_delayed(eloop->sched, interval, ev_id, context))) {
  657. faux_free(context);
  658. return NULL;
  659. }
  660. faux_ev_set_free_data_cb(ev, faux_free);
  661. return ev;
  662. }
  663. /** @brief Registers scheduled time event. See faux_sched_periodic().
  664. *
  665. * @param [in] eloop Allocated and initialized event loop object.
  666. * @param [in] time See faux_sched_periodic().
  667. * @param [in] ev_id See faux_sched_periodic().
  668. * @param [in] event_cb See faux_sched_periodic().
  669. * @param [in] data See faux_sched_periodic().
  670. * @param [in] period See faux_sched_periodic().
  671. * @param [in] cycle_num See faux_sched_periodic().
  672. * @return Pointer to created faux_ev_t object or NULL on error.
  673. */
  674. faux_ev_t *faux_eloop_add_sched_periodic(faux_eloop_t *eloop, const struct timespec *time,
  675. int ev_id, faux_eloop_cb_fn event_cb, void *data,
  676. const struct timespec *period, unsigned int cycle_num)
  677. {
  678. faux_eloop_context_t *context = NULL;
  679. faux_ev_t *ev = NULL;
  680. assert(eloop);
  681. if (!eloop)
  682. return NULL;
  683. context = faux_eloop_new_context(event_cb, data);
  684. assert(context);
  685. if (!context)
  686. return NULL;
  687. if (!(ev = faux_sched_periodic(eloop->sched, time, ev_id, context,
  688. period, cycle_num))) {
  689. faux_free(context);
  690. return NULL;
  691. }
  692. faux_ev_set_free_data_cb(ev, faux_free);
  693. return ev;
  694. }
  695. /** @brief Registers scheduled time event. See faux_sched_periodic_delayed().
  696. *
  697. * @param [in] eloop Allocated and initialized event loop object.
  698. * @param [in] ev_id See faux_sched_periodic_delayed().
  699. * @param [in] event_cb See faux_sched_periodic_delayed().
  700. * @param [in] data See faux_sched_periodic_delayed().
  701. * @param [in] period See faux_sched_periodic_delayed().
  702. * @param [in] cycle_num See faux_sched_periodic_delayed().
  703. * @return Pointer to created faux_ev_t object or NULL on error.
  704. */
  705. faux_ev_t *faux_eloop_add_sched_periodic_delayed(faux_eloop_t *eloop,
  706. int ev_id, faux_eloop_cb_fn event_cb, void *data,
  707. const struct timespec *period, unsigned int cycle_num)
  708. {
  709. faux_eloop_context_t *context = NULL;
  710. faux_ev_t *ev = NULL;
  711. assert(eloop);
  712. if (!eloop)
  713. return NULL;
  714. context = faux_eloop_new_context(event_cb, data);
  715. assert(context);
  716. if (!context)
  717. return NULL;
  718. if (!(ev = faux_sched_periodic_delayed(eloop->sched, ev_id, context,
  719. period, cycle_num))) {
  720. faux_free(context);
  721. return NULL;
  722. }
  723. faux_ev_set_free_data_cb(ev, faux_free);
  724. return ev;
  725. }
  726. /** @brief Unregisters scheduled time event.
  727. *
  728. * @param [in] eloop Allocated and initialized event loop object.
  729. * @param [in] ev Event object to unregister.
  730. * @return Number of unregistered entries or < 0 on error.
  731. */
  732. ssize_t faux_eloop_del_sched(faux_eloop_t *eloop, faux_ev_t *ev)
  733. {
  734. assert(eloop);
  735. if (!eloop)
  736. return -1;
  737. return faux_sched_del(eloop->sched, ev);
  738. }
  739. /** @brief Unregisters all scheduled time events.
  740. *
  741. * @param [in] eloop Allocated and initialized event loop object.
  742. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  743. */
  744. bool_t faux_eloop_del_sched_all(faux_eloop_t *eloop)
  745. {
  746. assert(eloop);
  747. if (!eloop)
  748. return BOOL_FALSE;
  749. faux_sched_del_all(eloop->sched);
  750. return BOOL_TRUE;
  751. }
  752. /** @brief Unregisters scheduled time event by event ID.
  753. *
  754. * @param [in] eloop Allocated and initialized event loop object.
  755. * @param [in] ev_id Event ID to unregister.
  756. * @return Number of unregistered entries or < 0 on error.
  757. */
  758. ssize_t faux_eloop_del_sched_by_id(faux_eloop_t *eloop, int ev_id)
  759. {
  760. assert(eloop);
  761. if (!eloop)
  762. return -1;
  763. return faux_sched_del_by_id(eloop->sched, ev_id);
  764. }