Licensing: Update copyright dates for Ian Jackson
[adns] / src / poll.c
CommitLineData
620c146d 1/*
2 * poll.c
3 * - wrappers for poll(2)
4 */
5/*
ae8cc977 6 * This file is part of adns, which is
26e1c3d6 7 * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
ae8cc977 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.)
620c146d 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
7f8bbe29 14 * the Free Software Foundation; either version 3, or (at your option)
620c146d 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
8c09a4c6 23 * along with this program; if not, write to the Free Software Foundation.
620c146d 24 */
25
26#include <limits.h>
940356bd 27#include <string.h>
620c146d 28
29#include "internal.h"
30
31#ifdef HAVE_POLL
32
609133ee 33int adns_beforepoll(adns_state ads, struct pollfd *fds, int *nfds_io,
34 int *timeout_io, const struct timeval *now) {
620c146d 35 struct timeval tv_nowbuf, tv_tobuf, *tv_to;
3e2e5fab 36 int space, found, timeout_ms, r;
620c146d 37 struct pollfd fds_tmp[MAX_POLLFDS];
38
28de6442 39 adns__consistency(ads,0,cc_entex);
3e2e5fab 40
fc6a52ae 41 if (timeout_io) {
42 adns__must_gettimeofday(ads,&now,&tv_nowbuf);
3e2e5fab 43 if (!now) { *nfds_io= 0; r= 0; goto xit; }
620c146d 44
fc6a52ae 45 timeout_ms= *timeout_io;
46 if (timeout_ms == -1) {
47 tv_to= 0;
48 } else {
49 tv_tobuf.tv_sec= timeout_ms / 1000;
50 tv_tobuf.tv_usec= (timeout_ms % 1000)*1000;
51 tv_to= &tv_tobuf;
52 }
620c146d 53
fc6a52ae 54 adns__timeouts(ads, 0, &tv_to,&tv_tobuf, *now);
620c146d 55
fc6a52ae 56 if (tv_to) {
57 assert(tv_to == &tv_tobuf);
58 timeout_ms= (tv_tobuf.tv_usec+999)/1000;
59 assert(tv_tobuf.tv_sec < (INT_MAX-timeout_ms)/1000);
60 timeout_ms += tv_tobuf.tv_sec*1000;
61 } else {
62 timeout_ms= -1;
63 }
64 *timeout_io= timeout_ms;
620c146d 65 }
620c146d 66
67 space= *nfds_io;
68 if (space >= MAX_POLLFDS) {
69 found= adns__pollfds(ads,fds);
70 *nfds_io= found;
71 } else {
72 found= adns__pollfds(ads,fds_tmp);
73 *nfds_io= found;
3e2e5fab 74 if (space < found) { r= ERANGE; goto xit; }
620c146d 75 memcpy(fds,fds_tmp,sizeof(struct pollfd)*found);
76 }
3e2e5fab 77 r= 0;
78xit:
eb2a930e 79 adns__returning(ads,0);
3e2e5fab 80 return r;
620c146d 81}
82
83void adns_afterpoll(adns_state ads, const struct pollfd *fds, int nfds,
84 const struct timeval *now) {
85 struct timeval tv_buf;
86
28de6442 87 adns__consistency(ads,0,cc_entex);
620c146d 88 adns__must_gettimeofday(ads,&now,&tv_buf);
3e2e5fab 89 if (now) {
90 adns__timeouts(ads, 1, 0,0, *now);
91 adns__fdevents(ads, fds,nfds, 0,0,0,0, *now,0);
92 }
eb2a930e 93 adns__returning(ads,0);
620c146d 94}
95
940356bd 96int adns_wait_poll(adns_state ads,
97 adns_query *query_io,
98 adns_answer **answer_r,
99 void **context_r) {
100 int r, nfds, to;
101 struct pollfd fds[MAX_POLLFDS];
102
103 adns__consistency(ads,0,cc_entex);
104
105 for (;;) {
106 r= adns__internal_check(ads,query_io,answer_r,context_r);
226c5eef 107 if (r != EAGAIN) goto xit;
940356bd 108 nfds= MAX_POLLFDS; to= -1;
109 adns_beforepoll(ads,fds,&nfds,&to,0);
110 r= poll(fds,nfds,to);
111 if (r == -1) {
112 if (errno == EINTR) {
113 if (ads->iflags & adns_if_eintr) { r= EINTR; goto xit; }
114 } else {
115 adns__diag(ads,-1,0,"poll failed in wait: %s",strerror(errno));
116 adns_globalsystemfailure(ads);
117 }
118 } else {
119 assert(r >= 0);
120 adns_afterpoll(ads,fds,nfds,0);
121 }
122 }
123
124 xit:
eb2a930e 125 adns__returning(ads,0);
940356bd 126 return r;
127}
128
620c146d 129#endif