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()
31 static int initialised;
32 static int select_pipe[2];
33 static VOLATILE SIG_ATOMIC_T 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 ********************************************************************/
40 void sys_select_signal(void)
43 if (!initialised) return;
45 if (pipe_written > pipe_read+256) return;
47 if (write(select_pipe[1], &c, 1) == 1) pipe_written++;
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 int sys_select(int maxfd, fd_set *fds,struct timeval *tval)
64 maxfd = MAX(select_pipe[0], maxfd);
65 FD_SET(select_pipe[0], fds);
67 ret = select(maxfd,fds,NULL,NULL,tval);
73 if (FD_ISSET(select_pipe[0], fds)) {
74 FD_CLR(select_pipe[0], fds);
82 while (pipe_written != pipe_read) {
84 if (read(select_pipe[0], &c, 1) == 1) pipe_read++;
90 /*******************************************************************
91 similar to sys_select() but catch EINTR and continue
92 this is what sys_select() used to do in Samba
93 ********************************************************************/
94 int sys_select_intr(int maxfd, fd_set *fds,struct timeval *tval)
101 ret = sys_select(maxfd, &fds2, tval);
102 } while (ret == -1 && errno == EINTR);