chiark / gitweb /
Fix typo in changelog entry for 1.6.1
[adns.git] / src / poll.c
1 /*
2  * poll.c
3  * - wrappers for poll(2)
4  */
5 /*
6  *  This file is part of adns, which is Copyright Ian Jackson
7  *  and contributors (see the file INSTALL for full details).
8  *  
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 3, or (at your option)
12  *  any later version.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software Foundation.
21  */
22
23 #include <limits.h>
24 #include <string.h>
25
26 #include "internal.h"
27
28 #ifdef HAVE_POLL
29
30 int adns_beforepoll(adns_state ads, struct pollfd *fds, int *nfds_io,
31                     int *timeout_io, const struct timeval *now) {
32   struct timeval tv_nowbuf, tv_tobuf, *tv_to;
33   int space, found, timeout_ms, r;
34   struct pollfd fds_tmp[MAX_POLLFDS];
35
36   adns__consistency(ads,0,cc_enter);
37
38   if (timeout_io) {
39     adns__must_gettimeofday(ads,&now,&tv_nowbuf);
40     if (!now) { *nfds_io= 0; r= 0; goto xit; }
41
42     timeout_ms= *timeout_io;
43     if (timeout_ms == -1) {
44       tv_to= 0;
45     } else {
46       tv_tobuf.tv_sec= timeout_ms / 1000;
47       tv_tobuf.tv_usec= (timeout_ms % 1000)*1000;
48       tv_to= &tv_tobuf;
49     }
50
51     adns__timeouts(ads, 0, &tv_to,&tv_tobuf, *now);
52
53     if (tv_to) {
54       assert(tv_to == &tv_tobuf);
55       timeout_ms= (tv_tobuf.tv_usec+999)/1000;
56       assert(tv_tobuf.tv_sec < (INT_MAX-timeout_ms)/1000);
57       timeout_ms += tv_tobuf.tv_sec*1000;
58     } else {
59       timeout_ms= -1;
60     }
61     *timeout_io= timeout_ms;
62   }
63   
64   space= *nfds_io;
65   if (space >= MAX_POLLFDS) {
66     found= adns__pollfds(ads,fds);
67     *nfds_io= found;
68   } else {
69     found= adns__pollfds(ads,fds_tmp);
70     *nfds_io= found;
71     if (space < found) { r= ERANGE; goto xit; }
72     memcpy(fds,fds_tmp,sizeof(struct pollfd)*found);
73   }
74   r= 0;
75 xit:
76   adns__returning(ads,0);
77   return r;
78 }
79
80 void adns_afterpoll(adns_state ads, const struct pollfd *fds, int nfds,
81                     const struct timeval *now) {
82   struct timeval tv_buf;
83
84   adns__consistency(ads,0,cc_enter);
85   adns__must_gettimeofday(ads,&now,&tv_buf);
86   if (now) {
87     adns__timeouts(ads, 1, 0,0, *now);
88     adns__intdone_process(ads); /* fdevents calls adns_processwriteable */
89     adns__fdevents(ads, fds,nfds, 0,0,0,0, *now,0);
90   }
91   adns__returning(ads,0);
92 }
93
94 int adns_wait_poll(adns_state ads,
95                    adns_query *query_io,
96                    adns_answer **answer_r,
97                    void **context_r) {
98   int r, nfds, to;
99   struct pollfd fds[MAX_POLLFDS];
100   
101   adns__consistency(ads,0,cc_enter);
102
103   for (;;) {
104     r= adns__internal_check(ads,query_io,answer_r,context_r);
105     if (r != EAGAIN) goto xit;
106     nfds= MAX_POLLFDS; to= -1;
107     adns_beforepoll(ads,fds,&nfds,&to,0);
108     r= poll(fds,nfds,to);
109     if (r == -1) {
110       if (errno == EINTR) {
111         if (ads->iflags & adns_if_eintr) { r= EINTR; goto xit; }
112       } else {
113         adns__diag(ads,-1,0,"poll failed in wait: %s",strerror(errno));
114         adns_globalsystemfailure(ads);
115       }
116     } else {
117       assert(r >= 0);
118       adns_afterpoll(ads,fds,nfds,0);
119     }
120   }
121
122  xit:
123   adns__returning(ads,0);
124   return r;
125 }
126
127 #endif