source: trunk/server/source3/lib/select.c@ 656

Last change on this file since 656 was 656, checked in by Herwig Bauernfeind, 14 years ago

Samba Server 3.5: Some more work to get high-mem working

File size: 6.5 KB
Line 
1/*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 Samba select/poll implementation
5 Copyright (C) Andrew Tridgell 1992-1998
6
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.
11
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.
16
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/>.
19*/
20
21#include "includes.h"
22
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
27
28 This means all Samba signal handlers should call sys_select_signal().
29*/
30#ifdef __OS2__
31#define pipe(A) os2_socketpair(A)
32#endif
33
34static pid_t initialised;
35static int select_pipe[2];
36static volatile unsigned pipe_written, pipe_read;
37
38/*******************************************************************
39 Call this from all Samba signal handlers if you want to avoid a
40 nasty signal race condition.
41********************************************************************/
42
43void sys_select_signal(char c)
44{
45 int saved_errno = errno;
46
47 if (!initialised) return;
48
49 if (pipe_written > pipe_read+256) return;
50
51 if (write(select_pipe[1], &c, 1) == 1) pipe_written++;
52
53 errno = saved_errno;
54}
55
56/*******************************************************************
57 Like select() but avoids the signal race using a pipe
58 it also guuarantees that fds on return only ever contains bits set
59 for file descriptors that were readable.
60********************************************************************/
61
62int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
63{
64 int ret, saved_errno;
65 fd_set *readfds2, readfds_buf;
66
67 if (initialised != sys_getpid()) {
68#ifndef __OS2__
69 if (pipe(select_pipe) == -1)
70#else
71 if (socketpair(AF_UNIX, SOCK_STREAM,0, select_pipe) == -1)
72#endif
73 {
74 DEBUG(0, ("sys_select: pipe failed (%s)\n",
75 strerror(errno)));
76 if (readfds != NULL)
77 FD_ZERO(readfds);
78 if (writefds != NULL)
79 FD_ZERO(writefds);
80 if (errorfds != NULL)
81 FD_ZERO(errorfds);
82 return -1;
83 }
84
85 if (select_pipe[0] < 0 || select_pipe[0] >= FD_SETSIZE) {
86 DEBUG(0, ("sys_select: bad fd\n"));
87 if (readfds != NULL)
88 FD_ZERO(readfds);
89 if (writefds != NULL)
90 FD_ZERO(writefds);
91 if (errorfds != NULL)
92 FD_ZERO(errorfds);
93 errno = EBADF;
94 return -1;
95 }
96 /*
97 * These next two lines seem to fix a bug with the Linux
98 * 2.0.x kernel (and probably other UNIXes as well) where
99 * the one byte read below can block even though the
100 * select returned that there is data in the pipe and
101 * the pipe_written variable was incremented. Thanks to
102 * HP for finding this one. JRA.
103 */
104
105 if(set_blocking(select_pipe[0],0)==-1)
106 smb_panic("select_pipe[0]: O_NONBLOCK failed");
107 if(set_blocking(select_pipe[1],0)==-1)
108 smb_panic("select_pipe[1]: O_NONBLOCK failed");
109
110 initialised = sys_getpid();
111 }
112
113 maxfd = MAX(select_pipe[0]+1, maxfd);
114
115 /* If readfds is NULL we need to provide our own set. */
116 if (readfds) {
117 readfds2 = readfds;
118 } else {
119 readfds2 = &readfds_buf;
120 FD_ZERO(readfds2);
121 }
122
123 FD_SET(select_pipe[0], readfds2);
124
125 errno = 0;
126 ret = select(maxfd,readfds2,writefds,errorfds,tval);
127
128 if (ret <= 0) {
129 FD_ZERO(readfds2);
130 if (writefds)
131 FD_ZERO(writefds);
132 if (errorfds)
133 FD_ZERO(errorfds);
134 } else if (FD_ISSET(select_pipe[0], readfds2)) {
135 char c;
136 saved_errno = errno;
137 if (read(select_pipe[0], &c, 1) == 1) {
138 pipe_read++;
139 /* Mark Weaver <mark-clist@npsl.co.uk> pointed out a critical
140 fix to ensure we don't lose signals. We must always
141 return -1 when the select pipe is set, otherwise if another
142 fd is also ready (so ret == 2) then we used to eat the
143 byte in the pipe and lose the signal. JRA.
144 */
145 ret = -1;
146#if 0
147 /* JRA - we can use this to debug the signal messaging... */
148 DEBUG(0,("select got %u signal\n", (unsigned int)c));
149#endif
150 errno = EINTR;
151 } else {
152 FD_CLR(select_pipe[0], readfds2);
153 ret--;
154 errno = saved_errno;
155 }
156 }
157
158 return ret;
159}
160
161/*******************************************************************
162 Similar to sys_select() but catch EINTR and continue.
163 This is what sys_select() used to do in Samba.
164********************************************************************/
165
166int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
167{
168 int ret;
169 fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf;
170 struct timeval tval2, *ptval, end_time;
171
172 readfds2 = (readfds ? &readfds_buf : NULL);
173 writefds2 = (writefds ? &writefds_buf : NULL);
174 errorfds2 = (errorfds ? &errorfds_buf : NULL);
175 if (tval) {
176 GetTimeOfDay(&end_time);
177 end_time.tv_sec += tval->tv_sec;
178 end_time.tv_usec += tval->tv_usec;
179 end_time.tv_sec += end_time.tv_usec / 1000000;
180 end_time.tv_usec %= 1000000;
181 errno = 0;
182 tval2 = *tval;
183 ptval = &tval2;
184 } else {
185 ptval = NULL;
186 }
187
188 do {
189 if (readfds)
190 readfds_buf = *readfds;
191 if (writefds)
192 writefds_buf = *writefds;
193 if (errorfds)
194 errorfds_buf = *errorfds;
195 if (ptval && (errno == EINTR)) {
196 struct timeval now_time;
197 int64_t tdif;
198
199 GetTimeOfDay(&now_time);
200 tdif = usec_time_diff(&end_time, &now_time);
201 if (tdif <= 0) {
202 ret = 0; /* time expired. */
203 break;
204 }
205 ptval->tv_sec = tdif / 1000000;
206 ptval->tv_usec = tdif % 1000000;
207 }
208
209 /* We must use select and not sys_select here. If we use
210 sys_select we'd lose the fact a signal occurred when sys_select
211 read a byte from the pipe. Fix from Mark Weaver
212 <mark-clist@npsl.co.uk>
213 */
214 ret = select(maxfd, readfds2, writefds2, errorfds2, ptval);
215 } while (ret == -1 && errno == EINTR);
216
217 if (readfds)
218 *readfds = readfds_buf;
219 if (writefds)
220 *writefds = writefds_buf;
221 if (errorfds)
222 *errorfds = errorfds_buf;
223
224 return ret;
225}
Note: See TracBrowser for help on using the repository browser.