source: branches/samba-3.3.x/source/lib/select.c@ 578

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

Update Samba 3.3 to 3.3.15 (security update)

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