xcb_conn.c revision 602e473d
1602e473dSmrg/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp. 2602e473dSmrg * 3602e473dSmrg * Permission is hereby granted, free of charge, to any person obtaining a 4602e473dSmrg * copy of this software and associated documentation files (the "Software"), 5602e473dSmrg * to deal in the Software without restriction, including without limitation 6602e473dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 7602e473dSmrg * and/or sell copies of the Software, and to permit persons to whom the 8602e473dSmrg * Software is furnished to do so, subject to the following conditions: 9602e473dSmrg * 10602e473dSmrg * The above copyright notice and this permission notice shall be included in 11602e473dSmrg * all copies or substantial portions of the Software. 12602e473dSmrg * 13602e473dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14602e473dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15602e473dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16602e473dSmrg * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 17602e473dSmrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18602e473dSmrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19602e473dSmrg * 20602e473dSmrg * Except as contained in this notice, the names of the authors or their 21602e473dSmrg * institutions shall not be used in advertising or otherwise to promote the 22602e473dSmrg * sale, use or other dealings in this Software without prior written 23602e473dSmrg * authorization from the authors. 24602e473dSmrg */ 25602e473dSmrg 26602e473dSmrg/* Connection management: the core of XCB. */ 27602e473dSmrg 28602e473dSmrg#include <assert.h> 29602e473dSmrg#include <string.h> 30602e473dSmrg#include <stdio.h> 31602e473dSmrg#include <unistd.h> 32602e473dSmrg#include <stdlib.h> 33602e473dSmrg#include <netinet/in.h> 34602e473dSmrg#include <fcntl.h> 35602e473dSmrg#include <errno.h> 36602e473dSmrg 37602e473dSmrg#include "xcb.h" 38602e473dSmrg#include "xcbint.h" 39602e473dSmrg#if USE_POLL 40602e473dSmrg#include <poll.h> 41602e473dSmrg#else 42602e473dSmrg#include <sys/select.h> 43602e473dSmrg#endif 44602e473dSmrg 45602e473dSmrgtypedef struct { 46602e473dSmrg uint8_t status; 47602e473dSmrg uint8_t pad0[5]; 48602e473dSmrg uint16_t length; 49602e473dSmrg} xcb_setup_generic_t; 50602e473dSmrg 51602e473dSmrgstatic const int error_connection = 1; 52602e473dSmrg 53602e473dSmrgstatic int set_fd_flags(const int fd) 54602e473dSmrg{ 55602e473dSmrg int flags = fcntl(fd, F_GETFL, 0); 56602e473dSmrg if(flags == -1) 57602e473dSmrg return 0; 58602e473dSmrg flags |= O_NONBLOCK; 59602e473dSmrg if(fcntl(fd, F_SETFL, flags) == -1) 60602e473dSmrg return 0; 61602e473dSmrg if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) 62602e473dSmrg return 0; 63602e473dSmrg return 1; 64602e473dSmrg} 65602e473dSmrg 66602e473dSmrgstatic int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info) 67602e473dSmrg{ 68602e473dSmrg static const char pad[3]; 69602e473dSmrg xcb_setup_request_t out; 70602e473dSmrg struct iovec parts[6]; 71602e473dSmrg int count = 0; 72602e473dSmrg static const uint32_t endian = 0x01020304; 73602e473dSmrg int ret; 74602e473dSmrg 75602e473dSmrg memset(&out, 0, sizeof(out)); 76602e473dSmrg 77602e473dSmrg /* B = 0x42 = MSB first, l = 0x6c = LSB first */ 78602e473dSmrg if(htonl(endian) == endian) 79602e473dSmrg out.byte_order = 0x42; 80602e473dSmrg else 81602e473dSmrg out.byte_order = 0x6c; 82602e473dSmrg out.protocol_major_version = X_PROTOCOL; 83602e473dSmrg out.protocol_minor_version = X_PROTOCOL_REVISION; 84602e473dSmrg out.authorization_protocol_name_len = 0; 85602e473dSmrg out.authorization_protocol_data_len = 0; 86602e473dSmrg parts[count].iov_len = sizeof(xcb_setup_request_t); 87602e473dSmrg parts[count++].iov_base = &out; 88602e473dSmrg parts[count].iov_len = XCB_PAD(sizeof(xcb_setup_request_t)); 89602e473dSmrg parts[count++].iov_base = (char *) pad; 90602e473dSmrg 91602e473dSmrg if(auth_info) 92602e473dSmrg { 93602e473dSmrg parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen; 94602e473dSmrg parts[count++].iov_base = auth_info->name; 95602e473dSmrg parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len); 96602e473dSmrg parts[count++].iov_base = (char *) pad; 97602e473dSmrg parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen; 98602e473dSmrg parts[count++].iov_base = auth_info->data; 99602e473dSmrg parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len); 100602e473dSmrg parts[count++].iov_base = (char *) pad; 101602e473dSmrg } 102602e473dSmrg assert(count <= (int) (sizeof(parts) / sizeof(*parts))); 103602e473dSmrg 104602e473dSmrg pthread_mutex_lock(&c->iolock); 105602e473dSmrg { 106602e473dSmrg struct iovec *parts_ptr = parts; 107602e473dSmrg ret = _xcb_out_send(c, &parts_ptr, &count); 108602e473dSmrg } 109602e473dSmrg pthread_mutex_unlock(&c->iolock); 110602e473dSmrg return ret; 111602e473dSmrg} 112602e473dSmrg 113602e473dSmrgstatic int read_setup(xcb_connection_t *c) 114602e473dSmrg{ 115602e473dSmrg /* Read the server response */ 116602e473dSmrg c->setup = malloc(sizeof(xcb_setup_generic_t)); 117602e473dSmrg if(!c->setup) 118602e473dSmrg return 0; 119602e473dSmrg 120602e473dSmrg if(_xcb_in_read_block(c, c->setup, sizeof(xcb_setup_generic_t)) != sizeof(xcb_setup_generic_t)) 121602e473dSmrg return 0; 122602e473dSmrg 123602e473dSmrg { 124602e473dSmrg void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(xcb_setup_generic_t)); 125602e473dSmrg if(!tmp) 126602e473dSmrg return 0; 127602e473dSmrg c->setup = tmp; 128602e473dSmrg } 129602e473dSmrg 130602e473dSmrg if(_xcb_in_read_block(c, (char *) c->setup + sizeof(xcb_setup_generic_t), c->setup->length * 4) <= 0) 131602e473dSmrg return 0; 132602e473dSmrg 133602e473dSmrg /* 0 = failed, 2 = authenticate, 1 = success */ 134602e473dSmrg switch(c->setup->status) 135602e473dSmrg { 136602e473dSmrg case 0: /* failed */ 137602e473dSmrg { 138602e473dSmrg xcb_setup_failed_t *setup = (xcb_setup_failed_t *) c->setup; 139602e473dSmrg write(STDERR_FILENO, xcb_setup_failed_reason(setup), xcb_setup_failed_reason_length(setup)); 140602e473dSmrg return 0; 141602e473dSmrg } 142602e473dSmrg 143602e473dSmrg case 2: /* authenticate */ 144602e473dSmrg { 145602e473dSmrg xcb_setup_authenticate_t *setup = (xcb_setup_authenticate_t *) c->setup; 146602e473dSmrg write(STDERR_FILENO, xcb_setup_authenticate_reason(setup), xcb_setup_authenticate_reason_length(setup)); 147602e473dSmrg return 0; 148602e473dSmrg } 149602e473dSmrg } 150602e473dSmrg 151602e473dSmrg return 1; 152602e473dSmrg} 153602e473dSmrg 154602e473dSmrg/* precondition: there must be something for us to write. */ 155602e473dSmrgstatic int write_vec(xcb_connection_t *c, struct iovec **vector, int *count) 156602e473dSmrg{ 157602e473dSmrg int n; 158602e473dSmrg assert(!c->out.queue_len); 159602e473dSmrg n = writev(c->fd, *vector, *count); 160602e473dSmrg if(n < 0 && errno == EAGAIN) 161602e473dSmrg return 1; 162602e473dSmrg if(n <= 0) 163602e473dSmrg { 164602e473dSmrg _xcb_conn_shutdown(c); 165602e473dSmrg return 0; 166602e473dSmrg } 167602e473dSmrg 168602e473dSmrg for(; *count; --*count, ++*vector) 169602e473dSmrg { 170602e473dSmrg int cur = (*vector)->iov_len; 171602e473dSmrg if(cur > n) 172602e473dSmrg cur = n; 173602e473dSmrg (*vector)->iov_len -= cur; 174602e473dSmrg (*vector)->iov_base = (char *) (*vector)->iov_base + cur; 175602e473dSmrg n -= cur; 176602e473dSmrg if((*vector)->iov_len) 177602e473dSmrg break; 178602e473dSmrg } 179602e473dSmrg if(!*count) 180602e473dSmrg *vector = 0; 181602e473dSmrg assert(n == 0); 182602e473dSmrg return 1; 183602e473dSmrg} 184602e473dSmrg 185602e473dSmrg/* Public interface */ 186602e473dSmrg 187602e473dSmrgconst xcb_setup_t *xcb_get_setup(xcb_connection_t *c) 188602e473dSmrg{ 189602e473dSmrg if(c->has_error) 190602e473dSmrg return 0; 191602e473dSmrg /* doesn't need locking because it's never written to. */ 192602e473dSmrg return c->setup; 193602e473dSmrg} 194602e473dSmrg 195602e473dSmrgint xcb_get_file_descriptor(xcb_connection_t *c) 196602e473dSmrg{ 197602e473dSmrg if(c->has_error) 198602e473dSmrg return -1; 199602e473dSmrg /* doesn't need locking because it's never written to. */ 200602e473dSmrg return c->fd; 201602e473dSmrg} 202602e473dSmrg 203602e473dSmrgint xcb_connection_has_error(xcb_connection_t *c) 204602e473dSmrg{ 205602e473dSmrg /* doesn't need locking because it's read and written atomically. */ 206602e473dSmrg return c->has_error; 207602e473dSmrg} 208602e473dSmrg 209602e473dSmrgxcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info) 210602e473dSmrg{ 211602e473dSmrg xcb_connection_t* c; 212602e473dSmrg 213602e473dSmrg#ifndef USE_POLL 214602e473dSmrg if(fd >= FD_SETSIZE) /* would overflow in FD_SET */ 215602e473dSmrg { 216602e473dSmrg close(fd); 217602e473dSmrg return (xcb_connection_t *) &error_connection; 218602e473dSmrg } 219602e473dSmrg#endif 220602e473dSmrg 221602e473dSmrg c = calloc(1, sizeof(xcb_connection_t)); 222602e473dSmrg if(!c) { 223602e473dSmrg close(fd); 224602e473dSmrg return (xcb_connection_t *) &error_connection; 225602e473dSmrg } 226602e473dSmrg 227602e473dSmrg c->fd = fd; 228602e473dSmrg 229602e473dSmrg if(!( 230602e473dSmrg set_fd_flags(fd) && 231602e473dSmrg pthread_mutex_init(&c->iolock, 0) == 0 && 232602e473dSmrg _xcb_in_init(&c->in) && 233602e473dSmrg _xcb_out_init(&c->out) && 234602e473dSmrg write_setup(c, auth_info) && 235602e473dSmrg read_setup(c) && 236602e473dSmrg _xcb_ext_init(c) && 237602e473dSmrg _xcb_xid_init(c) 238602e473dSmrg )) 239602e473dSmrg { 240602e473dSmrg xcb_disconnect(c); 241602e473dSmrg return (xcb_connection_t *) &error_connection; 242602e473dSmrg } 243602e473dSmrg 244602e473dSmrg return c; 245602e473dSmrg} 246602e473dSmrg 247602e473dSmrgvoid xcb_disconnect(xcb_connection_t *c) 248602e473dSmrg{ 249602e473dSmrg if(c->has_error) 250602e473dSmrg return; 251602e473dSmrg 252602e473dSmrg free(c->setup); 253602e473dSmrg close(c->fd); 254602e473dSmrg 255602e473dSmrg pthread_mutex_destroy(&c->iolock); 256602e473dSmrg _xcb_in_destroy(&c->in); 257602e473dSmrg _xcb_out_destroy(&c->out); 258602e473dSmrg 259602e473dSmrg _xcb_ext_destroy(c); 260602e473dSmrg _xcb_xid_destroy(c); 261602e473dSmrg 262602e473dSmrg free(c); 263602e473dSmrg} 264602e473dSmrg 265602e473dSmrg/* Private interface */ 266602e473dSmrg 267602e473dSmrgvoid _xcb_conn_shutdown(xcb_connection_t *c) 268602e473dSmrg{ 269602e473dSmrg c->has_error = 1; 270602e473dSmrg} 271602e473dSmrg 272602e473dSmrgint _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count) 273602e473dSmrg{ 274602e473dSmrg int ret; 275602e473dSmrg#if USE_POLL 276602e473dSmrg struct pollfd fd; 277602e473dSmrg#else 278602e473dSmrg fd_set rfds, wfds; 279602e473dSmrg#endif 280602e473dSmrg 281602e473dSmrg /* If the thing I should be doing is already being done, wait for it. */ 282602e473dSmrg if(count ? c->out.writing : c->in.reading) 283602e473dSmrg { 284602e473dSmrg pthread_cond_wait(cond, &c->iolock); 285602e473dSmrg return 1; 286602e473dSmrg } 287602e473dSmrg 288602e473dSmrg#if USE_POLL 289602e473dSmrg memset(&fd, 0, sizeof(fd)); 290602e473dSmrg fd.fd = c->fd; 291602e473dSmrg fd.events = POLLIN; 292602e473dSmrg#else 293602e473dSmrg FD_ZERO(&rfds); 294602e473dSmrg FD_SET(c->fd, &rfds); 295602e473dSmrg#endif 296602e473dSmrg ++c->in.reading; 297602e473dSmrg 298602e473dSmrg#if USE_POLL 299602e473dSmrg if(count) 300602e473dSmrg { 301602e473dSmrg fd.events |= POLLOUT; 302602e473dSmrg ++c->out.writing; 303602e473dSmrg } 304602e473dSmrg#else 305602e473dSmrg FD_ZERO(&wfds); 306602e473dSmrg if(count) 307602e473dSmrg { 308602e473dSmrg FD_SET(c->fd, &wfds); 309602e473dSmrg ++c->out.writing; 310602e473dSmrg } 311602e473dSmrg#endif 312602e473dSmrg 313602e473dSmrg pthread_mutex_unlock(&c->iolock); 314602e473dSmrg do { 315602e473dSmrg#if USE_POLL 316602e473dSmrg ret = poll(&fd, 1, -1); 317602e473dSmrg#else 318602e473dSmrg ret = select(c->fd + 1, &rfds, &wfds, 0, 0); 319602e473dSmrg#endif 320602e473dSmrg } while (ret == -1 && errno == EINTR); 321602e473dSmrg if (ret < 0) 322602e473dSmrg { 323602e473dSmrg _xcb_conn_shutdown(c); 324602e473dSmrg ret = 0; 325602e473dSmrg } 326602e473dSmrg pthread_mutex_lock(&c->iolock); 327602e473dSmrg 328602e473dSmrg if(ret) 329602e473dSmrg { 330602e473dSmrg#if USE_POLL 331602e473dSmrg if((fd.revents & POLLIN) == POLLIN) 332602e473dSmrg#else 333602e473dSmrg if(FD_ISSET(c->fd, &rfds)) 334602e473dSmrg#endif 335602e473dSmrg ret = ret && _xcb_in_read(c); 336602e473dSmrg 337602e473dSmrg#if USE_POLL 338602e473dSmrg if((fd.revents & POLLOUT) == POLLOUT) 339602e473dSmrg#else 340602e473dSmrg if(FD_ISSET(c->fd, &wfds)) 341602e473dSmrg#endif 342602e473dSmrg ret = ret && write_vec(c, vector, count); 343602e473dSmrg } 344602e473dSmrg 345602e473dSmrg if(count) 346602e473dSmrg --c->out.writing; 347602e473dSmrg --c->in.reading; 348602e473dSmrg 349602e473dSmrg return ret; 350602e473dSmrg} 351