io.c 27 KB

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