xcb_conn.c revision 7204935c
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
677204935cSmrgstatic const xcb_setup_t xcb_error_setup = {
687204935cSmrg    0,     /* status: failed (but we wouldn't have a xcb_setup_t in this case) */
697204935cSmrg    0,     /* pad0 */
707204935cSmrg    0, 0,  /* protocol version, should be 11.0, but isn't */
717204935cSmrg    0,     /* length, invalid value */
727204935cSmrg    0,     /* release_number */
737204935cSmrg    0, 0,  /* resource_id_{base,mask} */
747204935cSmrg    0,     /* motion_buffer_size */
757204935cSmrg    0,     /* vendor_len */
767204935cSmrg    0,     /* maximum_request_length */
777204935cSmrg    0,     /* roots_len */
787204935cSmrg    0,     /* pixmap_formats_len */
797204935cSmrg    0,     /* image_byte_order */
807204935cSmrg    0,     /* bitmap_format_bit_order */
817204935cSmrg    0,     /* bitmap_format_scanline_unit */
827204935cSmrg    0,     /* bitmap_format_scanline_pad */
837204935cSmrg    0, 0,  /* {min,max}_keycode */
847204935cSmrg    { 0, 0, 0, 0 } /* pad1 */
857204935cSmrg};
867204935cSmrg
871c7386f4Smrg/* Keep this list in sync with is_static_error_conn()! */
8821298544Smrgstatic const int xcb_con_error = XCB_CONN_ERROR;
8921298544Smrgstatic const int xcb_con_closed_mem_er = XCB_CONN_CLOSED_MEM_INSUFFICIENT;
9021298544Smrgstatic const int xcb_con_closed_parse_er = XCB_CONN_CLOSED_PARSE_ERR;
9121298544Smrgstatic const int xcb_con_closed_screen_er = XCB_CONN_CLOSED_INVALID_SCREEN;
92602e473dSmrg
931c7386f4Smrgstatic int is_static_error_conn(xcb_connection_t *c)
941c7386f4Smrg{
951c7386f4Smrg    return c == (xcb_connection_t *) &xcb_con_error ||
961c7386f4Smrg           c == (xcb_connection_t *) &xcb_con_closed_mem_er ||
971c7386f4Smrg           c == (xcb_connection_t *) &xcb_con_closed_parse_er ||
981c7386f4Smrg           c == (xcb_connection_t *) &xcb_con_closed_screen_er;
991c7386f4Smrg}
1001c7386f4Smrg
101602e473dSmrgstatic int set_fd_flags(const int fd)
102602e473dSmrg{
10321298544Smrg/* Win32 doesn't have file descriptors and the fcntl function. This block sets the socket in non-blocking mode */
10421298544Smrg
10521298544Smrg#ifdef _WIN32
1061c7386f4Smrg   u_long iMode = 1; /* non-zero puts it in non-blocking mode, 0 in blocking mode */
10721298544Smrg   int ret = 0;
10821298544Smrg
10921298544Smrg   ret = ioctlsocket(fd, FIONBIO, &iMode);
1101c7386f4Smrg   if(ret != 0)
11121298544Smrg       return 0;
11221298544Smrg   return 1;
11321298544Smrg#else
114602e473dSmrg    int flags = fcntl(fd, F_GETFL, 0);
115602e473dSmrg    if(flags == -1)
116602e473dSmrg        return 0;
117602e473dSmrg    flags |= O_NONBLOCK;
118602e473dSmrg    if(fcntl(fd, F_SETFL, flags) == -1)
119602e473dSmrg        return 0;
120602e473dSmrg    if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
121602e473dSmrg        return 0;
122602e473dSmrg    return 1;
12321298544Smrg#endif /* _WIN32 */
124602e473dSmrg}
125602e473dSmrg
126602e473dSmrgstatic int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info)
127602e473dSmrg{
128602e473dSmrg    static const char pad[3];
129602e473dSmrg    xcb_setup_request_t out;
130602e473dSmrg    struct iovec parts[6];
131602e473dSmrg    int count = 0;
132602e473dSmrg    static const uint32_t endian = 0x01020304;
133602e473dSmrg    int ret;
134602e473dSmrg
135602e473dSmrg    memset(&out, 0, sizeof(out));
136602e473dSmrg
137602e473dSmrg    /* B = 0x42 = MSB first, l = 0x6c = LSB first */
138602e473dSmrg    if(htonl(endian) == endian)
139602e473dSmrg        out.byte_order = 0x42;
140602e473dSmrg    else
141602e473dSmrg        out.byte_order = 0x6c;
142602e473dSmrg    out.protocol_major_version = X_PROTOCOL;
143602e473dSmrg    out.protocol_minor_version = X_PROTOCOL_REVISION;
144602e473dSmrg    out.authorization_protocol_name_len = 0;
145602e473dSmrg    out.authorization_protocol_data_len = 0;
146602e473dSmrg    parts[count].iov_len = sizeof(xcb_setup_request_t);
147602e473dSmrg    parts[count++].iov_base = &out;
148602e473dSmrg    parts[count].iov_len = XCB_PAD(sizeof(xcb_setup_request_t));
149602e473dSmrg    parts[count++].iov_base = (char *) pad;
150602e473dSmrg
151602e473dSmrg    if(auth_info)
152602e473dSmrg    {
153602e473dSmrg        parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
154602e473dSmrg        parts[count++].iov_base = auth_info->name;
155602e473dSmrg        parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len);
156602e473dSmrg        parts[count++].iov_base = (char *) pad;
157602e473dSmrg        parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
158602e473dSmrg        parts[count++].iov_base = auth_info->data;
159602e473dSmrg        parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len);
160602e473dSmrg        parts[count++].iov_base = (char *) pad;
161602e473dSmrg    }
162602e473dSmrg    assert(count <= (int) (sizeof(parts) / sizeof(*parts)));
163602e473dSmrg
164602e473dSmrg    pthread_mutex_lock(&c->iolock);
165b20a2039Smrg    ret = _xcb_out_send(c, parts, count);
166602e473dSmrg    pthread_mutex_unlock(&c->iolock);
167602e473dSmrg    return ret;
168602e473dSmrg}
169602e473dSmrg
170602e473dSmrgstatic int read_setup(xcb_connection_t *c)
171602e473dSmrg{
172602e473dSmrg    /* Read the server response */
173602e473dSmrg    c->setup = malloc(sizeof(xcb_setup_generic_t));
174602e473dSmrg    if(!c->setup)
175602e473dSmrg        return 0;
176602e473dSmrg
177602e473dSmrg    if(_xcb_in_read_block(c, c->setup, sizeof(xcb_setup_generic_t)) != sizeof(xcb_setup_generic_t))
178602e473dSmrg        return 0;
179602e473dSmrg
180602e473dSmrg    {
181602e473dSmrg        void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(xcb_setup_generic_t));
182602e473dSmrg        if(!tmp)
183602e473dSmrg            return 0;
184602e473dSmrg        c->setup = tmp;
185602e473dSmrg    }
186602e473dSmrg
187602e473dSmrg    if(_xcb_in_read_block(c, (char *) c->setup + sizeof(xcb_setup_generic_t), c->setup->length * 4) <= 0)
188602e473dSmrg        return 0;
189602e473dSmrg
190602e473dSmrg    /* 0 = failed, 2 = authenticate, 1 = success */
191602e473dSmrg    switch(c->setup->status)
192602e473dSmrg    {
193602e473dSmrg    case 0: /* failed */
194602e473dSmrg        {
195602e473dSmrg            xcb_setup_failed_t *setup = (xcb_setup_failed_t *) c->setup;
196602e473dSmrg            write(STDERR_FILENO, xcb_setup_failed_reason(setup), xcb_setup_failed_reason_length(setup));
197602e473dSmrg            return 0;
198602e473dSmrg        }
199602e473dSmrg
200602e473dSmrg    case 2: /* authenticate */
201602e473dSmrg        {
202602e473dSmrg            xcb_setup_authenticate_t *setup = (xcb_setup_authenticate_t *) c->setup;
203602e473dSmrg            write(STDERR_FILENO, xcb_setup_authenticate_reason(setup), xcb_setup_authenticate_reason_length(setup));
204602e473dSmrg            return 0;
205602e473dSmrg        }
206602e473dSmrg    }
207602e473dSmrg
208602e473dSmrg    return 1;
209602e473dSmrg}
210602e473dSmrg
211602e473dSmrg/* precondition: there must be something for us to write. */
212602e473dSmrgstatic int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
213602e473dSmrg{
214602e473dSmrg    int n;
215602e473dSmrg    assert(!c->out.queue_len);
21621298544Smrg
21721298544Smrg#ifdef _WIN32
21821298544Smrg    int i = 0;
21921298544Smrg    int ret = 0,err = 0;
22021298544Smrg    struct iovec *vec;
22121298544Smrg    n = 0;
22221298544Smrg
22321298544Smrg    /* Could use the WSASend win32 function for scatter/gather i/o but setting up the WSABUF struct from
22421298544Smrg       an iovec would require more work and I'm not sure of the benefit....works for now */
22521298544Smrg    vec = *vector;
22621298544Smrg    while(i < *count)
2271c7386f4Smrg    {
2281c7386f4Smrg         ret = send(c->fd,vec->iov_base,vec->iov_len,0);
22921298544Smrg         if(ret == SOCKET_ERROR)
23021298544Smrg         {
23121298544Smrg             err  = WSAGetLastError();
23221298544Smrg             if(err == WSAEWOULDBLOCK)
23321298544Smrg             {
23421298544Smrg                 return 1;
23521298544Smrg             }
23621298544Smrg         }
23721298544Smrg         n += ret;
23821298544Smrg         *vec++;
23921298544Smrg         i++;
24021298544Smrg    }
24121298544Smrg#else
24221298544Smrg    n = *count;
24321298544Smrg    if (n > IOV_MAX)
2441c7386f4Smrg        n = IOV_MAX;
24521298544Smrg
2461016ad83Smrg#if HAVE_SENDMSG
2471016ad83Smrg    if (c->out.out_fd.nfd) {
2481016ad83Smrg        union {
2491016ad83Smrg            struct cmsghdr cmsghdr;
2501016ad83Smrg            char buf[CMSG_SPACE(XCB_MAX_PASS_FD * sizeof(int))];
2511016ad83Smrg        } cmsgbuf;
2521016ad83Smrg        struct msghdr msg = {
2531016ad83Smrg            .msg_name = NULL,
2541016ad83Smrg            .msg_namelen = 0,
2551016ad83Smrg            .msg_iov = *vector,
2561016ad83Smrg            .msg_iovlen = n,
2571016ad83Smrg            .msg_control = cmsgbuf.buf,
2581016ad83Smrg            .msg_controllen = CMSG_LEN(c->out.out_fd.nfd * sizeof (int)),
2591016ad83Smrg        };
2601016ad83Smrg        int i;
2611016ad83Smrg        struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg);
2621016ad83Smrg
2631016ad83Smrg        hdr->cmsg_len = msg.msg_controllen;
2641016ad83Smrg        hdr->cmsg_level = SOL_SOCKET;
2651016ad83Smrg        hdr->cmsg_type = SCM_RIGHTS;
2661016ad83Smrg        memcpy(CMSG_DATA(hdr), c->out.out_fd.fd, c->out.out_fd.nfd * sizeof (int));
2671016ad83Smrg
2681016ad83Smrg        n = sendmsg(c->fd, &msg, 0);
2691016ad83Smrg        if(n < 0 && errno == EAGAIN)
2701016ad83Smrg            return 1;
2711016ad83Smrg        for (i = 0; i < c->out.out_fd.nfd; i++)
2721016ad83Smrg            close(c->out.out_fd.fd[i]);
2731016ad83Smrg        c->out.out_fd.nfd = 0;
2741016ad83Smrg    } else
2751016ad83Smrg#endif
2761016ad83Smrg    {
2771016ad83Smrg        n = writev(c->fd, *vector, n);
2781016ad83Smrg        if(n < 0 && errno == EAGAIN)
2791016ad83Smrg            return 1;
2801016ad83Smrg    }
2811016ad83Smrg
2821c7386f4Smrg#endif /* _WIN32 */
28321298544Smrg
284602e473dSmrg    if(n <= 0)
285602e473dSmrg    {
28621298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_ERROR);
287602e473dSmrg        return 0;
288602e473dSmrg    }
289602e473dSmrg
290602e473dSmrg    for(; *count; --*count, ++*vector)
291602e473dSmrg    {
292602e473dSmrg        int cur = (*vector)->iov_len;
293602e473dSmrg        if(cur > n)
294602e473dSmrg            cur = n;
295602e473dSmrg        (*vector)->iov_len -= cur;
296602e473dSmrg        (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
297602e473dSmrg        n -= cur;
298602e473dSmrg        if((*vector)->iov_len)
299602e473dSmrg            break;
300602e473dSmrg    }
301602e473dSmrg    if(!*count)
302602e473dSmrg        *vector = 0;
303602e473dSmrg    assert(n == 0);
304602e473dSmrg    return 1;
305602e473dSmrg}
306602e473dSmrg
307602e473dSmrg/* Public interface */
308602e473dSmrg
309602e473dSmrgconst xcb_setup_t *xcb_get_setup(xcb_connection_t *c)
310602e473dSmrg{
3117204935cSmrg    if(is_static_error_conn(c))
3127204935cSmrg        return &xcb_error_setup;
313602e473dSmrg    /* doesn't need locking because it's never written to. */
314602e473dSmrg    return c->setup;
315602e473dSmrg}
316602e473dSmrg
317602e473dSmrgint xcb_get_file_descriptor(xcb_connection_t *c)
318602e473dSmrg{
3197204935cSmrg    if(is_static_error_conn(c))
320602e473dSmrg        return -1;
321602e473dSmrg    /* doesn't need locking because it's never written to. */
322602e473dSmrg    return c->fd;
323602e473dSmrg}
324602e473dSmrg
325602e473dSmrgint xcb_connection_has_error(xcb_connection_t *c)
326602e473dSmrg{
327602e473dSmrg    /* doesn't need locking because it's read and written atomically. */
328602e473dSmrg    return c->has_error;
329602e473dSmrg}
330602e473dSmrg
331602e473dSmrgxcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
332602e473dSmrg{
333602e473dSmrg    xcb_connection_t* c;
334602e473dSmrg
33521298544Smrg#ifndef _WIN32
336602e473dSmrg#ifndef USE_POLL
337602e473dSmrg    if(fd >= FD_SETSIZE) /* would overflow in FD_SET */
338602e473dSmrg    {
339602e473dSmrg        close(fd);
34021298544Smrg        return _xcb_conn_ret_error(XCB_CONN_ERROR);
341602e473dSmrg    }
342602e473dSmrg#endif
34321298544Smrg#endif /* !_WIN32*/
344602e473dSmrg
345602e473dSmrg    c = calloc(1, sizeof(xcb_connection_t));
346602e473dSmrg    if(!c) {
347602e473dSmrg        close(fd);
34821298544Smrg        return _xcb_conn_ret_error(XCB_CONN_CLOSED_MEM_INSUFFICIENT) ;
349602e473dSmrg    }
350602e473dSmrg
351602e473dSmrg    c->fd = fd;
352602e473dSmrg
353602e473dSmrg    if(!(
354602e473dSmrg        set_fd_flags(fd) &&
355602e473dSmrg        pthread_mutex_init(&c->iolock, 0) == 0 &&
356602e473dSmrg        _xcb_in_init(&c->in) &&
357602e473dSmrg        _xcb_out_init(&c->out) &&
358602e473dSmrg        write_setup(c, auth_info) &&
359602e473dSmrg        read_setup(c) &&
360602e473dSmrg        _xcb_ext_init(c) &&
361602e473dSmrg        _xcb_xid_init(c)
362602e473dSmrg        ))
363602e473dSmrg    {
364602e473dSmrg        xcb_disconnect(c);
36521298544Smrg        return _xcb_conn_ret_error(XCB_CONN_ERROR);
366602e473dSmrg    }
367602e473dSmrg
368602e473dSmrg    return c;
369602e473dSmrg}
370602e473dSmrg
371602e473dSmrgvoid xcb_disconnect(xcb_connection_t *c)
372602e473dSmrg{
3731c7386f4Smrg    if(c == NULL || is_static_error_conn(c))
374602e473dSmrg        return;
375602e473dSmrg
376602e473dSmrg    free(c->setup);
37721298544Smrg
37821298544Smrg    /* disallow further sends and receives */
37921298544Smrg    shutdown(c->fd, SHUT_RDWR);
380602e473dSmrg    close(c->fd);
381602e473dSmrg
382602e473dSmrg    pthread_mutex_destroy(&c->iolock);
383602e473dSmrg    _xcb_in_destroy(&c->in);
384602e473dSmrg    _xcb_out_destroy(&c->out);
385602e473dSmrg
386602e473dSmrg    _xcb_ext_destroy(c);
387602e473dSmrg    _xcb_xid_destroy(c);
388602e473dSmrg
389602e473dSmrg    free(c);
39021298544Smrg
39121298544Smrg#ifdef _WIN32
39221298544Smrg    WSACleanup();
39321298544Smrg#endif
394602e473dSmrg}
395602e473dSmrg
396602e473dSmrg/* Private interface */
397602e473dSmrg
39821298544Smrgvoid _xcb_conn_shutdown(xcb_connection_t *c, int err)
399602e473dSmrg{
40021298544Smrg    c->has_error = err;
40121298544Smrg}
40221298544Smrg
40321298544Smrg/* Return connection error state.
40421298544Smrg * To make thread-safe, I need a seperate static
40521298544Smrg * variable for every possible error.
4061c7386f4Smrg * has_error is the first field in xcb_connection_t, so just
4071c7386f4Smrg * return a casted int here; checking has_error (and only
4081c7386f4Smrg * has_error) will be safe.
40921298544Smrg */
41021298544Smrgxcb_connection_t *_xcb_conn_ret_error(int err)
41121298544Smrg{
41221298544Smrg
41321298544Smrg    switch(err)
41421298544Smrg    {
41521298544Smrg        case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
41621298544Smrg        {
41721298544Smrg            return (xcb_connection_t *) &xcb_con_closed_mem_er;
41821298544Smrg        }
41921298544Smrg        case XCB_CONN_CLOSED_PARSE_ERR:
42021298544Smrg        {
42121298544Smrg            return (xcb_connection_t *) &xcb_con_closed_parse_er;
42221298544Smrg        }
42321298544Smrg        case XCB_CONN_CLOSED_INVALID_SCREEN:
42421298544Smrg        {
42521298544Smrg            return (xcb_connection_t *) &xcb_con_closed_screen_er;
42621298544Smrg        }
42721298544Smrg        case XCB_CONN_ERROR:
42821298544Smrg        default:
42921298544Smrg        {
43021298544Smrg            return (xcb_connection_t *) &xcb_con_error;
43121298544Smrg        }
43221298544Smrg    }
433602e473dSmrg}
434602e473dSmrg
435602e473dSmrgint _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
436602e473dSmrg{
437602e473dSmrg    int ret;
438602e473dSmrg#if USE_POLL
439602e473dSmrg    struct pollfd fd;
440602e473dSmrg#else
441602e473dSmrg    fd_set rfds, wfds;
442602e473dSmrg#endif
443602e473dSmrg
444602e473dSmrg    /* If the thing I should be doing is already being done, wait for it. */
445602e473dSmrg    if(count ? c->out.writing : c->in.reading)
446602e473dSmrg    {
447602e473dSmrg        pthread_cond_wait(cond, &c->iolock);
448602e473dSmrg        return 1;
449602e473dSmrg    }
450602e473dSmrg
451602e473dSmrg#if USE_POLL
452602e473dSmrg    memset(&fd, 0, sizeof(fd));
453602e473dSmrg    fd.fd = c->fd;
454602e473dSmrg    fd.events = POLLIN;
455602e473dSmrg#else
456602e473dSmrg    FD_ZERO(&rfds);
457602e473dSmrg    FD_SET(c->fd, &rfds);
458602e473dSmrg#endif
459602e473dSmrg    ++c->in.reading;
460602e473dSmrg
461602e473dSmrg#if USE_POLL
462602e473dSmrg    if(count)
463602e473dSmrg    {
464602e473dSmrg        fd.events |= POLLOUT;
465602e473dSmrg        ++c->out.writing;
466602e473dSmrg    }
467602e473dSmrg#else
468602e473dSmrg    FD_ZERO(&wfds);
469602e473dSmrg    if(count)
470602e473dSmrg    {
471602e473dSmrg        FD_SET(c->fd, &wfds);
472602e473dSmrg        ++c->out.writing;
473602e473dSmrg    }
474602e473dSmrg#endif
475602e473dSmrg
476602e473dSmrg    pthread_mutex_unlock(&c->iolock);
477602e473dSmrg    do {
478602e473dSmrg#if USE_POLL
479b20a2039Smrg        ret = poll(&fd, 1, -1);
48021298544Smrg        /* If poll() returns an event we didn't expect, such as POLLNVAL, treat
48121298544Smrg         * it as if it failed. */
48221298544Smrg        if(ret >= 0 && (fd.revents & ~fd.events))
48321298544Smrg        {
48421298544Smrg            ret = -1;
48521298544Smrg            break;
48621298544Smrg        }
487602e473dSmrg#else
488b20a2039Smrg        ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
489602e473dSmrg#endif
490602e473dSmrg    } while (ret == -1 && errno == EINTR);
491b20a2039Smrg    if(ret < 0)
492602e473dSmrg    {
49321298544Smrg        _xcb_conn_shutdown(c, XCB_CONN_ERROR);
494b20a2039Smrg        ret = 0;
495602e473dSmrg    }
496602e473dSmrg    pthread_mutex_lock(&c->iolock);
497602e473dSmrg
498602e473dSmrg    if(ret)
499602e473dSmrg    {
50021298544Smrg        /* The code allows two threads to call select()/poll() at the same time.
50121298544Smrg         * First thread just wants to read, a second thread wants to write, too.
50221298544Smrg         * We have to make sure that we don't steal the reading thread's reply
50321298544Smrg         * and let it get stuck in select()/poll().
50421298544Smrg         * So a thread may read if either:
50521298544Smrg         * - There is no other thread that wants to read (the above situation
50621298544Smrg         *   did not occur).
50721298544Smrg         * - It is the reading thread (above situation occurred).
50821298544Smrg         */
50921298544Smrg        int may_read = c->in.reading == 1 || !count;
510602e473dSmrg#if USE_POLL
5111016ad83Smrg        if(may_read && (fd.revents & POLLIN) != 0)
512602e473dSmrg#else
51321298544Smrg        if(may_read && FD_ISSET(c->fd, &rfds))
514602e473dSmrg#endif
515602e473dSmrg            ret = ret && _xcb_in_read(c);
516602e473dSmrg
517602e473dSmrg#if USE_POLL
5181016ad83Smrg        if((fd.revents & POLLOUT) != 0)
519602e473dSmrg#else
520602e473dSmrg        if(FD_ISSET(c->fd, &wfds))
521602e473dSmrg#endif
522602e473dSmrg            ret = ret && write_vec(c, vector, count);
523602e473dSmrg    }
524602e473dSmrg
525602e473dSmrg    if(count)
526602e473dSmrg        --c->out.writing;
527602e473dSmrg    --c->in.reading;
528602e473dSmrg
529602e473dSmrg    return ret;
530602e473dSmrg}
531