xcb_conn.c revision 1016ad83
1602e473dSmrg/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2602e473dSmrg *
3602e473dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
4602e473dSmrg * copy of this software and associated documentation files (the "Software"),
5602e473dSmrg * to deal in the Software without restriction, including without limitation
6602e473dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7602e473dSmrg * and/or sell copies of the Software, and to permit persons to whom the
8602e473dSmrg * Software is furnished to do so, subject to the following conditions:
9602e473dSmrg *
10602e473dSmrg * The above copyright notice and this permission notice shall be included in
11602e473dSmrg * all copies or substantial portions of the Software.
12602e473dSmrg *
13602e473dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14602e473dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15602e473dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16602e473dSmrg * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17602e473dSmrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18602e473dSmrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19602e473dSmrg *
20602e473dSmrg * Except as contained in this notice, the names of the authors or their
21602e473dSmrg * institutions shall not be used in advertising or otherwise to promote the
22602e473dSmrg * sale, use or other dealings in this Software without prior written
23602e473dSmrg * authorization from the authors.
24602e473dSmrg */
25602e473dSmrg
26602e473dSmrg/* Connection management: the core of XCB. */
27602e473dSmrg
2821298544Smrg#ifdef HAVE_CONFIG_H
2921298544Smrg#include "config.h"
3021298544Smrg#endif
3121298544Smrg
32602e473dSmrg#include <assert.h>
33602e473dSmrg#include <string.h>
34602e473dSmrg#include <stdio.h>
35602e473dSmrg#include <unistd.h>
36602e473dSmrg#include <stdlib.h>
37602e473dSmrg#include <fcntl.h>
38602e473dSmrg#include <errno.h>
3921298544Smrg#include <limits.h>
40602e473dSmrg
41602e473dSmrg#include "xcb.h"
42602e473dSmrg#include "xcbint.h"
43602e473dSmrg#if USE_POLL
44602e473dSmrg#include <poll.h>
4521298544Smrg#elif !defined _WIN32
46602e473dSmrg#include <sys/select.h>
47602e473dSmrg#endif
48602e473dSmrg
4921298544Smrg#ifdef _WIN32
5021298544Smrg#include "xcb_windefs.h"
5121298544Smrg#else
5221298544Smrg#include <sys/socket.h>
5321298544Smrg#include <netinet/in.h>
5421298544Smrg#endif /* _WIN32 */
5521298544Smrg
5621298544Smrg/* SHUT_RDWR is fairly recent and is not available on all platforms */
5721298544Smrg#if !defined(SHUT_RDWR)
5821298544Smrg#define SHUT_RDWR 2
5921298544Smrg#endif
6021298544Smrg
61602e473dSmrgtypedef struct {
62602e473dSmrg    uint8_t  status;
63602e473dSmrg    uint8_t  pad0[5];
64602e473dSmrg    uint16_t length;
65602e473dSmrg} xcb_setup_generic_t;
66602e473dSmrg
6721298544Smrgstatic const int xcb_con_error = XCB_CONN_ERROR;
6821298544Smrgstatic const int xcb_con_closed_mem_er = XCB_CONN_CLOSED_MEM_INSUFFICIENT;
6921298544Smrgstatic const int xcb_con_closed_parse_er = XCB_CONN_CLOSED_PARSE_ERR;
7021298544Smrgstatic const int xcb_con_closed_screen_er = XCB_CONN_CLOSED_INVALID_SCREEN;
71602e473dSmrg
72602e473dSmrgstatic int set_fd_flags(const int fd)
73602e473dSmrg{
7421298544Smrg/* Win32 doesn't have file descriptors and the fcntl function. This block sets the socket in non-blocking mode */
7521298544Smrg
7621298544Smrg#ifdef _WIN32
7721298544Smrg   u_long iMode = 1; /* non-zero puts it in non-blocking mode, 0 in blocking mode */
7821298544Smrg   int ret = 0;
7921298544Smrg
8021298544Smrg   ret = ioctlsocket(fd, FIONBIO, &iMode);
8121298544Smrg   if(ret != 0)
8221298544Smrg       return 0;
8321298544Smrg   return 1;
8421298544Smrg#else
85602e473dSmrg    int flags = fcntl(fd, F_GETFL, 0);
86602e473dSmrg    if(flags == -1)
87602e473dSmrg        return 0;
88602e473dSmrg    flags |= O_NONBLOCK;
89602e473dSmrg    if(fcntl(fd, F_SETFL, flags) == -1)
90602e473dSmrg        return 0;
91602e473dSmrg    if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
92602e473dSmrg        return 0;
93602e473dSmrg    return 1;
9421298544Smrg#endif /* _WIN32 */
95602e473dSmrg}
96602e473dSmrg
97602e473dSmrgstatic int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info)
98602e473dSmrg{
99602e473dSmrg    static const char pad[3];
100602e473dSmrg    xcb_setup_request_t out;
101602e473dSmrg    struct iovec parts[6];
102602e473dSmrg    int count = 0;
103602e473dSmrg    static const uint32_t endian = 0x01020304;
104602e473dSmrg    int ret;
105602e473dSmrg
106602e473dSmrg    memset(&out, 0, sizeof(out));
107602e473dSmrg
108602e473dSmrg    /* B = 0x42 = MSB first, l = 0x6c = LSB first */
109602e473dSmrg    if(htonl(endian) == endian)
110602e473dSmrg        out.byte_order = 0x42;
111602e473dSmrg    else
112602e473dSmrg        out.byte_order = 0x6c;
113602e473dSmrg    out.protocol_major_version = X_PROTOCOL;
114602e473dSmrg    out.protocol_minor_version = X_PROTOCOL_REVISION;
115602e473dSmrg    out.authorization_protocol_name_len = 0;
116602e473dSmrg    out.authorization_protocol_data_len = 0;
117602e473dSmrg    parts[count].iov_len = sizeof(xcb_setup_request_t);
118602e473dSmrg    parts[count++].iov_base = &out;
119602e473dSmrg    parts[count].iov_len = XCB_PAD(sizeof(xcb_setup_request_t));
120602e473dSmrg    parts[count++].iov_base = (char *) pad;
121602e473dSmrg
122602e473dSmrg    if(auth_info)
123602e473dSmrg    {
124602e473dSmrg        parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
125602e473dSmrg        parts[count++].iov_base = auth_info->name;
126602e473dSmrg        parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len);
127602e473dSmrg        parts[count++].iov_base = (char *) pad;
128602e473dSmrg        parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
129602e473dSmrg        parts[count++].iov_base = auth_info->data;
130602e473dSmrg        parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len);
131602e473dSmrg        parts[count++].iov_base = (char *) pad;
132602e473dSmrg    }
133602e473dSmrg    assert(count <= (int) (sizeof(parts) / sizeof(*parts)));
134602e473dSmrg
135602e473dSmrg    pthread_mutex_lock(&c->iolock);
136b20a2039Smrg    ret = _xcb_out_send(c, parts, count);
137602e473dSmrg    pthread_mutex_unlock(&c->iolock);
138602e473dSmrg    return ret;
139602e473dSmrg}
140602e473dSmrg
141602e473dSmrgstatic int read_setup(xcb_connection_t *c)
142602e473dSmrg{
143602e473dSmrg    /* Read the server response */
144602e473dSmrg    c->setup = malloc(sizeof(xcb_setup_generic_t));
145602e473dSmrg    if(!c->setup)
146602e473dSmrg        return 0;
147602e473dSmrg
148602e473dSmrg    if(_xcb_in_read_block(c, c->setup, sizeof(xcb_setup_generic_t)) != sizeof(xcb_setup_generic_t))
149602e473dSmrg        return 0;
150602e473dSmrg
151602e473dSmrg    {
152602e473dSmrg        void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(xcb_setup_generic_t));
153602e473dSmrg        if(!tmp)
154602e473dSmrg            return 0;
155602e473dSmrg        c->setup = tmp;
156602e473dSmrg    }
157602e473dSmrg
158602e473dSmrg    if(_xcb_in_read_block(c, (char *) c->setup + sizeof(xcb_setup_generic_t), c->setup->length * 4) <= 0)
159602e473dSmrg        return 0;
160602e473dSmrg
161602e473dSmrg    /* 0 = failed, 2 = authenticate, 1 = success */
162602e473dSmrg    switch(c->setup->status)
163602e473dSmrg    {
164602e473dSmrg    case 0: /* failed */
165602e473dSmrg        {
166602e473dSmrg            xcb_setup_failed_t *setup = (xcb_setup_failed_t *) c->setup;
167602e473dSmrg            write(STDERR_FILENO, xcb_setup_failed_reason(setup), xcb_setup_failed_reason_length(setup));
168602e473dSmrg            return 0;
169602e473dSmrg        }
170602e473dSmrg
171602e473dSmrg    case 2: /* authenticate */
172602e473dSmrg        {
173602e473dSmrg            xcb_setup_authenticate_t *setup = (xcb_setup_authenticate_t *) c->setup;
174602e473dSmrg            write(STDERR_FILENO, xcb_setup_authenticate_reason(setup), xcb_setup_authenticate_reason_length(setup));
175602e473dSmrg            return 0;
176602e473dSmrg        }
177602e473dSmrg    }
178602e473dSmrg
179602e473dSmrg    return 1;
180602e473dSmrg}
181602e473dSmrg
182602e473dSmrg/* precondition: there must be something for us to write. */
183602e473dSmrgstatic int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
184602e473dSmrg{
185602e473dSmrg    int n;
186602e473dSmrg    assert(!c->out.queue_len);
18721298544Smrg
18821298544Smrg#ifdef _WIN32
18921298544Smrg    int i = 0;
19021298544Smrg    int ret = 0,err = 0;
19121298544Smrg    struct iovec *vec;
19221298544Smrg    n = 0;
19321298544Smrg
19421298544Smrg    /* Could use the WSASend win32 function for scatter/gather i/o but setting up the WSABUF struct from
19521298544Smrg       an iovec would require more work and I'm not sure of the benefit....works for now */
19621298544Smrg    vec = *vector;
19721298544Smrg    while(i < *count)
19821298544Smrg    {
19921298544Smrg         ret = send(c->fd,vec->iov_base,vec->iov_len,0);
20021298544Smrg         if(ret == SOCKET_ERROR)
20121298544Smrg         {
20221298544Smrg             err  = WSAGetLastError();
20321298544Smrg             if(err == WSAEWOULDBLOCK)
20421298544Smrg             {
20521298544Smrg                 return 1;
20621298544Smrg             }
20721298544Smrg         }
20821298544Smrg         n += ret;
20921298544Smrg         *vec++;
21021298544Smrg         i++;
21121298544Smrg    }
21221298544Smrg#else
21321298544Smrg    n = *count;
21421298544Smrg    if (n > IOV_MAX)
21521298544Smrg	n = IOV_MAX;
21621298544Smrg
2171016ad83Smrg#if HAVE_SENDMSG
2181016ad83Smrg    if (c->out.out_fd.nfd) {
2191016ad83Smrg        union {
2201016ad83Smrg            struct cmsghdr cmsghdr;
2211016ad83Smrg            char buf[CMSG_SPACE(XCB_MAX_PASS_FD * sizeof(int))];
2221016ad83Smrg        } cmsgbuf;
2231016ad83Smrg        struct msghdr msg = {
2241016ad83Smrg            .msg_name = NULL,
2251016ad83Smrg            .msg_namelen = 0,
2261016ad83Smrg            .msg_iov = *vector,
2271016ad83Smrg            .msg_iovlen = n,
2281016ad83Smrg            .msg_control = cmsgbuf.buf,
2291016ad83Smrg            .msg_controllen = CMSG_LEN(c->out.out_fd.nfd * sizeof (int)),
2301016ad83Smrg        };
2311016ad83Smrg        int i;
2321016ad83Smrg        struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg);
2331016ad83Smrg
2341016ad83Smrg        hdr->cmsg_len = msg.msg_controllen;
2351016ad83Smrg        hdr->cmsg_level = SOL_SOCKET;
2361016ad83Smrg        hdr->cmsg_type = SCM_RIGHTS;
2371016ad83Smrg        memcpy(CMSG_DATA(hdr), c->out.out_fd.fd, c->out.out_fd.nfd * sizeof (int));
2381016ad83Smrg
2391016ad83Smrg        n = sendmsg(c->fd, &msg, 0);
2401016ad83Smrg        if(n < 0 && errno == EAGAIN)
2411016ad83Smrg            return 1;
2421016ad83Smrg        for (i = 0; i < c->out.out_fd.nfd; i++)
2431016ad83Smrg            close(c->out.out_fd.fd[i]);
2441016ad83Smrg        c->out.out_fd.nfd = 0;
2451016ad83Smrg    } else
2461016ad83Smrg#endif
2471016ad83Smrg    {
2481016ad83Smrg        n = writev(c->fd, *vector, n);
2491016ad83Smrg        if(n < 0 && errno == EAGAIN)
2501016ad83Smrg            return 1;
2511016ad83Smrg    }
2521016ad83Smrg
25321298544Smrg#endif /* _WIN32 */
25421298544Smrg
255602e473dSmrg    if(n <= 0)
256602e473dSmrg    {
25721298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_ERROR);
258602e473dSmrg        return 0;
259602e473dSmrg    }
260602e473dSmrg
261602e473dSmrg    for(; *count; --*count, ++*vector)
262602e473dSmrg    {
263602e473dSmrg        int cur = (*vector)->iov_len;
264602e473dSmrg        if(cur > n)
265602e473dSmrg            cur = n;
266602e473dSmrg        (*vector)->iov_len -= cur;
267602e473dSmrg        (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
268602e473dSmrg        n -= cur;
269602e473dSmrg        if((*vector)->iov_len)
270602e473dSmrg            break;
271602e473dSmrg    }
272602e473dSmrg    if(!*count)
273602e473dSmrg        *vector = 0;
274602e473dSmrg    assert(n == 0);
275602e473dSmrg    return 1;
276602e473dSmrg}
277602e473dSmrg
278602e473dSmrg/* Public interface */
279602e473dSmrg
280602e473dSmrgconst xcb_setup_t *xcb_get_setup(xcb_connection_t *c)
281602e473dSmrg{
282602e473dSmrg    if(c->has_error)
283602e473dSmrg        return 0;
284602e473dSmrg    /* doesn't need locking because it's never written to. */
285602e473dSmrg    return c->setup;
286602e473dSmrg}
287602e473dSmrg
288602e473dSmrgint xcb_get_file_descriptor(xcb_connection_t *c)
289602e473dSmrg{
290602e473dSmrg    if(c->has_error)
291602e473dSmrg        return -1;
292602e473dSmrg    /* doesn't need locking because it's never written to. */
293602e473dSmrg    return c->fd;
294602e473dSmrg}
295602e473dSmrg
296602e473dSmrgint xcb_connection_has_error(xcb_connection_t *c)
297602e473dSmrg{
298602e473dSmrg    /* doesn't need locking because it's read and written atomically. */
299602e473dSmrg    return c->has_error;
300602e473dSmrg}
301602e473dSmrg
302602e473dSmrgxcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
303602e473dSmrg{
304602e473dSmrg    xcb_connection_t* c;
305602e473dSmrg
30621298544Smrg#ifndef _WIN32
307602e473dSmrg#ifndef USE_POLL
308602e473dSmrg    if(fd >= FD_SETSIZE) /* would overflow in FD_SET */
309602e473dSmrg    {
310602e473dSmrg        close(fd);
31121298544Smrg        return _xcb_conn_ret_error(XCB_CONN_ERROR);
312602e473dSmrg    }
313602e473dSmrg#endif
31421298544Smrg#endif /* !_WIN32*/
315602e473dSmrg
316602e473dSmrg    c = calloc(1, sizeof(xcb_connection_t));
317602e473dSmrg    if(!c) {
318602e473dSmrg        close(fd);
31921298544Smrg        return _xcb_conn_ret_error(XCB_CONN_CLOSED_MEM_INSUFFICIENT) ;
320602e473dSmrg    }
321602e473dSmrg
322602e473dSmrg    c->fd = fd;
323602e473dSmrg
324602e473dSmrg    if(!(
325602e473dSmrg        set_fd_flags(fd) &&
326602e473dSmrg        pthread_mutex_init(&c->iolock, 0) == 0 &&
327602e473dSmrg        _xcb_in_init(&c->in) &&
328602e473dSmrg        _xcb_out_init(&c->out) &&
329602e473dSmrg        write_setup(c, auth_info) &&
330602e473dSmrg        read_setup(c) &&
331602e473dSmrg        _xcb_ext_init(c) &&
332602e473dSmrg        _xcb_xid_init(c)
333602e473dSmrg        ))
334602e473dSmrg    {
335602e473dSmrg        xcb_disconnect(c);
33621298544Smrg        return _xcb_conn_ret_error(XCB_CONN_ERROR);
337602e473dSmrg    }
338602e473dSmrg
339602e473dSmrg    return c;
340602e473dSmrg}
341602e473dSmrg
342602e473dSmrgvoid xcb_disconnect(xcb_connection_t *c)
343602e473dSmrg{
344602e473dSmrg    if(c->has_error)
345602e473dSmrg        return;
346602e473dSmrg
347602e473dSmrg    free(c->setup);
34821298544Smrg
34921298544Smrg    /* disallow further sends and receives */
35021298544Smrg    shutdown(c->fd, SHUT_RDWR);
351602e473dSmrg    close(c->fd);
352602e473dSmrg
353602e473dSmrg    pthread_mutex_destroy(&c->iolock);
354602e473dSmrg    _xcb_in_destroy(&c->in);
355602e473dSmrg    _xcb_out_destroy(&c->out);
356602e473dSmrg
357602e473dSmrg    _xcb_ext_destroy(c);
358602e473dSmrg    _xcb_xid_destroy(c);
359602e473dSmrg
360602e473dSmrg    free(c);
36121298544Smrg
36221298544Smrg#ifdef _WIN32
36321298544Smrg    WSACleanup();
36421298544Smrg#endif
365602e473dSmrg}
366602e473dSmrg
367602e473dSmrg/* Private interface */
368602e473dSmrg
36921298544Smrgvoid _xcb_conn_shutdown(xcb_connection_t *c, int err)
370602e473dSmrg{
37121298544Smrg    c->has_error = err;
37221298544Smrg}
37321298544Smrg
37421298544Smrg/* Return connection error state.
37521298544Smrg * To make thread-safe, I need a seperate static
37621298544Smrg * variable for every possible error.
37721298544Smrg */
37821298544Smrgxcb_connection_t *_xcb_conn_ret_error(int err)
37921298544Smrg{
38021298544Smrg
38121298544Smrg    switch(err)
38221298544Smrg    {
38321298544Smrg        case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
38421298544Smrg        {
38521298544Smrg            return (xcb_connection_t *) &xcb_con_closed_mem_er;
38621298544Smrg        }
38721298544Smrg        case XCB_CONN_CLOSED_PARSE_ERR:
38821298544Smrg        {
38921298544Smrg            return (xcb_connection_t *) &xcb_con_closed_parse_er;
39021298544Smrg        }
39121298544Smrg        case XCB_CONN_CLOSED_INVALID_SCREEN:
39221298544Smrg        {
39321298544Smrg            return (xcb_connection_t *) &xcb_con_closed_screen_er;
39421298544Smrg        }
39521298544Smrg        case XCB_CONN_ERROR:
39621298544Smrg        default:
39721298544Smrg        {
39821298544Smrg            return (xcb_connection_t *) &xcb_con_error;
39921298544Smrg        }
40021298544Smrg    }
401602e473dSmrg}
402602e473dSmrg
403602e473dSmrgint _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
404602e473dSmrg{
405602e473dSmrg    int ret;
406602e473dSmrg#if USE_POLL
407602e473dSmrg    struct pollfd fd;
408602e473dSmrg#else
409602e473dSmrg    fd_set rfds, wfds;
410602e473dSmrg#endif
411602e473dSmrg
412602e473dSmrg    /* If the thing I should be doing is already being done, wait for it. */
413602e473dSmrg    if(count ? c->out.writing : c->in.reading)
414602e473dSmrg    {
415602e473dSmrg        pthread_cond_wait(cond, &c->iolock);
416602e473dSmrg        return 1;
417602e473dSmrg    }
418602e473dSmrg
419602e473dSmrg#if USE_POLL
420602e473dSmrg    memset(&fd, 0, sizeof(fd));
421602e473dSmrg    fd.fd = c->fd;
422602e473dSmrg    fd.events = POLLIN;
423602e473dSmrg#else
424602e473dSmrg    FD_ZERO(&rfds);
425602e473dSmrg    FD_SET(c->fd, &rfds);
426602e473dSmrg#endif
427602e473dSmrg    ++c->in.reading;
428602e473dSmrg
429602e473dSmrg#if USE_POLL
430602e473dSmrg    if(count)
431602e473dSmrg    {
432602e473dSmrg        fd.events |= POLLOUT;
433602e473dSmrg        ++c->out.writing;
434602e473dSmrg    }
435602e473dSmrg#else
436602e473dSmrg    FD_ZERO(&wfds);
437602e473dSmrg    if(count)
438602e473dSmrg    {
439602e473dSmrg        FD_SET(c->fd, &wfds);
440602e473dSmrg        ++c->out.writing;
441602e473dSmrg    }
442602e473dSmrg#endif
443602e473dSmrg
444602e473dSmrg    pthread_mutex_unlock(&c->iolock);
445602e473dSmrg    do {
446602e473dSmrg#if USE_POLL
447b20a2039Smrg        ret = poll(&fd, 1, -1);
44821298544Smrg        /* If poll() returns an event we didn't expect, such as POLLNVAL, treat
44921298544Smrg         * it as if it failed. */
45021298544Smrg        if(ret >= 0 && (fd.revents & ~fd.events))
45121298544Smrg        {
45221298544Smrg            ret = -1;
45321298544Smrg            break;
45421298544Smrg        }
455602e473dSmrg#else
456b20a2039Smrg        ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
457602e473dSmrg#endif
458602e473dSmrg    } while (ret == -1 && errno == EINTR);
459b20a2039Smrg    if(ret < 0)
460602e473dSmrg    {
46121298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_ERROR);
462b20a2039Smrg        ret = 0;
463602e473dSmrg    }
464602e473dSmrg    pthread_mutex_lock(&c->iolock);
465602e473dSmrg
466602e473dSmrg    if(ret)
467602e473dSmrg    {
46821298544Smrg        /* The code allows two threads to call select()/poll() at the same time.
46921298544Smrg         * First thread just wants to read, a second thread wants to write, too.
47021298544Smrg         * We have to make sure that we don't steal the reading thread's reply
47121298544Smrg         * and let it get stuck in select()/poll().
47221298544Smrg         * So a thread may read if either:
47321298544Smrg         * - There is no other thread that wants to read (the above situation
47421298544Smrg         *   did not occur).
47521298544Smrg         * - It is the reading thread (above situation occurred).
47621298544Smrg         */
47721298544Smrg        int may_read = c->in.reading == 1 || !count;
478602e473dSmrg#if USE_POLL
4791016ad83Smrg        if(may_read && (fd.revents & POLLIN) != 0)
480602e473dSmrg#else
48121298544Smrg        if(may_read && FD_ISSET(c->fd, &rfds))
482602e473dSmrg#endif
483602e473dSmrg            ret = ret && _xcb_in_read(c);
484602e473dSmrg
485602e473dSmrg#if USE_POLL
4861016ad83Smrg        if((fd.revents & POLLOUT) != 0)
487602e473dSmrg#else
488602e473dSmrg        if(FD_ISSET(c->fd, &wfds))
489602e473dSmrg#endif
490602e473dSmrg            ret = ret && write_vec(c, vector, count);
491602e473dSmrg    }
492602e473dSmrg
493602e473dSmrg    if(count)
494602e473dSmrg        --c->out.writing;
495602e473dSmrg    --c->in.reading;
496602e473dSmrg
497602e473dSmrg    return ret;
498602e473dSmrg}
499