uloop.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * uloop - event loop implementation
  3. *
  4. * Copyright (C) 2010-2016 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/time.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include <poll.h>
  25. #include <string.h>
  26. #include <fcntl.h>
  27. #include <stdbool.h>
  28. #include "uloop.h"
  29. #include "utils.h"
  30. #ifdef USE_KQUEUE
  31. #include <sys/event.h>
  32. #endif
  33. #ifdef USE_EPOLL
  34. #include <sys/epoll.h>
  35. #endif
  36. #include <sys/wait.h>
  37. struct uloop_fd_event {
  38. struct uloop_fd *fd;
  39. unsigned int events;
  40. };
  41. struct uloop_fd_stack {
  42. struct uloop_fd_stack *next;
  43. struct uloop_fd *fd;
  44. unsigned int events;
  45. };
  46. static struct uloop_fd_stack *fd_stack = NULL;
  47. #define ULOOP_MAX_EVENTS 10
  48. static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
  49. static struct list_head processes = LIST_HEAD_INIT(processes);
  50. static int poll_fd = -1;
  51. bool uloop_cancelled = false;
  52. bool uloop_handle_sigchld = true;
  53. static int uloop_status = 0;
  54. static bool do_sigchld = false;
  55. static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
  56. static int cur_fd, cur_nfds;
  57. static int uloop_run_depth = 0;
  58. int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
  59. #ifdef USE_KQUEUE
  60. #include "uloop-kqueue.c"
  61. #endif
  62. #ifdef USE_EPOLL
  63. #include "uloop-epoll.c"
  64. #endif
  65. static void waker_consume(struct uloop_fd *fd, unsigned int events)
  66. {
  67. char buf[4];
  68. while (read(fd->fd, buf, 4) > 0)
  69. ;
  70. }
  71. static int waker_pipe = -1;
  72. static struct uloop_fd waker_fd = {
  73. .fd = -1,
  74. .cb = waker_consume,
  75. };
  76. static void waker_init_fd(int fd)
  77. {
  78. fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
  79. fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
  80. }
  81. static int waker_init(void)
  82. {
  83. int fds[2];
  84. if (waker_pipe >= 0)
  85. return 0;
  86. if (pipe(fds) < 0)
  87. return -1;
  88. waker_init_fd(fds[0]);
  89. waker_init_fd(fds[1]);
  90. waker_pipe = fds[1];
  91. waker_fd.fd = fds[0];
  92. waker_fd.cb = waker_consume;
  93. uloop_fd_add(&waker_fd, ULOOP_READ);
  94. return 0;
  95. }
  96. static void uloop_setup_signals(bool add);
  97. int uloop_init(void)
  98. {
  99. if (uloop_init_pollfd() < 0)
  100. return -1;
  101. if (waker_init() < 0) {
  102. uloop_done();
  103. return -1;
  104. }
  105. uloop_setup_signals(true);
  106. return 0;
  107. }
  108. static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
  109. {
  110. struct uloop_fd_stack *cur;
  111. /*
  112. * Do not buffer events for level-triggered fds, they will keep firing.
  113. * Caller needs to take care of recursion issues.
  114. */
  115. if (!(fd->flags & ULOOP_EDGE_TRIGGER))
  116. return false;
  117. for (cur = fd_stack; cur; cur = cur->next) {
  118. if (cur->fd != fd)
  119. continue;
  120. if (events < 0)
  121. cur->fd = NULL;
  122. else
  123. cur->events |= events | ULOOP_EVENT_BUFFERED;
  124. return true;
  125. }
  126. return false;
  127. }
  128. static void uloop_run_events(int timeout)
  129. {
  130. struct uloop_fd_event *cur;
  131. struct uloop_fd *fd;
  132. if (!cur_nfds) {
  133. cur_fd = 0;
  134. cur_nfds = uloop_fetch_events(timeout);
  135. if (cur_nfds < 0)
  136. cur_nfds = 0;
  137. }
  138. while (cur_nfds > 0) {
  139. struct uloop_fd_stack stack_cur;
  140. unsigned int events;
  141. cur = &cur_fds[cur_fd++];
  142. cur_nfds--;
  143. fd = cur->fd;
  144. events = cur->events;
  145. if (!fd)
  146. continue;
  147. if (!fd->cb)
  148. continue;
  149. if (uloop_fd_stack_event(fd, cur->events))
  150. continue;
  151. stack_cur.next = fd_stack;
  152. stack_cur.fd = fd;
  153. fd_stack = &stack_cur;
  154. do {
  155. stack_cur.events = 0;
  156. fd->cb(fd, events);
  157. events = stack_cur.events & ULOOP_EVENT_MASK;
  158. } while (stack_cur.fd && events);
  159. fd_stack = stack_cur.next;
  160. return;
  161. }
  162. }
  163. int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
  164. {
  165. unsigned int fl;
  166. int ret;
  167. if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
  168. return uloop_fd_delete(sock);
  169. if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
  170. fl = fcntl(sock->fd, F_GETFL, 0);
  171. fl |= O_NONBLOCK;
  172. fcntl(sock->fd, F_SETFL, fl);
  173. }
  174. ret = register_poll(sock, flags);
  175. if (ret < 0)
  176. goto out;
  177. sock->registered = true;
  178. sock->eof = false;
  179. sock->error = false;
  180. out:
  181. return ret;
  182. }
  183. int uloop_fd_delete(struct uloop_fd *fd)
  184. {
  185. int i;
  186. for (i = 0; i < cur_nfds; i++) {
  187. if (cur_fds[cur_fd + i].fd != fd)
  188. continue;
  189. cur_fds[cur_fd + i].fd = NULL;
  190. }
  191. if (!fd->registered)
  192. return 0;
  193. fd->registered = false;
  194. uloop_fd_stack_event(fd, -1);
  195. return __uloop_fd_delete(fd);
  196. }
  197. static int tv_diff(struct timeval *t1, struct timeval *t2)
  198. {
  199. return
  200. (t1->tv_sec - t2->tv_sec) * 1000 +
  201. (t1->tv_usec - t2->tv_usec) / 1000;
  202. }
  203. int uloop_timeout_add(struct uloop_timeout *timeout)
  204. {
  205. struct uloop_timeout *tmp;
  206. struct list_head *h = &timeouts;
  207. if (timeout->pending)
  208. return -1;
  209. list_for_each_entry(tmp, &timeouts, list) {
  210. if (tv_diff(&tmp->time, &timeout->time) > 0) {
  211. h = &tmp->list;
  212. break;
  213. }
  214. }
  215. list_add_tail(&timeout->list, h);
  216. timeout->pending = true;
  217. return 0;
  218. }
  219. static void uloop_gettime(struct timeval *tv)
  220. {
  221. struct timespec ts;
  222. clock_gettime(CLOCK_MONOTONIC, &ts);
  223. tv->tv_sec = ts.tv_sec;
  224. tv->tv_usec = ts.tv_nsec / 1000;
  225. }
  226. int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
  227. {
  228. struct timeval *time = &timeout->time;
  229. if (timeout->pending)
  230. uloop_timeout_cancel(timeout);
  231. uloop_gettime(time);
  232. time->tv_sec += msecs / 1000;
  233. time->tv_usec += (msecs % 1000) * 1000;
  234. if (time->tv_usec > 1000000) {
  235. time->tv_sec++;
  236. time->tv_usec -= 1000000;
  237. }
  238. return uloop_timeout_add(timeout);
  239. }
  240. int uloop_timeout_cancel(struct uloop_timeout *timeout)
  241. {
  242. if (!timeout->pending)
  243. return -1;
  244. list_del(&timeout->list);
  245. timeout->pending = false;
  246. return 0;
  247. }
  248. int uloop_timeout_remaining(struct uloop_timeout *timeout)
  249. {
  250. struct timeval now;
  251. if (!timeout->pending)
  252. return -1;
  253. uloop_gettime(&now);
  254. return tv_diff(&timeout->time, &now);
  255. }
  256. int uloop_process_add(struct uloop_process *p)
  257. {
  258. struct uloop_process *tmp;
  259. struct list_head *h = &processes;
  260. if (p->pending)
  261. return -1;
  262. list_for_each_entry(tmp, &processes, list) {
  263. if (tmp->pid > p->pid) {
  264. h = &tmp->list;
  265. break;
  266. }
  267. }
  268. list_add_tail(&p->list, h);
  269. p->pending = true;
  270. return 0;
  271. }
  272. int uloop_process_delete(struct uloop_process *p)
  273. {
  274. if (!p->pending)
  275. return -1;
  276. list_del(&p->list);
  277. p->pending = false;
  278. return 0;
  279. }
  280. static void uloop_handle_processes(void)
  281. {
  282. struct uloop_process *p, *tmp;
  283. pid_t pid;
  284. int ret;
  285. do_sigchld = false;
  286. while (1) {
  287. pid = waitpid(-1, &ret, WNOHANG);
  288. if (pid < 0 && errno == EINTR)
  289. continue;
  290. if (pid <= 0)
  291. return;
  292. list_for_each_entry_safe(p, tmp, &processes, list) {
  293. if (p->pid < pid)
  294. continue;
  295. if (p->pid > pid)
  296. break;
  297. uloop_process_delete(p);
  298. p->cb(p, ret);
  299. }
  300. }
  301. }
  302. static void uloop_signal_wake(void)
  303. {
  304. do {
  305. if (write(waker_pipe, "w", 1) < 0) {
  306. if (errno == EINTR)
  307. continue;
  308. }
  309. break;
  310. } while (1);
  311. }
  312. static void uloop_handle_sigint(int signo)
  313. {
  314. uloop_status = signo;
  315. uloop_cancelled = true;
  316. uloop_signal_wake();
  317. }
  318. static void uloop_sigchld(int signo)
  319. {
  320. do_sigchld = true;
  321. uloop_signal_wake();
  322. }
  323. static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
  324. {
  325. struct sigaction s;
  326. struct sigaction *act;
  327. act = NULL;
  328. sigaction(signum, NULL, &s);
  329. if (add) {
  330. if (s.sa_handler == SIG_DFL) { /* Do not override existing custom signal handlers */
  331. memcpy(old, &s, sizeof(struct sigaction));
  332. s.sa_handler = handler;
  333. s.sa_flags = 0;
  334. act = &s;
  335. }
  336. }
  337. else if (s.sa_handler == handler) { /* Do not restore if someone modified our handler */
  338. act = old;
  339. }
  340. if (act != NULL)
  341. sigaction(signum, act, NULL);
  342. }
  343. static void uloop_ignore_signal(int signum, bool ignore)
  344. {
  345. struct sigaction s;
  346. void *new_handler = NULL;
  347. sigaction(signum, NULL, &s);
  348. if (ignore) {
  349. if (s.sa_handler == SIG_DFL) /* Ignore only if there isn't any custom handler */
  350. new_handler = SIG_IGN;
  351. } else {
  352. if (s.sa_handler == SIG_IGN) /* Restore only if noone modified our SIG_IGN */
  353. new_handler = SIG_DFL;
  354. }
  355. if (new_handler) {
  356. s.sa_handler = new_handler;
  357. s.sa_flags = 0;
  358. sigaction(signum, &s, NULL);
  359. }
  360. }
  361. static void uloop_setup_signals(bool add)
  362. {
  363. static struct sigaction old_sigint, old_sigchld, old_sigterm;
  364. uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
  365. uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
  366. if (uloop_handle_sigchld)
  367. uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
  368. uloop_ignore_signal(SIGPIPE, add);
  369. }
  370. static int uloop_get_next_timeout(struct timeval *tv)
  371. {
  372. struct uloop_timeout *timeout;
  373. int diff;
  374. if (list_empty(&timeouts))
  375. return -1;
  376. timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
  377. diff = tv_diff(&timeout->time, tv);
  378. if (diff < 0)
  379. return 0;
  380. return diff;
  381. }
  382. static void uloop_process_timeouts(struct timeval *tv)
  383. {
  384. struct uloop_timeout *t;
  385. while (!list_empty(&timeouts)) {
  386. t = list_first_entry(&timeouts, struct uloop_timeout, list);
  387. if (tv_diff(&t->time, tv) > 0)
  388. break;
  389. uloop_timeout_cancel(t);
  390. if (t->cb)
  391. t->cb(t);
  392. }
  393. }
  394. static void uloop_clear_timeouts(void)
  395. {
  396. struct uloop_timeout *t, *tmp;
  397. list_for_each_entry_safe(t, tmp, &timeouts, list)
  398. uloop_timeout_cancel(t);
  399. }
  400. static void uloop_clear_processes(void)
  401. {
  402. struct uloop_process *p, *tmp;
  403. list_for_each_entry_safe(p, tmp, &processes, list)
  404. uloop_process_delete(p);
  405. }
  406. bool uloop_cancelling(void)
  407. {
  408. return uloop_run_depth > 0 && uloop_cancelled;
  409. }
  410. int uloop_run_timeout(int timeout)
  411. {
  412. int next_time = 0;
  413. struct timeval tv;
  414. uloop_run_depth++;
  415. uloop_status = 0;
  416. uloop_cancelled = false;
  417. while (!uloop_cancelled)
  418. {
  419. uloop_gettime(&tv);
  420. uloop_process_timeouts(&tv);
  421. if (do_sigchld)
  422. uloop_handle_processes();
  423. if (uloop_cancelled)
  424. break;
  425. uloop_gettime(&tv);
  426. next_time = uloop_get_next_timeout(&tv);
  427. if (timeout >= 0 && timeout < next_time)
  428. next_time = timeout;
  429. uloop_run_events(next_time);
  430. }
  431. --uloop_run_depth;
  432. return uloop_status;
  433. }
  434. void uloop_done(void)
  435. {
  436. uloop_setup_signals(false);
  437. if (poll_fd >= 0) {
  438. close(poll_fd);
  439. poll_fd = -1;
  440. }
  441. if (waker_pipe >= 0) {
  442. uloop_fd_delete(&waker_fd);
  443. close(waker_pipe);
  444. close(waker_fd.fd);
  445. waker_pipe = -1;
  446. }
  447. uloop_clear_timeouts();
  448. uloop_clear_processes();
  449. }