xcb_conn.c revision 602e473d
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    {
106        struct iovec *parts_ptr = parts;
107        ret = _xcb_out_send(c, &parts_ptr, &count);
108    }
109    pthread_mutex_unlock(&c->iolock);
110    return ret;
111}
112
113static int read_setup(xcb_connection_t *c)
114{
115    /* Read the server response */
116    c->setup = malloc(sizeof(xcb_setup_generic_t));
117    if(!c->setup)
118        return 0;
119
120    if(_xcb_in_read_block(c, c->setup, sizeof(xcb_setup_generic_t)) != sizeof(xcb_setup_generic_t))
121        return 0;
122
123    {
124        void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(xcb_setup_generic_t));
125        if(!tmp)
126            return 0;
127        c->setup = tmp;
128    }
129
130    if(_xcb_in_read_block(c, (char *) c->setup + sizeof(xcb_setup_generic_t), c->setup->length * 4) <= 0)
131        return 0;
132
133    /* 0 = failed, 2 = authenticate, 1 = success */
134    switch(c->setup->status)
135    {
136    case 0: /* failed */
137        {
138            xcb_setup_failed_t *setup = (xcb_setup_failed_t *) c->setup;
139            write(STDERR_FILENO, xcb_setup_failed_reason(setup), xcb_setup_failed_reason_length(setup));
140            return 0;
141        }
142
143    case 2: /* authenticate */
144        {
145            xcb_setup_authenticate_t *setup = (xcb_setup_authenticate_t *) c->setup;
146            write(STDERR_FILENO, xcb_setup_authenticate_reason(setup), xcb_setup_authenticate_reason_length(setup));
147            return 0;
148        }
149    }
150
151    return 1;
152}
153
154/* precondition: there must be something for us to write. */
155static int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
156{
157    int n;
158    assert(!c->out.queue_len);
159    n = writev(c->fd, *vector, *count);
160    if(n < 0 && errno == EAGAIN)
161        return 1;
162    if(n <= 0)
163    {
164        _xcb_conn_shutdown(c);
165        return 0;
166    }
167
168    for(; *count; --*count, ++*vector)
169    {
170        int cur = (*vector)->iov_len;
171        if(cur > n)
172            cur = n;
173        (*vector)->iov_len -= cur;
174        (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
175        n -= cur;
176        if((*vector)->iov_len)
177            break;
178    }
179    if(!*count)
180        *vector = 0;
181    assert(n == 0);
182    return 1;
183}
184
185/* Public interface */
186
187const xcb_setup_t *xcb_get_setup(xcb_connection_t *c)
188{
189    if(c->has_error)
190        return 0;
191    /* doesn't need locking because it's never written to. */
192    return c->setup;
193}
194
195int xcb_get_file_descriptor(xcb_connection_t *c)
196{
197    if(c->has_error)
198        return -1;
199    /* doesn't need locking because it's never written to. */
200    return c->fd;
201}
202
203int xcb_connection_has_error(xcb_connection_t *c)
204{
205    /* doesn't need locking because it's read and written atomically. */
206    return c->has_error;
207}
208
209xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
210{
211    xcb_connection_t* c;
212
213#ifndef USE_POLL
214    if(fd >= FD_SETSIZE) /* would overflow in FD_SET */
215    {
216        close(fd);
217        return (xcb_connection_t *) &error_connection;
218    }
219#endif
220
221    c = calloc(1, sizeof(xcb_connection_t));
222    if(!c) {
223        close(fd);
224        return (xcb_connection_t *) &error_connection;
225    }
226
227    c->fd = fd;
228
229    if(!(
230        set_fd_flags(fd) &&
231        pthread_mutex_init(&c->iolock, 0) == 0 &&
232        _xcb_in_init(&c->in) &&
233        _xcb_out_init(&c->out) &&
234        write_setup(c, auth_info) &&
235        read_setup(c) &&
236        _xcb_ext_init(c) &&
237        _xcb_xid_init(c)
238        ))
239    {
240        xcb_disconnect(c);
241        return (xcb_connection_t *) &error_connection;
242    }
243
244    return c;
245}
246
247void xcb_disconnect(xcb_connection_t *c)
248{
249    if(c->has_error)
250        return;
251
252    free(c->setup);
253    close(c->fd);
254
255    pthread_mutex_destroy(&c->iolock);
256    _xcb_in_destroy(&c->in);
257    _xcb_out_destroy(&c->out);
258
259    _xcb_ext_destroy(c);
260    _xcb_xid_destroy(c);
261
262    free(c);
263}
264
265/* Private interface */
266
267void _xcb_conn_shutdown(xcb_connection_t *c)
268{
269    c->has_error = 1;
270}
271
272int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
273{
274    int ret;
275#if USE_POLL
276    struct pollfd fd;
277#else
278    fd_set rfds, wfds;
279#endif
280
281    /* If the thing I should be doing is already being done, wait for it. */
282    if(count ? c->out.writing : c->in.reading)
283    {
284        pthread_cond_wait(cond, &c->iolock);
285        return 1;
286    }
287
288#if USE_POLL
289    memset(&fd, 0, sizeof(fd));
290    fd.fd = c->fd;
291    fd.events = POLLIN;
292#else
293    FD_ZERO(&rfds);
294    FD_SET(c->fd, &rfds);
295#endif
296    ++c->in.reading;
297
298#if USE_POLL
299    if(count)
300    {
301        fd.events |= POLLOUT;
302        ++c->out.writing;
303    }
304#else
305    FD_ZERO(&wfds);
306    if(count)
307    {
308        FD_SET(c->fd, &wfds);
309        ++c->out.writing;
310    }
311#endif
312
313    pthread_mutex_unlock(&c->iolock);
314    do {
315#if USE_POLL
316    ret = poll(&fd, 1, -1);
317#else
318	ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
319#endif
320    } while (ret == -1 && errno == EINTR);
321    if (ret < 0)
322    {
323        _xcb_conn_shutdown(c);
324	ret = 0;
325    }
326    pthread_mutex_lock(&c->iolock);
327
328    if(ret)
329    {
330#if USE_POLL
331        if((fd.revents & POLLIN) == POLLIN)
332#else
333        if(FD_ISSET(c->fd, &rfds))
334#endif
335            ret = ret && _xcb_in_read(c);
336
337#if USE_POLL
338        if((fd.revents & POLLOUT) == POLLOUT)
339#else
340        if(FD_ISSET(c->fd, &wfds))
341#endif
342            ret = ret && write_vec(c, vector, count);
343    }
344
345    if(count)
346        --c->out.writing;
347    --c->in.reading;
348
349    return ret;
350}
351