1 /***********************************************************
3 Copyright 1987, 1989, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
25 Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
29 Permission to use, copy, modify, and distribute this software and its
30 documentation for any purpose and without fee is hereby granted,
31 provided that the above copyright notice appear in all copies and that
32 both that copyright notice and this permission notice appear in
33 supporting documentation, and that the name of Digital not be
34 used in advertising or publicity pertaining to distribution of the
35 software without specific, written prior permission.
37 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 ******************************************************************/
46 /*****************************************************************
49 * WriteToClient, ReadRequestFromClient
50 * InsertFakeRequest, ResetCurrentRequest
52 *****************************************************************/
54 #include <dix-config.h>
56 #undef DEBUG_COMMUNICATION
58 #include "dixstruct_priv.h"
61 #include <X11/Xwinsock.h>
67 #include <X11/Xtrans/Xtrans.h>
74 #include <X11/Xproto.h>
76 #include "dix/dix_priv.h"
81 #include "dixstruct.h"
84 CallbackListPtr ReplyCallback
;
85 CallbackListPtr FlushCallback
;
87 typedef struct _connectionInput
{
88 struct _connectionInput
*next
;
89 char *buffer
; /* contains current client input */
90 char *bufptr
; /* pointer to current start of data */
91 int bufcnt
; /* count of bytes in buffer */
94 unsigned int ignoreBytes
; /* bytes to ignore before the next request */
97 typedef struct _connectionOutput
{
98 struct _connectionOutput
*next
;
104 static ConnectionInputPtr
AllocateInputBuffer(void);
105 static ConnectionOutputPtr
AllocateOutputBuffer(void);
107 static Bool CriticalOutputPending
;
108 static int timesThisConnection
= 0;
109 static ConnectionInputPtr FreeInputs
= (ConnectionInputPtr
) NULL
;
110 static ConnectionOutputPtr FreeOutputs
= (ConnectionOutputPtr
) NULL
;
111 static OsCommPtr AvailableInput
= (OsCommPtr
) NULL
;
113 #define get_req_len(req,cli) ((cli)->swapped ? \
114 bswap_16((req)->length) : (req)->length)
116 #include <X11/extensions/bigreqsproto.h>
118 #define get_big_req_len(req,cli) ((cli)->swapped ? \
119 bswap_32(((xBigReq *)(req))->length) : \
120 ((xBigReq *)(req))->length)
122 #define BUFSIZE 16384
123 #define BUFWATERMARK 32768
126 * A lot of the code in this file manipulates a ConnectionInputPtr:
128 * -----------------------------------------------
129 * |------- bufcnt ------->| | |
130 * | |- gotnow ->| | |
131 * | |-------- needed ------>| |
132 * |-----------+--------- size --------+---------->|
133 * -----------------------------------------------
138 * buffer is a pointer to the start of the buffer.
139 * bufptr points to the start of the current request.
140 * bufcnt counts how many bytes are in the buffer.
141 * size is the size of the buffer in bytes.
143 * In several of the functions, gotnow and needed are local variables
144 * that do the following:
146 * gotnow is the number of bytes of the request that we're
147 * trying to read that are currently in the buffer.
148 * Typically, gotnow = (buffer + bufcnt) - bufptr
150 * needed = the length of the request that we're trying to
151 * read. Watch out: needed sometimes counts bytes and sometimes
155 /*****************************************************************
156 * ReadRequestFromClient
157 * Returns one request in client->requestBuffer. The request
158 * length will be in client->req_len. Return status is:
160 * > 0 if successful, specifies length in bytes of the request
161 * = 0 if entire request is not yet available
162 * < 0 if client should be terminated
164 * The request returned must be contiguous so that it can be
165 * cast in the dispatcher to the correct request type. Because requests
166 * are variable length, ReadRequestFromClient() must look at the first 4
167 * or 8 bytes of a request to determine the length (the request length is
168 * in the 3rd and 4th bytes of the request unless it is a Big Request
169 * (see the Big Request Extension), in which case the 3rd and 4th bytes
170 * are zero and the following 4 bytes are the request length.
172 * Note: in order to make the server scheduler (WaitForSomething())
173 * "fair", the ClientsWithInput mask is used. This mask tells which
174 * clients have FULL requests left in their buffers. Clients with
175 * partial requests require a read. Basically, client buffers
176 * are drained before select() is called again. But, we can't keep
177 * reading from a client that is sending buckets of data (or has
178 * a partial request) because others clients need to be scheduled.
179 *****************************************************************/
184 isItTimeToYield
= TRUE
;
185 timesThisConnection
= 0;
189 YieldControlNoInput(ClientPtr client
)
191 OsCommPtr oc
= client
->osPrivate
;
194 ospoll_reset_events(server_poll
, oc
->fd
);
198 YieldControlDeath(void)
200 timesThisConnection
= 0;
203 /* If an input buffer was empty, either free it if it is too big or link it
204 * into our list of free input buffers. This means that different clients can
205 * share the same input buffer (at different times). This was done to save
209 NextAvailableInput(OsCommPtr oc
)
211 if (AvailableInput
) {
212 if (AvailableInput
!= oc
) {
213 ConnectionInputPtr aci
= AvailableInput
->input
;
215 if (aci
->size
> BUFWATERMARK
) {
220 aci
->next
= FreeInputs
;
223 AvailableInput
->input
= NULL
;
225 AvailableInput
= NULL
;
230 ReadRequestFromClient(ClientPtr client
)
232 OsCommPtr oc
= (OsCommPtr
) client
->osPrivate
;
233 ConnectionInputPtr oci
= oc
->input
;
234 unsigned int gotnow
, needed
;
236 register xReq
*request
;
240 NextAvailableInput(oc
);
242 /* make sure we have an input buffer */
245 if ((oci
= FreeInputs
)) {
246 FreeInputs
= oci
->next
;
248 else if (!(oci
= AllocateInputBuffer())) {
256 /* Discard any unused file descriptors */
257 while (client
->req_fds
> 0) {
258 int req_fd
= ReadFdFromClient(client
);
263 /* advance to start of next request */
265 oci
->bufptr
+= oci
->lenLastReq
;
269 gotnow
= oci
->bufcnt
+ oci
->buffer
- oci
->bufptr
;
271 if (oci
->ignoreBytes
> 0) {
272 if (oci
->ignoreBytes
> oci
->size
)
275 needed
= oci
->ignoreBytes
;
277 else if (gotnow
< sizeof(xReq
)) {
278 /* We don't have an entire xReq yet. Can't tell how big
279 * the request will be until we get the whole xReq.
281 needed
= sizeof(xReq
);
285 /* We have a whole xReq. We can tell how big the whole
286 * request will be unless it is a Big Request.
288 request
= (xReq
*) oci
->bufptr
;
289 needed
= get_req_len(request
, client
);
290 if (!needed
&& client
->big_requests
) {
291 /* It's a Big Request. */
293 if (gotnow
< sizeof(xBigReq
)) {
294 /* Still need more data to tell just how big. */
295 needed
= bytes_to_int32(sizeof(xBigReq
)); /* needed is in CARD32s now */
299 needed
= get_big_req_len(request
, client
);
301 client
->req_len
= needed
;
302 needed
<<= 2; /* needed is in bytes now */
304 if (gotnow
< needed
) {
305 /* Need to read more data, either so that we can get a
306 * complete xReq (if need_header is TRUE), a complete
307 * xBigReq (if move_header is TRUE), or the rest of the
308 * request (if need_header and move_header are both FALSE).
312 if (needed
> maxBigRequestSize
<< 2) {
313 /* request is too big for us to handle */
315 * Mark the rest of it as needing to be ignored, and then return
316 * the full size. Dispatch() will turn it into a BadLength error.
318 oci
->ignoreBytes
= needed
- gotnow
;
319 oci
->lenLastReq
= gotnow
;
322 if ((gotnow
== 0) || ((oci
->bufptr
- oci
->buffer
+ needed
) > oci
->size
)) {
323 /* no data, or the request is too big to fit in the buffer */
325 if ((gotnow
> 0) && (oci
->bufptr
!= oci
->buffer
))
326 /* save the data we've already read */
327 memmove(oci
->buffer
, oci
->bufptr
, gotnow
);
328 if (needed
> oci
->size
) {
329 /* make buffer bigger to accommodate request */
332 ibuf
= (char *) realloc(oci
->buffer
, needed
);
340 oci
->bufptr
= oci
->buffer
;
341 oci
->bufcnt
= gotnow
;
343 /* XXX this is a workaround. This function is sometimes called
344 * after the trans_conn has been freed. In this case trans_conn
345 * will be null. Really ought to restructure things so that we
346 * never get here in those circumstances.
348 if (!oc
->trans_conn
) {
349 /* treat as if an error occurred on the read, which is what
355 result
= _XSERVTransRead(oc
->trans_conn
, oci
->buffer
+ oci
->bufcnt
,
356 oci
->size
- oci
->bufcnt
);
358 if ((result
< 0) && ETEST(errno
)) {
359 mark_client_not_ready(client
);
360 YieldControlNoInput(client
);
366 oci
->bufcnt
+= result
;
368 /* free up some space after huge requests */
369 if ((oci
->size
> BUFWATERMARK
) &&
370 (oci
->bufcnt
< BUFSIZE
) && (needed
< BUFSIZE
)) {
373 ibuf
= (char *) realloc(oci
->buffer
, BUFSIZE
);
377 oci
->bufptr
= ibuf
+ oci
->bufcnt
- gotnow
;
380 if (need_header
&& gotnow
>= needed
) {
381 /* We wanted an xReq, now we've gotten it. */
382 request
= (xReq
*) oci
->bufptr
;
383 needed
= get_req_len(request
, client
);
384 if (!needed
&& client
->big_requests
) {
386 if (gotnow
< sizeof(xBigReq
))
387 needed
= bytes_to_int32(sizeof(xBigReq
));
389 needed
= get_big_req_len(request
, client
);
391 client
->req_len
= needed
;
394 if (gotnow
< needed
) {
395 /* Still don't have enough; punt. */
396 YieldControlNoInput(client
);
401 if (client
->big_requests
)
402 needed
= sizeof(xBigReq
);
404 needed
= sizeof(xReq
);
407 /* If there are bytes to ignore, ignore them now. */
409 if (oci
->ignoreBytes
> 0) {
410 assert(needed
== oci
->ignoreBytes
|| needed
== oci
->size
);
412 * The _XSERVTransRead call above may return more or fewer bytes than we
413 * want to ignore. Ignore the smaller of the two sizes.
415 if (gotnow
< needed
) {
416 oci
->ignoreBytes
-= gotnow
;
417 oci
->bufptr
+= gotnow
;
421 oci
->ignoreBytes
-= needed
;
422 oci
->bufptr
+= needed
;
428 oci
->lenLastReq
= needed
;
431 * Check to see if client has at least one whole request in the
432 * buffer beyond the request we're returning to the caller.
433 * If there is only a partial request, treat like buffer
434 * is empty so that select() will be called again and other clients
435 * can get into the queue.
442 if (client
->req_len
< bytes_to_int32(sizeof(xBigReq
) - sizeof(xReq
))) {
447 request
= (xReq
*) oci
->bufptr
;
448 oci
->bufptr
+= (sizeof(xBigReq
) - sizeof(xReq
));
449 *(xReq
*) oci
->bufptr
= *request
;
450 oci
->lenLastReq
-= (sizeof(xBigReq
) - sizeof(xReq
));
451 client
->req_len
-= bytes_to_int32(sizeof(xBigReq
) - sizeof(xReq
));
453 client
->requestBuffer
= (void *) oci
->bufptr
;
454 #ifdef DEBUG_COMMUNICATION
456 xReq
*req
= client
->requestBuffer
;
458 ErrorF("REQUEST: ClientIDX: %i, type: 0x%x data: 0x%x len: %i\n",
459 client
->index
, req
->reqType
, req
->data
, req
->length
);
466 ReadFdFromClient(ClientPtr client
)
471 if (client
->req_fds
> 0) {
472 OsCommPtr oc
= (OsCommPtr
) client
->osPrivate
;
475 fd
= _XSERVTransRecvFd(oc
->trans_conn
);
477 LogMessage(X_ERROR
, "Request asks for FD without setting req_fds\n");
484 WriteFdToClient(ClientPtr client
, int fd
, Bool do_close
)
487 OsCommPtr oc
= (OsCommPtr
) client
->osPrivate
;
489 return _XSERVTransSendFd(oc
->trans_conn
, fd
, do_close
);
495 /*****************************************************************
497 * Splice a consed up (possibly partial) request in as the next request.
499 **********************/
502 InsertFakeRequest(ClientPtr client
, char *data
, int count
)
504 OsCommPtr oc
= (OsCommPtr
) client
->osPrivate
;
505 ConnectionInputPtr oci
= oc
->input
;
508 NextAvailableInput(oc
);
511 if ((oci
= FreeInputs
))
512 FreeInputs
= oci
->next
;
513 else if (!(oci
= AllocateInputBuffer()))
517 oci
->bufptr
+= oci
->lenLastReq
;
519 gotnow
= oci
->bufcnt
+ oci
->buffer
- oci
->bufptr
;
520 if ((gotnow
+ count
) > oci
->size
) {
523 ibuf
= (char *) realloc(oci
->buffer
, gotnow
+ count
);
526 oci
->size
= gotnow
+ count
;
528 oci
->bufptr
= ibuf
+ oci
->bufcnt
- gotnow
;
530 moveup
= count
- (oci
->bufptr
- oci
->buffer
);
533 memmove(oci
->bufptr
+ moveup
, oci
->bufptr
, gotnow
);
534 oci
->bufptr
+= moveup
;
535 oci
->bufcnt
+= moveup
;
537 memmove(oci
->bufptr
- count
, data
, count
);
538 oci
->bufptr
-= count
;
540 if ((gotnow
>= sizeof(xReq
)) &&
541 (gotnow
>= (int) (get_req_len((xReq
*) oci
->bufptr
, client
) << 2)))
542 mark_client_ready(client
);
544 YieldControlNoInput(client
);
548 /*****************************************************************
549 * ResetRequestFromClient
550 * Reset to reexecute the current request, and yield.
552 **********************/
555 ResetCurrentRequest(ClientPtr client
)
557 OsCommPtr oc
= (OsCommPtr
) client
->osPrivate
;
559 /* ignore dying clients */
563 register ConnectionInputPtr oci
= oc
->input
;
564 register xReq
*request
;
567 if (AvailableInput
== oc
)
568 AvailableInput
= (OsCommPtr
) NULL
;
570 gotnow
= oci
->bufcnt
+ oci
->buffer
- oci
->bufptr
;
571 if (gotnow
< sizeof(xReq
)) {
572 YieldControlNoInput(client
);
575 request
= (xReq
*) oci
->bufptr
;
576 needed
= get_req_len(request
, client
);
577 if (!needed
&& client
->big_requests
) {
578 oci
->bufptr
-= sizeof(xBigReq
) - sizeof(xReq
);
579 *(xReq
*) oci
->bufptr
= *request
;
580 ((xBigReq
*) oci
->bufptr
)->length
= client
->req_len
;
581 if (client
->swapped
) {
582 swapl(&((xBigReq
*) oci
->bufptr
)->length
);
585 if (gotnow
>= (needed
<< 2)) {
586 if (listen_to_client(client
))
587 mark_client_ready(client
);
591 YieldControlNoInput(client
);
595 /********************
597 * Flush all clients with output. However, if some client still
598 * has input in the queue (more requests), then don't flush. This
599 * will prevent the output queue from being flushed every time around
600 * the round robin queue. Now, some say that it SHOULD be flushed
601 * every time around, but...
603 **********************/
609 register ClientPtr client
, tmp
;
610 Bool newoutput
= NewOutputPending
;
616 * It may be that some client still has critical output pending,
617 * but he is not yet ready to receive it anyway, so we will
618 * simply wait for the select to tell us when he's ready to receive.
620 CriticalOutputPending
= FALSE
;
621 NewOutputPending
= FALSE
;
623 xorg_list_for_each_entry_safe(client
, tmp
, &output_pending_clients
, output_pending
) {
624 if (client
->clientGone
)
626 if (!client_is_ready(client
)) {
627 oc
= (OsCommPtr
) client
->osPrivate
;
628 (void) FlushClient(client
, oc
, (char *) NULL
, 0);
630 NewOutputPending
= TRUE
;
635 FlushIfCriticalOutputPending(void)
637 if (CriticalOutputPending
)
642 SetCriticalOutputPending(void)
644 CriticalOutputPending
= TRUE
;
649 * When a write error occurs to a client, close
650 * the connection and clean things up. Mark
651 * the client as 'ready' so that the server will
652 * try to read from it again, notice that the fd is
653 * closed and clean up from there.
657 AbortClient(ClientPtr client
)
659 OsCommPtr oc
= client
->osPrivate
;
661 if (oc
->trans_conn
) {
662 CloseDownFileDescriptor(oc
);
663 mark_client_ready(client
);
669 * Copies buf into ClientPtr.buf if it fits (with padding), else
670 * flushes ClientPtr.buf and buf to client. As of this writing,
671 * every use of WriteToClient is cast to void, and the result
672 * is ignored. Potentially, this could be used by requests
673 * that are sending several chunks of data and want to break
674 * out of a loop on error. Thus, we will leave the type of
675 * this routine as int.
679 WriteToClient(ClientPtr who
, int count
, const void *__buf
)
682 ConnectionOutputPtr oco
;
684 const char *buf
= __buf
;
686 BUG_RETURN_VAL_MSG(in_input_thread(), 0,
687 "******** %s called from input thread *********\n", __func__
);
689 #ifdef DEBUG_COMMUNICATION
690 Bool multicount
= FALSE
;
692 if (!count
|| !who
|| who
== serverClient
|| who
->clientGone
)
696 #ifdef DEBUG_COMMUNICATION
703 if (!who
->replyBytesRemaining
) {
706 rep
= (xGenericReply
*) buf
;
707 if (rep
->sequenceNumber
== who
->sequence
) {
708 snprintf(info
, 127, "Xreply: type: 0x%x data: 0x%x "
709 "len: %i seq#: 0x%x", rep
->type
, rep
->data1
,
710 rep
->length
, rep
->sequenceNumber
);
715 err
= (xError
*) buf
;
716 snprintf(info
, 127, "Xerror: Code: 0x%x resID: 0x%x maj: 0x%x "
717 "min: %x", err
->errorCode
, err
->resourceID
,
718 err
->minorCode
, err
->majorCode
);
721 if ((buf
[0] & 0x7f) == KeymapNotify
)
722 snprintf(info
, 127, "KeymapNotifyEvent: %i", buf
[0]);
725 snprintf(info
, 127, "XEvent: type: 0x%x detail: 0x%x "
726 "seq#: 0x%x", ev
->u
.u
.type
, ev
->u
.u
.detail
,
727 ev
->u
.u
.sequenceNumber
);
730 ErrorF("REPLY: ClientIDX: %i %s\n", who
->index
, info
);
738 if ((oco
= FreeOutputs
)) {
739 FreeOutputs
= oco
->next
;
741 else if (!(oco
= AllocateOutputBuffer())) {
743 MarkClientException(who
);
749 padBytes
= padding_for_int32(count
);
752 ReplyInfoRec replyinfo
;
754 replyinfo
.client
= who
;
755 replyinfo
.replyData
= buf
;
756 replyinfo
.dataLenBytes
= count
+ padBytes
;
757 replyinfo
.padBytes
= padBytes
;
758 if (who
->replyBytesRemaining
) { /* still sending data of an earlier reply */
759 who
->replyBytesRemaining
-= count
+ padBytes
;
760 replyinfo
.startOfReply
= FALSE
;
761 replyinfo
.bytesRemaining
= who
->replyBytesRemaining
;
762 CallCallbacks((&ReplyCallback
), (void *) &replyinfo
);
764 else if (who
->clientState
== ClientStateRunning
&& buf
[0] == X_Reply
) { /* start of new reply */
766 unsigned long bytesleft
;
768 replylen
= ((const xGenericReply
*) buf
)->length
;
771 bytesleft
= (replylen
* 4) + SIZEOF(xReply
) - count
- padBytes
;
772 replyinfo
.startOfReply
= TRUE
;
773 replyinfo
.bytesRemaining
= who
->replyBytesRemaining
= bytesleft
;
774 CallCallbacks((&ReplyCallback
), (void *) &replyinfo
);
777 #ifdef DEBUG_COMMUNICATION
778 else if (multicount
) {
779 if (who
->replyBytesRemaining
) {
780 who
->replyBytesRemaining
-= (count
+ padBytes
);
785 replylen
= ((xGenericReply
*) buf
)->length
;
786 who
->replyBytesRemaining
=
787 (replylen
* 4) + SIZEOF(xReply
) - count
- padBytes
;
791 if ((oco
->count
== 0 && who
->local
) || oco
->count
+ count
+ padBytes
> oco
->size
) {
792 output_pending_clear(who
);
793 if (!any_output_pending()) {
794 CriticalOutputPending
= FALSE
;
795 NewOutputPending
= FALSE
;
798 return FlushClient(who
, oc
, buf
, count
);
801 NewOutputPending
= TRUE
;
802 output_pending_mark(who
);
803 memmove((char *) oco
->buf
+ oco
->count
, buf
, count
);
806 memset(oco
->buf
+ oco
->count
, '\0', padBytes
);
807 oco
->count
+= padBytes
;
812 /********************
814 * If the client isn't keeping up with us, then we try to continue
815 * buffering the data and set the appropriate bit in ClientsWritable
816 * (which is used by WaitFor in the select). If the connection yields
817 * a permanent error, or we can't allocate any more space, we then
818 * close the connection.
820 **********************/
823 FlushClient(ClientPtr who
, OsCommPtr oc
, const void *__extraBuf
, int extraCount
)
825 ConnectionOutputPtr oco
= oc
->output
;
826 XtransConnInfo trans_conn
= oc
->trans_conn
;
828 static char padBuffer
[3];
829 const char *extraBuf
= __extraBuf
;
838 padsize
= padding_for_int32(extraCount
);
839 notWritten
= oco
->count
+ extraCount
+ padsize
;
844 CallCallbacks(&FlushCallback
, who
);
848 long before
= written
; /* amount of whole thing written */
849 long remain
= todo
; /* amount to try this time, <= notWritten */
853 /* You could be very general here and have "in" and "out" iovecs
854 * and write a loop without using a macro, but what the heck. This
857 * how much of this piece is new?
858 * if more new then we are trying this time, clamp
860 * then bump down amount already written, for next piece
861 * else put new stuff in iovec, will need all of next piece
863 * Note that todo had better be at least 1 or else we'll end up
866 #define InsertIOV(pointer, length) \
867 len = (length) - before; \
873 iov[i].iov_len = len; \
874 iov[i].iov_base = (pointer) + before; \
880 InsertIOV((char *) oco
->buf
, oco
->count
)
881 InsertIOV((char *) extraBuf
, extraCount
)
882 InsertIOV(padBuffer
, padsize
)
885 if (trans_conn
&& (len
= _XSERVTransWritev(trans_conn
, iov
, i
)) >= 0) {
890 else if (ETEST(errno
)
891 #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
892 || ((errno
== EMSGSIZE
) && (todo
== 1))
895 /* If we've arrived here, then the client is stuffed to the gills
896 and not ready to accept more. Make a note of it and buffer
898 output_pending_mark(who
);
900 if (written
< oco
->count
) {
902 oco
->count
-= written
;
903 memmove((char *) oco
->buf
,
904 (char *) oco
->buf
+ written
, oco
->count
);
909 written
-= oco
->count
;
913 if (notWritten
> oco
->size
) {
914 unsigned char *obuf
= NULL
;
916 if (notWritten
+ BUFSIZE
<= INT_MAX
) {
917 obuf
= realloc(oco
->buf
, notWritten
+ BUFSIZE
);
921 MarkClientException(who
);
925 oco
->size
= notWritten
+ BUFSIZE
;
929 /* If the amount written extended into the padBuffer, then the
930 difference "extraCount - written" may be less than 0 */
931 if ((len
= extraCount
- written
) > 0)
932 memmove((char *) oco
->buf
+ oco
->count
,
933 extraBuf
+ written
, len
);
935 oco
->count
= notWritten
; /* this will include the pad */
936 ospoll_listen(server_poll
, oc
->fd
, X_NOTIFY_WRITE
);
938 /* return only the amount explicitly requested */
941 #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
942 else if (errno
== EMSGSIZE
) {
948 MarkClientException(who
);
954 /* everything was flushed out */
956 output_pending_clear(who
);
958 if (oco
->size
> BUFWATERMARK
) {
963 oco
->next
= FreeOutputs
;
966 oc
->output
= (ConnectionOutputPtr
) NULL
;
967 return extraCount
; /* return only the amount explicitly requested */
970 static ConnectionInputPtr
971 AllocateInputBuffer(void)
973 ConnectionInputPtr oci
;
975 oci
= malloc(sizeof(ConnectionInput
));
978 oci
->buffer
= malloc(BUFSIZE
);
984 oci
->bufptr
= oci
->buffer
;
987 oci
->ignoreBytes
= 0;
991 static ConnectionOutputPtr
992 AllocateOutputBuffer(void)
994 ConnectionOutputPtr oco
;
996 oco
= malloc(sizeof(ConnectionOutput
));
999 oco
->buf
= calloc(1, BUFSIZE
);
1004 oco
->size
= BUFSIZE
;
1010 FreeOsBuffers(OsCommPtr oc
)
1012 ConnectionInputPtr oci
;
1013 ConnectionOutputPtr oco
;
1015 if (AvailableInput
== oc
)
1016 AvailableInput
= (OsCommPtr
) NULL
;
1017 if ((oci
= oc
->input
)) {
1024 oci
->next
= (ConnectionInputPtr
) NULL
;
1025 oci
->bufptr
= oci
->buffer
;
1027 oci
->lenLastReq
= 0;
1028 oci
->ignoreBytes
= 0;
1031 if ((oco
= oc
->output
)) {
1038 oco
->next
= (ConnectionOutputPtr
) NULL
;
1045 ResetOsBuffers(void)
1047 ConnectionInputPtr oci
;
1048 ConnectionOutputPtr oco
;
1050 while ((oci
= FreeInputs
)) {
1051 FreeInputs
= oci
->next
;
1055 while ((oco
= FreeOutputs
)) {
1056 FreeOutputs
= oco
->next
;