Add configure option to enable or disable fd passing with sendmsg
[free-sw/xcb/libxcb] / src / xcb_out.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 /* Stuff that sends stuff to the server. */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <assert.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36
37 #include "xcb.h"
38 #include "xcbext.h"
39 #include "xcbint.h"
40 #include "bigreq.h"
41
42 static inline void send_request(xcb_connection_t *c, int isvoid, enum workarounds workaround, int flags, struct iovec *vector, int count)
43 {
44     if(c->has_error)
45         return;
46
47     ++c->out.request;
48     if(!isvoid)
49         c->in.request_expected = c->out.request;
50     if(workaround != WORKAROUND_NONE || flags != 0)
51         _xcb_in_expect_reply(c, c->out.request, workaround, flags);
52
53     while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
54     {
55         memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
56         c->out.queue_len += vector[0].iov_len;
57         vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
58         vector[0].iov_len = 0;
59         ++vector, --count;
60     }
61     if(!count)
62         return;
63
64     --vector, ++count;
65     vector[0].iov_base = c->out.queue;
66     vector[0].iov_len = c->out.queue_len;
67     c->out.queue_len = 0;
68     _xcb_out_send(c, vector, count);
69 }
70
71 static void send_sync(xcb_connection_t *c)
72 {
73     static const union {
74         struct {
75             uint8_t major;
76             uint8_t pad;
77             uint16_t len;
78         } fields;
79         uint32_t packet;
80     } sync_req = { { /* GetInputFocus */ 43, 0, 1 } };
81     struct iovec vector[2];
82     vector[1].iov_base = (char *) &sync_req;
83     vector[1].iov_len = sizeof(sync_req);
84     send_request(c, 0, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY, vector + 1, 1);
85 }
86
87 static void get_socket_back(xcb_connection_t *c)
88 {
89     while (c->out.return_socket) {
90         /* we are about to release the lock,
91            so make a copy of the current status */
92         xcb_return_socket_func_t return_socket = c->out.return_socket;
93         void *socket_closure = c->out.socket_closure;
94         int socket_seq = c->out.socket_seq;
95
96         pthread_mutex_unlock(&c->iolock);
97         return_socket(socket_closure);
98         pthread_mutex_lock(&c->iolock);
99
100         /* make sure nobody else has acquired the socket */
101         if (socket_seq == c->out.socket_seq) {
102             c->out.return_socket = 0;
103             c->out.socket_closure = 0;
104             _xcb_in_replies_done(c);
105         }
106     }
107 }
108
109 /* Public interface */
110
111 void xcb_prefetch_maximum_request_length(xcb_connection_t *c)
112 {
113     if(c->has_error)
114         return;
115     pthread_mutex_lock(&c->out.reqlenlock);
116     if(c->out.maximum_request_length_tag == LAZY_NONE)
117     {
118         const xcb_query_extension_reply_t *ext;
119         ext = xcb_get_extension_data(c, &xcb_big_requests_id);
120         if(ext && ext->present)
121         {
122             c->out.maximum_request_length_tag = LAZY_COOKIE;
123             c->out.maximum_request_length.cookie = xcb_big_requests_enable(c);
124         }
125         else
126         {
127             c->out.maximum_request_length_tag = LAZY_FORCED;
128             c->out.maximum_request_length.value = c->setup->maximum_request_length;
129         }
130     }
131     pthread_mutex_unlock(&c->out.reqlenlock);
132 }
133
134 uint32_t xcb_get_maximum_request_length(xcb_connection_t *c)
135 {
136     if(c->has_error)
137         return 0;
138     xcb_prefetch_maximum_request_length(c);
139     pthread_mutex_lock(&c->out.reqlenlock);
140     if(c->out.maximum_request_length_tag == LAZY_COOKIE)
141     {
142         xcb_big_requests_enable_reply_t *r = xcb_big_requests_enable_reply(c, c->out.maximum_request_length.cookie, 0);
143         c->out.maximum_request_length_tag = LAZY_FORCED;
144         if(r)
145         {
146             c->out.maximum_request_length.value = r->maximum_request_length;
147             free(r);
148         }
149         else
150             c->out.maximum_request_length.value = c->setup->maximum_request_length;
151     }
152     pthread_mutex_unlock(&c->out.reqlenlock);
153     return c->out.maximum_request_length.value;
154 }
155
156 unsigned int xcb_send_request(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *req)
157 {
158     uint64_t request;
159     uint32_t prefix[2];
160     int veclen = req->count;
161     enum workarounds workaround = WORKAROUND_NONE;
162
163     if(c->has_error)
164         return 0;
165
166     assert(c != 0);
167     assert(vector != 0);
168     assert(req->count > 0);
169
170     if(!(flags & XCB_REQUEST_RAW))
171     {
172         static const char pad[3];
173         unsigned int i;
174         uint16_t shortlen = 0;
175         size_t longlen = 0;
176         assert(vector[0].iov_len >= 4);
177         /* set the major opcode, and the minor opcode for extensions */
178         if(req->ext)
179         {
180             const xcb_query_extension_reply_t *extension = xcb_get_extension_data(c, req->ext);
181             if(!(extension && extension->present))
182             {
183                 _xcb_conn_shutdown(c, XCB_CONN_CLOSED_EXT_NOTSUPPORTED);
184                 return 0;
185             }
186             ((uint8_t *) vector[0].iov_base)[0] = extension->major_opcode;
187             ((uint8_t *) vector[0].iov_base)[1] = req->opcode;
188         }
189         else
190             ((uint8_t *) vector[0].iov_base)[0] = req->opcode;
191
192         /* put together the length field, possibly using BIGREQUESTS */
193         for(i = 0; i < req->count; ++i)
194         {
195             longlen += vector[i].iov_len;
196             if(!vector[i].iov_base)
197             {
198                 vector[i].iov_base = (char *) pad;
199                 assert(vector[i].iov_len <= sizeof(pad));
200             }
201         }
202         assert((longlen & 3) == 0);
203         longlen >>= 2;
204
205         if(longlen <= c->setup->maximum_request_length)
206         {
207             /* we don't need BIGREQUESTS. */
208             shortlen = longlen;
209             longlen = 0;
210         }
211         else if(longlen > xcb_get_maximum_request_length(c))
212         {
213             _xcb_conn_shutdown(c, XCB_CONN_CLOSED_REQ_LEN_EXCEED);
214             return 0; /* server can't take this; maybe need BIGREQUESTS? */
215         }
216
217         /* set the length field. */
218         ((uint16_t *) vector[0].iov_base)[1] = shortlen;
219         if(!shortlen)
220         {
221             prefix[0] = ((uint32_t *) vector[0].iov_base)[0];
222             prefix[1] = ++longlen;
223             vector[0].iov_base = (uint32_t *) vector[0].iov_base + 1;
224             vector[0].iov_len -= sizeof(uint32_t);
225             --vector, ++veclen;
226             vector[0].iov_base = prefix;
227             vector[0].iov_len = sizeof(prefix);
228         }
229     }
230     flags &= ~XCB_REQUEST_RAW;
231
232     /* do we need to work around the X server bug described in glx.xml? */
233     /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane
234      * configuration, but that should be handled here anyway. */
235     if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") &&
236             ((req->opcode == 17 && ((uint32_t *) vector[0].iov_base)[1] == 0x10004) ||
237              req->opcode == 21))
238         workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
239
240     /* get a sequence number and arrange for delivery. */
241     pthread_mutex_lock(&c->iolock);
242     /* wait for other writing threads to get out of my way. */
243     while(c->out.writing)
244         pthread_cond_wait(&c->out.cond, &c->iolock);
245     get_socket_back(c);
246
247     /* send GetInputFocus (sync_req) when 64k-2 requests have been sent without
248      * a reply. */
249     if(req->isvoid && c->out.request == c->in.request_expected + (1 << 16) - 2)
250         send_sync(c);
251     /* Also send sync_req (could use NoOp) at 32-bit wrap to avoid having
252      * applications see sequence 0 as that is used to indicate
253      * an error in sending the request */
254     if((unsigned int) (c->out.request + 1) == 0)
255         send_sync(c);
256
257     /* The above send_sync calls could drop the I/O lock, but this
258      * thread will still exclude any other thread that tries to write,
259      * so the sequence number postconditions still hold. */
260     send_request(c, req->isvoid, workaround, flags, vector, veclen);
261     request = c->has_error ? 0 : c->out.request;
262     pthread_mutex_unlock(&c->iolock);
263     return request;
264 }
265
266 void
267 xcb_send_fd(xcb_connection_t *c, int fd)
268 {
269 #if HAVE_SENDMSG
270     if (c->has_error)
271         return;
272     pthread_mutex_lock(&c->iolock);
273     while (c->out.out_fd.nfd == XCB_MAX_PASS_FD) {
274         _xcb_out_flush_to(c, c->out.request);
275         if (c->has_error)
276             break;
277     }
278     if (!c->has_error)
279         c->out.out_fd.fd[c->out.out_fd.nfd++] = fd;
280     pthread_mutex_unlock(&c->iolock);
281 #endif
282 }
283
284 int xcb_take_socket(xcb_connection_t *c, void (*return_socket)(void *closure), void *closure, int flags, uint64_t *sent)
285 {
286     int ret;
287     if(c->has_error)
288         return 0;
289     pthread_mutex_lock(&c->iolock);
290     get_socket_back(c);
291
292     /* _xcb_out_flush may drop the iolock allowing other threads to
293      * write requests, so keep flushing until we're done
294      */
295     do
296         ret = _xcb_out_flush_to(c, c->out.request);
297     while (ret && c->out.request != c->out.request_written);
298     if(ret)
299     {
300         c->out.return_socket = return_socket;
301         c->out.socket_closure = closure;
302         ++c->out.socket_seq;
303         if(flags)
304             _xcb_in_expect_reply(c, c->out.request, WORKAROUND_EXTERNAL_SOCKET_OWNER, flags);
305         assert(c->out.request == c->out.request_written);
306         *sent = c->out.request;
307     }
308     pthread_mutex_unlock(&c->iolock);
309     return ret;
310 }
311
312 int xcb_writev(xcb_connection_t *c, struct iovec *vector, int count, uint64_t requests)
313 {
314     int ret;
315     if(c->has_error)
316         return 0;
317     pthread_mutex_lock(&c->iolock);
318     c->out.request += requests;
319     ret = _xcb_out_send(c, vector, count);
320     pthread_mutex_unlock(&c->iolock);
321     return ret;
322 }
323
324 int xcb_flush(xcb_connection_t *c)
325 {
326     int ret;
327     if(c->has_error)
328         return 0;
329     pthread_mutex_lock(&c->iolock);
330     ret = _xcb_out_flush_to(c, c->out.request);
331     pthread_mutex_unlock(&c->iolock);
332     return ret;
333 }
334
335 /* Private interface */
336
337 int _xcb_out_init(_xcb_out *out)
338 {
339     out->return_socket = 0;
340     out->socket_closure = 0;
341     out->socket_seq = 0;
342
343     if(pthread_cond_init(&out->cond, 0))
344         return 0;
345     out->writing = 0;
346
347     out->queue_len = 0;
348
349     out->request = 0;
350     out->request_written = 0;
351
352     if(pthread_mutex_init(&out->reqlenlock, 0))
353         return 0;
354     out->maximum_request_length_tag = LAZY_NONE;
355
356     return 1;
357 }
358
359 void _xcb_out_destroy(_xcb_out *out)
360 {
361     pthread_cond_destroy(&out->cond);
362     pthread_mutex_destroy(&out->reqlenlock);
363 }
364
365 int _xcb_out_send(xcb_connection_t *c, struct iovec *vector, int count)
366 {
367     int ret = 1;
368     while(ret && count)
369         ret = _xcb_conn_wait(c, &c->out.cond, &vector, &count);
370     c->out.request_written = c->out.request;
371     pthread_cond_broadcast(&c->out.cond);
372     _xcb_in_wake_up_next_reader(c);
373     return ret;
374 }
375
376 void _xcb_out_send_sync(xcb_connection_t *c)
377 {
378     /* wait for other writing threads to get out of my way. */
379     while(c->out.writing)
380         pthread_cond_wait(&c->out.cond, &c->iolock);
381     get_socket_back(c);
382     send_sync(c);
383 }
384
385 int _xcb_out_flush_to(xcb_connection_t *c, uint64_t request)
386 {
387     assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
388     if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
389         return 1;
390     if(c->out.queue_len)
391     {
392         struct iovec vec;
393         vec.iov_base = c->out.queue;
394         vec.iov_len = c->out.queue_len;
395         c->out.queue_len = 0;
396         return _xcb_out_send(c, &vec, 1);
397     }
398     while(c->out.writing)
399         pthread_cond_wait(&c->out.cond, &c->iolock);
400     assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));
401     return 1;
402 }