2 Unix SMB/CIFS implementation.
3 Samba select/poll implementation
4 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
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
28 this means all Samba signal handlers should call sys_select_signal()
30 static pid_t initialised;
31 static int select_pipe[2];
32 static VOLATILE unsigned pipe_written, pipe_read;
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)
42 if (!initialised) return;
44 if (pipe_written > pipe_read+256) return;
46 if (write(select_pipe[1], &c, 1) == 1) pipe_written++;
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)
58 if (initialised != sys_getpid()) {
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.
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");
75 initialised = sys_getpid();
78 maxfd = MAX(select_pipe[0]+1, maxfd);
79 FD_SET(select_pipe[0], fds);
81 ret = select(maxfd,fds,NULL,NULL,tval);
87 if (FD_ISSET(select_pipe[0], fds)) {
88 FD_CLR(select_pipe[0], fds);
98 while (pipe_written != pipe_read) {
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);
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)
122 ret = sys_select(maxfd, &fds2, tval);
123 } while (ret == -1 && errno == EINTR);