util.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2011,2012 Oswald Buddenhagen <ossi@users.sf.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * As a special exception, mbsync may be linked with the OpenSSL library,
  20. * despite that library's more restrictive license.
  21. */
  22. #include "common.h"
  23. #include <assert.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <string.h>
  28. #include <pwd.h>
  29. static int need_nl;
  30. void
  31. flushn( void )
  32. {
  33. if (need_nl) {
  34. putchar( '\n' );
  35. fflush( stdout );
  36. need_nl = 0;
  37. }
  38. }
  39. static void
  40. printn( const char *msg, va_list va )
  41. {
  42. if (*msg == '\v')
  43. msg++;
  44. else
  45. flushn();
  46. vprintf( msg, va );
  47. fflush( stdout );
  48. }
  49. void
  50. debug( const char *msg, ... )
  51. {
  52. va_list va;
  53. if (DFlags & DEBUG) {
  54. va_start( va, msg );
  55. vprintf( msg, va );
  56. va_end( va );
  57. fflush( stdout );
  58. need_nl = 0;
  59. }
  60. }
  61. void
  62. debugn( const char *msg, ... )
  63. {
  64. va_list va;
  65. if (DFlags & DEBUG) {
  66. va_start( va, msg );
  67. vprintf( msg, va );
  68. va_end( va );
  69. fflush( stdout );
  70. need_nl = 1;
  71. }
  72. }
  73. void
  74. info( const char *msg, ... )
  75. {
  76. va_list va;
  77. if (!(DFlags & QUIET)) {
  78. va_start( va, msg );
  79. printn( msg, va );
  80. va_end( va );
  81. need_nl = 0;
  82. }
  83. }
  84. void
  85. infon( const char *msg, ... )
  86. {
  87. va_list va;
  88. if (!(DFlags & QUIET)) {
  89. va_start( va, msg );
  90. printn( msg, va );
  91. va_end( va );
  92. need_nl = 1;
  93. }
  94. }
  95. void
  96. warn( const char *msg, ... )
  97. {
  98. va_list va;
  99. if (!(DFlags & VERYQUIET)) {
  100. flushn();
  101. va_start( va, msg );
  102. vfprintf( stderr, msg, va );
  103. va_end( va );
  104. }
  105. }
  106. void
  107. error( const char *msg, ... )
  108. {
  109. va_list va;
  110. flushn();
  111. va_start( va, msg );
  112. vfprintf( stderr, msg, va );
  113. va_end( va );
  114. }
  115. void
  116. sys_error( const char *msg, ... )
  117. {
  118. va_list va;
  119. char buf[1024];
  120. flushn();
  121. va_start( va, msg );
  122. if ((uint)vsnprintf( buf, sizeof(buf), msg, va ) >= sizeof(buf))
  123. oob();
  124. va_end( va );
  125. perror( buf );
  126. }
  127. void
  128. add_string_list_n( string_list_t **list, const char *str, int len )
  129. {
  130. string_list_t *elem;
  131. elem = nfmalloc( sizeof(*elem) + len );
  132. elem->next = *list;
  133. *list = elem;
  134. memcpy( elem->string, str, len );
  135. elem->string[len] = 0;
  136. }
  137. void
  138. add_string_list( string_list_t **list, const char *str )
  139. {
  140. add_string_list_n( list, str, strlen( str ) );
  141. }
  142. void
  143. free_string_list( string_list_t *list )
  144. {
  145. string_list_t *tlist;
  146. for (; list; list = tlist) {
  147. tlist = list->next;
  148. free( list );
  149. }
  150. }
  151. #ifndef HAVE_VASPRINTF
  152. static int
  153. vasprintf( char **strp, const char *fmt, va_list ap )
  154. {
  155. int len;
  156. char tmp[1024];
  157. if ((len = vsnprintf( tmp, sizeof(tmp), fmt, ap )) < 0 || !(*strp = malloc( len + 1 )))
  158. return -1;
  159. if (len >= (int)sizeof(tmp))
  160. vsprintf( *strp, fmt, ap );
  161. else
  162. memcpy( *strp, tmp, len + 1 );
  163. return len;
  164. }
  165. #endif
  166. #ifndef HAVE_MEMRCHR
  167. void *
  168. memrchr( const void *s, int c, size_t n )
  169. {
  170. u_char *b = (u_char *)s, *e = b + n;
  171. while (--e >= b)
  172. if (*e == c)
  173. return (void *)e;
  174. return 0;
  175. }
  176. #endif
  177. #ifndef HAVE_STRNLEN
  178. int
  179. strnlen( const char *str, size_t maxlen )
  180. {
  181. size_t len;
  182. /* It's tempting to use memchr(), but it's allowed to read past the end of the actual string. */
  183. for (len = 0; len < maxlen && str[len]; len++) {}
  184. return len;
  185. }
  186. #endif
  187. int
  188. starts_with( const char *str, int strl, const char *cmp, int cmpl )
  189. {
  190. if (strl < 0)
  191. strl = strnlen( str, cmpl + 1 );
  192. return (strl >= cmpl) && !memcmp( str, cmp, cmpl );
  193. }
  194. int
  195. equals( const char *str, int strl, const char *cmp, int cmpl )
  196. {
  197. if (strl < 0)
  198. strl = strnlen( str, cmpl + 1 );
  199. return (strl == cmpl) && !memcmp( str, cmp, cmpl );
  200. }
  201. #ifndef HAVE_TIMEGM
  202. /*
  203. Converts struct tm to time_t, assuming the data in tm is UTC rather
  204. than local timezone.
  205. mktime is similar but assumes struct tm, also known as the
  206. "broken-down" form of time, is in local time zone. timegm
  207. uses mktime to make the conversion understanding that an offset
  208. will be introduced by the local time assumption.
  209. mktime_from_utc then measures the introduced offset by applying
  210. gmtime to the initial result and applying mktime to the resulting
  211. "broken-down" form. The difference between the two mktime results
  212. is the measured offset which is then subtracted from the initial
  213. mktime result to yield a calendar time which is the value returned.
  214. tm_isdst in struct tm is set to 0 to force mktime to introduce a
  215. consistent offset (the non DST offset) since tm and tm+o might be
  216. on opposite sides of a DST change.
  217. Some implementations of mktime return -1 for the nonexistent
  218. localtime hour at the beginning of DST. In this event, use
  219. mktime(tm - 1hr) + 3600.
  220. Schematically
  221. mktime(tm) --> t+o
  222. gmtime(t+o) --> tm+o
  223. mktime(tm+o) --> t+2o
  224. t+o - (t+2o - t+o) = t
  225. Contributed by Roger Beeman <beeman@cisco.com>, with the help of
  226. Mark Baushke <mdb@cisco.com> and the rest of the Gurus at CISCO.
  227. Further improved by Roger with assistance from Edward J. Sabol
  228. based on input by Jamie Zawinski.
  229. */
  230. static time_t
  231. my_mktime( struct tm *t )
  232. {
  233. time_t tl = mktime( t );
  234. if (tl == -1) {
  235. t->tm_hour--;
  236. tl = mktime( t );
  237. if (tl != -1)
  238. tl += 3600;
  239. }
  240. return tl;
  241. }
  242. time_t
  243. timegm( struct tm *t )
  244. {
  245. time_t tl, tb;
  246. struct tm *tg;
  247. if ((tl = my_mktime( t )) == -1)
  248. return tl;
  249. tg = gmtime( &tl );
  250. tg->tm_isdst = 0;
  251. if ((tb = my_mktime( tg )) == -1)
  252. return tb;
  253. return tl - (tb - tl);
  254. }
  255. #endif
  256. void
  257. oob( void )
  258. {
  259. fputs( "Fatal: buffer too small. Please report a bug.\n", stderr );
  260. abort();
  261. }
  262. int
  263. nfsnprintf( char *buf, int blen, const char *fmt, ... )
  264. {
  265. int ret;
  266. va_list va;
  267. va_start( va, fmt );
  268. if (blen <= 0 || (uint)(ret = vsnprintf( buf, blen, fmt, va )) >= (uint)blen)
  269. oob();
  270. va_end( va );
  271. return ret;
  272. }
  273. static void ATTR_NORETURN
  274. oom( void )
  275. {
  276. fputs( "Fatal: Out of memory\n", stderr );
  277. abort();
  278. }
  279. void *
  280. nfmalloc( size_t sz )
  281. {
  282. void *ret;
  283. if (!(ret = malloc( sz )))
  284. oom();
  285. return ret;
  286. }
  287. void *
  288. nfcalloc( size_t sz )
  289. {
  290. void *ret;
  291. if (!(ret = calloc( sz, 1 )))
  292. oom();
  293. return ret;
  294. }
  295. void *
  296. nfrealloc( void *mem, size_t sz )
  297. {
  298. char *ret;
  299. if (!(ret = realloc( mem, sz )) && sz)
  300. oom();
  301. return ret;
  302. }
  303. char *
  304. nfstrndup( const char *str, size_t nchars )
  305. {
  306. char *ret = nfmalloc( nchars + 1 );
  307. memcpy( ret, str, nchars );
  308. ret[nchars] = 0;
  309. return ret;
  310. }
  311. char *
  312. nfstrdup( const char *str )
  313. {
  314. return nfstrndup( str, strlen( str ) );
  315. }
  316. int
  317. nfvasprintf( char **str, const char *fmt, va_list va )
  318. {
  319. int ret = vasprintf( str, fmt, va );
  320. if (ret < 0)
  321. oom();
  322. return ret;
  323. }
  324. int
  325. nfasprintf( char **str, const char *fmt, ... )
  326. {
  327. int ret;
  328. va_list va;
  329. va_start( va, fmt );
  330. ret = nfvasprintf( str, fmt, va );
  331. va_end( va );
  332. return ret;
  333. }
  334. /*
  335. static struct passwd *
  336. cur_user( void )
  337. {
  338. char *p;
  339. struct passwd *pw;
  340. uid_t uid;
  341. uid = getuid();
  342. if ((!(p = getenv("LOGNAME")) || !(pw = getpwnam( p )) || pw->pw_uid != uid) &&
  343. (!(p = getenv("USER")) || !(pw = getpwnam( p )) || pw->pw_uid != uid) &&
  344. !(pw = getpwuid( uid )))
  345. {
  346. fputs ("Cannot determinate current user\n", stderr);
  347. return 0;
  348. }
  349. return pw;
  350. }
  351. */
  352. char *
  353. expand_strdup( const char *s )
  354. {
  355. struct passwd *pw;
  356. const char *p, *q;
  357. char *r;
  358. if (*s == '~') {
  359. s++;
  360. if (!*s) {
  361. p = 0;
  362. q = Home;
  363. } else if (*s == '/') {
  364. p = s;
  365. q = Home;
  366. } else {
  367. if ((p = strchr( s, '/' ))) {
  368. r = nfstrndup( s, (int)(p - s) );
  369. pw = getpwnam( r );
  370. free( r );
  371. } else
  372. pw = getpwnam( s );
  373. if (!pw)
  374. return 0;
  375. q = pw->pw_dir;
  376. }
  377. nfasprintf( &r, "%s%s", q, p ? p : "" );
  378. return r;
  379. } else
  380. return nfstrdup( s );
  381. }
  382. /* Return value: 0 = ok, -1 = out found in arg, -2 = in found in arg but no out specified */
  383. int
  384. map_name( const char *arg, char **result, int reserve, const char *in, const char *out )
  385. {
  386. char *p;
  387. int i, l, ll, num, inl, outl;
  388. l = strlen( arg );
  389. if (!in) {
  390. copy:
  391. *result = nfmalloc( reserve + l + 1 );
  392. memcpy( *result + reserve, arg, l + 1 );
  393. return 0;
  394. }
  395. inl = strlen( in );
  396. if (out) {
  397. outl = strlen( out );
  398. if (inl == outl && !memcmp( in, out, inl ))
  399. goto copy;
  400. }
  401. for (num = 0, i = 0; i < l; ) {
  402. for (ll = 0; ll < inl; ll++)
  403. if (arg[i + ll] != in[ll])
  404. goto fout;
  405. num++;
  406. i += inl;
  407. continue;
  408. fout:
  409. if (out) {
  410. for (ll = 0; ll < outl; ll++)
  411. if (arg[i + ll] != out[ll])
  412. goto fnexti;
  413. return -1;
  414. }
  415. fnexti:
  416. i++;
  417. }
  418. if (!num)
  419. goto copy;
  420. if (!out)
  421. return -2;
  422. *result = nfmalloc( reserve + l + num * (outl - inl) + 1 );
  423. p = *result + reserve;
  424. for (i = 0; i < l; ) {
  425. for (ll = 0; ll < inl; ll++)
  426. if (arg[i + ll] != in[ll])
  427. goto rnexti;
  428. memcpy( p, out, outl );
  429. p += outl;
  430. i += inl;
  431. continue;
  432. rnexti:
  433. *p++ = arg[i++];
  434. }
  435. *p = 0;
  436. return 0;
  437. }
  438. static int
  439. compare_ints( const void *l, const void *r )
  440. {
  441. return *(int *)l - *(int *)r;
  442. }
  443. void
  444. sort_ints( int *arr, int len )
  445. {
  446. qsort( arr, len, sizeof(int), compare_ints );
  447. }
  448. static struct {
  449. uchar i, j, s[256];
  450. } rs;
  451. void
  452. arc4_init( void )
  453. {
  454. int i, fd;
  455. uchar j, si, dat[128];
  456. if ((fd = open( "/dev/urandom", O_RDONLY )) < 0 && (fd = open( "/dev/random", O_RDONLY )) < 0) {
  457. error( "Fatal: no random number source available.\n" );
  458. exit( 3 );
  459. }
  460. if (read( fd, dat, 128 ) != 128) {
  461. error( "Fatal: cannot read random number source.\n" );
  462. exit( 3 );
  463. }
  464. close( fd );
  465. for (i = 0; i < 256; i++)
  466. rs.s[i] = i;
  467. for (i = j = 0; i < 256; i++) {
  468. si = rs.s[i];
  469. j += si + dat[i & 127];
  470. rs.s[i] = rs.s[j];
  471. rs.s[j] = si;
  472. }
  473. rs.i = rs.j = 0;
  474. for (i = 0; i < 256; i++)
  475. arc4_getbyte();
  476. }
  477. uchar
  478. arc4_getbyte( void )
  479. {
  480. uchar si, sj;
  481. rs.i++;
  482. si = rs.s[rs.i];
  483. rs.j += si;
  484. sj = rs.s[rs.j];
  485. rs.s[rs.i] = sj;
  486. rs.s[rs.j] = si;
  487. return rs.s[(si + sj) & 0xff];
  488. }
  489. static const uchar prime_deltas[] = {
  490. 0, 0, 1, 3, 1, 5, 3, 3, 1, 9, 7, 5, 3, 17, 27, 3,
  491. 1, 29, 3, 21, 7, 17, 15, 9, 43, 35, 15, 0, 0, 0, 0, 0
  492. };
  493. int
  494. bucketsForSize( int size )
  495. {
  496. int base = 4, bits = 2;
  497. for (;;) {
  498. int prime = base + prime_deltas[bits];
  499. if (prime >= size)
  500. return prime;
  501. base <<= 1;
  502. bits++;
  503. }
  504. }
  505. static void
  506. list_prepend( list_head_t *head, list_head_t *to )
  507. {
  508. assert( !head->next );
  509. assert( to->next );
  510. assert( to->prev->next == to );
  511. head->next = to;
  512. head->prev = to->prev;
  513. head->prev->next = head;
  514. to->prev = head;
  515. }
  516. static void
  517. list_unlink( list_head_t *head )
  518. {
  519. assert( head->next );
  520. assert( head->next->prev == head);
  521. assert( head->prev->next == head);
  522. head->next->prev = head->prev;
  523. head->prev->next = head->next;
  524. head->next = head->prev = 0;
  525. }
  526. static notifier_t *notifiers;
  527. static int changed; /* Iterator may be invalid now. */
  528. #ifdef HAVE_SYS_POLL_H
  529. static struct pollfd *pollfds;
  530. static int npolls, rpolls;
  531. #else
  532. # ifdef HAVE_SYS_SELECT_H
  533. # include <sys/select.h>
  534. # endif
  535. #endif
  536. void
  537. init_notifier( notifier_t *sn, int fd, void (*cb)( int, void * ), void *aux )
  538. {
  539. #ifdef HAVE_SYS_POLL_H
  540. int idx = npolls++;
  541. if (rpolls < npolls) {
  542. rpolls = npolls;
  543. pollfds = nfrealloc( pollfds, npolls * sizeof(*pollfds) );
  544. }
  545. pollfds[idx].fd = fd;
  546. pollfds[idx].events = 0; /* POLLERR & POLLHUP implicit */
  547. sn->index = idx;
  548. #else
  549. sn->fd = fd;
  550. sn->events = 0;
  551. #endif
  552. sn->cb = cb;
  553. sn->aux = aux;
  554. sn->next = notifiers;
  555. notifiers = sn;
  556. }
  557. void
  558. conf_notifier( notifier_t *sn, int and_events, int or_events )
  559. {
  560. #ifdef HAVE_SYS_POLL_H
  561. int idx = sn->index;
  562. pollfds[idx].events = (pollfds[idx].events & and_events) | or_events;
  563. #else
  564. sn->events = (sn->events & and_events) | or_events;
  565. #endif
  566. }
  567. void
  568. wipe_notifier( notifier_t *sn )
  569. {
  570. notifier_t **snp;
  571. #ifdef HAVE_SYS_POLL_H
  572. int idx;
  573. #endif
  574. for (snp = &notifiers; *snp != sn; snp = &(*snp)->next)
  575. assert( *snp );
  576. *snp = sn->next;
  577. sn->next = 0;
  578. changed = 1;
  579. #ifdef HAVE_SYS_POLL_H
  580. idx = sn->index;
  581. memmove( pollfds + idx, pollfds + idx + 1, (--npolls - idx) * sizeof(*pollfds) );
  582. for (sn = notifiers; sn; sn = sn->next) {
  583. if (sn->index > idx)
  584. sn->index--;
  585. }
  586. #endif
  587. }
  588. static int nowvalid;
  589. static time_t now;
  590. static time_t
  591. get_now( void )
  592. {
  593. if (!nowvalid) {
  594. nowvalid = 1;
  595. return time( &now );
  596. }
  597. return now;
  598. }
  599. static list_head_t timers = { &timers, &timers };
  600. void
  601. init_wakeup( wakeup_t *tmr, void (*cb)( void * ), void *aux )
  602. {
  603. tmr->cb = cb;
  604. tmr->aux = aux;
  605. tmr->links.next = tmr->links.prev = 0;
  606. }
  607. void
  608. wipe_wakeup( wakeup_t *tmr )
  609. {
  610. if (tmr->links.next)
  611. list_unlink( &tmr->links );
  612. }
  613. void
  614. conf_wakeup( wakeup_t *tmr, int to )
  615. {
  616. list_head_t *head, *succ;
  617. if (to < 0) {
  618. if (tmr->links.next)
  619. list_unlink( &tmr->links );
  620. } else {
  621. time_t timeout = get_now() + to;
  622. tmr->timeout = timeout;
  623. if (!to) {
  624. /* We always prepend null timers, to cluster related events. */
  625. succ = timers.next;
  626. } else {
  627. /* We start at the end in the expectation that the newest timer is likely to fire last
  628. * (which will be true only if all timeouts are equal, but it's an as good guess as any). */
  629. for (succ = &timers; (head = succ->prev) != &timers; succ = head) {
  630. if (head != &tmr->links && timeout > ((wakeup_t *)head)->timeout)
  631. break;
  632. }
  633. assert( head != &tmr->links );
  634. }
  635. if (succ != &tmr->links) {
  636. if (tmr->links.next)
  637. list_unlink( &tmr->links );
  638. list_prepend( &tmr->links, succ );
  639. }
  640. }
  641. }
  642. #define shifted_bit(in, from, to) \
  643. (((uint)(in) & from) \
  644. / (from > to ? from / to : 1) \
  645. * (to > from ? to / from : 1))
  646. static void
  647. event_wait( void )
  648. {
  649. list_head_t *head;
  650. notifier_t *sn;
  651. int m;
  652. #ifdef HAVE_SYS_POLL_H
  653. int timeout = -1;
  654. nowvalid = 0;
  655. if ((head = timers.next) != &timers) {
  656. wakeup_t *tmr = (wakeup_t *)head;
  657. int delta = tmr->timeout - get_now();
  658. if (delta <= 0) {
  659. list_unlink( head );
  660. tmr->cb( tmr->aux );
  661. return;
  662. }
  663. timeout = delta * 1000;
  664. }
  665. switch (poll( pollfds, npolls, timeout )) {
  666. case 0:
  667. return;
  668. case -1:
  669. perror( "poll() failed in event loop" );
  670. abort();
  671. default:
  672. break;
  673. }
  674. for (sn = notifiers; sn; sn = sn->next) {
  675. int n = sn->index;
  676. if ((m = pollfds[n].revents)) {
  677. assert( !(m & POLLNVAL) );
  678. sn->cb( m | shifted_bit( m, POLLHUP, POLLIN ), sn->aux );
  679. if (changed) {
  680. changed = 0;
  681. break;
  682. }
  683. }
  684. }
  685. #else
  686. struct timeval *timeout = 0;
  687. struct timeval to_tv;
  688. fd_set rfds, wfds, efds;
  689. int fd;
  690. nowvalid = 0;
  691. if ((head = timers.next) != &timers) {
  692. wakeup_t *tmr = (wakeup_t *)head;
  693. int delta = tmr->timeout - get_now();
  694. if (delta <= 0) {
  695. list_unlink( head );
  696. tmr->cb( tmr->aux );
  697. return;
  698. }
  699. to_tv.tv_sec = delta;
  700. to_tv.tv_usec = 0;
  701. timeout = &to_tv;
  702. }
  703. FD_ZERO( &rfds );
  704. FD_ZERO( &wfds );
  705. FD_ZERO( &efds );
  706. m = -1;
  707. for (sn = notifiers; sn; sn = sn->next) {
  708. fd = sn->fd;
  709. if (sn->events & POLLIN)
  710. FD_SET( fd, &rfds );
  711. if (sn->events & POLLOUT)
  712. FD_SET( fd, &wfds );
  713. FD_SET( fd, &efds );
  714. if (fd > m)
  715. m = fd;
  716. }
  717. switch (select( m + 1, &rfds, &wfds, &efds, timeout )) {
  718. case 0:
  719. return;
  720. case -1:
  721. perror( "select() failed in event loop" );
  722. abort();
  723. default:
  724. break;
  725. }
  726. for (sn = notifiers; sn; sn = sn->next) {
  727. fd = sn->fd;
  728. m = 0;
  729. if (FD_ISSET( fd, &rfds ))
  730. m |= POLLIN;
  731. if (FD_ISSET( fd, &wfds ))
  732. m |= POLLOUT;
  733. if (FD_ISSET( fd, &efds ))
  734. m |= POLLERR;
  735. if (m) {
  736. sn->cb( m, sn->aux );
  737. if (changed) {
  738. changed = 0;
  739. break;
  740. }
  741. }
  742. }
  743. #endif
  744. }
  745. void
  746. main_loop( void )
  747. {
  748. while (notifiers || timers.next != &timers)
  749. event_wait();
  750. }