74c94831 |
1 | /* |
2 | * poll.c |
3 | * - wrappers for poll(2) |
4 | */ |
5 | /* |
6 | * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2, or (at your option) |
11 | * any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program; if not, write to the Free Software Foundation, |
20 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 | */ |
22 | |
23 | #include <limits.h> |
24 | |
25 | #include "internal.h" |
26 | |
27 | #ifdef HAVE_POLL |
28 | |
29 | int adns_beforepoll(adns_state ads, struct pollfd *fds, int *nfds_io, int *timeout_io, |
30 | const struct timeval *now) { |
31 | struct timeval tv_nowbuf, tv_tobuf, *tv_to; |
32 | int space, found, timeout_ms; |
33 | struct pollfd fds_tmp[MAX_POLLFDS]; |
34 | |
4f973eb5 |
35 | if (timeout_io) { |
36 | adns__must_gettimeofday(ads,&now,&tv_nowbuf); |
37 | if (!now) { *nfds_io= 0; return 0; } |
74c94831 |
38 | |
4f973eb5 |
39 | timeout_ms= *timeout_io; |
40 | if (timeout_ms == -1) { |
41 | tv_to= 0; |
42 | } else { |
43 | tv_tobuf.tv_sec= timeout_ms / 1000; |
44 | tv_tobuf.tv_usec= (timeout_ms % 1000)*1000; |
45 | tv_to= &tv_tobuf; |
46 | } |
74c94831 |
47 | |
4f973eb5 |
48 | adns__timeouts(ads, 0, &tv_to,&tv_tobuf, *now); |
74c94831 |
49 | |
4f973eb5 |
50 | if (tv_to) { |
51 | assert(tv_to == &tv_tobuf); |
52 | timeout_ms= (tv_tobuf.tv_usec+999)/1000; |
53 | assert(tv_tobuf.tv_sec < (INT_MAX-timeout_ms)/1000); |
54 | timeout_ms += tv_tobuf.tv_sec*1000; |
55 | } else { |
56 | timeout_ms= -1; |
57 | } |
58 | *timeout_io= timeout_ms; |
74c94831 |
59 | } |
74c94831 |
60 | |
61 | space= *nfds_io; |
62 | if (space >= MAX_POLLFDS) { |
63 | found= adns__pollfds(ads,fds); |
64 | *nfds_io= found; |
65 | } else { |
66 | found= adns__pollfds(ads,fds_tmp); |
67 | *nfds_io= found; |
4f973eb5 |
68 | if (space < found) return ERANGE; |
74c94831 |
69 | memcpy(fds,fds_tmp,sizeof(struct pollfd)*found); |
70 | } |
71 | return 0; |
72 | } |
73 | |
74 | void adns_afterpoll(adns_state ads, const struct pollfd *fds, int nfds, |
75 | const struct timeval *now) { |
76 | struct timeval tv_buf; |
77 | |
78 | adns__must_gettimeofday(ads,&now,&tv_buf); |
79 | if (!now) return; |
80 | |
4f973eb5 |
81 | adns__timeouts(ads, 1, 0,0, *now); |
74c94831 |
82 | adns__fdevents(ads, fds,nfds, 0,0,0,0, *now,0); |
83 | } |
84 | |
85 | #endif |