util.c 16 KB

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