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