Don't abort() on locking assertions if LIBXCB_ALLOW_SLOPPY_LOCK is set.
[free-sw/xcb/libxcb] / src / xcb_conn.c
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 <sys/select.h>
35 #include <fcntl.h>
36 #include <errno.h>
37
38 #include "xcb.h"
39 #include "xcbint.h"
40
41 typedef struct {
42     uint8_t  status;
43     uint8_t  pad0[5];
44     uint16_t length;
45 } xcb_setup_generic_t;
46
47 static const int error_connection = 1;
48
49 static int set_fd_flags(const int fd)
50 {
51     int flags = fcntl(fd, F_GETFL, 0);
52     if(flags == -1)
53         return 0;
54     flags |= O_NONBLOCK;
55     if(fcntl(fd, F_SETFL, flags) == -1)
56         return 0;
57     if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
58         return 0;
59     return 1;
60 }
61
62 static int _xcb_xlib_init(_xcb_xlib *xlib)
63 {
64     xlib->lock = 0;
65 #ifndef NDEBUG
66     xlib->sloppy_lock = (getenv("LIBXCB_ALLOW_SLOPPY_LOCK") != 0);
67 #endif
68     pthread_cond_init(&xlib->cond, 0);
69     return 1;
70 }
71
72 static void _xcb_xlib_destroy(_xcb_xlib *xlib)
73 {
74     pthread_cond_destroy(&xlib->cond);
75 }
76
77 static int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info)
78 {
79     static const char pad[3];
80     xcb_setup_request_t out;
81     struct iovec parts[6];
82     int count = 0;
83     int endian = 0x01020304;
84     int ret;
85
86     memset(&out, 0, sizeof(out));
87
88     /* B = 0x42 = MSB first, l = 0x6c = LSB first */
89     if(htonl(endian) == endian)
90         out.byte_order = 0x42;
91     else
92         out.byte_order = 0x6c;
93     out.protocol_major_version = X_PROTOCOL;
94     out.protocol_minor_version = X_PROTOCOL_REVISION;
95     out.authorization_protocol_name_len = 0;
96     out.authorization_protocol_data_len = 0;
97     parts[count].iov_len = sizeof(xcb_setup_request_t);
98     parts[count++].iov_base = &out;
99     parts[count].iov_len = XCB_PAD(sizeof(xcb_setup_request_t));
100     parts[count++].iov_base = (char *) pad;
101
102     if(auth_info)
103     {
104         parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
105         parts[count++].iov_base = auth_info->name;
106         parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len);
107         parts[count++].iov_base = (char *) pad;
108         parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
109         parts[count++].iov_base = auth_info->data;
110         parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len);
111         parts[count++].iov_base = (char *) pad;
112     }
113     assert(count <= sizeof(parts) / sizeof(*parts));
114
115     _xcb_lock_io(c);
116     {
117         struct iovec *parts_ptr = parts;
118         ret = _xcb_out_send(c, &parts_ptr, &count);
119     }
120     _xcb_unlock_io(c);
121     return ret;
122 }
123
124 static int read_setup(xcb_connection_t *c)
125 {
126     /* Read the server response */
127     c->setup = malloc(sizeof(xcb_setup_generic_t));
128     if(!c->setup)
129         return 0;
130
131     if(_xcb_in_read_block(c, c->setup, sizeof(xcb_setup_generic_t)) != sizeof(xcb_setup_generic_t))
132         return 0;
133
134     {
135         void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(xcb_setup_generic_t));
136         if(!tmp)
137             return 0;
138         c->setup = tmp;
139     }
140
141     if(_xcb_in_read_block(c, (char *) c->setup + sizeof(xcb_setup_generic_t), c->setup->length * 4) <= 0)
142         return 0;
143
144     /* 0 = failed, 2 = authenticate, 1 = success */
145     switch(c->setup->status)
146     {
147     case 0: /* failed */
148         {
149             xcb_setup_failed_t *setup = (xcb_setup_failed_t *) c->setup;
150             write(STDERR_FILENO, xcb_setup_failed_reason(setup), xcb_setup_failed_reason_length(setup));
151             return 0;
152         }
153
154     case 2: /* authenticate */
155         {
156             xcb_setup_authenticate_t *setup = (xcb_setup_authenticate_t *) c->setup;
157             write(STDERR_FILENO, xcb_setup_authenticate_reason(setup), xcb_setup_authenticate_reason_length(setup));
158             return 0;
159         }
160     }
161
162     return 1;
163 }
164
165 /* precondition: there must be something for us to write. */
166 static int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
167 {
168     int n;
169     assert(!c->out.queue_len);
170     n = writev(c->fd, *vector, *count);
171     if(n < 0 && errno == EAGAIN)
172         return 1;
173     if(n <= 0)
174     {
175         _xcb_conn_shutdown(c);
176         return 0;
177     }
178
179     for(; *count; --*count, ++*vector)
180     {
181         int cur = (*vector)->iov_len;
182         if(cur > n)
183             cur = n;
184         (*vector)->iov_len -= cur;
185         (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
186         n -= cur;
187         if((*vector)->iov_len)
188             break;
189     }
190     if(!*count)
191         *vector = 0;
192     assert(n == 0);
193     return 1;
194 }
195
196 /* Public interface */
197
198 const xcb_setup_t *xcb_get_setup(xcb_connection_t *c)
199 {
200     if(c->has_error)
201         return 0;
202     /* doesn't need locking because it's never written to. */
203     return c->setup;
204 }
205
206 int xcb_get_file_descriptor(xcb_connection_t *c)
207 {
208     if(c->has_error)
209         return -1;
210     /* doesn't need locking because it's never written to. */
211     return c->fd;
212 }
213
214 int xcb_connection_has_error(xcb_connection_t *c)
215 {
216     /* doesn't need locking because it's read and written atomically. */
217     return c->has_error;
218 }
219
220 xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
221 {
222     xcb_connection_t* c;
223
224     c = calloc(1, sizeof(xcb_connection_t));
225     if(!c)
226         return (xcb_connection_t *) &error_connection;
227
228     c->fd = fd;
229
230     if(!(
231         set_fd_flags(fd) &&
232         pthread_mutex_init(&c->iolock, 0) == 0 &&
233         _xcb_xlib_init(&c->xlib) &&
234         _xcb_in_init(&c->in) &&
235         _xcb_out_init(&c->out) &&
236         write_setup(c, auth_info) &&
237         read_setup(c) &&
238         _xcb_ext_init(c) &&
239         _xcb_xid_init(c)
240         ))
241     {
242         xcb_disconnect(c);
243         return (xcb_connection_t *) &error_connection;
244     }
245
246     return c;
247 }
248
249 void xcb_disconnect(xcb_connection_t *c)
250 {
251     if(c->has_error)
252         return;
253
254     free(c->setup);
255     close(c->fd);
256
257     pthread_mutex_destroy(&c->iolock);
258     _xcb_xlib_destroy(&c->xlib);
259     _xcb_in_destroy(&c->in);
260     _xcb_out_destroy(&c->out);
261
262     _xcb_ext_destroy(c);
263     _xcb_xid_destroy(c);
264
265     free(c);
266 }
267
268 /* Private interface */
269
270 void _xcb_conn_shutdown(xcb_connection_t *c)
271 {
272     c->has_error = 1;
273 }
274
275 void _xcb_lock_io(xcb_connection_t *c)
276 {
277     pthread_mutex_lock(&c->iolock);
278     while(c->xlib.lock)
279     {
280         if(pthread_equal(c->xlib.thread, pthread_self()))
281             break;
282         pthread_cond_wait(&c->xlib.cond, &c->iolock);
283     }
284 }
285
286 void _xcb_unlock_io(xcb_connection_t *c)
287 {
288     pthread_mutex_unlock(&c->iolock);
289 }
290
291 int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
292 {
293     int ret;
294     fd_set rfds, wfds;
295
296     /* If the thing I should be doing is already being done, wait for it. */
297     if(count ? c->out.writing : c->in.reading)
298     {
299         pthread_cond_wait(cond, &c->iolock);
300         return 1;
301     }
302
303     FD_ZERO(&rfds);
304     FD_SET(c->fd, &rfds);
305     ++c->in.reading;
306
307     FD_ZERO(&wfds);
308     if(count)
309     {
310         FD_SET(c->fd, &wfds);
311         ++c->out.writing;
312     }
313
314     _xcb_unlock_io(c);
315     do {
316         ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
317     } while (ret == -1 && errno == EINTR);
318     if (ret < 0)
319     {
320         _xcb_conn_shutdown(c);
321         ret = 0;
322     }
323     _xcb_lock_io(c);
324
325     if(ret)
326     {
327         if(FD_ISSET(c->fd, &rfds))
328             ret = ret && _xcb_in_read(c);
329
330         if(FD_ISSET(c->fd, &wfds))
331             ret = ret && write_vec(c, vector, count);
332     }
333
334     if(count)
335         --c->out.writing;
336     --c->in.reading;
337
338     return ret;
339 }