a1b2e04065bc9c702ec5adc514bb91634df6db23
[samba.git] / source4 / lib / util / 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 #include "system/filesys.h"
24
25 /* This is here because it allows us to avoid a nasty race in signal handling. 
26    We need to guarantee that when we get a signal we get out of a select immediately
27    but doing that involves a race condition. We can avoid the race by getting the 
28    signal handler to write to a pipe that is in the select/poll list 
29
30    This means all Samba signal handlers should call sys_select_signal().
31 */
32
33 static pid_t initialised;
34 static int select_pipe[2];
35 static VOLATILE unsigned pipe_written, pipe_read;
36
37 /*******************************************************************
38  Call this from all Samba signal handlers if you want to avoid a 
39  nasty signal race condition.
40 ********************************************************************/
41
42 void sys_select_signal(void)
43 {
44         char c = 1;
45         if (!initialised) return;
46
47         if (pipe_written > pipe_read+256) return;
48
49         if (write(select_pipe[1], &c, 1) == 1) pipe_written++;
50 }
51
52 /*******************************************************************
53  Like select() but avoids the signal race using a pipe
54  it also guarantees that fds on return only ever contains bits set
55  for file descriptors that were readable.
56 ********************************************************************/
57
58 int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
59 {
60         int ret, saved_errno;
61         fd_set *readfds2, readfds_buf;
62
63         if (initialised != getpid()) {
64                 pipe(select_pipe);
65
66                 /*
67                  * These next two lines seem to fix a bug with the Linux
68                  * 2.0.x kernel (and probably other UNIXes as well) where
69                  * the one byte read below can block even though the
70                  * select returned that there is data in the pipe and
71                  * the pipe_written variable was incremented. Thanks to
72                  * HP for finding this one. JRA.
73                  */
74
75                 if(set_blocking(select_pipe[0],0)==-1)
76                         smb_panic("select_pipe[0]: O_NONBLOCK failed.\n");
77                 if(set_blocking(select_pipe[1],0)==-1)
78                         smb_panic("select_pipe[1]: O_NONBLOCK failed.\n");
79
80                 initialised = getpid();
81         }
82
83         maxfd = MAX(select_pipe[0]+1, maxfd);
84
85         /* If readfds is NULL we need to provide our own set. */
86         if (readfds) {
87                 readfds2 = readfds;
88         } else {
89                 readfds2 = &readfds_buf;
90                 FD_ZERO(readfds2);
91         }
92         FD_SET(select_pipe[0], readfds2);
93
94         errno = 0;
95         ret = select(maxfd,readfds2,writefds,errorfds,tval);
96
97         if (ret <= 0) {
98                 FD_ZERO(readfds2);
99                 if (writefds)
100                         FD_ZERO(writefds);
101                 if (errorfds)
102                         FD_ZERO(errorfds);
103         } else if (FD_ISSET(select_pipe[0], readfds2)) {
104                 char c;
105                 saved_errno = errno;
106                 if (read(select_pipe[0], &c, 1) == 1) {
107                         pipe_read++;
108                         /* Mark Weaver <mark-clist@npsl.co.uk> pointed out a critical
109                            fix to ensure we don't lose signals. We must always
110                            return -1 when the select pipe is set, otherwise if another
111                            fd is also ready (so ret == 2) then we used to eat the
112                            byte in the pipe and lose the signal. JRA.
113                         */
114                         ret = -1;
115                         errno = EINTR;
116                 } else {
117                         FD_CLR(select_pipe[0], readfds2);
118                         ret--;
119                         errno = saved_errno;
120                 }
121         }
122
123         return ret;
124 }
125
126 /*******************************************************************
127  Similar to sys_select() but catch EINTR and continue.
128  This is what sys_select() used to do in Samba.
129 ********************************************************************/
130
131 int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
132 {
133         int ret;
134         fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf;
135         struct timeval tval2, *ptval;
136
137         readfds2 = (readfds ? &readfds_buf : NULL);
138         writefds2 = (writefds ? &writefds_buf : NULL);
139         errorfds2 = (errorfds ? &errorfds_buf : NULL);
140         ptval = (tval ? &tval2 : NULL);
141
142         do {
143                 if (readfds)
144                         readfds_buf = *readfds;
145                 if (writefds)
146                         writefds_buf = *writefds;
147                 if (errorfds)
148                         errorfds_buf = *errorfds;
149                 if (tval)
150                         tval2 = *tval;
151
152                 /* We must use select and not sys_select here. If we use
153                    sys_select we'd lose the fact a signal occurred when sys_select
154                    read a byte from the pipe. Fix from Mark Weaver
155                    <mark-clist@npsl.co.uk>
156                 */
157
158                 ret = select(maxfd, readfds2, writefds2, errorfds2, ptval);
159         } while (ret == -1 && errno == EINTR);
160
161         if (readfds)
162                 *readfds = readfds_buf;
163         if (writefds)
164                 *writefds = writefds_buf;
165         if (errorfds)
166                 *errorfds = errorfds_buf;
167
168         return ret;
169 }