ReactOS  0.4.15-dev-3187-ge372f2b
poll.c
Go to the documentation of this file.
1 /*
2  * poll.c
3  * - wrappers for poll(2)
4  */
5 /*
6  * This file is
7  * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
8  *
9  * It is part of adns, which is
10  * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
11  * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  */
27 
28 #include <limits.h>
29 #include <string.h>
30 
31 #include "internal.h"
32 
33 #ifdef HAVE_POLL
34 
35 int adns_beforepoll(adns_state ads, struct pollfd *fds, int *nfds_io, int *timeout_io,
36  const struct timeval *now) {
37  struct timeval tv_nowbuf, tv_tobuf, *tv_to;
38  int space, found, timeout_ms, r;
39  struct pollfd fds_tmp[MAX_POLLFDS];
40 
42 
43  if (timeout_io) {
44  adns__must_gettimeofday(ads,&now,&tv_nowbuf);
45  if (!now) { *nfds_io= 0; r= 0; goto xit; }
46 
47  timeout_ms= *timeout_io;
48  if (timeout_ms == -1) {
49  tv_to= 0;
50  } else {
51  tv_tobuf.tv_sec= timeout_ms / 1000;
52  tv_tobuf.tv_usec= (timeout_ms % 1000)*1000;
53  tv_to= &tv_tobuf;
54  }
55 
56  adns__timeouts(ads, 0, &tv_to,&tv_tobuf, *now);
57 
58  if (tv_to) {
59  assert(tv_to == &tv_tobuf);
60  timeout_ms= (tv_tobuf.tv_usec+999)/1000;
61  assert(tv_tobuf.tv_sec < (INT_MAX-timeout_ms)/1000);
62  timeout_ms += tv_tobuf.tv_sec*1000;
63  } else {
64  timeout_ms= -1;
65  }
66  *timeout_io= timeout_ms;
67  }
68 
69  space= *nfds_io;
70  if (space >= MAX_POLLFDS) {
71  found= adns__pollfds(ads,fds);
72  *nfds_io= found;
73  } else {
74  found= adns__pollfds(ads,fds_tmp);
75  *nfds_io= found;
76  if (space < found) { r= ERANGE; goto xit; }
77  memcpy(fds,fds_tmp,sizeof(struct pollfd)*found);
78  }
79  r= 0;
80 xit:
82  return r;
83 }
84 
85 void adns_afterpoll(adns_state ads, const struct pollfd *fds, int nfds,
86  const struct timeval *now) {
87  struct timeval tv_buf;
88 
91  if (now) {
92  adns__timeouts(ads, 1, 0,0, *now);
93  adns__fdevents(ads, fds,nfds, 0,0,0,0, *now,0);
94  }
96 }
97 
99  adns_query *query_io,
100  adns_answer **answer_r,
101  void **context_r) {
102  int r, nfds, to;
103  struct pollfd fds[MAX_POLLFDS];
104 
106 
107  for (;;) {
108  r= adns__internal_check(ads,query_io,answer_r,context_r);
109  if (r != EAGAIN) goto xit;
110  nfds= MAX_POLLFDS; to= -1;
111  adns_beforepoll(ads,fds,&nfds,&to,0);
112  r= poll(fds,nfds,to);
113  if (r == -1) {
114  if (errno == EINTR) {
115  if (ads->iflags & adns_if_eintr) { r= EINTR; goto xit; }
116  } else {
117  adns__diag(ads,-1,0,"poll failed in wait: %s",strerror(errno));
119  }
120  } else {
121  assert(r >= 0);
122  adns_afterpoll(ads,fds,nfds,0);
123  }
124  }
125 
126  xit:
128  return r;
129 }
130 
131 #endif
adns_state ads
Definition: adh-query.c:35
void adns__fdevents(adns_state ads, const struct pollfd *pollfds, int npollfds, int maxfd, const fd_set *readfds, const fd_set *writefds, const fd_set *exceptfds, struct timeval now, int *r_r)
Definition: event.c:542
#define INT_MAX
Definition: limits.h:40
adns_initflags iflags
Definition: internal.h:292
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS])
Definition: event.c:312
#define EINTR
Definition: acclib.h:80
unsigned long tv_sec
Definition: linux.h:1738
#define assert(x)
Definition: debug.h:53
int errno
void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io, struct timeval *tv_buf)
Definition: event.c:149
const char * strerror(int err)
Definition: compat_str.c:23
void adns__timeouts(adns_state ads, int act, struct timeval **tv_io, struct timeval *tvbuf, struct timeval now)
Definition: event.c:283
ADNS_API int adns_wait_poll(adns_state ads, adns_query *query_io, adns_answer **answer_r, void **context_r)
void adns__diag(adns_state ads, int serv, adns_query qu, const char *fmt,...)
Definition: general.c:102
time_t now
Definition: finger.c:65
ADNS_API int adns_beforepoll(adns_state ads, struct pollfd *fds, int *nfds_io, int *timeout_io, const struct timeval *now)
Definition: arc.h:35
int adns__internal_check(adns_state ads, adns_query *query_io, adns_answer **answer, void **context_r)
Definition: event.c:670
int poll(struct pollfd *ufds, int nfds, int timeout)
Definition: adnstest.c:68
Definition: linux.h:1867
#define ERANGE
Definition: acclib.h:92
static const WCHAR space[]
Definition: ConsoleCP.c:24
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
ADNS_API void adns_globalsystemfailure(adns_state ads)
Definition: event.c:619
#define MAX_POLLFDS
Definition: internal.h:82
void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc)
Definition: check.c:185
ADNS_API void adns_afterpoll(adns_state ads, const struct pollfd *fds, int nfds, const struct timeval *now)