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
;
39 * sys_poll expects pollfd's to be a talloc'ed array.
41 * It expects the talloc_array_length(fds) >= num_fds+1 to give space
45 int sys_poll(struct pollfd
*fds
, int num_fds
, int timeout
)
49 if (talloc_array_length(fds
) < num_fds
+1) {
54 if (initialised
!= sys_getpid()) {
55 if (pipe(select_pipe
) == -1)
57 int saved_errno
= errno
;
58 DEBUG(0, ("sys_poll: pipe failed (%s)\n",
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.
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");
78 initialised
= sys_getpid();
81 ZERO_STRUCT(fds
[num_fds
]);
82 fds
[num_fds
].fd
= select_pipe
[0];
83 fds
[num_fds
].events
= POLLIN
|POLLHUP
;
86 ret
= poll(fds
, num_fds
+1, timeout
);
88 if ((ret
>= 0) && (fds
[num_fds
].revents
& (POLLIN
|POLLHUP
|POLLERR
))) {
90 int saved_errno
= errno
;
92 if (read(select_pipe
[0], &c
, 1) == 1) {
95 /* Mark Weaver <mark-clist@npsl.co.uk> pointed out a critical
96 fix to ensure we don't lose signals. We must always
97 return -1 when the select pipe is set, otherwise if another
98 fd is also ready (so ret == 2) then we used to eat the
99 byte in the pipe and lose the signal. JRA.
103 /* JRA - we can use this to debug the signal messaging... */
104 DEBUG(0,("select got %u signal\n", (unsigned int)c
));
116 int sys_poll_intr(struct pollfd
*fds
, int num_fds
, int timeout
)
118 int orig_timeout
= timeout
;
119 struct timespec start
;
122 clock_gettime_mono(&start
);
128 ret
= poll(fds
, num_fds
, timeout
);
132 if (errno
!= EINTR
) {
135 clock_gettime_mono(&now
);
136 elapsed
= nsec_time_diff(&now
, &start
);
137 timeout
= (orig_timeout
- elapsed
) / 1000000;