2 Unix SMB/Netbios implementation.
4 Samba select/poll implementation
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
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
29 This means all Samba signal handlers should call sys_select_signal().
32 static pid_t initialised
;
33 static int select_pipe
[2];
34 static VOLATILE
unsigned pipe_written
, pipe_read
;
36 /*******************************************************************
37 Call this from all Samba signal handlers if you want to avoid a
38 nasty signal race condition.
39 ********************************************************************/
41 void sys_select_signal(void)
44 if (!initialised
) return;
46 if (pipe_written
> pipe_read
+256) return;
48 if (write(select_pipe
[1], &c
, 1) == 1) pipe_written
++;
51 /*******************************************************************
52 Like select() but avoids the signal race using a pipe
53 it also guuarantees that fds on return only ever contains bits set
54 for file descriptors that were readable.
55 ********************************************************************/
57 int sys_select(int maxfd
, fd_set
*readfds
, fd_set
*writefds
, fd_set
*errorfds
, struct timeval
*tval
)
60 fd_set
*readfds2
, readfds_buf
;
62 if (initialised
!= sys_getpid()) {
66 * These next two lines seem to fix a bug with the Linux
67 * 2.0.x kernel (and probably other UNIXes as well) where
68 * the one byte read below can block even though the
69 * select returned that there is data in the pipe and
70 * the pipe_written variable was incremented. Thanks to
71 * HP for finding this one. JRA.
74 if(set_blocking(select_pipe
[0],0)==-1)
75 smb_panic("select_pipe[0]: O_NONBLOCK failed.\n");
76 if(set_blocking(select_pipe
[1],0)==-1)
77 smb_panic("select_pipe[1]: O_NONBLOCK failed.\n");
79 initialised
= sys_getpid();
82 maxfd
= MAX(select_pipe
[0]+1, maxfd
);
84 /* If readfds is NULL we need to provide our own set. */
88 readfds2
= &readfds_buf
;
91 FD_SET(select_pipe
[0], readfds2
);
94 ret
= select(maxfd
,readfds2
,writefds
,errorfds
,tval
);
102 } else if (FD_ISSET(select_pipe
[0], readfds2
)) {
105 if (read(select_pipe
[0], &c
, 1) == 1) {
107 /* Mark Weaver <mark-clist@npsl.co.uk> pointed out a critical
108 fix to ensure we don't lose signals. We must always
109 return -1 when the select pipe is set, otherwise if another
110 fd is also ready (so ret == 2) then we used to eat the
111 byte in the pipe and lose the signal. JRA.
116 FD_CLR(select_pipe
[0], readfds2
);
125 /*******************************************************************
126 Similar to sys_select() but catch EINTR and continue.
127 This is what sys_select() used to do in Samba.
128 ********************************************************************/
130 int sys_select_intr(int maxfd
, fd_set
*readfds
, fd_set
*writefds
, fd_set
*errorfds
, struct timeval
*tval
)
133 fd_set
*readfds2
, readfds_buf
, *writefds2
, writefds_buf
, *errorfds2
, errorfds_buf
;
134 struct timeval tval2
, *ptval
, end_time
;
136 readfds2
= (readfds
? &readfds_buf
: NULL
);
137 writefds2
= (writefds
? &writefds_buf
: NULL
);
138 errorfds2
= (errorfds
? &errorfds_buf
: NULL
);
140 GetTimeOfDay(&end_time
);
141 end_time
.tv_sec
+= tval
->tv_sec
;
142 end_time
.tv_usec
+= tval
->tv_usec
;
143 end_time
.tv_sec
+= end_time
.tv_usec
/ 1000000;
144 end_time
.tv_usec
%= 1000000;
154 readfds_buf
= *readfds
;
156 writefds_buf
= *writefds
;
158 errorfds_buf
= *errorfds
;
159 if (ptval
&& (errno
== EINTR
)) {
160 struct timeval now_time
;
163 GetTimeOfDay(&now_time
);
164 tdif
= usec_time_diff(&end_time
, &now_time
);
166 ret
= 0; /* time expired. */
169 ptval
->tv_sec
= tdif
/ 1000000;
170 ptval
->tv_usec
= tdif
% 1000000;
173 /* We must use select and not sys_select here. If we use
174 sys_select we'd lose the fact a signal occurred when sys_select
175 read a byte from the pipe. Fix from Mark Weaver
176 <mark-clist@npsl.co.uk>
178 ret
= select(maxfd
, readfds2
, writefds2
, errorfds2
, ptval
);
179 } while (ret
== -1 && errno
== EINTR
);
182 *readfds
= readfds_buf
;
184 *writefds
= writefds_buf
;
186 *errorfds
= errorfds_buf
;