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