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