📄 io.c
字号:
/***********************************************************Copyright (c) 1987, 1989 X ConsortiumPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEX CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER INAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR INCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.Except as contained in this notice, the name of the X Consortium shall not beused in advertising or otherwise to promote the sale, use or other dealingsin this Software without prior written authorization from the X Consortium.Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts. All Rights ReservedPermission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear in supporting documentation, and that the name of Digital not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE.******************************************************************//* $XConsortium: io.c /main/72 1996/12/27 15:40:56 rws $ *//* $XFree86: xc/programs/Xserver/os/io.c,v 3.15 1997/01/18 06:58:00 dawes Exp $ *//***************************************************************** * i/o functions * * WriteToClient, ReadRequestFromClient * InsertFakeRequest, ResetCurrentRequest * *****************************************************************/#ifdef WIN32#include <X11/Xwinsock.h>#endif#include <stdio.h>#include <X11/Xtrans.h>#ifdef X_NOT_STDC_ENVextern int errno;#endif#include "Xmd.h"#include <errno.h>#if !defined(AMOEBA) && !defined(MINIX) && !defined(__EMX__) && !defined(WIN32)#ifndef Lynx#include <sys/uio.h>#else#include <uio.h>#endif#endif#include "X.h"#define NEED_REPLIES#include "Xproto.h"#include "os.h"#include "Xpoll.h"#include "osdep.h"#include "opaque.h"#include "dixstruct.h"#include "misc.h"#ifdef LBX#include "lbxserve.h"#endifCallbackListPtr ReplyCallback;CallbackListPtr FlushCallback;/* check for both EAGAIN and EWOULDBLOCK, because some supposedly POSIX * systems are broken and return EWOULDBLOCK when they should return EAGAIN */#ifndef __EMX__#if defined(EAGAIN) && defined(EWOULDBLOCK)#define ETEST(err) (err == EAGAIN || err == EWOULDBLOCK)#else#ifdef EAGAIN#define ETEST(err) (err == EAGAIN)#else#define ETEST(err) (err == EWOULDBLOCK)#endif#endif#else /* __EMX__ Writing to full pipes may return ENOSPC */#define ETEST(err) (err == EAGAIN || err == EWOULDBLOCK || err == ENOSPC)#endifextern fd_set ClientsWithInput, IgnoredClientsWithInput, AllClients;extern fd_set ClientsWriteBlocked;extern fd_set OutputPending;extern int ConnectionTranslation[];extern Bool NewOutputPending;extern Bool AnyClientsWriteBlocked;Bool CriticalOutputPending;int timesThisConnection = 0;ConnectionInputPtr FreeInputs = (ConnectionInputPtr)NULL;ConnectionOutputPtr FreeOutputs = (ConnectionOutputPtr)NULL;OsCommPtr AvailableInput = (OsCommPtr)NULL;#define get_req_len(req,cli) ((cli)->swapped ? \ lswaps((req)->length) : (req)->length)#ifdef BIGREQS#include "bigreqstr.h"#define get_big_req_len(req,cli) ((cli)->swapped ? \ lswapl(((xBigReq *)(req))->length) : \ ((xBigReq *)(req))->length)#endif#define MAX_TIMES_PER 10/* * A lot of the code in this file manipulates a ConnectionInputPtr: * * ----------------------------------------------- * |------- bufcnt ------->| | | * | |- gotnow ->| | | * | |-------- needed ------>| | * |-----------+--------- size --------+---------->| * ----------------------------------------------- * ^ ^ * | | * buffer bufptr * * buffer is a pointer to the start of the buffer. * bufptr points to the start of the current request. * bufcnt counts how many bytes are in the buffer. * size is the size of the buffer in bytes. * * In several of the functions, gotnow and needed are local variables * that do the following: * * gotnow is the number of bytes of the request that we're * trying to read that are currently in the buffer. * Typically, gotnow = (buffer + bufcnt) - bufptr * * needed = the length of the request that we're trying to * read. Watch out: needed sometimes counts bytes and sometimes * counts CARD32's. *//***************************************************************** * ReadRequestFromClient * Returns one request in client->requestBuffer. The request * length will be in client->req_len. Return status is: * * > 0 if successful, specifies length in bytes of the request * = 0 if entire request is not yet available * < 0 if client should be terminated * * The request returned must be contiguous so that it can be * cast in the dispatcher to the correct request type. Because requests * are variable length, ReadRequestFromClient() must look at the first 4 * or 8 bytes of a request to determine the length (the request length is * in the 3rd and 4th bytes of the request unless it is a Big Request * (see the Big Request Extension), in which case the 3rd and 4th bytes * are zero and the following 4 bytes are the request length. * * Note: in order to make the server scheduler (WaitForSomething()) * "fair", the ClientsWithInput mask is used. This mask tells which * clients have FULL requests left in their buffers. Clients with * partial requests require a read. Basically, client buffers * are drained before select() is called again. But, we can't keep * reading from a client that is sending buckets of data (or has * a partial request) because others clients need to be scheduled. *****************************************************************/#define YieldControl() \ { isItTimeToYield = TRUE; \ timesThisConnection = 0; }#define YieldControlNoInput() \ { YieldControl(); \ FD_CLR(fd, &ClientsWithInput); }#define YieldControlDeath() \ { timesThisConnection = 0; }#if defined(LBX) || defined(LBX_COMPAT)intStandardReadRequestFromClient(client) ClientPtr client;#elseintReadRequestFromClient(client) ClientPtr client;#endif{ OsCommPtr oc = (OsCommPtr)client->osPrivate; register ConnectionInputPtr oci = oc->input; int fd = oc->fd; register int gotnow, needed; int result; register xReq *request; Bool need_header;#ifdef BIGREQS Bool move_header;#endif /* If an input buffer was empty, either free it if it is too big * or link it into our list of free input buffers. This means that * different clients can share the same input buffer (at different * times). This was done to save memory. */ if (AvailableInput) { if (AvailableInput != oc) { register ConnectionInputPtr aci = AvailableInput->input; if (aci->size > BUFWATERMARK) { xfree(aci->buffer); xfree(aci); } else { aci->next = FreeInputs; FreeInputs = aci; } AvailableInput->input = (ConnectionInputPtr)NULL; } AvailableInput = (OsCommPtr)NULL; } /* make sure we have an input buffer */ if (!oci) { if (oci = FreeInputs) { FreeInputs = oci->next; } else if (!(oci = AllocateInputBuffer())) { YieldControlDeath(); return -1; } oc->input = oci; } /* advance to start of next request */ oci->bufptr += oci->lenLastReq; need_header = FALSE;#ifdef BIGREQS move_header = FALSE;#endif gotnow = oci->bufcnt + oci->buffer - oci->bufptr; if (gotnow < sizeof(xReq)) { /* We don't have an entire xReq yet. Can't tell how big * the request will be until we get the whole xReq. */ needed = sizeof(xReq); need_header = TRUE; } else { /* We have a whole xReq. We can tell how big the whole * request will be unless it is a Big Request. */ request = (xReq *)oci->bufptr; needed = get_req_len(request, client);#ifdef BIGREQS if (!needed && client->big_requests) { /* It's a Big Request. */ move_header = TRUE; if (gotnow < sizeof(xBigReq)) { /* Still need more data to tell just how big. */ needed = sizeof(xBigReq) >> 2; /* needed is in CARD32s now */ need_header = TRUE; } else needed = get_big_req_len(request, client); }#endif client->req_len = needed; needed <<= 2; /* needed is in bytes now */ } if (gotnow < needed) { /* Need to read more data, either so that we can get a * complete xReq (if need_header is TRUE), a complete * xBigReq (if move_header is TRUE), or the rest of the * request (if need_header and move_header are both FALSE). */ oci->lenLastReq = 0; if (needed > MAXBUFSIZE) { /* request is too big for us to handle */ YieldControlDeath(); return -1; } if ((gotnow == 0) || ((oci->bufptr - oci->buffer + needed) > oci->size)) { /* no data, or the request is too big to fit in the buffer */ if ((gotnow > 0) && (oci->bufptr != oci->buffer)) /* save the data we've already read */ memmove(oci->buffer, oci->bufptr, gotnow); if (needed > oci->size) { /* make buffer bigger to accomodate request */ char *ibuf; ibuf = (char *)xrealloc(oci->buffer, needed); if (!ibuf) { YieldControlDeath(); return -1; } oci->size = needed; oci->buffer = ibuf; } oci->bufptr = oci->buffer; oci->bufcnt = gotnow; } /* XXX this is a workaround. This function is sometimes called * after the trans_conn has been freed. In this case trans_conn * will be null. Really ought to restructure things so that we * never get here in those circumstances. */ if (!oc->trans_conn) { /* treat as if an error occured on the read, which is what * used to happen */ YieldControlDeath(); return -1; }#ifdef LBX if (oc->proxy && oc->proxy->compHandle) result = (*oc->proxy->streamOpts.streamCompRead)(fd, (unsigned char *)oci->buffer + oci->bufcnt, oci->size - oci->bufcnt); else#endif result = _XSERVTransRead(oc->trans_conn, oci->buffer + oci->bufcnt, oci->size - oci->bufcnt); if (result <= 0) { if ((result < 0) && ETEST(errno)) {#if defined(SVR4) && defined(i386) && !defined(sun)#if defined(LBX) && 0 /* * For LBX connections, we can get a valid EWOULDBLOCK * There is probably a better way of distinguishing LBX * connections, but this works. (DHD) */ extern int LbxRead(); if (oc->Read == LbxRead)#else if (0)#endif#endif { YieldControlNoInput(); return 0; } } YieldControlDeath(); return -1; } oci->bufcnt += result; gotnow += result; /* free up some space after huge requests */ if ((oci->size > BUFWATERMARK) && (oci->bufcnt < BUFSIZE) && (needed < BUFSIZE)) { char *ibuf; ibuf = (char *)xrealloc(oci->buffer, BUFSIZE); if (ibuf) { oci->size = BUFSIZE; oci->buffer = ibuf; oci->bufptr = ibuf + oci->bufcnt - gotnow; } } if (need_header && gotnow >= needed) { /* We wanted an xReq, now we've gotten it. */ request = (xReq *)oci->bufptr; needed = get_req_len(request, client);#ifdef BIGREQS if (!needed && client->big_requests) { move_header = TRUE; if (gotnow < sizeof(xBigReq)) needed = sizeof(xBigReq) >> 2; else needed = get_big_req_len(request, client); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -