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 "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 66long LastReapTime; 67int xfd_ffs(fd_mask); 68 69 70/* like ffs, but uses fd_mask instead of int as argument, so it works 71 when fd_mask is longer than an int, such as common 64-bit platforms */ 72int 73xfd_ffs(fd_mask mask) 74{ 75 int i; 76 77 if (!mask) return 0; 78 79 for (i = 1; !(mask & 1); i++) 80 { 81 mask >>= 1; 82 } 83 return i; 84} 85 86 87/* 88 * wait_for_something 89 * 90 * server suspends until 91 * - data from clients 92 * - new client connects 93 * - room to write data to clients 94 */ 95 96int 97WaitForSomething(int *pClientsReady) 98{ 99 struct timeval *wt, 100 waittime; 101 fd_set clientsReadable; 102 fd_set clientsWriteable; 103 long curclient; 104 int selecterr; 105 long current_time = 0; 106 long timeout; 107 int nready, 108 i; 109 110 while (1) { 111 /* handle the work Q */ 112 if (workQueue) 113 ProcessWorkQueue(); 114 115 if (XFD_ANYSET(&ClientsWithInput)) { 116 XFD_COPYSET(&ClientsWithInput, &clientsReadable); 117 break; 118 } 119 /* 120 * deal with KeepAlive timeouts. if this seems to costly, SIGALRM 121 * could be used, but its more dangerous since some it could catch us 122 * at an inopportune moment (like inside un-reentrant malloc()). 123 */ 124 current_time = GetTimeInMillis(); 125 timeout = current_time - LastReapTime; 126 if (timeout > ReapClientTime) { 127 ReapAnyOldClients(); 128 LastReapTime = current_time; 129 timeout = ReapClientTime; 130 } 131 timeout = ReapClientTime - timeout; 132 waittime.tv_sec = timeout / MILLI_PER_SECOND; 133 waittime.tv_usec = (timeout % MILLI_PER_SECOND) * 134 (1000000 / MILLI_PER_SECOND); 135 wt = &waittime; 136 137 XFD_COPYSET(&AllSockets, &LastSelectMask); 138 139 BlockHandler(&wt, (pointer) &LastSelectMask); 140 if (NewOutputPending) 141 FlushAllOutput(); 142 143 if (AnyClientsWriteBlocked) { 144 XFD_COPYSET(&ClientsWriteBlocked, &clientsWriteable); 145 i = Select(MAXSOCKS, &LastSelectMask, &clientsWriteable, NULL, wt); 146 } else { 147 i = Select(MAXSOCKS, &LastSelectMask, NULL, NULL, wt); 148 } 149 selecterr = errno; 150 151 WakeupHandler(i, (unsigned long *) &LastSelectMask); 152 if (i <= 0) { /* error or timeout */ 153 FD_ZERO(&clientsWriteable); 154 if (i < 0) { 155 if (selecterr == EBADF) { /* somebody disconnected */ 156 CheckConnections(); 157 } else if (selecterr != EINTR) { 158 ErrorF("WaitForSomething: select(): errno %d\n", selecterr); 159 } else { 160 /* 161 * must have been broken by a signal. go deal with any 162 * exception flags 163 */ 164 return 0; 165 } 166 } else { /* must have timed out */ 167 ReapAnyOldClients(); 168 LastReapTime = GetTimeInMillis(); 169 } 170 } else { 171 if (AnyClientsWriteBlocked && XFD_ANYSET(&clientsWriteable)) { 172 NewOutputPending = TRUE; 173 XFD_ORSET(&OutputPending, &clientsWriteable, &OutputPending); 174 XFD_UNSET(&ClientsWriteBlocked, &clientsWriteable); 175 if (!XFD_ANYSET(&ClientsWriteBlocked)) 176 AnyClientsWriteBlocked = FALSE; 177 } 178 XFD_ANDSET(&clientsReadable, &LastSelectMask, &AllClients); 179 if (LastSelectMask.fds_bits[0] & WellKnownConnections.fds_bits[0]) 180 MakeNewConnections(); 181 if (XFD_ANYSET(&clientsReadable)) 182 break; 183 184 } 185 } 186 nready = 0; 187 188 if (XFD_ANYSET(&clientsReadable)) { 189 ClientPtr client; 190 int conn; 191 192 if (current_time) /* may not have been set */ 193 current_time = GetTimeInMillis(); 194 for (i = 0; i < howmany(XFD_SETSIZE, NFDBITS); i++) { 195 while (clientsReadable.fds_bits[i]) { 196 curclient = xfd_ffs(clientsReadable.fds_bits[i]) - 1; 197 conn = ConnectionTranslation[curclient + (i * (sizeof(fd_mask) * 8))]; 198 clientsReadable.fds_bits[i] &= ~(((fd_mask)1L) << curclient); 199 client = clients[conn]; 200 if (!client) 201 continue; 202 pClientsReady[nready++] = conn; 203 client->last_request_time = current_time; 204 client->clientGone = CLIENT_ALIVE; 205 206 if (nready >= MaxClients) { 207 /* pClientsReady buffer has no more room, get the 208 rest on the next time through select() loop */ 209 return nready; 210 } 211 } 212 } 213 } 214 return nready; 215} 216