Fix denial of service - memory corruption.
[Samba.git] / source / lib / select.c
blob2d5f02c094524e8964d9fcb131ae987353804b65
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"
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
28 This means all Samba signal handlers should call sys_select_signal().
31 static pid_t initialised;
32 static int select_pipe[2];
33 static VOLATILE unsigned pipe_written, pipe_read;
35 /*******************************************************************
36 Call this from all Samba signal handlers if you want to avoid a
37 nasty signal race condition.
38 ********************************************************************/
40 void sys_select_signal(char c)
42 if (!initialised) return;
44 if (pipe_written > pipe_read+256) return;
46 if (write(select_pipe[1], &c, 1) == 1) pipe_written++;
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 ********************************************************************/
55 int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
57 int ret, saved_errno;
58 fd_set *readfds2, readfds_buf;
60 if (initialised != sys_getpid()) {
61 if (pipe(select_pipe) == -1)
62 smb_panic("Could not create select pipe");
64 if (select_pipe[0] < 0 || select_pipe[0] >= FD_SETSIZE) {
65 errno = EBADF;
66 return -1;
70 * These next two lines seem to fix a bug with the Linux
71 * 2.0.x kernel (and probably other UNIXes as well) where
72 * the one byte read below can block even though the
73 * select returned that there is data in the pipe and
74 * the pipe_written variable was incremented. Thanks to
75 * HP for finding this one. JRA.
78 if(set_blocking(select_pipe[0],0)==-1)
79 smb_panic("select_pipe[0]: O_NONBLOCK failed");
80 if(set_blocking(select_pipe[1],0)==-1)
81 smb_panic("select_pipe[1]: O_NONBLOCK failed");
83 initialised = sys_getpid();
86 maxfd = MAX(select_pipe[0]+1, maxfd);
88 /* If readfds is NULL we need to provide our own set. */
89 if (readfds) {
90 readfds2 = readfds;
91 } else {
92 readfds2 = &readfds_buf;
93 FD_ZERO(readfds2);
96 FD_SET(select_pipe[0], readfds2);
98 errno = 0;
99 ret = select(maxfd,readfds2,writefds,errorfds,tval);
101 if (ret <= 0) {
102 FD_ZERO(readfds2);
103 if (writefds)
104 FD_ZERO(writefds);
105 if (errorfds)
106 FD_ZERO(errorfds);
107 } else if (FD_ISSET(select_pipe[0], readfds2)) {
108 char c;
109 saved_errno = errno;
110 if (read(select_pipe[0], &c, 1) == 1) {
111 pipe_read++;
112 /* Mark Weaver <mark-clist@npsl.co.uk> pointed out a critical
113 fix to ensure we don't lose signals. We must always
114 return -1 when the select pipe is set, otherwise if another
115 fd is also ready (so ret == 2) then we used to eat the
116 byte in the pipe and lose the signal. JRA.
118 ret = -1;
119 #if 0
120 /* JRA - we can use this to debug the signal messaging... */
121 DEBUG(0,("select got %u signal\n", (unsigned int)c));
122 #endif
123 errno = EINTR;
124 } else {
125 FD_CLR(select_pipe[0], readfds2);
126 ret--;
127 errno = saved_errno;
131 return ret;
134 /*******************************************************************
135 Similar to sys_select() but catch EINTR and continue.
136 This is what sys_select() used to do in Samba.
137 ********************************************************************/
139 int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
141 int ret;
142 fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf;
143 struct timeval tval2, *ptval, end_time;
145 readfds2 = (readfds ? &readfds_buf : NULL);
146 writefds2 = (writefds ? &writefds_buf : NULL);
147 errorfds2 = (errorfds ? &errorfds_buf : NULL);
148 if (tval) {
149 GetTimeOfDay(&end_time);
150 end_time.tv_sec += tval->tv_sec;
151 end_time.tv_usec += tval->tv_usec;
152 end_time.tv_sec += end_time.tv_usec / 1000000;
153 end_time.tv_usec %= 1000000;
154 errno = 0;
155 tval2 = *tval;
156 ptval = &tval2;
157 } else {
158 ptval = NULL;
161 do {
162 if (readfds)
163 readfds_buf = *readfds;
164 if (writefds)
165 writefds_buf = *writefds;
166 if (errorfds)
167 errorfds_buf = *errorfds;
168 if (ptval && (errno == EINTR)) {
169 struct timeval now_time;
170 SMB_BIG_INT tdif;
172 GetTimeOfDay(&now_time);
173 tdif = usec_time_diff(&end_time, &now_time);
174 if (tdif <= 0) {
175 ret = 0; /* time expired. */
176 break;
178 ptval->tv_sec = tdif / 1000000;
179 ptval->tv_usec = tdif % 1000000;
182 /* We must use select and not sys_select here. If we use
183 sys_select we'd lose the fact a signal occurred when sys_select
184 read a byte from the pipe. Fix from Mark Weaver
185 <mark-clist@npsl.co.uk>
187 ret = select(maxfd, readfds2, writefds2, errorfds2, ptval);
188 } while (ret == -1 && errno == EINTR);
190 if (readfds)
191 *readfds = readfds_buf;
192 if (writefds)
193 *writefds = writefds_buf;
194 if (errorfds)
195 *errorfds = errorfds_buf;
197 return ret;