Always increment even if the read fails (otherwise we spin if we hit the kernel bug...
[tprouty/samba.git] / source / lib / select.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    Samba select/poll implementation
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /* this is here because it allows us to avoid a nasty race in signal handling. 
25    We need to guarantee that when we get a signal we get out of a select immediately
26    but doing that involves a race condition. We can avoid the race by getting the 
27    signal handler to write to a pipe that is in the select/poll list 
28
29    this means all Samba signal handlers should call sys_select_signal()
30 */
31 static pid_t initialised;
32 static int select_pipe[2];
33 static VOLATILE unsigned pipe_written, pipe_read;
34
35
36 /*******************************************************************
37 call this from all Samba signal handlers if you want to avoid a 
38 nasty signal race condition
39 ********************************************************************/
40 void sys_select_signal(void)
41 {
42         char c = 1;
43         if (!initialised) return;
44
45         if (pipe_written > pipe_read+256) return;
46
47         if (write(select_pipe[1], &c, 1) == 1) pipe_written++;
48 }
49
50 /*******************************************************************
51 like select() but avoids the signal race using a pipe
52 it also guuarantees that fds on return only ever contains bits set
53 for file descriptors that were readable
54 ********************************************************************/
55 int sys_select(int maxfd, fd_set *fds,struct timeval *tval)
56 {
57         int ret, saved_errno;
58
59         if (initialised != sys_getpid()) {
60                 pipe(select_pipe);
61
62                 /*
63                  * These next two lines seem to fix a bug with the Linux
64                  * 2.0.x kernel (and probably other UNIXes as well) where
65                  * the one byte read below can block even though the
66                  * select returned that there is data in the pipe and
67                  * the pipe_written variable was incremented. Thanks to
68                  * HP for finding this one. JRA.
69                  */
70
71                 if(set_blocking(select_pipe[0],0)==-1)
72                         smb_panic("select_pipe[0]: O_NONBLOCK failed.\n");
73                 if(set_blocking(select_pipe[1],0)==-1)
74                         smb_panic("select_pipe[1]: O_NONBLOCK failed.\n");
75
76                 initialised = sys_getpid();
77         }
78
79         maxfd = MAX(select_pipe[0]+1, maxfd);
80         FD_SET(select_pipe[0], fds);
81         errno = 0;
82         ret = select(maxfd,fds,NULL,NULL,tval);
83
84         if (ret <= 0) {
85                 FD_ZERO(fds);
86         }
87
88         if (FD_ISSET(select_pipe[0], fds)) {
89                 FD_CLR(select_pipe[0], fds);
90                 ret--;
91                 if (ret == 0) {
92                         ret = -1;
93                         errno = EINTR;
94                 }
95         }
96
97         saved_errno = errno;
98
99         while (pipe_written != pipe_read) {
100                 char c;
101                 /* Due to the linux kernel bug in 2.0.x, we
102                  * always increment here even if the read failed... */
103                 read(select_pipe[0], &c, 1);
104                 pipe_read++;
105         }
106
107         errno = saved_errno;
108
109         return ret;
110 }
111
112 /*******************************************************************
113 similar to sys_select() but catch EINTR and continue
114 this is what sys_select() used to do in Samba
115 ********************************************************************/
116 int sys_select_intr(int maxfd, fd_set *fds,struct timeval *tval)
117 {
118         int ret;
119         fd_set fds2;
120
121         do {
122                 fds2 = *fds;
123                 ret = sys_select(maxfd, &fds2, tval);
124         } while (ret == -1 && errno == EINTR);
125
126         *fds = fds2;
127
128         return ret;
129 }