source: branches/samba-3.0/source/lib/select.c@ 140

Last change on this file since 140 was 33, checked in by Yuri Dario, 18 years ago

Added new files from rc3, removed dummy printf.

File size: 5.9 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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include "includes.h"
23
24/* This is here because it allows us to avoid a nasty race in signal handling.
25 We need to guarantee that when we get a signal we get out of a select immediately
26 but doing that involves a race condition. We can avoid the race by getting the
27 signal handler to write to a pipe that is in the select/poll list
28
29 This means all Samba signal handlers should call sys_select_signal().
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 pipe(select_pipe);
63#else
64 socketpair(AF_UNIX, SOCK_STREAM,0, select_pipe);
65#endif
66
67 /*
68 * These next two lines seem to fix a bug with the Linux
69 * 2.0.x kernel (and probably other UNIXes as well) where
70 * the one byte read below can block even though the
71 * select returned that there is data in the pipe and
72 * the pipe_written variable was incremented. Thanks to
73 * HP for finding this one. JRA.
74 */
75
76 if(set_blocking(select_pipe[0],0)==-1)
77 smb_panic("select_pipe[0]: O_NONBLOCK failed.\n");
78 if(set_blocking(select_pipe[1],0)==-1)
79 smb_panic("select_pipe[1]: O_NONBLOCK failed.\n");
80
81 initialised = sys_getpid();
82 }
83
84 maxfd = MAX(select_pipe[0]+1, maxfd);
85
86 /* If readfds is NULL we need to provide our own set. */
87 if (readfds) {
88 readfds2 = readfds;
89 } else {
90 readfds2 = &readfds_buf;
91 FD_ZERO(readfds2);
92 }
93 FD_SET(select_pipe[0], readfds2);
94
95 errno = 0;
96 ret = select(maxfd,readfds2,writefds,errorfds,tval);
97 if (ret <= 0) {
98 FD_ZERO(readfds2);
99 if (writefds)
100 FD_ZERO(writefds);
101 if (errorfds)
102 FD_ZERO(errorfds);
103 } else if (FD_ISSET(select_pipe[0], readfds2)) {
104 char c;
105 saved_errno = errno;
106 if (read(select_pipe[0], &c, 1) == 1) {
107 pipe_read++;
108 /* Mark Weaver <mark-clist@npsl.co.uk> pointed out a critical
109 fix to ensure we don't lose signals. We must always
110 return -1 when the select pipe is set, otherwise if another
111 fd is also ready (so ret == 2) then we used to eat the
112 byte in the pipe and lose the signal. JRA.
113 */
114 ret = -1;
115#if 0
116 /* JRA - we can use this to debug the signal messaging... */
117 DEBUG(0,("select got %u signal\n", (unsigned int)c));
118#endif
119 errno = EINTR;
120 } else {
121 FD_CLR(select_pipe[0], readfds2);
122 ret--;
123 errno = saved_errno;
124 }
125 }
126
127 return ret;
128}
129
130/*******************************************************************
131 Similar to sys_select() but catch EINTR and continue.
132 This is what sys_select() used to do in Samba.
133********************************************************************/
134
135int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
136{
137 int ret;
138 fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf;
139 struct timeval tval2, *ptval, end_time;
140
141 readfds2 = (readfds ? &readfds_buf : NULL);
142 writefds2 = (writefds ? &writefds_buf : NULL);
143 errorfds2 = (errorfds ? &errorfds_buf : NULL);
144 if (tval) {
145 GetTimeOfDay(&end_time);
146 end_time.tv_sec += tval->tv_sec;
147 end_time.tv_usec += tval->tv_usec;
148 end_time.tv_sec += end_time.tv_usec / 1000000;
149 end_time.tv_usec %= 1000000;
150 errno = 0;
151 tval2 = *tval;
152 ptval = &tval2;
153 } else {
154 ptval = NULL;
155 }
156
157 do {
158 if (readfds)
159 readfds_buf = *readfds;
160 if (writefds)
161 writefds_buf = *writefds;
162 if (errorfds)
163 errorfds_buf = *errorfds;
164 if (ptval && (errno == EINTR)) {
165 struct timeval now_time;
166 SMB_BIG_INT tdif;
167
168 GetTimeOfDay(&now_time);
169 tdif = usec_time_diff(&end_time, &now_time);
170 if (tdif <= 0) {
171 ret = 0; /* time expired. */
172 break;
173 }
174 ptval->tv_sec = tdif / 1000000;
175 ptval->tv_usec = tdif % 1000000;
176 }
177
178 /* We must use select and not sys_select here. If we use
179 sys_select we'd lose the fact a signal occurred when sys_select
180 read a byte from the pipe. Fix from Mark Weaver
181 <mark-clist@npsl.co.uk>
182 */
183 ret = select(maxfd, readfds2, writefds2, errorfds2, ptval);
184//printf("select2 called, ret = %d\n");
185 } while (ret == -1 && errno == EINTR);
186
187 if (readfds)
188 *readfds = readfds_buf;
189 if (writefds)
190 *writefds = writefds_buf;
191 if (errorfds)
192 *errorfds = errorfds_buf;
193
194 return ret;
195}
Note: See TracBrowser for help on using the repository browser.