xcb_connect_to_fd: fix descriptor leak on memory error path
[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 typedef struct {
46     uint8_t  status;
47     uint8_t  pad0[5];
48     uint16_t length;
49 } xcb_setup_generic_t;
50
51 static const int error_connection = 1;
52
53 static 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
66 static 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
113 static 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. */
155 static 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
187 const 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
195 int 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
203 int 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
209 xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
210 {
211     xcb_connection_t* c;
212
213     c = calloc(1, sizeof(xcb_connection_t));
214     if(!c) {
215         close(fd);
216         return (xcb_connection_t *) &error_connection;
217     }
218
219     c->fd = fd;
220
221     if(!(
222         set_fd_flags(fd) &&
223         pthread_mutex_init(&c->iolock, 0) == 0 &&
224         _xcb_in_init(&c->in) &&
225         _xcb_out_init(&c->out) &&
226         write_setup(c, auth_info) &&
227         read_setup(c) &&
228         _xcb_ext_init(c) &&
229         _xcb_xid_init(c)
230         ))
231     {
232         xcb_disconnect(c);
233         return (xcb_connection_t *) &error_connection;
234     }
235
236     return c;
237 }
238
239 void xcb_disconnect(xcb_connection_t *c)
240 {
241     if(c->has_error)
242         return;
243
244     free(c->setup);
245     close(c->fd);
246
247     pthread_mutex_destroy(&c->iolock);
248     _xcb_in_destroy(&c->in);
249     _xcb_out_destroy(&c->out);
250
251     _xcb_ext_destroy(c);
252     _xcb_xid_destroy(c);
253
254     free(c);
255 }
256
257 /* Private interface */
258
259 void _xcb_conn_shutdown(xcb_connection_t *c)
260 {
261     c->has_error = 1;
262 }
263
264 int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
265 {
266     int ret;
267 #if USE_POLL
268     struct pollfd fd;
269 #else
270     fd_set rfds, wfds;
271 #endif
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 #if USE_POLL
281     memset(&fd, 0, sizeof(fd));
282     fd.fd = c->fd;
283     fd.events = POLLIN;
284 #else
285     FD_ZERO(&rfds);
286     FD_SET(c->fd, &rfds);
287 #endif
288     ++c->in.reading;
289
290 #if USE_POLL
291     if(count)
292     {
293         fd.events |= POLLOUT;
294         ++c->out.writing;
295     }
296 #else
297     FD_ZERO(&wfds);
298     if(count)
299     {
300         FD_SET(c->fd, &wfds);
301         ++c->out.writing;
302     }
303 #endif
304
305     pthread_mutex_unlock(&c->iolock);
306     do {
307 #if USE_POLL
308     ret = poll(&fd, 1, -1);
309 #else
310         ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
311 #endif
312     } while (ret == -1 && errno == EINTR);
313     if (ret < 0)
314     {
315         _xcb_conn_shutdown(c);
316         ret = 0;
317     }
318     pthread_mutex_lock(&c->iolock);
319
320     if(ret)
321     {
322 #if USE_POLL
323         if((fd.revents & POLLIN) == POLLIN)
324 #else
325         if(FD_ISSET(c->fd, &rfds))
326 #endif
327             ret = ret && _xcb_in_read(c);
328
329 #if USE_POLL
330         if((fd.revents & POLLOUT) == POLLOUT)
331 #else
332         if(FD_ISSET(c->fd, &wfds))
333 #endif
334             ret = ret && write_vec(c, vector, count);
335     }
336
337     if(count)
338         --c->out.writing;
339     --c->in.reading;
340
341     return ret;
342 }