util.c 16 KB

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