waitfor.c revision 30f8ce46
1/* 2 * waits for input 3 */ 4/* 5Copyright 1987, 1998 The Open Group 6 7Permission to use, copy, modify, distribute, and sell this software and its 8documentation for any purpose is hereby granted without fee, provided that 9the above copyright notice appear in all copies and that both that 10copyright notice and this permission notice appear in supporting 11documentation. 12 13The above copyright notice and this permission notice shall be included in 14all copies or substantial portions of the Software. 15 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 23Except as contained in this notice, the name of The Open Group shall not be 24used in advertising or otherwise to promote the sale, use or other dealings 25in this Software without prior written authorization from The Open Group. 26 * Copyright 1990, 1991 Network Computing Devices; 27 * Portions Copyright 1987 by Digital Equipment Corporation 28 * 29 * Permission to use, copy, modify, distribute, and sell this software and its 30 * documentation for any purpose is hereby granted without fee, provided that 31 * the above copyright notice appear in all copies and that both that 32 * copyright notice and this permission notice appear in supporting 33 * documentation, and that the names of Network Computing Devices, 34 * or Digital not be used in advertising or 35 * publicity pertaining to distribution of the software without specific, 36 * written prior permission. Network Computing Devices, or Digital 37 * make no representations about the 38 * suitability of this software for any purpose. It is provided "as is" 39 * without express or implied warranty. 40 * 41 * NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH 42 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 43 * AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES, OR DIGITAL BE 44 * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 45 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 46 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 47 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 48 * 49 */ 50 51#include "xfs-config.h" 52 53#include <X11/Xos.h> /* strings, time, etc */ 54 55#include <stdio.h> 56#include <errno.h> 57#include <sys/param.h> 58 59#include "clientstr.h" 60#include "globals.h" 61#include "X11/Xpoll.h" 62#include "osdep.h" 63#include "os.h" 64 65#ifdef __UNIXOS2__ 66#define select(n,r,w,x,t) os2PseudoSelect(n,r,w,x,t) 67#endif 68 69long LastReapTime; 70int xfd_ffs(fd_mask); 71 72 73/* like ffs, but uses fd_mask instead of int as argument, so it works 74 when fd_mask is longer than an int, such as common 64-bit platforms */ 75int 76xfd_ffs(fd_mask mask) 77{ 78 int i; 79 80 if (!mask) return 0; 81 82 for (i = 1; !(mask & 1); i++) 83 { 84 mask >>= 1; 85 } 86 return i; 87} 88 89 90/* 91 * wait_for_something 92 * 93 * server suspends until 94 * - data from clients 95 * - new client connects 96 * - room to write data to clients 97 */ 98 99int 100WaitForSomething(int *pClientsReady) 101{ 102 struct timeval *wt, 103 waittime; 104 fd_set clientsReadable; 105 fd_set clientsWriteable; 106 long curclient; 107 int selecterr; 108 long current_time = 0; 109 long timeout; 110 int nready, 111 i; 112 113 while (1) { 114 /* handle the work Q */ 115 if (workQueue) 116 ProcessWorkQueue(); 117 118 if (XFD_ANYSET(&ClientsWithInput)) { 119 XFD_COPYSET(&ClientsWithInput, &clientsReadable); 120 break; 121 } 122 /* 123 * deal with KeepAlive timeouts. if this seems to costly, SIGALRM 124 * could be used, but its more dangerous since some it could catch us 125 * at an inopportune moment (like inside un-reentrant malloc()). 126 */ 127 current_time = GetTimeInMillis(); 128 timeout = current_time - LastReapTime; 129 if (timeout > ReapClientTime) { 130 ReapAnyOldClients(); 131 LastReapTime = current_time; 132 timeout = ReapClientTime; 133 } 134 timeout = ReapClientTime - timeout; 135 waittime.tv_sec = timeout / MILLI_PER_SECOND; 136 waittime.tv_usec = (timeout % MILLI_PER_SECOND) * 137 (1000000 / MILLI_PER_SECOND); 138 wt = &waittime; 139 140 XFD_COPYSET(&AllSockets, &LastSelectMask); 141 142 BlockHandler(&wt, (pointer) &LastSelectMask); 143 if (NewOutputPending) 144 FlushAllOutput(); 145 146 if (AnyClientsWriteBlocked) { 147 XFD_COPYSET(&ClientsWriteBlocked, &clientsWriteable); 148 i = Select(MAXSOCKS, &LastSelectMask, &clientsWriteable, NULL, wt); 149 } else { 150 i = Select(MAXSOCKS, &LastSelectMask, NULL, NULL, wt); 151 } 152 selecterr = errno; 153 154 WakeupHandler(i, (unsigned long *) &LastSelectMask); 155 if (i <= 0) { /* error or timeout */ 156 FD_ZERO(&clientsWriteable); 157 if (i < 0) { 158 if (selecterr == EBADF) { /* somebody disconnected */ 159 CheckConnections(); 160 } else if (selecterr != EINTR) { 161 ErrorF("WaitForSomething: select(): errno %d\n", selecterr); 162 } else { 163 /* 164 * must have been broken by a signal. go deal with any 165 * exception flags 166 */ 167 return 0; 168 } 169 } else { /* must have timed out */ 170 ReapAnyOldClients(); 171 LastReapTime = GetTimeInMillis(); 172 } 173 } else { 174 if (AnyClientsWriteBlocked && XFD_ANYSET(&clientsWriteable)) { 175 NewOutputPending = TRUE; 176 XFD_ORSET(&OutputPending, &clientsWriteable, &OutputPending); 177 XFD_UNSET(&ClientsWriteBlocked, &clientsWriteable); 178 if (!XFD_ANYSET(&ClientsWriteBlocked)) 179 AnyClientsWriteBlocked = FALSE; 180 } 181 XFD_ANDSET(&clientsReadable, &LastSelectMask, &AllClients); 182 if (LastSelectMask.fds_bits[0] & WellKnownConnections.fds_bits[0]) 183 MakeNewConnections(); 184 if (XFD_ANYSET(&clientsReadable)) 185 break; 186 187 } 188 } 189 nready = 0; 190 191 if (XFD_ANYSET(&clientsReadable)) { 192 ClientPtr client; 193 int conn; 194 195 if (current_time) /* may not have been set */ 196 current_time = GetTimeInMillis(); 197 for (i = 0; i < howmany(XFD_SETSIZE, NFDBITS); i++) { 198 while (clientsReadable.fds_bits[i]) { 199 curclient = xfd_ffs(clientsReadable.fds_bits[i]) - 1; 200 conn = ConnectionTranslation[curclient + (i * (sizeof(fd_mask) * 8))]; 201 clientsReadable.fds_bits[i] &= ~(((fd_mask)1L) << curclient); 202 client = clients[conn]; 203 if (!client) 204 continue; 205 pClientsReady[nready++] = conn; 206 client->last_request_time = current_time; 207 client->clientGone = CLIENT_ALIVE; 208 209 if (nready >= MaxClients) { 210 /* pClientsReady buffer has no more room, get the 211 rest on the next time through select() loop */ 212 return nready; 213 } 214 } 215 } 216 } 217 return nready; 218} 219