util.c 16 KB

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