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