1 Description: Use poll(2) instead of select(2) to support more than 1024 file descriptors
2 Author: Ben Smithurst <ben.smithurst@gradwell.com>
3 Bug-Debian: https://bugs.debian.org/478193
5 diff --git a/src/osdep/unix/os_lnx.c b/src/osdep/unix/os_lnx.c
6 index 03fd17d..671bbd6 100644
7 --- a/src/osdep/unix/os_lnx.c
8 +++ b/src/osdep/unix/os_lnx.c
10 extern int errno; /* just in case */
17 diff --git a/src/osdep/unix/os_slx.c b/src/osdep/unix/os_slx.c
18 index c94d632..f6bf27d 100644
19 --- a/src/osdep/unix/os_slx.c
20 +++ b/src/osdep/unix/os_slx.c
21 @@ -42,6 +42,7 @@ extern int errno; /* just in case */
29 diff --git a/src/osdep/unix/tcp_unix.c b/src/osdep/unix/tcp_unix.c
30 index 795fb4f..c69eaec 100644
31 --- a/src/osdep/unix/tcp_unix.c
32 +++ b/src/osdep/unix/tcp_unix.c
33 @@ -235,12 +235,11 @@ TCPSTREAM *tcp_open (char *host,char *service,unsigned long port)
34 int tcp_socket_open (int family,void *adr,size_t adrlen,unsigned short port,
35 char *tmp,int *ctr,char *hst)
38 + int i,ti,sock,flgs,tmo;
42 struct protoent *pt = getprotobyname ("tcp");
43 - fd_set rfds,wfds,efds;
45 struct sockaddr *sadr = ip_sockaddr (family,adr,adrlen,port,&len);
46 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
48 @@ -252,14 +251,6 @@ int tcp_socket_open (int family,void *adr,size_t adrlen,unsigned short port,
49 sprintf (tmp,"Unable to create TCP socket: %s",strerror (errno));
50 (*bn) (BLOCK_NONSENSITIVE,data);
52 - else if (sock >= FD_SETSIZE) {/* unselectable sockets are useless */
53 - sprintf (tmp,"Unable to create selectable TCP socket (%d >= %d)",
55 - (*bn) (BLOCK_NONSENSITIVE,data);
61 else { /* get current socket flags */
62 flgs = fcntl (sock,F_GETFL,0);
63 @@ -284,16 +275,11 @@ int tcp_socket_open (int family,void *adr,size_t adrlen,unsigned short port,
64 if ((sock >= 0) && ctr) { /* want open timeout? */
65 now = time (0); /* open timeout */
66 ti = ttmo_open ? now + ttmo_open : 0;
68 - FD_ZERO (&rfds); /* initialize selection vector */
69 - FD_ZERO (&wfds); /* initialize selection vector */
70 - FD_ZERO (&efds); /* handle errors too */
71 - FD_SET (sock,&rfds); /* block for error or readable or writable */
72 - FD_SET (sock,&wfds);
73 - FD_SET (sock,&efds);
75 + pfd.events = POLLIN | POLLOUT;
76 do { /* block under timeout */
77 - tmo.tv_sec = ti ? ti - now : 0;
78 - i = select (sock+1,&rfds,&wfds,&efds,ti ? &tmo : NIL);
79 + tmo = ti ? ti - now : 0;
80 + i = poll (&pfd, 1, ti ? tmo * 1000 : -1);
81 now = time (0); /* fake timeout if interrupt & time expired */
82 if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0;
83 } while ((i < 0) && (errno == EINTR));
84 @@ -302,7 +288,7 @@ int tcp_socket_open (int family,void *adr,size_t adrlen,unsigned short port,
85 fcntl (sock,F_SETFL,flgs);
86 /* This used to be a zero-byte read(), but that crashes Solaris */
87 /* get socket status */
88 - if(FD_ISSET(sock, &rfds)) while (((i = *ctr = read (sock,tmp,1)) < 0) && (errno == EINTR));
89 + if(pfd.revents & POLLIN) while (((i = *ctr = read (sock,tmp,1)) < 0) && (errno == EINTR));
91 if (i <= 0) { /* timeout or error? */
92 i = i ? errno : ETIMEDOUT;/* determine error code */
93 @@ -545,9 +531,8 @@ long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *s)
103 blocknotify_t bn=(blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
104 (*bn) (BLOCK_TCPREAD,NIL);
105 @@ -556,16 +541,13 @@ long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *s)
107 time_t ti = ttmo_read ? now + ttmo_read : 0;
108 if (tcpdebug) mm_log ("Reading TCP buffer",TCPDEBUG);
110 - FD_ZERO (&fds); /* initialize selection vector */
111 - FD_ZERO (&efds); /* handle errors too */
112 - /* set bit in selection vectors */
113 - FD_SET (stream->tcpsi,&fds);
114 - FD_SET (stream->tcpsi,&efds);
116 + pfd.events = POLLIN;
117 + pfd.fd = stream->tcpsi;
118 errno = NIL; /* initially no error */
119 do { /* block under timeout */
120 - tmo.tv_sec = ti ? ti - now : 0;
121 - i = select (stream->tcpsi+1,&fds,NIL,&efds,ti ? &tmo : NIL);
122 + tmo = ti ? ti - now : 0;
123 + i = poll (&pfd, 1, ti ? tmo * 1000 : -1);
124 now = time (0); /* fake timeout if interrupt & time expired */
125 if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0;
126 } while ((i < 0) && (errno == EINTR));
127 @@ -605,9 +587,8 @@ long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *s)
129 long tcp_getdata (TCPSTREAM *stream)
133 - struct timeval tmo;
137 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
138 if (stream->tcpsi < 0) return NIL;
139 @@ -617,15 +598,12 @@ long tcp_getdata (TCPSTREAM *stream)
141 time_t ti = ttmo_read ? now + ttmo_read : 0;
142 if (tcpdebug) mm_log ("Reading TCP data",TCPDEBUG);
144 - FD_ZERO (&fds); /* initialize selection vector */
145 - FD_ZERO (&efds); /* handle errors too */
146 - FD_SET (stream->tcpsi,&fds);/* set bit in selection vectors */
147 - FD_SET (stream->tcpsi,&efds);
148 + pfd.fd = stream->tcpsi;
149 + pfd.events = POLLIN;
150 errno = NIL; /* initially no error */
151 do { /* block under timeout */
152 - tmo.tv_sec = ti ? ti - now : 0;
153 - i = select (stream->tcpsi+1,&fds,NIL,&efds,ti ? &tmo : NIL);
154 + tmo = ti ? ti - now : 0;
155 + i = poll (&pfd, 1, ti ? tmo * 1000 : -1);
156 now = time (0); /* fake timeout if interrupt & time expired */
157 if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0;
158 } while ((i < 0) && (errno == EINTR));
159 @@ -677,9 +655,8 @@ long tcp_soutr (TCPSTREAM *stream,char *string)
161 long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size)
165 - struct timeval tmo;
169 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
170 if (stream->tcpso < 0) return NIL;
171 @@ -689,15 +666,12 @@ long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size)
173 time_t ti = ttmo_write ? now + ttmo_write : 0;
174 if (tcpdebug) mm_log ("Writing to TCP",TCPDEBUG);
176 - FD_ZERO (&fds); /* initialize selection vector */
177 - FD_ZERO (&efds); /* handle errors too */
178 - FD_SET (stream->tcpso,&fds);/* set bit in selection vector */
179 - FD_SET(stream->tcpso,&efds);/* set bit in error selection vector */
180 + pfd.fd = stream->tcpso;
181 + pfd.events = POLLOUT;
182 errno = NIL; /* block and write */
183 do { /* block under timeout */
184 - tmo.tv_sec = ti ? ti - now : 0;
185 - i = select (stream->tcpso+1,NIL,&fds,&efds,ti ? &tmo : NIL);
186 + tmo = ti ? ti - now : 0;
187 + i = poll (&pfd, 1, ti ? tmo * 1000 : -1);
188 now = time (0); /* fake timeout if interrupt & time expired */
189 if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0;
190 } while ((i < 0) && (errno == EINTR));