6f7df1f5fe56a9946a7525d1624d3899cea8e172
[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 3 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
32 static pid_t initialised;
33 static int select_pipe[2];
34 static VOLATILE unsigned pipe_written, pipe_read;
35
36 /*******************************************************************
37  Call this from all Samba signal handlers if you want to avoid a 
38  nasty signal race condition.
39 ********************************************************************/
40
41 void sys_select_signal(char c)
42 {
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
56 int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
57 {
58         int ret, saved_errno;
59         fd_set *readfds2, readfds_buf;
60
61         if (initialised != sys_getpid()) {
62                 pipe(select_pipe);
63
64                 /*
65                  * These next two lines seem to fix a bug with the Linux
66                  * 2.0.x kernel (and probably other UNIXes as well) where
67                  * the one byte read below can block even though the
68                  * select returned that there is data in the pipe and
69                  * the pipe_written variable was incremented. Thanks to
70                  * HP for finding this one. JRA.
71                  */
72
73                 if(set_blocking(select_pipe[0],0)==-1)
74                         smb_panic("select_pipe[0]: O_NONBLOCK failed");
75                 if(set_blocking(select_pipe[1],0)==-1)
76                         smb_panic("select_pipe[1]: O_NONBLOCK failed");
77
78                 initialised = sys_getpid();
79         }
80
81         maxfd = MAX(select_pipe[0]+1, maxfd);
82
83         /* If readfds is NULL we need to provide our own set. */
84         if (readfds) {
85                 readfds2 = readfds;
86         } else {
87                 readfds2 = &readfds_buf;
88                 FD_ZERO(readfds2);
89         }
90         FD_SET(select_pipe[0], readfds2);
91
92         errno = 0;
93         ret = select(maxfd,readfds2,writefds,errorfds,tval);
94
95         if (ret <= 0) {
96                 FD_ZERO(readfds2);
97                 if (writefds)
98                         FD_ZERO(writefds);
99                 if (errorfds)
100                         FD_ZERO(errorfds);
101         } else if (FD_ISSET(select_pipe[0], readfds2)) {
102                 char c;
103                 saved_errno = errno;
104                 if (read(select_pipe[0], &c, 1) == 1) {
105                         pipe_read++;
106                         /* Mark Weaver <mark-clist@npsl.co.uk> pointed out a critical
107                            fix to ensure we don't lose signals. We must always
108                            return -1 when the select pipe is set, otherwise if another
109                            fd is also ready (so ret == 2) then we used to eat the
110                            byte in the pipe and lose the signal. JRA.
111                         */
112                         ret = -1;
113 #if 0
114                         /* JRA - we can use this to debug the signal messaging... */
115                         DEBUG(0,("select got %u signal\n", (unsigned int)c));
116 #endif
117                         errno = EINTR;
118                 } else {
119                         FD_CLR(select_pipe[0], readfds2);
120                         ret--;
121                         errno = saved_errno;
122                 }
123         }
124
125         return ret;
126 }
127
128 /*******************************************************************
129  Similar to sys_select() but catch EINTR and continue.
130  This is what sys_select() used to do in Samba.
131 ********************************************************************/
132
133 int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
134 {
135         int ret;
136         fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf;
137         struct timeval tval2, *ptval, end_time;
138
139         readfds2 = (readfds ? &readfds_buf : NULL);
140         writefds2 = (writefds ? &writefds_buf : NULL);
141         errorfds2 = (errorfds ? &errorfds_buf : NULL);
142         if (tval) {
143                 GetTimeOfDay(&end_time);
144                 end_time.tv_sec += tval->tv_sec;
145                 end_time.tv_usec += tval->tv_usec;
146                 end_time.tv_sec += end_time.tv_usec / 1000000;
147                 end_time.tv_usec %= 1000000;
148                 errno = 0;
149                 tval2 = *tval;
150                 ptval = &tval2;
151         } else {
152                 ptval = NULL;
153         }
154
155         do {
156                 if (readfds)
157                         readfds_buf = *readfds;
158                 if (writefds)
159                         writefds_buf = *writefds;
160                 if (errorfds)
161                         errorfds_buf = *errorfds;
162                 if (ptval && (errno == EINTR)) {
163                         struct timeval now_time;
164                         SMB_BIG_INT tdif;
165
166                         GetTimeOfDay(&now_time);
167                         tdif = usec_time_diff(&end_time, &now_time);
168                         if (tdif <= 0) {
169                                 ret = 0; /* time expired. */
170                                 break;
171                         }
172                         ptval->tv_sec = tdif / 1000000;
173                         ptval->tv_usec = tdif % 1000000;
174                 }
175
176                 /* We must use select and not sys_select here. If we use
177                    sys_select we'd lose the fact a signal occurred when sys_select
178                    read a byte from the pipe. Fix from Mark Weaver
179                    <mark-clist@npsl.co.uk>
180                 */
181                 ret = select(maxfd, readfds2, writefds2, errorfds2, ptval);
182         } while (ret == -1 && errno == EINTR);
183
184         if (readfds)
185                 *readfds = readfds_buf;
186         if (writefds)
187                 *writefds = writefds_buf;
188         if (errorfds)
189                 *errorfds = errorfds_buf;
190
191         return ret;
192 }