1*4882a593Smuzhiyun /***********************************************************
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun Copyright 1987, 1989, 1998 The Open Group
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun Permission to use, copy, modify, distribute, and sell this software and its
6*4882a593Smuzhiyun documentation for any purpose is hereby granted without fee, provided that
7*4882a593Smuzhiyun the above copyright notice appear in all copies and that both that
8*4882a593Smuzhiyun copyright notice and this permission notice appear in supporting
9*4882a593Smuzhiyun documentation.
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun The above copyright notice and this permission notice shall be included in
12*4882a593Smuzhiyun all copies or substantial portions of the Software.
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*4882a593Smuzhiyun IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*4882a593Smuzhiyun FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17*4882a593Smuzhiyun OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18*4882a593Smuzhiyun AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19*4882a593Smuzhiyun CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun Except as contained in this notice, the name of The Open Group shall not be
22*4882a593Smuzhiyun used in advertising or otherwise to promote the sale, use or other dealings
23*4882a593Smuzhiyun in this Software without prior written authorization from The Open Group.
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun All Rights Reserved
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun Permission to use, copy, modify, and distribute this software and its
30*4882a593Smuzhiyun documentation for any purpose and without fee is hereby granted,
31*4882a593Smuzhiyun provided that the above copyright notice appear in all copies and that
32*4882a593Smuzhiyun both that copyright notice and this permission notice appear in
33*4882a593Smuzhiyun supporting documentation, and that the name of Digital not be
34*4882a593Smuzhiyun used in advertising or publicity pertaining to distribution of the
35*4882a593Smuzhiyun software without specific, written prior permission.
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38*4882a593Smuzhiyun ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39*4882a593Smuzhiyun DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40*4882a593Smuzhiyun ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41*4882a593Smuzhiyun WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42*4882a593Smuzhiyun ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43*4882a593Smuzhiyun SOFTWARE.
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun ******************************************************************/
46*4882a593Smuzhiyun /*****************************************************************
47*4882a593Smuzhiyun * i/o functions
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * WriteToClient, ReadRequestFromClient
50*4882a593Smuzhiyun * InsertFakeRequest, ResetCurrentRequest
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun *****************************************************************/
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
55*4882a593Smuzhiyun #include <dix-config.h>
56*4882a593Smuzhiyun #endif
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun #undef DEBUG_COMMUNICATION
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun #ifdef WIN32
61*4882a593Smuzhiyun #include <X11/Xwinsock.h>
62*4882a593Smuzhiyun #endif
63*4882a593Smuzhiyun #include <stdio.h>
64*4882a593Smuzhiyun #define XSERV_t
65*4882a593Smuzhiyun #define TRANS_SERVER
66*4882a593Smuzhiyun #define TRANS_REOPEN
67*4882a593Smuzhiyun #include <X11/Xtrans/Xtrans.h>
68*4882a593Smuzhiyun #include <X11/Xmd.h>
69*4882a593Smuzhiyun #include <errno.h>
70*4882a593Smuzhiyun #if !defined(WIN32)
71*4882a593Smuzhiyun #include <sys/uio.h>
72*4882a593Smuzhiyun #endif
73*4882a593Smuzhiyun #include <X11/X.h>
74*4882a593Smuzhiyun #include <X11/Xproto.h>
75*4882a593Smuzhiyun #include "os.h"
76*4882a593Smuzhiyun #include "osdep.h"
77*4882a593Smuzhiyun #include "opaque.h"
78*4882a593Smuzhiyun #include "dixstruct.h"
79*4882a593Smuzhiyun #include "misc.h"
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun CallbackListPtr ReplyCallback;
82*4882a593Smuzhiyun CallbackListPtr FlushCallback;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun typedef struct _connectionInput {
85*4882a593Smuzhiyun struct _connectionInput *next;
86*4882a593Smuzhiyun char *buffer; /* contains current client input */
87*4882a593Smuzhiyun char *bufptr; /* pointer to current start of data */
88*4882a593Smuzhiyun int bufcnt; /* count of bytes in buffer */
89*4882a593Smuzhiyun int lenLastReq;
90*4882a593Smuzhiyun int size;
91*4882a593Smuzhiyun unsigned int ignoreBytes; /* bytes to ignore before the next request */
92*4882a593Smuzhiyun } ConnectionInput;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun typedef struct _connectionOutput {
95*4882a593Smuzhiyun struct _connectionOutput *next;
96*4882a593Smuzhiyun unsigned char *buf;
97*4882a593Smuzhiyun int size;
98*4882a593Smuzhiyun int count;
99*4882a593Smuzhiyun } ConnectionOutput;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun static ConnectionInputPtr AllocateInputBuffer(void);
102*4882a593Smuzhiyun static ConnectionOutputPtr AllocateOutputBuffer(void);
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun static Bool CriticalOutputPending;
105*4882a593Smuzhiyun static int timesThisConnection = 0;
106*4882a593Smuzhiyun static ConnectionInputPtr FreeInputs = (ConnectionInputPtr) NULL;
107*4882a593Smuzhiyun static ConnectionOutputPtr FreeOutputs = (ConnectionOutputPtr) NULL;
108*4882a593Smuzhiyun static OsCommPtr AvailableInput = (OsCommPtr) NULL;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun #define get_req_len(req,cli) ((cli)->swapped ? \
111*4882a593Smuzhiyun bswap_16((req)->length) : (req)->length)
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun #include <X11/extensions/bigreqsproto.h>
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun #define get_big_req_len(req,cli) ((cli)->swapped ? \
116*4882a593Smuzhiyun bswap_32(((xBigReq *)(req))->length) : \
117*4882a593Smuzhiyun ((xBigReq *)(req))->length)
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun #define BUFSIZE 16384
120*4882a593Smuzhiyun #define BUFWATERMARK 32768
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /*
123*4882a593Smuzhiyun * A lot of the code in this file manipulates a ConnectionInputPtr:
124*4882a593Smuzhiyun *
125*4882a593Smuzhiyun * -----------------------------------------------
126*4882a593Smuzhiyun * |------- bufcnt ------->| | |
127*4882a593Smuzhiyun * | |- gotnow ->| | |
128*4882a593Smuzhiyun * | |-------- needed ------>| |
129*4882a593Smuzhiyun * |-----------+--------- size --------+---------->|
130*4882a593Smuzhiyun * -----------------------------------------------
131*4882a593Smuzhiyun * ^ ^
132*4882a593Smuzhiyun * | |
133*4882a593Smuzhiyun * buffer bufptr
134*4882a593Smuzhiyun *
135*4882a593Smuzhiyun * buffer is a pointer to the start of the buffer.
136*4882a593Smuzhiyun * bufptr points to the start of the current request.
137*4882a593Smuzhiyun * bufcnt counts how many bytes are in the buffer.
138*4882a593Smuzhiyun * size is the size of the buffer in bytes.
139*4882a593Smuzhiyun *
140*4882a593Smuzhiyun * In several of the functions, gotnow and needed are local variables
141*4882a593Smuzhiyun * that do the following:
142*4882a593Smuzhiyun *
143*4882a593Smuzhiyun * gotnow is the number of bytes of the request that we're
144*4882a593Smuzhiyun * trying to read that are currently in the buffer.
145*4882a593Smuzhiyun * Typically, gotnow = (buffer + bufcnt) - bufptr
146*4882a593Smuzhiyun *
147*4882a593Smuzhiyun * needed = the length of the request that we're trying to
148*4882a593Smuzhiyun * read. Watch out: needed sometimes counts bytes and sometimes
149*4882a593Smuzhiyun * counts CARD32's.
150*4882a593Smuzhiyun */
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /*****************************************************************
153*4882a593Smuzhiyun * ReadRequestFromClient
154*4882a593Smuzhiyun * Returns one request in client->requestBuffer. The request
155*4882a593Smuzhiyun * length will be in client->req_len. Return status is:
156*4882a593Smuzhiyun *
157*4882a593Smuzhiyun * > 0 if successful, specifies length in bytes of the request
158*4882a593Smuzhiyun * = 0 if entire request is not yet available
159*4882a593Smuzhiyun * < 0 if client should be terminated
160*4882a593Smuzhiyun *
161*4882a593Smuzhiyun * The request returned must be contiguous so that it can be
162*4882a593Smuzhiyun * cast in the dispatcher to the correct request type. Because requests
163*4882a593Smuzhiyun * are variable length, ReadRequestFromClient() must look at the first 4
164*4882a593Smuzhiyun * or 8 bytes of a request to determine the length (the request length is
165*4882a593Smuzhiyun * in the 3rd and 4th bytes of the request unless it is a Big Request
166*4882a593Smuzhiyun * (see the Big Request Extension), in which case the 3rd and 4th bytes
167*4882a593Smuzhiyun * are zero and the following 4 bytes are the request length.
168*4882a593Smuzhiyun *
169*4882a593Smuzhiyun * Note: in order to make the server scheduler (WaitForSomething())
170*4882a593Smuzhiyun * "fair", the ClientsWithInput mask is used. This mask tells which
171*4882a593Smuzhiyun * clients have FULL requests left in their buffers. Clients with
172*4882a593Smuzhiyun * partial requests require a read. Basically, client buffers
173*4882a593Smuzhiyun * are drained before select() is called again. But, we can't keep
174*4882a593Smuzhiyun * reading from a client that is sending buckets of data (or has
175*4882a593Smuzhiyun * a partial request) because others clients need to be scheduled.
176*4882a593Smuzhiyun *****************************************************************/
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun static void
YieldControl(void)179*4882a593Smuzhiyun YieldControl(void)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun isItTimeToYield = TRUE;
182*4882a593Smuzhiyun timesThisConnection = 0;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun static void
YieldControlNoInput(ClientPtr client)186*4882a593Smuzhiyun YieldControlNoInput(ClientPtr client)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun OsCommPtr oc = client->osPrivate;
189*4882a593Smuzhiyun YieldControl();
190*4882a593Smuzhiyun if (oc->trans_conn)
191*4882a593Smuzhiyun ospoll_reset_events(server_poll, oc->fd);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun static void
YieldControlDeath(void)195*4882a593Smuzhiyun YieldControlDeath(void)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun timesThisConnection = 0;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /* If an input buffer was empty, either free it if it is too big or link it
201*4882a593Smuzhiyun * into our list of free input buffers. This means that different clients can
202*4882a593Smuzhiyun * share the same input buffer (at different times). This was done to save
203*4882a593Smuzhiyun * memory.
204*4882a593Smuzhiyun */
205*4882a593Smuzhiyun static void
NextAvailableInput(OsCommPtr oc)206*4882a593Smuzhiyun NextAvailableInput(OsCommPtr oc)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun if (AvailableInput) {
209*4882a593Smuzhiyun if (AvailableInput != oc) {
210*4882a593Smuzhiyun ConnectionInputPtr aci = AvailableInput->input;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun if (aci->size > BUFWATERMARK) {
213*4882a593Smuzhiyun free(aci->buffer);
214*4882a593Smuzhiyun free(aci);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun else {
217*4882a593Smuzhiyun aci->next = FreeInputs;
218*4882a593Smuzhiyun FreeInputs = aci;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun AvailableInput->input = NULL;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun AvailableInput = NULL;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun int
ReadRequestFromClient(ClientPtr client)227*4882a593Smuzhiyun ReadRequestFromClient(ClientPtr client)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun OsCommPtr oc = (OsCommPtr) client->osPrivate;
230*4882a593Smuzhiyun ConnectionInputPtr oci = oc->input;
231*4882a593Smuzhiyun unsigned int gotnow, needed;
232*4882a593Smuzhiyun int result;
233*4882a593Smuzhiyun register xReq *request;
234*4882a593Smuzhiyun Bool need_header;
235*4882a593Smuzhiyun Bool move_header;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun NextAvailableInput(oc);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun /* make sure we have an input buffer */
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun if (!oci) {
242*4882a593Smuzhiyun if ((oci = FreeInputs)) {
243*4882a593Smuzhiyun FreeInputs = oci->next;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun else if (!(oci = AllocateInputBuffer())) {
246*4882a593Smuzhiyun YieldControlDeath();
247*4882a593Smuzhiyun return -1;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun oc->input = oci;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun #if XTRANS_SEND_FDS
253*4882a593Smuzhiyun /* Discard any unused file descriptors */
254*4882a593Smuzhiyun while (client->req_fds > 0) {
255*4882a593Smuzhiyun int req_fd = ReadFdFromClient(client);
256*4882a593Smuzhiyun if (req_fd >= 0)
257*4882a593Smuzhiyun close(req_fd);
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun #endif
260*4882a593Smuzhiyun /* advance to start of next request */
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun oci->bufptr += oci->lenLastReq;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun need_header = FALSE;
265*4882a593Smuzhiyun move_header = FALSE;
266*4882a593Smuzhiyun gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun if (oci->ignoreBytes > 0) {
269*4882a593Smuzhiyun if (oci->ignoreBytes > oci->size)
270*4882a593Smuzhiyun needed = oci->size;
271*4882a593Smuzhiyun else
272*4882a593Smuzhiyun needed = oci->ignoreBytes;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun else if (gotnow < sizeof(xReq)) {
275*4882a593Smuzhiyun /* We don't have an entire xReq yet. Can't tell how big
276*4882a593Smuzhiyun * the request will be until we get the whole xReq.
277*4882a593Smuzhiyun */
278*4882a593Smuzhiyun needed = sizeof(xReq);
279*4882a593Smuzhiyun need_header = TRUE;
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun else {
282*4882a593Smuzhiyun /* We have a whole xReq. We can tell how big the whole
283*4882a593Smuzhiyun * request will be unless it is a Big Request.
284*4882a593Smuzhiyun */
285*4882a593Smuzhiyun request = (xReq *) oci->bufptr;
286*4882a593Smuzhiyun needed = get_req_len(request, client);
287*4882a593Smuzhiyun if (!needed && client->big_requests) {
288*4882a593Smuzhiyun /* It's a Big Request. */
289*4882a593Smuzhiyun move_header = TRUE;
290*4882a593Smuzhiyun if (gotnow < sizeof(xBigReq)) {
291*4882a593Smuzhiyun /* Still need more data to tell just how big. */
292*4882a593Smuzhiyun needed = bytes_to_int32(sizeof(xBigReq)); /* needed is in CARD32s now */
293*4882a593Smuzhiyun need_header = TRUE;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun else
296*4882a593Smuzhiyun needed = get_big_req_len(request, client);
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun client->req_len = needed;
299*4882a593Smuzhiyun needed <<= 2; /* needed is in bytes now */
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun if (gotnow < needed) {
302*4882a593Smuzhiyun /* Need to read more data, either so that we can get a
303*4882a593Smuzhiyun * complete xReq (if need_header is TRUE), a complete
304*4882a593Smuzhiyun * xBigReq (if move_header is TRUE), or the rest of the
305*4882a593Smuzhiyun * request (if need_header and move_header are both FALSE).
306*4882a593Smuzhiyun */
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun oci->lenLastReq = 0;
309*4882a593Smuzhiyun if (needed > maxBigRequestSize << 2) {
310*4882a593Smuzhiyun /* request is too big for us to handle */
311*4882a593Smuzhiyun /*
312*4882a593Smuzhiyun * Mark the rest of it as needing to be ignored, and then return
313*4882a593Smuzhiyun * the full size. Dispatch() will turn it into a BadLength error.
314*4882a593Smuzhiyun */
315*4882a593Smuzhiyun oci->ignoreBytes = needed - gotnow;
316*4882a593Smuzhiyun oci->lenLastReq = gotnow;
317*4882a593Smuzhiyun return needed;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun if ((gotnow == 0) || ((oci->bufptr - oci->buffer + needed) > oci->size)) {
320*4882a593Smuzhiyun /* no data, or the request is too big to fit in the buffer */
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun if ((gotnow > 0) && (oci->bufptr != oci->buffer))
323*4882a593Smuzhiyun /* save the data we've already read */
324*4882a593Smuzhiyun memmove(oci->buffer, oci->bufptr, gotnow);
325*4882a593Smuzhiyun if (needed > oci->size) {
326*4882a593Smuzhiyun /* make buffer bigger to accomodate request */
327*4882a593Smuzhiyun char *ibuf;
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun ibuf = (char *) realloc(oci->buffer, needed);
330*4882a593Smuzhiyun if (!ibuf) {
331*4882a593Smuzhiyun YieldControlDeath();
332*4882a593Smuzhiyun return -1;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun oci->size = needed;
335*4882a593Smuzhiyun oci->buffer = ibuf;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun oci->bufptr = oci->buffer;
338*4882a593Smuzhiyun oci->bufcnt = gotnow;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun /* XXX this is a workaround. This function is sometimes called
341*4882a593Smuzhiyun * after the trans_conn has been freed. In this case trans_conn
342*4882a593Smuzhiyun * will be null. Really ought to restructure things so that we
343*4882a593Smuzhiyun * never get here in those circumstances.
344*4882a593Smuzhiyun */
345*4882a593Smuzhiyun if (!oc->trans_conn) {
346*4882a593Smuzhiyun /* treat as if an error occured on the read, which is what
347*4882a593Smuzhiyun * used to happen
348*4882a593Smuzhiyun */
349*4882a593Smuzhiyun YieldControlDeath();
350*4882a593Smuzhiyun return -1;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun result = _XSERVTransRead(oc->trans_conn, oci->buffer + oci->bufcnt,
353*4882a593Smuzhiyun oci->size - oci->bufcnt);
354*4882a593Smuzhiyun if (result <= 0) {
355*4882a593Smuzhiyun if ((result < 0) && ETEST(errno)) {
356*4882a593Smuzhiyun mark_client_not_ready(client);
357*4882a593Smuzhiyun #if defined(SVR4) && defined(__i386__) && !defined(__sun)
358*4882a593Smuzhiyun if (0)
359*4882a593Smuzhiyun #endif
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun YieldControlNoInput(client);
362*4882a593Smuzhiyun return 0;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun YieldControlDeath();
366*4882a593Smuzhiyun return -1;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun oci->bufcnt += result;
369*4882a593Smuzhiyun gotnow += result;
370*4882a593Smuzhiyun /* free up some space after huge requests */
371*4882a593Smuzhiyun if ((oci->size > BUFWATERMARK) &&
372*4882a593Smuzhiyun (oci->bufcnt < BUFSIZE) && (needed < BUFSIZE)) {
373*4882a593Smuzhiyun char *ibuf;
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun ibuf = (char *) realloc(oci->buffer, BUFSIZE);
376*4882a593Smuzhiyun if (ibuf) {
377*4882a593Smuzhiyun oci->size = BUFSIZE;
378*4882a593Smuzhiyun oci->buffer = ibuf;
379*4882a593Smuzhiyun oci->bufptr = ibuf + oci->bufcnt - gotnow;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun if (need_header && gotnow >= needed) {
383*4882a593Smuzhiyun /* We wanted an xReq, now we've gotten it. */
384*4882a593Smuzhiyun request = (xReq *) oci->bufptr;
385*4882a593Smuzhiyun needed = get_req_len(request, client);
386*4882a593Smuzhiyun if (!needed && client->big_requests) {
387*4882a593Smuzhiyun move_header = TRUE;
388*4882a593Smuzhiyun if (gotnow < sizeof(xBigReq))
389*4882a593Smuzhiyun needed = bytes_to_int32(sizeof(xBigReq));
390*4882a593Smuzhiyun else
391*4882a593Smuzhiyun needed = get_big_req_len(request, client);
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun client->req_len = needed;
394*4882a593Smuzhiyun needed <<= 2;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun if (gotnow < needed) {
397*4882a593Smuzhiyun /* Still don't have enough; punt. */
398*4882a593Smuzhiyun YieldControlNoInput(client);
399*4882a593Smuzhiyun return 0;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun if (needed == 0) {
403*4882a593Smuzhiyun if (client->big_requests)
404*4882a593Smuzhiyun needed = sizeof(xBigReq);
405*4882a593Smuzhiyun else
406*4882a593Smuzhiyun needed = sizeof(xReq);
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun /* If there are bytes to ignore, ignore them now. */
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun if (oci->ignoreBytes > 0) {
412*4882a593Smuzhiyun assert(needed == oci->ignoreBytes || needed == oci->size);
413*4882a593Smuzhiyun /*
414*4882a593Smuzhiyun * The _XSERVTransRead call above may return more or fewer bytes than we
415*4882a593Smuzhiyun * want to ignore. Ignore the smaller of the two sizes.
416*4882a593Smuzhiyun */
417*4882a593Smuzhiyun if (gotnow < needed) {
418*4882a593Smuzhiyun oci->ignoreBytes -= gotnow;
419*4882a593Smuzhiyun oci->bufptr += gotnow;
420*4882a593Smuzhiyun gotnow = 0;
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun else {
423*4882a593Smuzhiyun oci->ignoreBytes -= needed;
424*4882a593Smuzhiyun oci->bufptr += needed;
425*4882a593Smuzhiyun gotnow -= needed;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun needed = 0;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun oci->lenLastReq = needed;
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun /*
433*4882a593Smuzhiyun * Check to see if client has at least one whole request in the
434*4882a593Smuzhiyun * buffer beyond the request we're returning to the caller.
435*4882a593Smuzhiyun * If there is only a partial request, treat like buffer
436*4882a593Smuzhiyun * is empty so that select() will be called again and other clients
437*4882a593Smuzhiyun * can get into the queue.
438*4882a593Smuzhiyun */
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun gotnow -= needed;
441*4882a593Smuzhiyun if (!gotnow)
442*4882a593Smuzhiyun AvailableInput = oc;
443*4882a593Smuzhiyun if (move_header) {
444*4882a593Smuzhiyun if (client->req_len < bytes_to_int32(sizeof(xBigReq) - sizeof(xReq))) {
445*4882a593Smuzhiyun YieldControlDeath();
446*4882a593Smuzhiyun return -1;
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun request = (xReq *) oci->bufptr;
450*4882a593Smuzhiyun oci->bufptr += (sizeof(xBigReq) - sizeof(xReq));
451*4882a593Smuzhiyun *(xReq *) oci->bufptr = *request;
452*4882a593Smuzhiyun oci->lenLastReq -= (sizeof(xBigReq) - sizeof(xReq));
453*4882a593Smuzhiyun client->req_len -= bytes_to_int32(sizeof(xBigReq) - sizeof(xReq));
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun client->requestBuffer = (void *) oci->bufptr;
456*4882a593Smuzhiyun #ifdef DEBUG_COMMUNICATION
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun xReq *req = client->requestBuffer;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun ErrorF("REQUEST: ClientIDX: %i, type: 0x%x data: 0x%x len: %i\n",
461*4882a593Smuzhiyun client->index, req->reqType, req->data, req->length);
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun #endif
464*4882a593Smuzhiyun return needed;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun int
ReadFdFromClient(ClientPtr client)468*4882a593Smuzhiyun ReadFdFromClient(ClientPtr client)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun int fd = -1;
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun #if XTRANS_SEND_FDS
473*4882a593Smuzhiyun if (client->req_fds > 0) {
474*4882a593Smuzhiyun OsCommPtr oc = (OsCommPtr) client->osPrivate;
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun --client->req_fds;
477*4882a593Smuzhiyun fd = _XSERVTransRecvFd(oc->trans_conn);
478*4882a593Smuzhiyun } else
479*4882a593Smuzhiyun LogMessage(X_ERROR, "Request asks for FD without setting req_fds\n");
480*4882a593Smuzhiyun #endif
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun return fd;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun int
WriteFdToClient(ClientPtr client,int fd,Bool do_close)486*4882a593Smuzhiyun WriteFdToClient(ClientPtr client, int fd, Bool do_close)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun #if XTRANS_SEND_FDS
489*4882a593Smuzhiyun OsCommPtr oc = (OsCommPtr) client->osPrivate;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun return _XSERVTransSendFd(oc->trans_conn, fd, do_close);
492*4882a593Smuzhiyun #else
493*4882a593Smuzhiyun return -1;
494*4882a593Smuzhiyun #endif
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun /*****************************************************************
498*4882a593Smuzhiyun * InsertFakeRequest
499*4882a593Smuzhiyun * Splice a consed up (possibly partial) request in as the next request.
500*4882a593Smuzhiyun *
501*4882a593Smuzhiyun **********************/
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun Bool
InsertFakeRequest(ClientPtr client,char * data,int count)504*4882a593Smuzhiyun InsertFakeRequest(ClientPtr client, char *data, int count)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun OsCommPtr oc = (OsCommPtr) client->osPrivate;
507*4882a593Smuzhiyun ConnectionInputPtr oci = oc->input;
508*4882a593Smuzhiyun int gotnow, moveup;
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun NextAvailableInput(oc);
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun if (!oci) {
513*4882a593Smuzhiyun if ((oci = FreeInputs))
514*4882a593Smuzhiyun FreeInputs = oci->next;
515*4882a593Smuzhiyun else if (!(oci = AllocateInputBuffer()))
516*4882a593Smuzhiyun return FALSE;
517*4882a593Smuzhiyun oc->input = oci;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun oci->bufptr += oci->lenLastReq;
520*4882a593Smuzhiyun oci->lenLastReq = 0;
521*4882a593Smuzhiyun gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
522*4882a593Smuzhiyun if ((gotnow + count) > oci->size) {
523*4882a593Smuzhiyun char *ibuf;
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun ibuf = (char *) realloc(oci->buffer, gotnow + count);
526*4882a593Smuzhiyun if (!ibuf)
527*4882a593Smuzhiyun return FALSE;
528*4882a593Smuzhiyun oci->size = gotnow + count;
529*4882a593Smuzhiyun oci->buffer = ibuf;
530*4882a593Smuzhiyun oci->bufptr = ibuf + oci->bufcnt - gotnow;
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun moveup = count - (oci->bufptr - oci->buffer);
533*4882a593Smuzhiyun if (moveup > 0) {
534*4882a593Smuzhiyun if (gotnow > 0)
535*4882a593Smuzhiyun memmove(oci->bufptr + moveup, oci->bufptr, gotnow);
536*4882a593Smuzhiyun oci->bufptr += moveup;
537*4882a593Smuzhiyun oci->bufcnt += moveup;
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun memmove(oci->bufptr - count, data, count);
540*4882a593Smuzhiyun oci->bufptr -= count;
541*4882a593Smuzhiyun gotnow += count;
542*4882a593Smuzhiyun if ((gotnow >= sizeof(xReq)) &&
543*4882a593Smuzhiyun (gotnow >= (int) (get_req_len((xReq *) oci->bufptr, client) << 2)))
544*4882a593Smuzhiyun mark_client_ready(client);
545*4882a593Smuzhiyun else
546*4882a593Smuzhiyun YieldControlNoInput(client);
547*4882a593Smuzhiyun return TRUE;
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun /*****************************************************************
551*4882a593Smuzhiyun * ResetRequestFromClient
552*4882a593Smuzhiyun * Reset to reexecute the current request, and yield.
553*4882a593Smuzhiyun *
554*4882a593Smuzhiyun **********************/
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun void
ResetCurrentRequest(ClientPtr client)557*4882a593Smuzhiyun ResetCurrentRequest(ClientPtr client)
558*4882a593Smuzhiyun {
559*4882a593Smuzhiyun OsCommPtr oc = (OsCommPtr) client->osPrivate;
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun /* ignore dying clients */
562*4882a593Smuzhiyun if (!oc)
563*4882a593Smuzhiyun return;
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun register ConnectionInputPtr oci = oc->input;
566*4882a593Smuzhiyun register xReq *request;
567*4882a593Smuzhiyun int gotnow, needed;
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun if (AvailableInput == oc)
570*4882a593Smuzhiyun AvailableInput = (OsCommPtr) NULL;
571*4882a593Smuzhiyun oci->lenLastReq = 0;
572*4882a593Smuzhiyun gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
573*4882a593Smuzhiyun if (gotnow < sizeof(xReq)) {
574*4882a593Smuzhiyun YieldControlNoInput(client);
575*4882a593Smuzhiyun }
576*4882a593Smuzhiyun else {
577*4882a593Smuzhiyun request = (xReq *) oci->bufptr;
578*4882a593Smuzhiyun needed = get_req_len(request, client);
579*4882a593Smuzhiyun if (!needed && client->big_requests) {
580*4882a593Smuzhiyun oci->bufptr -= sizeof(xBigReq) - sizeof(xReq);
581*4882a593Smuzhiyun *(xReq *) oci->bufptr = *request;
582*4882a593Smuzhiyun ((xBigReq *) oci->bufptr)->length = client->req_len;
583*4882a593Smuzhiyun if (client->swapped) {
584*4882a593Smuzhiyun swapl(&((xBigReq *) oci->bufptr)->length);
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun if (gotnow >= (needed << 2)) {
588*4882a593Smuzhiyun if (listen_to_client(client))
589*4882a593Smuzhiyun mark_client_ready(client);
590*4882a593Smuzhiyun YieldControl();
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun else
593*4882a593Smuzhiyun YieldControlNoInput(client);
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun /********************
598*4882a593Smuzhiyun * FlushAllOutput()
599*4882a593Smuzhiyun * Flush all clients with output. However, if some client still
600*4882a593Smuzhiyun * has input in the queue (more requests), then don't flush. This
601*4882a593Smuzhiyun * will prevent the output queue from being flushed every time around
602*4882a593Smuzhiyun * the round robin queue. Now, some say that it SHOULD be flushed
603*4882a593Smuzhiyun * every time around, but...
604*4882a593Smuzhiyun *
605*4882a593Smuzhiyun **********************/
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun void
FlushAllOutput(void)608*4882a593Smuzhiyun FlushAllOutput(void)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun OsCommPtr oc;
611*4882a593Smuzhiyun register ClientPtr client, tmp;
612*4882a593Smuzhiyun Bool newoutput = NewOutputPending;
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun if (!newoutput)
615*4882a593Smuzhiyun return;
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun /*
618*4882a593Smuzhiyun * It may be that some client still has critical output pending,
619*4882a593Smuzhiyun * but he is not yet ready to receive it anyway, so we will
620*4882a593Smuzhiyun * simply wait for the select to tell us when he's ready to receive.
621*4882a593Smuzhiyun */
622*4882a593Smuzhiyun CriticalOutputPending = FALSE;
623*4882a593Smuzhiyun NewOutputPending = FALSE;
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun xorg_list_for_each_entry_safe(client, tmp, &output_pending_clients, output_pending) {
626*4882a593Smuzhiyun if (client->clientGone)
627*4882a593Smuzhiyun continue;
628*4882a593Smuzhiyun if (!client_is_ready(client)) {
629*4882a593Smuzhiyun oc = (OsCommPtr) client->osPrivate;
630*4882a593Smuzhiyun (void) FlushClient(client, oc, (char *) NULL, 0);
631*4882a593Smuzhiyun } else
632*4882a593Smuzhiyun NewOutputPending = TRUE;
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun }
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun void
FlushIfCriticalOutputPending(void)637*4882a593Smuzhiyun FlushIfCriticalOutputPending(void)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun if (CriticalOutputPending)
640*4882a593Smuzhiyun FlushAllOutput();
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun void
SetCriticalOutputPending(void)644*4882a593Smuzhiyun SetCriticalOutputPending(void)
645*4882a593Smuzhiyun {
646*4882a593Smuzhiyun CriticalOutputPending = TRUE;
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun /*****************
650*4882a593Smuzhiyun * AbortClient:
651*4882a593Smuzhiyun * When a write error occurs to a client, close
652*4882a593Smuzhiyun * the connection and clean things up. Mark
653*4882a593Smuzhiyun * the client as 'ready' so that the server will
654*4882a593Smuzhiyun * try to read from it again, notice that the fd is
655*4882a593Smuzhiyun * closed and clean up from there.
656*4882a593Smuzhiyun *****************/
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun static void
AbortClient(ClientPtr client)659*4882a593Smuzhiyun AbortClient(ClientPtr client)
660*4882a593Smuzhiyun {
661*4882a593Smuzhiyun OsCommPtr oc = client->osPrivate;
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun if (oc->trans_conn) {
664*4882a593Smuzhiyun CloseDownFileDescriptor(oc);
665*4882a593Smuzhiyun mark_client_ready(client);
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun /*****************
670*4882a593Smuzhiyun * WriteToClient
671*4882a593Smuzhiyun * Copies buf into ClientPtr.buf if it fits (with padding), else
672*4882a593Smuzhiyun * flushes ClientPtr.buf and buf to client. As of this writing,
673*4882a593Smuzhiyun * every use of WriteToClient is cast to void, and the result
674*4882a593Smuzhiyun * is ignored. Potentially, this could be used by requests
675*4882a593Smuzhiyun * that are sending several chunks of data and want to break
676*4882a593Smuzhiyun * out of a loop on error. Thus, we will leave the type of
677*4882a593Smuzhiyun * this routine as int.
678*4882a593Smuzhiyun *****************/
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun int
WriteToClient(ClientPtr who,int count,const void * __buf)681*4882a593Smuzhiyun WriteToClient(ClientPtr who, int count, const void *__buf)
682*4882a593Smuzhiyun {
683*4882a593Smuzhiyun OsCommPtr oc;
684*4882a593Smuzhiyun ConnectionOutputPtr oco;
685*4882a593Smuzhiyun int padBytes;
686*4882a593Smuzhiyun const char *buf = __buf;
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun BUG_RETURN_VAL_MSG(in_input_thread(), 0,
689*4882a593Smuzhiyun "******** %s called from input thread *********\n", __func__);
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun #ifdef DEBUG_COMMUNICATION
692*4882a593Smuzhiyun Bool multicount = FALSE;
693*4882a593Smuzhiyun #endif
694*4882a593Smuzhiyun if (!count || !who || who == serverClient || who->clientGone)
695*4882a593Smuzhiyun return 0;
696*4882a593Smuzhiyun oc = who->osPrivate;
697*4882a593Smuzhiyun oco = oc->output;
698*4882a593Smuzhiyun #ifdef DEBUG_COMMUNICATION
699*4882a593Smuzhiyun {
700*4882a593Smuzhiyun char info[128];
701*4882a593Smuzhiyun xError *err;
702*4882a593Smuzhiyun xGenericReply *rep;
703*4882a593Smuzhiyun xEvent *ev;
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun if (!who->replyBytesRemaining) {
706*4882a593Smuzhiyun switch (buf[0]) {
707*4882a593Smuzhiyun case X_Reply:
708*4882a593Smuzhiyun rep = (xGenericReply *) buf;
709*4882a593Smuzhiyun if (rep->sequenceNumber == who->sequence) {
710*4882a593Smuzhiyun snprintf(info, 127, "Xreply: type: 0x%x data: 0x%x "
711*4882a593Smuzhiyun "len: %i seq#: 0x%x", rep->type, rep->data1,
712*4882a593Smuzhiyun rep->length, rep->sequenceNumber);
713*4882a593Smuzhiyun multicount = TRUE;
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun break;
716*4882a593Smuzhiyun case X_Error:
717*4882a593Smuzhiyun err = (xError *) buf;
718*4882a593Smuzhiyun snprintf(info, 127, "Xerror: Code: 0x%x resID: 0x%x maj: 0x%x "
719*4882a593Smuzhiyun "min: %x", err->errorCode, err->resourceID,
720*4882a593Smuzhiyun err->minorCode, err->majorCode);
721*4882a593Smuzhiyun break;
722*4882a593Smuzhiyun default:
723*4882a593Smuzhiyun if ((buf[0] & 0x7f) == KeymapNotify)
724*4882a593Smuzhiyun snprintf(info, 127, "KeymapNotifyEvent: %i", buf[0]);
725*4882a593Smuzhiyun else {
726*4882a593Smuzhiyun ev = (xEvent *) buf;
727*4882a593Smuzhiyun snprintf(info, 127, "XEvent: type: 0x%x detail: 0x%x "
728*4882a593Smuzhiyun "seq#: 0x%x", ev->u.u.type, ev->u.u.detail,
729*4882a593Smuzhiyun ev->u.u.sequenceNumber);
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun ErrorF("REPLY: ClientIDX: %i %s\n", who->index, info);
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun else
735*4882a593Smuzhiyun multicount = TRUE;
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun #endif
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun if (!oco) {
740*4882a593Smuzhiyun if ((oco = FreeOutputs)) {
741*4882a593Smuzhiyun FreeOutputs = oco->next;
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun else if (!(oco = AllocateOutputBuffer())) {
744*4882a593Smuzhiyun AbortClient(who);
745*4882a593Smuzhiyun MarkClientException(who);
746*4882a593Smuzhiyun return -1;
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun oc->output = oco;
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun padBytes = padding_for_int32(count);
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun if (ReplyCallback) {
754*4882a593Smuzhiyun ReplyInfoRec replyinfo;
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun replyinfo.client = who;
757*4882a593Smuzhiyun replyinfo.replyData = buf;
758*4882a593Smuzhiyun replyinfo.dataLenBytes = count + padBytes;
759*4882a593Smuzhiyun replyinfo.padBytes = padBytes;
760*4882a593Smuzhiyun if (who->replyBytesRemaining) { /* still sending data of an earlier reply */
761*4882a593Smuzhiyun who->replyBytesRemaining -= count + padBytes;
762*4882a593Smuzhiyun replyinfo.startOfReply = FALSE;
763*4882a593Smuzhiyun replyinfo.bytesRemaining = who->replyBytesRemaining;
764*4882a593Smuzhiyun CallCallbacks((&ReplyCallback), (void *) &replyinfo);
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun else if (who->clientState == ClientStateRunning && buf[0] == X_Reply) { /* start of new reply */
767*4882a593Smuzhiyun CARD32 replylen;
768*4882a593Smuzhiyun unsigned long bytesleft;
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun replylen = ((const xGenericReply *) buf)->length;
771*4882a593Smuzhiyun if (who->swapped)
772*4882a593Smuzhiyun swapl(&replylen);
773*4882a593Smuzhiyun bytesleft = (replylen * 4) + SIZEOF(xReply) - count - padBytes;
774*4882a593Smuzhiyun replyinfo.startOfReply = TRUE;
775*4882a593Smuzhiyun replyinfo.bytesRemaining = who->replyBytesRemaining = bytesleft;
776*4882a593Smuzhiyun CallCallbacks((&ReplyCallback), (void *) &replyinfo);
777*4882a593Smuzhiyun }
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun #ifdef DEBUG_COMMUNICATION
780*4882a593Smuzhiyun else if (multicount) {
781*4882a593Smuzhiyun if (who->replyBytesRemaining) {
782*4882a593Smuzhiyun who->replyBytesRemaining -= (count + padBytes);
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun else {
785*4882a593Smuzhiyun CARD32 replylen;
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun replylen = ((xGenericReply *) buf)->length;
788*4882a593Smuzhiyun who->replyBytesRemaining =
789*4882a593Smuzhiyun (replylen * 4) + SIZEOF(xReply) - count - padBytes;
790*4882a593Smuzhiyun }
791*4882a593Smuzhiyun }
792*4882a593Smuzhiyun #endif
793*4882a593Smuzhiyun if (oco->count == 0 || oco->count + count + padBytes > oco->size) {
794*4882a593Smuzhiyun output_pending_clear(who);
795*4882a593Smuzhiyun if (!any_output_pending()) {
796*4882a593Smuzhiyun CriticalOutputPending = FALSE;
797*4882a593Smuzhiyun NewOutputPending = FALSE;
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun return FlushClient(who, oc, buf, count);
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun NewOutputPending = TRUE;
804*4882a593Smuzhiyun output_pending_mark(who);
805*4882a593Smuzhiyun memmove((char *) oco->buf + oco->count, buf, count);
806*4882a593Smuzhiyun oco->count += count;
807*4882a593Smuzhiyun if (padBytes) {
808*4882a593Smuzhiyun memset(oco->buf + oco->count, '\0', padBytes);
809*4882a593Smuzhiyun oco->count += padBytes;
810*4882a593Smuzhiyun }
811*4882a593Smuzhiyun return count;
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun /********************
815*4882a593Smuzhiyun * FlushClient()
816*4882a593Smuzhiyun * If the client isn't keeping up with us, then we try to continue
817*4882a593Smuzhiyun * buffering the data and set the apropriate bit in ClientsWritable
818*4882a593Smuzhiyun * (which is used by WaitFor in the select). If the connection yields
819*4882a593Smuzhiyun * a permanent error, or we can't allocate any more space, we then
820*4882a593Smuzhiyun * close the connection.
821*4882a593Smuzhiyun *
822*4882a593Smuzhiyun **********************/
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun int
FlushClient(ClientPtr who,OsCommPtr oc,const void * __extraBuf,int extraCount)825*4882a593Smuzhiyun FlushClient(ClientPtr who, OsCommPtr oc, const void *__extraBuf, int extraCount)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun ConnectionOutputPtr oco = oc->output;
828*4882a593Smuzhiyun XtransConnInfo trans_conn = oc->trans_conn;
829*4882a593Smuzhiyun struct iovec iov[3];
830*4882a593Smuzhiyun static char padBuffer[3];
831*4882a593Smuzhiyun const char *extraBuf = __extraBuf;
832*4882a593Smuzhiyun long written;
833*4882a593Smuzhiyun long padsize;
834*4882a593Smuzhiyun long notWritten;
835*4882a593Smuzhiyun long todo;
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun if (!oco)
838*4882a593Smuzhiyun return 0;
839*4882a593Smuzhiyun written = 0;
840*4882a593Smuzhiyun padsize = padding_for_int32(extraCount);
841*4882a593Smuzhiyun notWritten = oco->count + extraCount + padsize;
842*4882a593Smuzhiyun if (!notWritten)
843*4882a593Smuzhiyun return 0;
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun if (FlushCallback)
846*4882a593Smuzhiyun CallCallbacks(&FlushCallback, who);
847*4882a593Smuzhiyun
848*4882a593Smuzhiyun todo = notWritten;
849*4882a593Smuzhiyun while (notWritten) {
850*4882a593Smuzhiyun long before = written; /* amount of whole thing written */
851*4882a593Smuzhiyun long remain = todo; /* amount to try this time, <= notWritten */
852*4882a593Smuzhiyun int i = 0;
853*4882a593Smuzhiyun long len;
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun /* You could be very general here and have "in" and "out" iovecs
856*4882a593Smuzhiyun * and write a loop without using a macro, but what the heck. This
857*4882a593Smuzhiyun * translates to:
858*4882a593Smuzhiyun *
859*4882a593Smuzhiyun * how much of this piece is new?
860*4882a593Smuzhiyun * if more new then we are trying this time, clamp
861*4882a593Smuzhiyun * if nothing new
862*4882a593Smuzhiyun * then bump down amount already written, for next piece
863*4882a593Smuzhiyun * else put new stuff in iovec, will need all of next piece
864*4882a593Smuzhiyun *
865*4882a593Smuzhiyun * Note that todo had better be at least 1 or else we'll end up
866*4882a593Smuzhiyun * writing 0 iovecs.
867*4882a593Smuzhiyun */
868*4882a593Smuzhiyun #define InsertIOV(pointer, length) \
869*4882a593Smuzhiyun len = (length) - before; \
870*4882a593Smuzhiyun if (len > remain) \
871*4882a593Smuzhiyun len = remain; \
872*4882a593Smuzhiyun if (len <= 0) { \
873*4882a593Smuzhiyun before = (-len); \
874*4882a593Smuzhiyun } else { \
875*4882a593Smuzhiyun iov[i].iov_len = len; \
876*4882a593Smuzhiyun iov[i].iov_base = (pointer) + before; \
877*4882a593Smuzhiyun i++; \
878*4882a593Smuzhiyun remain -= len; \
879*4882a593Smuzhiyun before = 0; \
880*4882a593Smuzhiyun }
881*4882a593Smuzhiyun
882*4882a593Smuzhiyun InsertIOV((char *) oco->buf, oco->count)
883*4882a593Smuzhiyun InsertIOV((char *) extraBuf, extraCount)
884*4882a593Smuzhiyun InsertIOV(padBuffer, padsize)
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun errno = 0;
887*4882a593Smuzhiyun if (trans_conn && (len = _XSERVTransWritev(trans_conn, iov, i)) >= 0) {
888*4882a593Smuzhiyun written += len;
889*4882a593Smuzhiyun notWritten -= len;
890*4882a593Smuzhiyun todo = notWritten;
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun else if (ETEST(errno)
893*4882a593Smuzhiyun #ifdef SUNSYSV /* check for another brain-damaged OS bug */
894*4882a593Smuzhiyun || (errno == 0)
895*4882a593Smuzhiyun #endif
896*4882a593Smuzhiyun #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
897*4882a593Smuzhiyun || ((errno == EMSGSIZE) && (todo == 1))
898*4882a593Smuzhiyun #endif
899*4882a593Smuzhiyun ) {
900*4882a593Smuzhiyun /* If we've arrived here, then the client is stuffed to the gills
901*4882a593Smuzhiyun and not ready to accept more. Make a note of it and buffer
902*4882a593Smuzhiyun the rest. */
903*4882a593Smuzhiyun output_pending_mark(who);
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun if (written < oco->count) {
906*4882a593Smuzhiyun if (written > 0) {
907*4882a593Smuzhiyun oco->count -= written;
908*4882a593Smuzhiyun memmove((char *) oco->buf,
909*4882a593Smuzhiyun (char *) oco->buf + written, oco->count);
910*4882a593Smuzhiyun written = 0;
911*4882a593Smuzhiyun }
912*4882a593Smuzhiyun }
913*4882a593Smuzhiyun else {
914*4882a593Smuzhiyun written -= oco->count;
915*4882a593Smuzhiyun oco->count = 0;
916*4882a593Smuzhiyun }
917*4882a593Smuzhiyun
918*4882a593Smuzhiyun if (notWritten > oco->size) {
919*4882a593Smuzhiyun unsigned char *obuf = NULL;
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun if (notWritten + BUFSIZE <= INT_MAX) {
922*4882a593Smuzhiyun obuf = realloc(oco->buf, notWritten + BUFSIZE);
923*4882a593Smuzhiyun }
924*4882a593Smuzhiyun if (!obuf) {
925*4882a593Smuzhiyun AbortClient(who);
926*4882a593Smuzhiyun MarkClientException(who);
927*4882a593Smuzhiyun oco->count = 0;
928*4882a593Smuzhiyun return -1;
929*4882a593Smuzhiyun }
930*4882a593Smuzhiyun oco->size = notWritten + BUFSIZE;
931*4882a593Smuzhiyun oco->buf = obuf;
932*4882a593Smuzhiyun }
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun /* If the amount written extended into the padBuffer, then the
935*4882a593Smuzhiyun difference "extraCount - written" may be less than 0 */
936*4882a593Smuzhiyun if ((len = extraCount - written) > 0)
937*4882a593Smuzhiyun memmove((char *) oco->buf + oco->count,
938*4882a593Smuzhiyun extraBuf + written, len);
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun oco->count = notWritten; /* this will include the pad */
941*4882a593Smuzhiyun ospoll_listen(server_poll, oc->fd, X_NOTIFY_WRITE);
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun /* return only the amount explicitly requested */
944*4882a593Smuzhiyun return extraCount;
945*4882a593Smuzhiyun }
946*4882a593Smuzhiyun #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
947*4882a593Smuzhiyun else if (errno == EMSGSIZE) {
948*4882a593Smuzhiyun todo >>= 1;
949*4882a593Smuzhiyun }
950*4882a593Smuzhiyun #endif
951*4882a593Smuzhiyun else {
952*4882a593Smuzhiyun AbortClient(who);
953*4882a593Smuzhiyun MarkClientException(who);
954*4882a593Smuzhiyun oco->count = 0;
955*4882a593Smuzhiyun return -1;
956*4882a593Smuzhiyun }
957*4882a593Smuzhiyun }
958*4882a593Smuzhiyun
959*4882a593Smuzhiyun /* everything was flushed out */
960*4882a593Smuzhiyun oco->count = 0;
961*4882a593Smuzhiyun output_pending_clear(who);
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun if (oco->size > BUFWATERMARK) {
964*4882a593Smuzhiyun free(oco->buf);
965*4882a593Smuzhiyun free(oco);
966*4882a593Smuzhiyun }
967*4882a593Smuzhiyun else {
968*4882a593Smuzhiyun oco->next = FreeOutputs;
969*4882a593Smuzhiyun FreeOutputs = oco;
970*4882a593Smuzhiyun }
971*4882a593Smuzhiyun oc->output = (ConnectionOutputPtr) NULL;
972*4882a593Smuzhiyun return extraCount; /* return only the amount explicitly requested */
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun static ConnectionInputPtr
AllocateInputBuffer(void)976*4882a593Smuzhiyun AllocateInputBuffer(void)
977*4882a593Smuzhiyun {
978*4882a593Smuzhiyun ConnectionInputPtr oci;
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun oci = malloc(sizeof(ConnectionInput));
981*4882a593Smuzhiyun if (!oci)
982*4882a593Smuzhiyun return NULL;
983*4882a593Smuzhiyun oci->buffer = malloc(BUFSIZE);
984*4882a593Smuzhiyun if (!oci->buffer) {
985*4882a593Smuzhiyun free(oci);
986*4882a593Smuzhiyun return NULL;
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun oci->size = BUFSIZE;
989*4882a593Smuzhiyun oci->bufptr = oci->buffer;
990*4882a593Smuzhiyun oci->bufcnt = 0;
991*4882a593Smuzhiyun oci->lenLastReq = 0;
992*4882a593Smuzhiyun oci->ignoreBytes = 0;
993*4882a593Smuzhiyun return oci;
994*4882a593Smuzhiyun }
995*4882a593Smuzhiyun
996*4882a593Smuzhiyun static ConnectionOutputPtr
AllocateOutputBuffer(void)997*4882a593Smuzhiyun AllocateOutputBuffer(void)
998*4882a593Smuzhiyun {
999*4882a593Smuzhiyun ConnectionOutputPtr oco;
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun oco = malloc(sizeof(ConnectionOutput));
1002*4882a593Smuzhiyun if (!oco)
1003*4882a593Smuzhiyun return NULL;
1004*4882a593Smuzhiyun oco->buf = calloc(1, BUFSIZE);
1005*4882a593Smuzhiyun if (!oco->buf) {
1006*4882a593Smuzhiyun free(oco);
1007*4882a593Smuzhiyun return NULL;
1008*4882a593Smuzhiyun }
1009*4882a593Smuzhiyun oco->size = BUFSIZE;
1010*4882a593Smuzhiyun oco->count = 0;
1011*4882a593Smuzhiyun return oco;
1012*4882a593Smuzhiyun }
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun void
FreeOsBuffers(OsCommPtr oc)1015*4882a593Smuzhiyun FreeOsBuffers(OsCommPtr oc)
1016*4882a593Smuzhiyun {
1017*4882a593Smuzhiyun ConnectionInputPtr oci;
1018*4882a593Smuzhiyun ConnectionOutputPtr oco;
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun if (AvailableInput == oc)
1021*4882a593Smuzhiyun AvailableInput = (OsCommPtr) NULL;
1022*4882a593Smuzhiyun if ((oci = oc->input)) {
1023*4882a593Smuzhiyun if (FreeInputs) {
1024*4882a593Smuzhiyun free(oci->buffer);
1025*4882a593Smuzhiyun free(oci);
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun else {
1028*4882a593Smuzhiyun FreeInputs = oci;
1029*4882a593Smuzhiyun oci->next = (ConnectionInputPtr) NULL;
1030*4882a593Smuzhiyun oci->bufptr = oci->buffer;
1031*4882a593Smuzhiyun oci->bufcnt = 0;
1032*4882a593Smuzhiyun oci->lenLastReq = 0;
1033*4882a593Smuzhiyun oci->ignoreBytes = 0;
1034*4882a593Smuzhiyun }
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun if ((oco = oc->output)) {
1037*4882a593Smuzhiyun if (FreeOutputs) {
1038*4882a593Smuzhiyun free(oco->buf);
1039*4882a593Smuzhiyun free(oco);
1040*4882a593Smuzhiyun }
1041*4882a593Smuzhiyun else {
1042*4882a593Smuzhiyun FreeOutputs = oco;
1043*4882a593Smuzhiyun oco->next = (ConnectionOutputPtr) NULL;
1044*4882a593Smuzhiyun oco->count = 0;
1045*4882a593Smuzhiyun }
1046*4882a593Smuzhiyun }
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun
1049*4882a593Smuzhiyun void
ResetOsBuffers(void)1050*4882a593Smuzhiyun ResetOsBuffers(void)
1051*4882a593Smuzhiyun {
1052*4882a593Smuzhiyun ConnectionInputPtr oci;
1053*4882a593Smuzhiyun ConnectionOutputPtr oco;
1054*4882a593Smuzhiyun
1055*4882a593Smuzhiyun while ((oci = FreeInputs)) {
1056*4882a593Smuzhiyun FreeInputs = oci->next;
1057*4882a593Smuzhiyun free(oci->buffer);
1058*4882a593Smuzhiyun free(oci);
1059*4882a593Smuzhiyun }
1060*4882a593Smuzhiyun while ((oco = FreeOutputs)) {
1061*4882a593Smuzhiyun FreeOutputs = oco->next;
1062*4882a593Smuzhiyun free(oco->buf);
1063*4882a593Smuzhiyun free(oco);
1064*4882a593Smuzhiyun }
1065*4882a593Smuzhiyun }
1066