source: trunk/samba-3.0.25pre1/source/lib/select.c@ 1

Last change on this file since 1 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 6.0 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//printf("Select called, ret = %d\n",ret);
98 if (ret <= 0) {
99 FD_ZERO(readfds2);
100 if (writefds)
101 FD_ZERO(writefds);
102 if (errorfds)
103 FD_ZERO(errorfds);
104 } else if (FD_ISSET(select_pipe[0], readfds2)) {
105 char c;
106 saved_errno = errno;
107 if (read(select_pipe[0], &c, 1) == 1) {
108 pipe_read++;
109 /* Mark Weaver <mark-clist@npsl.co.uk> pointed out a critical
110 fix to ensure we don't lose signals. We must always
111 return -1 when the select pipe is set, otherwise if another
112 fd is also ready (so ret == 2) then we used to eat the
113 byte in the pipe and lose the signal. JRA.
114 */
115 ret = -1;
116#if 0
117 /* JRA - we can use this to debug the signal messaging... */
118 DEBUG(0,("select got %u signal\n", (unsigned int)c));
119#endif
120 errno = EINTR;
121 } else {
122 FD_CLR(select_pipe[0], readfds2);
123 ret--;
124 errno = saved_errno;
125 }
126 }
127
128 return ret;
129}
130
131/*******************************************************************
132 Similar to sys_select() but catch EINTR and continue.
133 This is what sys_select() used to do in Samba.
134********************************************************************/
135
136int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
137{
138 int ret;
139 fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf;
140 struct timeval tval2, *ptval, end_time;
141
142 readfds2 = (readfds ? &readfds_buf : NULL);
143 writefds2 = (writefds ? &writefds_buf : NULL);
144 errorfds2 = (errorfds ? &errorfds_buf : NULL);
145 if (tval) {
146 GetTimeOfDay(&end_time);
147 end_time.tv_sec += tval->tv_sec;
148 end_time.tv_usec += tval->tv_usec;
149 end_time.tv_sec += end_time.tv_usec / 1000000;
150 end_time.tv_usec %= 1000000;
151 errno = 0;
152 tval2 = *tval;
153 ptval = &tval2;
154 } else {
155 ptval = NULL;
156 }
157
158 do {
159 if (readfds)
160 readfds_buf = *readfds;
161 if (writefds)
162 writefds_buf = *writefds;
163 if (errorfds)
164 errorfds_buf = *errorfds;
165 if (ptval && (errno == EINTR)) {
166 struct timeval now_time;
167 SMB_BIG_INT tdif;
168
169 GetTimeOfDay(&now_time);
170 tdif = usec_time_diff(&end_time, &now_time);
171 if (tdif <= 0) {
172 ret = 0; /* time expired. */
173 break;
174 }
175 ptval->tv_sec = tdif / 1000000;
176 ptval->tv_usec = tdif % 1000000;
177 }
178
179 /* We must use select and not sys_select here. If we use
180 sys_select we'd lose the fact a signal occurred when sys_select
181 read a byte from the pipe. Fix from Mark Weaver
182 <mark-clist@npsl.co.uk>
183 */
184 ret = select(maxfd, readfds2, writefds2, errorfds2, ptval);
185//printf("select2 called, ret = %d\n");
186 } while (ret == -1 && errno == EINTR);
187
188 if (readfds)
189 *readfds = readfds_buf;
190 if (writefds)
191 *writefds = writefds_buf;
192 if (errorfds)
193 *errorfds = errorfds_buf;
194
195 return ret;
196}
Note: See TracBrowser for help on using the repository browser.