PageRenderTime 54ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/nx-3.5.0/nx-X11/programs/Xserver/os/lbxio.c

#
C | 555 lines | 456 code | 39 blank | 60 comment | 97 complexity | 0d4c9706615aeaa470aa07431ed1feff MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0
  1. /* $XFree86: xc/programs/Xserver/os/lbxio.c,v 3.17 2002/05/31 18:46:06 dawes Exp $ */
  2. /*
  3. Copyright 1996, 1998 The Open Group
  4. Permission to use, copy, modify, distribute, and sell this software and its
  5. documentation for any purpose is hereby granted without fee, provided that
  6. the above copyright notice appear in all copies and that both that
  7. copyright notice and this permission notice appear in supporting
  8. documentation.
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  15. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall not be
  18. used in advertising or otherwise to promote the sale, use or other dealings
  19. in this Software without prior written authorization from The Open Group.
  20. */
  21. /***********************************************************
  22. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  23. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  24. All Rights Reserved
  25. Permission to use, copy, modify, and distribute this software and its
  26. documentation for any purpose and without fee is hereby granted,
  27. provided that the above copyright notice appear in all copies and that
  28. both that copyright notice and this permission notice appear in
  29. supporting documentation, and that the names of Digital or MIT not be
  30. used in advertising or publicity pertaining to distribution of the
  31. software without specific, written prior permission.
  32. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  33. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  34. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  35. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  36. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  37. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  38. SOFTWARE.
  39. ******************************************************************/
  40. /* $Xorg: lbxio.c,v 1.4 2001/02/09 02:05:23 xorgcvs Exp $ */
  41. #ifdef HAVE_DIX_CONFIG_H
  42. #include <dix-config.h>
  43. #endif
  44. #include <stdio.h>
  45. #include <X11/Xtrans/Xtrans.h>
  46. #include <X11/Xmd.h>
  47. #include <errno.h>
  48. #ifndef Lynx
  49. #include <sys/param.h>
  50. #ifndef __UNIXOS2__
  51. #include <sys/uio.h>
  52. #endif
  53. #else
  54. #include <uio.h>
  55. #endif
  56. #include <X11/X.h>
  57. #include <X11/Xproto.h>
  58. #include "os.h"
  59. #include <X11/Xpoll.h>
  60. #include "osdep.h"
  61. #include "opaque.h"
  62. #include "dixstruct.h"
  63. #include "misc.h"
  64. #include "colormapst.h"
  65. #include "propertyst.h"
  66. #include "lbxserve.h"
  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. #define get_req_len(req,cli) ((cli)->swapped ? \
  80. lswaps((req)->length) : (req)->length)
  81. #define YieldControl() \
  82. { isItTimeToYield = TRUE; \
  83. timesThisConnection = 0; }
  84. #define YieldControlNoInput() \
  85. { YieldControl(); \
  86. FD_CLR(fd, &ClientsWithInput); }
  87. void
  88. SwitchClientInput (ClientPtr client, Bool pending)
  89. {
  90. OsCommPtr oc = (OsCommPtr)client->osPrivate;
  91. #ifndef WIN32
  92. ConnectionTranslation[oc->fd] = client->index;
  93. #else
  94. SetConnectionTranslation(oc->fd, client->index);
  95. #endif
  96. if (pending)
  97. FD_SET(oc->fd, &ClientsWithInput);
  98. else
  99. YieldControl();
  100. }
  101. void
  102. LbxPrimeInput(ClientPtr client, LbxProxyPtr proxy)
  103. {
  104. OsCommPtr oc = (OsCommPtr)client->osPrivate;
  105. ConnectionInputPtr oci = oc->input;
  106. if (oci && proxy->compHandle) {
  107. char *extra = oci->bufptr + oci->lenLastReq;
  108. int left = oci->bufcnt + oci->buffer - extra;
  109. (*proxy->streamOpts.streamCompStuffInput)(oc->fd,
  110. (unsigned char *)extra,
  111. left);
  112. oci->bufcnt -= left;
  113. AvailableInput = oc;
  114. }
  115. }
  116. void
  117. AvailableClientInput (ClientPtr client)
  118. {
  119. OsCommPtr oc = (OsCommPtr)client->osPrivate;
  120. if (FD_ISSET(oc->fd, &AllSockets))
  121. FD_SET(oc->fd, &ClientsWithInput);
  122. }
  123. /*****************************************************************
  124. * AppendFakeRequest
  125. * Append a (possibly partial) request in as the last request.
  126. *
  127. **********************/
  128. Bool
  129. AppendFakeRequest (ClientPtr client, char *data, int count)
  130. {
  131. OsCommPtr oc = (OsCommPtr)client->osPrivate;
  132. ConnectionInputPtr oci = oc->input;
  133. int fd = oc->fd;
  134. int gotnow;
  135. if (!oci)
  136. {
  137. if ((oci = FreeInputs))
  138. FreeInputs = oci->next;
  139. else if (!(oci = AllocateInputBuffer()))
  140. return FALSE;
  141. oc->input = oci;
  142. } else if (AvailableInput == oc)
  143. AvailableInput = (OsCommPtr)NULL;
  144. /* do not free AvailableInput here, it could be proxy's */
  145. oci->bufptr += oci->lenLastReq;
  146. oci->lenLastReq = 0;
  147. gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  148. if ((gotnow + count) > oci->size)
  149. {
  150. char *ibuf;
  151. ibuf = (char *)xrealloc(oci->buffer, gotnow + count);
  152. if (!ibuf)
  153. return(FALSE);
  154. oci->size = gotnow + count;
  155. oci->buffer = ibuf;
  156. oci->bufptr = ibuf + oci->bufcnt - gotnow;
  157. }
  158. if (oci->bufcnt + count > oci->size) {
  159. memmove(oci->buffer, oci->bufptr, gotnow);
  160. oci->bufcnt = gotnow;
  161. oci->bufptr = oci->buffer;
  162. }
  163. memmove(oci->bufptr + gotnow, data, count);
  164. oci->bufcnt += count;
  165. gotnow += count;
  166. if ((gotnow >= sizeof(xReq)) &&
  167. (gotnow >= (int)(get_req_len((xReq *)oci->bufptr, client) << 2)))
  168. FD_SET(fd, &ClientsWithInput);
  169. else
  170. YieldControlNoInput();
  171. return(TRUE);
  172. }
  173. static int
  174. LbxWrite(XtransConnInfo trans_conn, LbxProxyPtr proxy,
  175. char *buf, int len)
  176. {
  177. struct iovec iov;
  178. int n;
  179. int notWritten;
  180. notWritten = len;
  181. iov.iov_base = buf;
  182. iov.iov_len = len;
  183. while (notWritten) {
  184. errno = 0;
  185. if (proxy->compHandle)
  186. n = (*proxy->streamOpts.streamCompWriteV)(proxy->fd, &iov, 1);
  187. else
  188. n = _XSERVTransWritev(trans_conn, &iov, 1);
  189. if (n >= 0) {
  190. iov.iov_base = (char *)iov.iov_base + n;
  191. notWritten -= n;
  192. iov.iov_len = notWritten;
  193. }
  194. else if (ETEST(errno)
  195. #ifdef SUNSYSV /* check for another brain-damaged OS bug */
  196. || (errno == 0)
  197. #endif
  198. #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
  199. || ((errno == EMSGSIZE) && (iov.iov_len == 1))
  200. #endif
  201. )
  202. break;
  203. #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
  204. else if (errno == EMSGSIZE)
  205. iov.iov_len >>= 1;
  206. #endif
  207. else
  208. return -1;
  209. }
  210. return len - notWritten;
  211. }
  212. static Bool
  213. LbxAppendOutput(LbxProxyPtr proxy, ClientPtr client, ConnectionOutputPtr oco)
  214. {
  215. ConnectionOutputPtr noco = proxy->olast;
  216. LbxClientPtr lbxClient = LbxClient(client);
  217. if (!lbxClient) {
  218. xfree(oco->buf);
  219. xfree(oco);
  220. return TRUE;
  221. }
  222. if (noco)
  223. LbxReencodeOutput(client,
  224. (char *)noco->buf, &noco->count,
  225. (char *)oco->buf, &oco->count);
  226. else
  227. LbxReencodeOutput(client,
  228. (char *)NULL, (int *)NULL,
  229. (char *)oco->buf, &oco->count);
  230. if (!oco->count) {
  231. if (oco->size > BUFWATERMARK)
  232. {
  233. xfree(oco->buf);
  234. xfree(oco);
  235. }
  236. else
  237. {
  238. oco->next = FreeOutputs;
  239. FreeOutputs = oco;
  240. }
  241. return TRUE;
  242. }
  243. if ((lbxClient->id != proxy->cur_send_id) && proxy->lbxClients[0]) {
  244. xLbxSwitchEvent *ev;
  245. int n;
  246. if (!noco || (noco->size - noco->count) < sz_xLbxSwitchEvent) {
  247. if ((noco = FreeOutputs))
  248. FreeOutputs = noco->next;
  249. else
  250. noco = AllocateOutputBuffer();
  251. if (!noco) {
  252. MarkClientException(client);
  253. return FALSE;
  254. }
  255. noco->next = NULL;
  256. if (proxy->olast)
  257. proxy->olast->next = noco;
  258. else
  259. proxy->ofirst = noco;
  260. proxy->olast = noco;
  261. }
  262. ev = (xLbxSwitchEvent *) (noco->buf + noco->count);
  263. noco->count += sz_xLbxSwitchEvent;
  264. proxy->cur_send_id = lbxClient->id;
  265. ev->type = LbxEventCode;
  266. ev->lbxType = LbxSwitchEvent;
  267. ev->pad = 0;
  268. ev->client = proxy->cur_send_id;
  269. if (LbxProxyClient(proxy)->swapped) {
  270. swapl(&ev->client, n);
  271. }
  272. }
  273. oco->next = NULL;
  274. if (proxy->olast)
  275. proxy->olast->next = oco;
  276. else
  277. proxy->ofirst = oco;
  278. proxy->olast = oco;
  279. return TRUE;
  280. }
  281. static int
  282. LbxClientOutput(ClientPtr client, OsCommPtr oc,
  283. char *extraBuf, int extraCount, Bool nocompress)
  284. {
  285. ConnectionOutputPtr oco;
  286. int len;
  287. if ((oco = oc->output)) {
  288. oc->output = NULL;
  289. if (!LbxAppendOutput(oc->proxy, client, oco))
  290. return -1;
  291. }
  292. if (extraCount) {
  293. NewOutputPending = TRUE;
  294. FD_SET(oc->fd, &OutputPending);
  295. len = (extraCount + 3) & ~3;
  296. if ((oco = FreeOutputs) && (oco->size >= len))
  297. FreeOutputs = oco->next;
  298. else {
  299. oco = (ConnectionOutputPtr)xalloc(sizeof(ConnectionOutput));
  300. if (!oco) {
  301. MarkClientException(client);
  302. return -1;
  303. }
  304. oco->size = len;
  305. if (oco->size < BUFSIZE)
  306. oco->size = BUFSIZE;
  307. oco->buf = (unsigned char *) xalloc(oco->size);
  308. if (!oco->buf) {
  309. xfree(oco);
  310. MarkClientException(client);
  311. return -1;
  312. }
  313. }
  314. oco->count = len;
  315. oco->nocompress = nocompress;
  316. memmove((char *)oco->buf, extraBuf, extraCount);
  317. if (!nocompress && oco->count < oco->size)
  318. oc->output = oco;
  319. else if (!LbxAppendOutput(oc->proxy, client, oco))
  320. return -1;
  321. }
  322. return extraCount;
  323. }
  324. void
  325. LbxForceOutput(LbxProxyPtr proxy)
  326. {
  327. int i;
  328. LbxClientPtr lbxClient;
  329. OsCommPtr coc;
  330. ConnectionOutputPtr oco;
  331. for (i = proxy->maxIndex; i >= 0; i--) { /* proxy must be last */
  332. lbxClient = proxy->lbxClients[i];
  333. if (!lbxClient)
  334. continue;
  335. coc = (OsCommPtr)lbxClient->client->osPrivate;
  336. if ((oco = coc->output)) {
  337. coc->output = NULL;
  338. LbxAppendOutput(proxy, lbxClient->client, oco);
  339. }
  340. }
  341. }
  342. int
  343. LbxFlushClient(ClientPtr who, OsCommPtr oc,
  344. char *extraBuf, int extraCount)
  345. {
  346. LbxProxyPtr proxy;
  347. ConnectionOutputPtr oco;
  348. int n;
  349. XtransConnInfo trans_conn = NULL;
  350. if (extraBuf)
  351. return LbxClientOutput(who, oc, extraBuf, extraCount, FALSE);
  352. proxy = oc->proxy;
  353. if (!proxy->lbxClients[0])
  354. return 0;
  355. LbxForceOutput(proxy);
  356. if (!proxy->compHandle)
  357. trans_conn = ((OsCommPtr)LbxProxyClient(proxy)->osPrivate)->trans_conn;
  358. while ((oco = proxy->ofirst)) {
  359. /* XXX bundle up into writev someday */
  360. if (proxy->compHandle) {
  361. if (oco->nocompress)
  362. (*proxy->streamOpts.streamCompOff)(proxy->fd);
  363. n = LbxWrite(NULL, proxy, (char *)oco->buf, oco->count);
  364. if (oco->nocompress)
  365. (*proxy->streamOpts.streamCompOn)(proxy->fd);
  366. } else
  367. n = LbxWrite(trans_conn, proxy, (char *)oco->buf, oco->count);
  368. if (n < 0) {
  369. ClientPtr pclient = LbxProxyClient(proxy);
  370. if (proxy->compHandle)
  371. trans_conn = ((OsCommPtr)pclient->osPrivate)->trans_conn;
  372. _XSERVTransDisconnect(trans_conn);
  373. _XSERVTransClose(trans_conn);
  374. ((OsCommPtr)pclient->osPrivate)->trans_conn = NULL;
  375. MarkClientException(pclient);
  376. return 0;
  377. } else if (n == oco->count) {
  378. proxy->ofirst = oco->next;
  379. if (!proxy->ofirst)
  380. proxy->olast = NULL;
  381. if (oco->size > BUFWATERMARK)
  382. {
  383. xfree(oco->buf);
  384. xfree(oco);
  385. }
  386. else
  387. {
  388. oco->next = FreeOutputs;
  389. oco->count = 0;
  390. FreeOutputs = oco;
  391. }
  392. } else {
  393. if (n) {
  394. oco->count -= n;
  395. memmove((char *)oco->buf, (char *)oco->buf + n, oco->count);
  396. }
  397. break;
  398. }
  399. }
  400. if ((proxy->compHandle &&
  401. (*proxy->streamOpts.streamCompFlush)(proxy->fd)) ||
  402. proxy->ofirst) {
  403. FD_SET(proxy->fd, &ClientsWriteBlocked);
  404. AnyClientsWriteBlocked = TRUE;
  405. }
  406. return 0;
  407. }
  408. int
  409. UncompressedWriteToClient (ClientPtr who, int count, char *buf)
  410. {
  411. return LbxClientOutput(who, (OsCommPtr)who->osPrivate, buf, count, TRUE);
  412. }
  413. void
  414. LbxFreeOsBuffers(LbxProxyPtr proxy)
  415. {
  416. ConnectionOutputPtr oco;
  417. while ((oco = proxy->ofirst)) {
  418. proxy->ofirst = oco->next;
  419. xfree(oco->buf);
  420. xfree(oco);
  421. }
  422. }
  423. Bool
  424. AllocateLargeReqBuffer(ClientPtr client, int size)
  425. {
  426. OsCommPtr oc = (OsCommPtr)client->osPrivate;
  427. ConnectionInputPtr oci;
  428. if (!(oci = oc->largereq)) {
  429. if ((oci = FreeInputs))
  430. FreeInputs = oci->next;
  431. else {
  432. oci = (ConnectionInputPtr)xalloc(sizeof(ConnectionInput));
  433. if (!oci)
  434. return FALSE;
  435. oci->buffer = NULL;
  436. oci->size = 0;
  437. }
  438. }
  439. if (oci->size < size) {
  440. char *ibuf;
  441. oci->size = size;
  442. if (size < BUFSIZE)
  443. oci->size = BUFSIZE;
  444. if (!(ibuf = (char *)xrealloc(oci->buffer, oci->size)))
  445. {
  446. xfree(oci->buffer);
  447. xfree(oci);
  448. oc->largereq = NULL;
  449. return FALSE;
  450. }
  451. oci->buffer = ibuf;
  452. }
  453. oci->bufptr = oci->buffer;
  454. oci->bufcnt = 0;
  455. oci->lenLastReq = size;
  456. oc->largereq = oci;
  457. return TRUE;
  458. }
  459. Bool
  460. AddToLargeReqBuffer(ClientPtr client, char *data, int size)
  461. {
  462. OsCommPtr oc = (OsCommPtr)client->osPrivate;
  463. ConnectionInputPtr oci = oc->largereq;
  464. if (!oci || (oci->bufcnt + size > oci->lenLastReq))
  465. return FALSE;
  466. memcpy(oci->buffer + oci->bufcnt, data, size);
  467. oci->bufcnt += size;
  468. return TRUE;
  469. }
  470. static OsCommRec lbxAvailableInput;
  471. int
  472. PrepareLargeReqBuffer(ClientPtr client)
  473. {
  474. OsCommPtr oc = (OsCommPtr)client->osPrivate;
  475. ConnectionInputPtr oci = oc->largereq;
  476. if (!oci)
  477. return client->req_len << 2;
  478. oc->largereq = NULL;
  479. if (oci->bufcnt != oci->lenLastReq) {
  480. xfree(oci->buffer);
  481. xfree(oci);
  482. return client->req_len << 2;
  483. }
  484. client->requestBuffer = oci->buffer;
  485. client->req_len = oci->lenLastReq >> 2;
  486. oci->bufcnt = 0;
  487. oci->lenLastReq = 0;
  488. if (AvailableInput)
  489. {
  490. ConnectionInputPtr aci = AvailableInput->input;
  491. if (aci->size > BUFWATERMARK)
  492. {
  493. xfree(aci->buffer);
  494. xfree(aci);
  495. }
  496. else
  497. {
  498. aci->next = FreeInputs;
  499. FreeInputs = aci;
  500. }
  501. AvailableInput->input = (ConnectionInputPtr)NULL;
  502. }
  503. lbxAvailableInput.input = oci;
  504. AvailableInput = &lbxAvailableInput;
  505. return client->req_len << 2;
  506. }