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