X-Git-Url: http://git.demorecorder.com/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fxcb_conn.c;h=7979491d344f9614cd8856722eb3c2fb90105d57;hb=ff53285ae3f604e9f2cc5f4837255220459b5e44;hp=4a50af3805e231d1f96b0078d0453bc66e3f8b84;hpb=bce72f63d2dfb61661f81e305ad3a7db0334403c;p=free-sw%2Fxcb%2Flibxcb diff --git a/src/xcb_conn.c b/src/xcb_conn.c index 4a50af3..7979491 100644 --- a/src/xcb_conn.c +++ b/src/xcb_conn.c @@ -25,6 +25,10 @@ /* Connection management: the core of XCB. */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include #include @@ -37,23 +41,32 @@ #include "xcbint.h" #if USE_POLL #include -#elif !defined WIN32 +#elif !defined _WIN32 #include #endif #ifdef _WIN32 -#include "windefs.h" +#include "xcb_windefs.h" #else +#include #include #endif /* _WIN32 */ +/* SHUT_RDWR is fairly recent and is not available on all platforms */ +#if !defined(SHUT_RDWR) +#define SHUT_RDWR 2 +#endif + typedef struct { uint8_t status; uint8_t pad0[5]; uint16_t length; } xcb_setup_generic_t; -static const int error_connection = 1; +static const int xcb_con_error = XCB_CONN_ERROR; +static const int xcb_con_closed_mem_er = XCB_CONN_CLOSED_MEM_INSUFFICIENT; +static const int xcb_con_closed_parse_er = XCB_CONN_CLOSED_PARSE_ERR; +static const int xcb_con_closed_screen_er = XCB_CONN_CLOSED_INVALID_SCREEN; static int set_fd_flags(const int fd) { @@ -77,7 +90,7 @@ static int set_fd_flags(const int fd) if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) return 0; return 1; -#endif /* WIN32 */ +#endif /* _WIN32 */ } static int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info) @@ -119,10 +132,7 @@ static int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info) assert(count <= (int) (sizeof(parts) / sizeof(*parts))); pthread_mutex_lock(&c->iolock); - { - struct iovec *parts_ptr = parts; - ret = _xcb_out_send(c, &parts_ptr, &count); - } + ret = _xcb_out_send(c, parts, count); pthread_mutex_unlock(&c->iolock); return ret; } @@ -206,7 +216,7 @@ static int write_vec(xcb_connection_t *c, struct iovec **vector, int *count) if(n <= 0) { - _xcb_conn_shutdown(c); + _xcb_conn_shutdown(c, XCB_CONN_ERROR); return 0; } @@ -255,10 +265,20 @@ xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info) { xcb_connection_t* c; +#ifndef _WIN32 +#ifndef USE_POLL + if(fd >= FD_SETSIZE) /* would overflow in FD_SET */ + { + close(fd); + return _xcb_conn_ret_error(XCB_CONN_ERROR); + } +#endif +#endif /* !_WIN32*/ + c = calloc(1, sizeof(xcb_connection_t)); if(!c) { close(fd); - return (xcb_connection_t *) &error_connection; + return _xcb_conn_ret_error(XCB_CONN_CLOSED_MEM_INSUFFICIENT) ; } c->fd = fd; @@ -275,7 +295,7 @@ xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info) )) { xcb_disconnect(c); - return (xcb_connection_t *) &error_connection; + return _xcb_conn_ret_error(XCB_CONN_ERROR); } return c; @@ -287,6 +307,9 @@ void xcb_disconnect(xcb_connection_t *c) return; free(c->setup); + + /* disallow further sends and receives */ + shutdown(c->fd, SHUT_RDWR); close(c->fd); pthread_mutex_destroy(&c->iolock); @@ -297,13 +320,46 @@ void xcb_disconnect(xcb_connection_t *c) _xcb_xid_destroy(c); free(c); + +#ifdef _WIN32 + WSACleanup(); +#endif } /* Private interface */ -void _xcb_conn_shutdown(xcb_connection_t *c) +void _xcb_conn_shutdown(xcb_connection_t *c, int err) +{ + c->has_error = err; +} + +/* Return connection error state. + * To make thread-safe, I need a seperate static + * variable for every possible error. + */ +xcb_connection_t *_xcb_conn_ret_error(int err) { - c->has_error = 1; + + switch(err) + { + case XCB_CONN_CLOSED_MEM_INSUFFICIENT: + { + return (xcb_connection_t *) &xcb_con_closed_mem_er; + } + case XCB_CONN_CLOSED_PARSE_ERR: + { + return (xcb_connection_t *) &xcb_con_closed_parse_er; + } + case XCB_CONN_CLOSED_INVALID_SCREEN: + { + return (xcb_connection_t *) &xcb_con_closed_screen_er; + } + case XCB_CONN_ERROR: + default: + { + return (xcb_connection_t *) &xcb_con_error; + } + } } int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count) @@ -350,15 +406,22 @@ int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vec pthread_mutex_unlock(&c->iolock); do { #if USE_POLL - ret = poll(&fd, 1, -1); + ret = poll(&fd, 1, -1); + /* If poll() returns an event we didn't expect, such as POLLNVAL, treat + * it as if it failed. */ + if(ret >= 0 && (fd.revents & ~fd.events)) + { + ret = -1; + break; + } #else - ret = select(c->fd + 1, &rfds, &wfds, 0, 0); + ret = select(c->fd + 1, &rfds, &wfds, 0, 0); #endif } while (ret == -1 && errno == EINTR); - if (ret < 0) + if(ret < 0) { - _xcb_conn_shutdown(c); - ret = 0; + _xcb_conn_shutdown(c, XCB_CONN_ERROR); + ret = 0; } pthread_mutex_lock(&c->iolock);