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