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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "system/select.h"
24 #include "lib/util/select.h"
26 /* This is here because it allows us to avoid a nasty race in signal handling.
27 We need to guarantee that when we get a signal we get out of a select immediately
28 but doing that involves a race condition. We can avoid the race by getting the
29 signal handler to write to a pipe that is in the select/poll list
31 This means all Samba signal handlers should call sys_select_signal().
34 static pid_t initialised
;
35 static int select_pipe
[2];
36 static volatile unsigned pipe_written
, pipe_read
;
38 /*******************************************************************
39 Call this from all Samba signal handlers if you want to avoid a
40 nasty signal race condition.
41 ********************************************************************/
43 void sys_select_signal(char c
)
45 int saved_errno
= errno
;
47 if (!initialised
) return;
49 if (pipe_written
> pipe_read
+256) return;
51 if (write(select_pipe
[1], &c
, 1) == 1) pipe_written
++;
57 * sys_poll expects pollfd's to be a talloc'ed array.
59 * It expects the talloc_array_length(fds) >= num_fds+1 to give space
63 int sys_poll(struct pollfd
*fds
, int num_fds
, int timeout
)
67 if (talloc_array_length(fds
) < num_fds
+1) {
72 if (initialised
!= sys_getpid()) {
73 if (pipe(select_pipe
) == -1)
75 int saved_errno
= errno
;
76 DEBUG(0, ("sys_poll: pipe failed (%s)\n",
83 * These next two lines seem to fix a bug with the Linux
84 * 2.0.x kernel (and probably other UNIXes as well) where
85 * the one byte read below can block even though the
86 * select returned that there is data in the pipe and
87 * the pipe_written variable was incremented. Thanks to
88 * HP for finding this one. JRA.
91 if(set_blocking(select_pipe
[0],0)==-1)
92 smb_panic("select_pipe[0]: O_NONBLOCK failed");
93 if(set_blocking(select_pipe
[1],0)==-1)
94 smb_panic("select_pipe[1]: O_NONBLOCK failed");
96 initialised
= sys_getpid();
99 ZERO_STRUCT(fds
[num_fds
]);
100 fds
[num_fds
].fd
= select_pipe
[0];
101 fds
[num_fds
].events
= POLLIN
|POLLHUP
;
104 ret
= poll(fds
, num_fds
+1, timeout
);
106 if ((ret
>= 0) && (fds
[num_fds
].revents
& (POLLIN
|POLLHUP
|POLLERR
))) {
108 int saved_errno
= errno
;
110 if (read(select_pipe
[0], &c
, 1) == 1) {
113 /* Mark Weaver <mark-clist@npsl.co.uk> pointed out a critical
114 fix to ensure we don't lose signals. We must always
115 return -1 when the select pipe is set, otherwise if another
116 fd is also ready (so ret == 2) then we used to eat the
117 byte in the pipe and lose the signal. JRA.
121 /* JRA - we can use this to debug the signal messaging... */
122 DEBUG(0,("select got %u signal\n", (unsigned int)c
));
134 int sys_poll_intr(struct pollfd
*fds
, int num_fds
, int timeout
)
136 int orig_timeout
= timeout
;
137 struct timespec start
;
140 clock_gettime_mono(&start
);
146 ret
= poll(fds
, num_fds
, timeout
);
150 if (errno
!= EINTR
) {
153 clock_gettime_mono(&now
);
154 elapsed
= nsec_time_diff(&now
, &start
);
155 timeout
= (orig_timeout
- elapsed
) / 1000000;