Shut down the connection in all "fatal" error cases.
[free-sw/xcb/libxcb] / src / xcb_out.c
index b0130e7..1b68215 100644 (file)
@@ -29,7 +29,6 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
-#include <errno.h>
 
 #include "xcb.h"
 #include "xcbext.h"
@@ -60,6 +59,8 @@ static int write_block(XCBConnection *c, struct iovec *vector, int count)
 
 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
 {
+    if(c->has_error)
+        return 0;
     pthread_mutex_lock(&c->out.reqlenlock);
     if(!c->out.maximum_request_length)
     {
@@ -92,6 +93,9 @@ unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, c
     int veclen = req->count;
     enum workarounds workaround = WORKAROUND_NONE;
 
+    if(c->has_error)
+        return 0;
+
     assert(c != 0);
     assert(vector != 0);
     assert(req->count > 0);
@@ -108,7 +112,10 @@ unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, c
         {
             const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
             if(!(extension && extension->present))
+            {
+                _xcb_conn_shutdown(c);
                 return 0;
+            }
             ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode;
             ((CARD8 *) vector[0].iov_base)[1] = req->opcode;
         }
@@ -121,7 +128,7 @@ unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, c
             longlen += vector[i].iov_len;
             if(!vector[i].iov_base)
             {
-                vector[i].iov_base = (caddr_t) pad;
+                vector[i].iov_base = (char *) pad;
                 assert(vector[i].iov_len <= sizeof(pad));
             }
         }
@@ -135,7 +142,10 @@ unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, c
             longlen = 0;
         }
         else if(longlen > XCBGetMaximumRequestLength(c))
+        {
+            _xcb_conn_shutdown(c);
             return 0; /* server can't take this; maybe need BIGREQUESTS? */
+        }
 
         /* set the length field. */
         ((CARD16 *) vector[0].iov_base)[1] = shortlen;
@@ -145,8 +155,10 @@ unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, c
     flags &= ~XCB_REQUEST_RAW;
 
     /* do we need to work around the X server bug described in glx.xml? */
-    if(req->ext && !req->isvoid && strcmp(req->ext->name, "GLX") &&
-            ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) ||
+    /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane
+     * configuration, but that should be handled here anyway. */
+    if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") &&
+            ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[1] == 0x10004) ||
              req->opcode == 21))
         workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
 
@@ -156,17 +168,24 @@ unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, c
     while(c->out.writing)
         pthread_cond_wait(&c->out.cond, &c->iolock);
 
-    if(req->isvoid && c->out.request == c->in.request_expected + (1 << 16) - 2)
+    request = ++c->out.request;
+    /* send GetInputFocus (sync) when 64k-2 requests have been sent without
+     * a reply.
+     * Also send sync (could use NoOp) at 32-bit wrap to avoid having
+     * applications see sequence 0 as that is used to indicate
+     * an error in sending the request */
+    while((req->isvoid &&
+       c->out.request == c->in.request_expected + (1 << 16) - 1) ||
+       request == 0)
     {
         prefix[0] = sync.packet;
-        request = ++c->out.request;
         _xcb_in_expect_reply(c, request, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY);
         c->in.request_expected = c->out.request;
+       request = ++c->out.request;
     }
 
-    request = ++c->out.request;
-    assert(request != 0);
-    _xcb_in_expect_reply(c, request, workaround, flags);
+    if(workaround != WORKAROUND_NONE || flags != 0)
+        _xcb_in_expect_reply(c, request, workaround, flags);
     if(!req->isvoid)
         c->in.request_expected = c->out.request;
 
@@ -184,7 +203,10 @@ unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, c
     }
 
     if(!write_block(c, vector, veclen))
+    {
+        _xcb_conn_shutdown(c);
         request = 0;
+    }
     pthread_mutex_unlock(&c->iolock);
     return request;
 }
@@ -192,6 +214,8 @@ unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, c
 int XCBFlush(XCBConnection *c)
 {
     int ret;
+    if(c->has_error)
+        return 0;
     pthread_mutex_lock(&c->iolock);
     ret = _xcb_out_flush_to(c, c->out.request);
     pthread_mutex_unlock(&c->iolock);
@@ -224,34 +248,6 @@ void _xcb_out_destroy(_xcb_out *out)
     pthread_mutex_destroy(&out->reqlenlock);
 }
 
-/* precondition: there must be something for us to write. */
-int _xcb_out_write(XCBConnection *c, struct iovec **vector, int *count)
-{
-    int n;
-    assert(!c->out.queue_len);
-    n = writev(c->fd, *vector, *count);
-    if(n < 0 && errno == EAGAIN)
-        return 1;
-    if(n <= 0)
-        return 0;
-
-    for(; *count; --*count, ++*vector)
-    {
-        int cur = (*vector)->iov_len;
-        if(cur > n)
-            cur = n;
-        (*vector)->iov_len -= cur;
-        (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
-        n -= cur;
-        if((*vector)->iov_len)
-            break;
-    }
-    if(!*count)
-        *vector = 0;
-    assert(n == 0);
-    return 1;
-}
-
 int _xcb_out_send(XCBConnection *c, struct iovec **vector, int *count)
 {
     int ret = 1;
@@ -264,8 +260,8 @@ int _xcb_out_send(XCBConnection *c, struct iovec **vector, int *count)
 
 int _xcb_out_flush_to(XCBConnection *c, unsigned int request)
 {
-    assert(request <= c->out.request);
-    if(c->out.request_written >= request)
+    assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
+    if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
         return 1;
     if(c->out.queue_len)
     {
@@ -278,6 +274,6 @@ int _xcb_out_flush_to(XCBConnection *c, unsigned int request)
     }
     while(c->out.writing)
         pthread_cond_wait(&c->out.cond, &c->iolock);
-    assert(c->out.request_written >= request);
+    assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));
     return 1;
 }