1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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.
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.
26 /* Connection management: the core of XCB. */
46 #include <sys/select.h>
50 #include "xcb_windefs.h"
52 #include <sys/socket.h>
53 #include <netinet/in.h>
56 /* SHUT_RDWR is fairly recent and is not available on all platforms */
57 #if !defined(SHUT_RDWR)
65 } xcb_setup_generic_t;
67 /* Keep this list in sync with is_static_error_conn()! */
68 static const int xcb_con_error = XCB_CONN_ERROR;
69 static const int xcb_con_closed_mem_er = XCB_CONN_CLOSED_MEM_INSUFFICIENT;
70 static const int xcb_con_closed_parse_er = XCB_CONN_CLOSED_PARSE_ERR;
71 static const int xcb_con_closed_screen_er = XCB_CONN_CLOSED_INVALID_SCREEN;
73 static int is_static_error_conn(xcb_connection_t *c)
75 return c == (xcb_connection_t *) &xcb_con_error ||
76 c == (xcb_connection_t *) &xcb_con_closed_mem_er ||
77 c == (xcb_connection_t *) &xcb_con_closed_parse_er ||
78 c == (xcb_connection_t *) &xcb_con_closed_screen_er;
81 static int set_fd_flags(const int fd)
83 /* Win32 doesn't have file descriptors and the fcntl function. This block sets the socket in non-blocking mode */
86 u_long iMode = 1; /* non-zero puts it in non-blocking mode, 0 in blocking mode */
89 ret = ioctlsocket(fd, FIONBIO, &iMode);
94 int flags = fcntl(fd, F_GETFL, 0);
98 if(fcntl(fd, F_SETFL, flags) == -1)
100 if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
106 static int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info)
108 static const char pad[3];
109 xcb_setup_request_t out;
110 struct iovec parts[6];
112 static const uint32_t endian = 0x01020304;
115 memset(&out, 0, sizeof(out));
117 /* B = 0x42 = MSB first, l = 0x6c = LSB first */
118 if(htonl(endian) == endian)
119 out.byte_order = 0x42;
121 out.byte_order = 0x6c;
122 out.protocol_major_version = X_PROTOCOL;
123 out.protocol_minor_version = X_PROTOCOL_REVISION;
124 out.authorization_protocol_name_len = 0;
125 out.authorization_protocol_data_len = 0;
126 parts[count].iov_len = sizeof(xcb_setup_request_t);
127 parts[count++].iov_base = &out;
128 parts[count].iov_len = XCB_PAD(sizeof(xcb_setup_request_t));
129 parts[count++].iov_base = (char *) pad;
133 parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
134 parts[count++].iov_base = auth_info->name;
135 parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len);
136 parts[count++].iov_base = (char *) pad;
137 parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
138 parts[count++].iov_base = auth_info->data;
139 parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len);
140 parts[count++].iov_base = (char *) pad;
142 assert(count <= (int) (sizeof(parts) / sizeof(*parts)));
144 pthread_mutex_lock(&c->iolock);
145 ret = _xcb_out_send(c, parts, count);
146 pthread_mutex_unlock(&c->iolock);
150 static int read_setup(xcb_connection_t *c)
152 /* Read the server response */
153 c->setup = malloc(sizeof(xcb_setup_generic_t));
157 if(_xcb_in_read_block(c, c->setup, sizeof(xcb_setup_generic_t)) != sizeof(xcb_setup_generic_t))
161 void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(xcb_setup_generic_t));
167 if(_xcb_in_read_block(c, (char *) c->setup + sizeof(xcb_setup_generic_t), c->setup->length * 4) <= 0)
170 /* 0 = failed, 2 = authenticate, 1 = success */
171 switch(c->setup->status)
175 xcb_setup_failed_t *setup = (xcb_setup_failed_t *) c->setup;
176 write(STDERR_FILENO, xcb_setup_failed_reason(setup), xcb_setup_failed_reason_length(setup));
180 case 2: /* authenticate */
182 xcb_setup_authenticate_t *setup = (xcb_setup_authenticate_t *) c->setup;
183 write(STDERR_FILENO, xcb_setup_authenticate_reason(setup), xcb_setup_authenticate_reason_length(setup));
191 /* precondition: there must be something for us to write. */
192 static int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
195 assert(!c->out.queue_len);
203 /* Could use the WSASend win32 function for scatter/gather i/o but setting up the WSABUF struct from
204 an iovec would require more work and I'm not sure of the benefit....works for now */
208 ret = send(c->fd,vec->iov_base,vec->iov_len,0);
209 if(ret == SOCKET_ERROR)
211 err = WSAGetLastError();
212 if(err == WSAEWOULDBLOCK)
227 if (c->out.out_fd.nfd) {
229 struct cmsghdr cmsghdr;
230 char buf[CMSG_SPACE(XCB_MAX_PASS_FD * sizeof(int))];
232 struct msghdr msg = {
237 .msg_control = cmsgbuf.buf,
238 .msg_controllen = CMSG_LEN(c->out.out_fd.nfd * sizeof (int)),
241 struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg);
243 hdr->cmsg_len = msg.msg_controllen;
244 hdr->cmsg_level = SOL_SOCKET;
245 hdr->cmsg_type = SCM_RIGHTS;
246 memcpy(CMSG_DATA(hdr), c->out.out_fd.fd, c->out.out_fd.nfd * sizeof (int));
248 n = sendmsg(c->fd, &msg, 0);
249 if(n < 0 && errno == EAGAIN)
251 for (i = 0; i < c->out.out_fd.nfd; i++)
252 close(c->out.out_fd.fd[i]);
253 c->out.out_fd.nfd = 0;
257 n = writev(c->fd, *vector, n);
258 if(n < 0 && errno == EAGAIN)
266 _xcb_conn_shutdown(c, XCB_CONN_ERROR);
270 for(; *count; --*count, ++*vector)
272 int cur = (*vector)->iov_len;
275 (*vector)->iov_len -= cur;
276 (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
278 if((*vector)->iov_len)
287 /* Public interface */
289 const xcb_setup_t *xcb_get_setup(xcb_connection_t *c)
293 /* doesn't need locking because it's never written to. */
297 int xcb_get_file_descriptor(xcb_connection_t *c)
301 /* doesn't need locking because it's never written to. */
305 int xcb_connection_has_error(xcb_connection_t *c)
307 /* doesn't need locking because it's read and written atomically. */
311 xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
317 if(fd >= FD_SETSIZE) /* would overflow in FD_SET */
320 return _xcb_conn_ret_error(XCB_CONN_ERROR);
325 c = calloc(1, sizeof(xcb_connection_t));
328 return _xcb_conn_ret_error(XCB_CONN_CLOSED_MEM_INSUFFICIENT) ;
335 pthread_mutex_init(&c->iolock, 0) == 0 &&
336 _xcb_in_init(&c->in) &&
337 _xcb_out_init(&c->out) &&
338 write_setup(c, auth_info) &&
345 return _xcb_conn_ret_error(XCB_CONN_ERROR);
351 void xcb_disconnect(xcb_connection_t *c)
353 if(c == NULL || is_static_error_conn(c))
358 /* disallow further sends and receives */
359 shutdown(c->fd, SHUT_RDWR);
362 pthread_mutex_destroy(&c->iolock);
363 _xcb_in_destroy(&c->in);
364 _xcb_out_destroy(&c->out);
376 /* Private interface */
378 void _xcb_conn_shutdown(xcb_connection_t *c, int err)
383 /* Return connection error state.
384 * To make thread-safe, I need a seperate static
385 * variable for every possible error.
386 * has_error is the first field in xcb_connection_t, so just
387 * return a casted int here; checking has_error (and only
388 * has_error) will be safe.
390 xcb_connection_t *_xcb_conn_ret_error(int err)
395 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
397 return (xcb_connection_t *) &xcb_con_closed_mem_er;
399 case XCB_CONN_CLOSED_PARSE_ERR:
401 return (xcb_connection_t *) &xcb_con_closed_parse_er;
403 case XCB_CONN_CLOSED_INVALID_SCREEN:
405 return (xcb_connection_t *) &xcb_con_closed_screen_er;
410 return (xcb_connection_t *) &xcb_con_error;
415 int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
424 /* If the thing I should be doing is already being done, wait for it. */
425 if(count ? c->out.writing : c->in.reading)
427 pthread_cond_wait(cond, &c->iolock);
432 memset(&fd, 0, sizeof(fd));
437 FD_SET(c->fd, &rfds);
444 fd.events |= POLLOUT;
451 FD_SET(c->fd, &wfds);
456 pthread_mutex_unlock(&c->iolock);
459 ret = poll(&fd, 1, -1);
460 /* If poll() returns an event we didn't expect, such as POLLNVAL, treat
461 * it as if it failed. */
462 if(ret >= 0 && (fd.revents & ~fd.events))
468 ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
470 } while (ret == -1 && errno == EINTR);
473 _xcb_conn_shutdown(c, XCB_CONN_ERROR);
476 pthread_mutex_lock(&c->iolock);
480 /* The code allows two threads to call select()/poll() at the same time.
481 * First thread just wants to read, a second thread wants to write, too.
482 * We have to make sure that we don't steal the reading thread's reply
483 * and let it get stuck in select()/poll().
484 * So a thread may read if either:
485 * - There is no other thread that wants to read (the above situation
487 * - It is the reading thread (above situation occurred).
489 int may_read = c->in.reading == 1 || !count;
491 if(may_read && (fd.revents & POLLIN) != 0)
493 if(may_read && FD_ISSET(c->fd, &rfds))
495 ret = ret && _xcb_in_read(c);
498 if((fd.revents & POLLOUT) != 0)
500 if(FD_ISSET(c->fd, &wfds))
502 ret = ret && write_vec(c, vector, count);