lib/util: Remove unused sys_select_signal()
[Samba.git] / lib / util / select.c
blob63be55cf0c6b703e0068233d3aa8c1425e55821f
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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/>.
21 #include "includes.h"
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
42 * to the signal pipe.
45 int sys_poll(struct pollfd *fds, int num_fds, int timeout)
47 int ret;
49 if (talloc_array_length(fds) < num_fds+1) {
50 errno = ENOSPC;
51 return -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",
59 strerror(errno)));
60 errno = saved_errno;
61 return -1;
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;
85 errno = 0;
86 ret = poll(fds, num_fds+1, timeout);
88 if ((ret >= 0) && (fds[num_fds].revents & (POLLIN|POLLHUP|POLLERR))) {
89 char c;
90 int saved_errno = errno;
92 if (read(select_pipe[0], &c, 1) == 1) {
93 pipe_read += 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.
101 ret = -1;
102 #if 0
103 /* JRA - we can use this to debug the signal messaging... */
104 DEBUG(0,("select got %u signal\n", (unsigned int)c));
105 #endif
106 errno = EINTR;
107 } else {
108 ret -= 1;
109 errno = saved_errno;
113 return ret;
116 int sys_poll_intr(struct pollfd *fds, int num_fds, int timeout)
118 int orig_timeout = timeout;
119 struct timespec start;
120 int ret;
122 clock_gettime_mono(&start);
124 while (true) {
125 struct timespec now;
126 int64_t elapsed;
128 ret = poll(fds, num_fds, timeout);
129 if (ret != -1) {
130 break;
132 if (errno != EINTR) {
133 break;
135 clock_gettime_mono(&now);
136 elapsed = nsec_time_diff(&now, &start);
137 timeout = (orig_timeout - elapsed) / 1000000;
139 return ret;