xcb_conn.c revision b20a2039
1/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * Except as contained in this notice, the names of the authors or their
21 * institutions shall not be used in advertising or otherwise to promote the
22 * sale, use or other dealings in this Software without prior written
23 * authorization from the authors.
24 */
25
26/* Connection management: the core of XCB. */
27
28#include <assert.h>
29#include <string.h>
30#include <stdio.h>
31#include <unistd.h>
32#include <stdlib.h>
33#include <netinet/in.h>
34#include <fcntl.h>
35#include <errno.h>
36
37#include "xcb.h"
38#include "xcbint.h"
39#if USE_POLL
40#include <poll.h>
41#else
42#include <sys/select.h>
43#endif
44
45typedef struct {
46    uint8_t  status;
47    uint8_t  pad0[5];
48    uint16_t length;
49} xcb_setup_generic_t;
50
51static const int error_connection = 1;
52
53static int set_fd_flags(const int fd)
54{
55    int flags = fcntl(fd, F_GETFL, 0);
56    if(flags == -1)
57        return 0;
58    flags |= O_NONBLOCK;
59    if(fcntl(fd, F_SETFL, flags) == -1)
60        return 0;
61    if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
62        return 0;
63    return 1;
64}
65
66static int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info)
67{
68    static const char pad[3];
69    xcb_setup_request_t out;
70    struct iovec parts[6];
71    int count = 0;
72    static const uint32_t endian = 0x01020304;
73    int ret;
74
75    memset(&out, 0, sizeof(out));
76
77    /* B = 0x42 = MSB first, l = 0x6c = LSB first */
78    if(htonl(endian) == endian)
79        out.byte_order = 0x42;
80    else
81        out.byte_order = 0x6c;
82    out.protocol_major_version = X_PROTOCOL;
83    out.protocol_minor_version = X_PROTOCOL_REVISION;
84    out.authorization_protocol_name_len = 0;
85    out.authorization_protocol_data_len = 0;
86    parts[count].iov_len = sizeof(xcb_setup_request_t);
87    parts[count++].iov_base = &out;
88    parts[count].iov_len = XCB_PAD(sizeof(xcb_setup_request_t));
89    parts[count++].iov_base = (char *) pad;
90
91    if(auth_info)
92    {
93        parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
94        parts[count++].iov_base = auth_info->name;
95        parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len);
96        parts[count++].iov_base = (char *) pad;
97        parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
98        parts[count++].iov_base = auth_info->data;
99        parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len);
100        parts[count++].iov_base = (char *) pad;
101    }
102    assert(count <= (int) (sizeof(parts) / sizeof(*parts)));
103
104    pthread_mutex_lock(&c->iolock);
105    ret = _xcb_out_send(c, parts, count);
106    pthread_mutex_unlock(&c->iolock);
107    return ret;
108}
109
110static int read_setup(xcb_connection_t *c)
111{
112    /* Read the server response */
113    c->setup = malloc(sizeof(xcb_setup_generic_t));
114    if(!c->setup)
115        return 0;
116
117    if(_xcb_in_read_block(c, c->setup, sizeof(xcb_setup_generic_t)) != sizeof(xcb_setup_generic_t))
118        return 0;
119
120    {
121        void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(xcb_setup_generic_t));
122        if(!tmp)
123            return 0;
124        c->setup = tmp;
125    }
126
127    if(_xcb_in_read_block(c, (char *) c->setup + sizeof(xcb_setup_generic_t), c->setup->length * 4) <= 0)
128        return 0;
129
130    /* 0 = failed, 2 = authenticate, 1 = success */
131    switch(c->setup->status)
132    {
133    case 0: /* failed */
134        {
135            xcb_setup_failed_t *setup = (xcb_setup_failed_t *) c->setup;
136            write(STDERR_FILENO, xcb_setup_failed_reason(setup), xcb_setup_failed_reason_length(setup));
137            return 0;
138        }
139
140    case 2: /* authenticate */
141        {
142            xcb_setup_authenticate_t *setup = (xcb_setup_authenticate_t *) c->setup;
143            write(STDERR_FILENO, xcb_setup_authenticate_reason(setup), xcb_setup_authenticate_reason_length(setup));
144            return 0;
145        }
146    }
147
148    return 1;
149}
150
151/* precondition: there must be something for us to write. */
152static int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
153{
154    int n;
155    assert(!c->out.queue_len);
156    n = writev(c->fd, *vector, *count);
157    if(n < 0 && errno == EAGAIN)
158        return 1;
159    if(n <= 0)
160    {
161        _xcb_conn_shutdown(c);
162        return 0;
163    }
164
165    for(; *count; --*count, ++*vector)
166    {
167        int cur = (*vector)->iov_len;
168        if(cur > n)
169            cur = n;
170        (*vector)->iov_len -= cur;
171        (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
172        n -= cur;
173        if((*vector)->iov_len)
174            break;
175    }
176    if(!*count)
177        *vector = 0;
178    assert(n == 0);
179    return 1;
180}
181
182/* Public interface */
183
184const xcb_setup_t *xcb_get_setup(xcb_connection_t *c)
185{
186    if(c->has_error)
187        return 0;
188    /* doesn't need locking because it's never written to. */
189    return c->setup;
190}
191
192int xcb_get_file_descriptor(xcb_connection_t *c)
193{
194    if(c->has_error)
195        return -1;
196    /* doesn't need locking because it's never written to. */
197    return c->fd;
198}
199
200int xcb_connection_has_error(xcb_connection_t *c)
201{
202    /* doesn't need locking because it's read and written atomically. */
203    return c->has_error;
204}
205
206xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
207{
208    xcb_connection_t* c;
209
210#ifndef USE_POLL
211    if(fd >= FD_SETSIZE) /* would overflow in FD_SET */
212    {
213        close(fd);
214        return (xcb_connection_t *) &error_connection;
215    }
216#endif
217
218    c = calloc(1, sizeof(xcb_connection_t));
219    if(!c) {
220        close(fd);
221        return (xcb_connection_t *) &error_connection;
222    }
223
224    c->fd = fd;
225
226    if(!(
227        set_fd_flags(fd) &&
228        pthread_mutex_init(&c->iolock, 0) == 0 &&
229        _xcb_in_init(&c->in) &&
230        _xcb_out_init(&c->out) &&
231        write_setup(c, auth_info) &&
232        read_setup(c) &&
233        _xcb_ext_init(c) &&
234        _xcb_xid_init(c)
235        ))
236    {
237        xcb_disconnect(c);
238        return (xcb_connection_t *) &error_connection;
239    }
240
241    return c;
242}
243
244void xcb_disconnect(xcb_connection_t *c)
245{
246    if(c->has_error)
247        return;
248
249    free(c->setup);
250    close(c->fd);
251
252    pthread_mutex_destroy(&c->iolock);
253    _xcb_in_destroy(&c->in);
254    _xcb_out_destroy(&c->out);
255
256    _xcb_ext_destroy(c);
257    _xcb_xid_destroy(c);
258
259    free(c);
260}
261
262/* Private interface */
263
264void _xcb_conn_shutdown(xcb_connection_t *c)
265{
266    c->has_error = 1;
267}
268
269int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
270{
271    int ret;
272#if USE_POLL
273    struct pollfd fd;
274#else
275    fd_set rfds, wfds;
276#endif
277
278    /* If the thing I should be doing is already being done, wait for it. */
279    if(count ? c->out.writing : c->in.reading)
280    {
281        pthread_cond_wait(cond, &c->iolock);
282        return 1;
283    }
284
285#if USE_POLL
286    memset(&fd, 0, sizeof(fd));
287    fd.fd = c->fd;
288    fd.events = POLLIN;
289#else
290    FD_ZERO(&rfds);
291    FD_SET(c->fd, &rfds);
292#endif
293    ++c->in.reading;
294
295#if USE_POLL
296    if(count)
297    {
298        fd.events |= POLLOUT;
299        ++c->out.writing;
300    }
301#else
302    FD_ZERO(&wfds);
303    if(count)
304    {
305        FD_SET(c->fd, &wfds);
306        ++c->out.writing;
307    }
308#endif
309
310    pthread_mutex_unlock(&c->iolock);
311    do {
312#if USE_POLL
313        ret = poll(&fd, 1, -1);
314#else
315        ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
316#endif
317    } while (ret == -1 && errno == EINTR);
318    if(ret < 0)
319    {
320        _xcb_conn_shutdown(c);
321        ret = 0;
322    }
323    pthread_mutex_lock(&c->iolock);
324
325    if(ret)
326    {
327#if USE_POLL
328        if((fd.revents & POLLIN) == POLLIN)
329#else
330        if(FD_ISSET(c->fd, &rfds))
331#endif
332            ret = ret && _xcb_in_read(c);
333
334#if USE_POLL
335        if((fd.revents & POLLOUT) == POLLOUT)
336#else
337        if(FD_ISSET(c->fd, &wfds))
338#endif
339            ret = ret && write_vec(c, vector, count);
340    }
341
342    if(count)
343        --c->out.writing;
344    --c->in.reading;
345
346    return ret;
347}
348