2 * Copyright © 2008 Kristian Høgsberg
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
35 #include <sys/types.h>
36 #include <sys/socket.h>
39 #include "wayland-util.h"
40 #include "wayland-private.h"
41 #include "wayland-os.h"
43 #define DIV_ROUNDUP(n, a) ( ((n) + ((a) - 1)) / (a) )
50 #define MASK(i) ((i) & 4095)
52 #define MAX_FDS_OUT 28
53 #define CLEN (CMSG_LEN(MAX_FDS_OUT * sizeof(int32_t)))
55 struct wl_connection {
56 struct wl_buffer in, out;
57 struct wl_buffer fds_in, fds_out;
60 wl_connection_update_func_t update;
61 struct wl_closure receive_closure, send_closure;
68 struct wl_object *object;
70 struct wl_array *array;
74 wl_buffer_put(struct wl_buffer *b, const void *data, size_t count)
79 if (head + count <= sizeof b->data) {
80 memcpy(b->data + head, data, count);
82 size = sizeof b->data - head;
83 memcpy(b->data + head, data, size);
84 memcpy(b->data, (const char *) data + size, count - size);
91 wl_buffer_put_iov(struct wl_buffer *b, struct iovec *iov, int *count)
98 iov[0].iov_base = b->data + head;
99 iov[0].iov_len = tail - head;
101 } else if (tail == 0) {
102 iov[0].iov_base = b->data + head;
103 iov[0].iov_len = sizeof b->data - head;
106 iov[0].iov_base = b->data + head;
107 iov[0].iov_len = sizeof b->data - head;
108 iov[1].iov_base = b->data;
109 iov[1].iov_len = tail;
115 wl_buffer_get_iov(struct wl_buffer *b, struct iovec *iov, int *count)
119 head = MASK(b->head);
120 tail = MASK(b->tail);
122 iov[0].iov_base = b->data + tail;
123 iov[0].iov_len = head - tail;
125 } else if (head == 0) {
126 iov[0].iov_base = b->data + tail;
127 iov[0].iov_len = sizeof b->data - tail;
130 iov[0].iov_base = b->data + tail;
131 iov[0].iov_len = sizeof b->data - tail;
132 iov[1].iov_base = b->data;
133 iov[1].iov_len = head;
139 wl_buffer_copy(struct wl_buffer *b, void *data, size_t count)
143 tail = MASK(b->tail);
144 if (tail + count <= sizeof b->data) {
145 memcpy(data, b->data + tail, count);
147 size = sizeof b->data - tail;
148 memcpy(data, b->data + tail, size);
149 memcpy((char *) data + size, b->data, count - size);
154 wl_buffer_size(struct wl_buffer *b)
156 return b->head - b->tail;
159 struct wl_connection *
160 wl_connection_create(int fd,
161 wl_connection_update_func_t update,
164 struct wl_connection *connection;
166 connection = malloc(sizeof *connection);
167 if (connection == NULL)
169 memset(connection, 0, sizeof *connection);
171 connection->update = update;
172 connection->data = data;
174 connection->update(connection,
175 WL_CONNECTION_READABLE,
182 wl_connection_destroy(struct wl_connection *connection)
184 close(connection->fd);
189 wl_connection_copy(struct wl_connection *connection, void *data, size_t size)
191 wl_buffer_copy(&connection->in, data, size);
195 wl_connection_consume(struct wl_connection *connection, size_t size)
197 connection->in.tail += size;
201 build_cmsg(struct wl_buffer *buffer, char *data, int *clen)
203 struct cmsghdr *cmsg;
206 size = buffer->head - buffer->tail;
208 cmsg = (struct cmsghdr *) data;
209 cmsg->cmsg_level = SOL_SOCKET;
210 cmsg->cmsg_type = SCM_RIGHTS;
211 cmsg->cmsg_len = CMSG_LEN(size);
212 wl_buffer_copy(buffer, CMSG_DATA(cmsg), size);
213 *clen = cmsg->cmsg_len;
220 close_fds(struct wl_buffer *buffer)
222 int fds[MAX_FDS_OUT], i, count;
225 size = buffer->head - buffer->tail;
229 wl_buffer_copy(buffer, fds, size);
230 count = size / sizeof fds[0];
231 for (i = 0; i < count; i++)
233 buffer->tail += size;
237 decode_cmsg(struct wl_buffer *buffer, struct msghdr *msg)
239 struct cmsghdr *cmsg;
242 for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
243 cmsg = CMSG_NXTHDR(msg, cmsg)) {
244 if (cmsg->cmsg_level == SOL_SOCKET &&
245 cmsg->cmsg_type == SCM_RIGHTS) {
246 size = cmsg->cmsg_len - CMSG_LEN(0);
247 wl_buffer_put(buffer, CMSG_DATA(cmsg), size);
253 wl_connection_data(struct wl_connection *connection, uint32_t mask)
258 int len, count, clen;
260 if (mask & WL_CONNECTION_WRITABLE) {
261 wl_buffer_get_iov(&connection->out, iov, &count);
263 build_cmsg(&connection->fds_out, cmsg, &clen);
268 msg.msg_iovlen = count;
269 msg.msg_control = cmsg;
270 msg.msg_controllen = clen;
274 len = sendmsg(connection->fd, &msg,
275 MSG_NOSIGNAL | MSG_DONTWAIT);
276 } while (len < 0 && errno == EINTR);
278 if (len == -1 && errno == EPIPE) {
280 } else if (len < 0) {
282 "write error for connection %p, fd %d: %m\n",
283 connection, connection->fd);
287 close_fds(&connection->fds_out);
289 connection->out.tail += len;
290 if (connection->out.tail == connection->out.head &&
291 connection->write_signalled) {
292 connection->update(connection,
293 WL_CONNECTION_READABLE,
295 connection->write_signalled = 0;
299 if (mask & WL_CONNECTION_READABLE) {
300 wl_buffer_put_iov(&connection->in, iov, &count);
305 msg.msg_iovlen = count;
306 msg.msg_control = cmsg;
307 msg.msg_controllen = sizeof cmsg;
311 len = wl_os_recvmsg_cloexec(connection->fd, &msg, 0);
312 } while (len < 0 && errno == EINTR);
316 "read error from connection %p: %m (%d)\n",
319 } else if (len == 0) {
320 /* FIXME: Handle this better? */
324 decode_cmsg(&connection->fds_in, &msg);
326 connection->in.head += len;
329 return connection->in.head - connection->in.tail;
333 wl_connection_write(struct wl_connection *connection,
334 const void *data, size_t count)
336 if (connection->out.head - connection->out.tail +
337 count > ARRAY_LENGTH(connection->out.data))
338 if (wl_connection_data(connection, WL_CONNECTION_WRITABLE))
341 wl_buffer_put(&connection->out, data, count);
343 if (!connection->write_signalled) {
344 connection->update(connection,
345 WL_CONNECTION_READABLE |
346 WL_CONNECTION_WRITABLE,
348 connection->write_signalled = 1;
355 wl_connection_queue(struct wl_connection *connection,
356 const void *data, size_t count)
358 if (connection->out.head - connection->out.tail +
359 count > ARRAY_LENGTH(connection->out.data))
360 if (wl_connection_data(connection, WL_CONNECTION_WRITABLE))
363 wl_buffer_put(&connection->out, data, count);
369 wl_message_size_extra(const struct wl_message *message)
373 for (i = 0, extra = 0; message->signature[i]; i++) {
375 switch (message->signature[i]) {
378 extra += sizeof (void *);
381 extra += sizeof (void *) + sizeof (struct wl_array);
384 extra += sizeof (int);
395 wl_connection_put_fd(struct wl_connection *connection, int32_t fd)
397 if (wl_buffer_size(&connection->fds_out) == MAX_FDS_OUT * sizeof fd)
398 if (wl_connection_data(connection, WL_CONNECTION_WRITABLE))
401 wl_buffer_put(&connection->fds_out, &fd, sizeof fd);
407 wl_closure_vmarshal(struct wl_closure *closure,
408 struct wl_object *sender,
409 uint32_t opcode, va_list ap,
410 const struct wl_message *message)
412 struct wl_object **objectp, *object;
413 uint32_t length, *p, *start, size, *end;
415 struct wl_array **arrayp, *array;
418 int i, count, fd, extra_size, *fd_ptr;
420 extra_size = wl_message_size_extra(message);
421 count = strlen(message->signature) + 2;
422 extra = (char *) closure->buffer;
423 start = &closure->buffer[DIV_ROUNDUP(extra_size, sizeof *p)];
424 end = &closure->buffer[ARRAY_LENGTH(closure->buffer)];
427 closure->types[0] = &ffi_type_pointer;
428 closure->types[1] = &ffi_type_pointer;
430 for (i = 2; i < count; i++) {
431 switch (message->signature[i - 2]) {
433 closure->types[i] = &ffi_type_sint32;
434 closure->args[i] = p;
437 *p++ = va_arg(ap, wl_fixed_t);
440 closure->types[i] = &ffi_type_uint32;
441 closure->args[i] = p;
444 *p++ = va_arg(ap, uint32_t);
447 closure->types[i] = &ffi_type_sint32;
448 closure->args[i] = p;
451 *p++ = va_arg(ap, int32_t);
454 closure->types[i] = &ffi_type_pointer;
455 closure->args[i] = extra;
456 sp = (const char **) extra;
459 s = va_arg(ap, const char *);
460 length = s ? strlen(s) + 1: 0;
461 if (p + DIV_ROUNDUP(length, sizeof *p) + 1 > end)
466 *sp = (const char *) p;
470 memcpy(p, s, length);
471 p += DIV_ROUNDUP(length, sizeof *p);
474 closure->types[i] = &ffi_type_pointer;
475 closure->args[i] = extra;
476 objectp = (struct wl_object **) extra;
477 extra += sizeof *objectp;
479 object = va_arg(ap, struct wl_object *);
483 *p++ = object ? object->id : 0;
487 closure->types[i] = &ffi_type_uint32;
488 closure->args[i] = p;
489 object = va_arg(ap, struct wl_object *);
492 *p++ = object ? object->id : 0;
496 closure->types[i] = &ffi_type_pointer;
497 closure->args[i] = extra;
498 arrayp = (struct wl_array **) extra;
499 extra += sizeof *arrayp;
501 *arrayp = (struct wl_array *) extra;
502 extra += sizeof **arrayp;
504 array = va_arg(ap, struct wl_array *);
505 if (array == NULL || array->size == 0) {
511 if (p + DIV_ROUNDUP(array->size, sizeof *p) + 1 > end)
514 memcpy(p, array->data, array->size);
516 (*arrayp)->size = array->size;
517 (*arrayp)->alloc = array->alloc;
520 p += DIV_ROUNDUP(array->size, sizeof *p);
524 closure->types[i] = &ffi_type_sint;
525 closure->args[i] = extra;
526 fd_ptr = (int *) extra;
527 extra += sizeof *fd_ptr;
529 fd = va_arg(ap, int);
530 dup_fd = wl_os_dupfd_cloexec(fd, 0);
532 fprintf(stderr, "dup failed: %m");
538 fprintf(stderr, "unhandled format code: '%c'\n",
539 message->signature[i - 2]);
545 size = (p - start) * sizeof *p;
546 start[0] = sender->id;
547 start[1] = opcode | (size << 16);
549 closure->start = start;
550 closure->message = message;
551 closure->count = count;
553 ffi_prep_cif(&closure->cif, FFI_DEFAULT_ABI,
554 closure->count, &ffi_type_void, closure->types);
559 printf("request too big to marshal, maximum size is %zu\n",
560 sizeof closure->buffer);
566 wl_connection_demarshal(struct wl_connection *connection,
567 struct wl_closure *closure,
569 struct wl_map *objects,
570 const struct wl_message *message)
572 uint32_t *p, *next, *end, length;
575 unsigned int i, count, extra_space;
576 struct wl_object **object;
577 struct wl_array **array;
579 count = strlen(message->signature) + 2;
580 if (count > ARRAY_LENGTH(closure->types)) {
581 printf("too many args (%d)\n", count);
583 wl_connection_consume(connection, size);
587 extra_space = wl_message_size_extra(message);
588 if (sizeof closure->buffer < size + extra_space) {
589 printf("request too big to demarshal, maximum %zu actual %d\n",
590 sizeof closure->buffer, size + extra_space);
592 wl_connection_consume(connection, size);
596 closure->message = message;
597 closure->types[0] = &ffi_type_pointer;
598 closure->types[1] = &ffi_type_pointer;
600 wl_connection_copy(connection, closure->buffer, size);
601 p = &closure->buffer[2];
602 end = (uint32_t *) ((char *) p + size);
603 extra = (char *) end;
604 for (i = 2; i < count; i++) {
606 printf("message too short, "
607 "object (%d), message %s(%s)\n",
608 *p, message->name, message->signature);
613 switch (message->signature[i - 2]) {
615 closure->types[i] = &ffi_type_uint32;
616 closure->args[i] = p++;
619 closure->types[i] = &ffi_type_sint32;
620 closure->args[i] = p++;
623 closure->types[i] = &ffi_type_sint32;
624 closure->args[i] = p++;
627 closure->types[i] = &ffi_type_pointer;
630 next = p + DIV_ROUNDUP(length, sizeof *p);
632 printf("message too short, "
633 "object (%d), message %s(%s)\n",
634 *p, message->name, message->signature);
641 closure->args[i] = s;
649 if (length > 0 && (*s)[length - 1] != '\0') {
650 printf("string not nul-terminated, "
652 message->name, message->signature);
659 closure->types[i] = &ffi_type_pointer;
660 object = (struct wl_object **) extra;
661 extra += sizeof *object;
662 closure->args[i] = object;
664 *object = wl_map_lookup(objects, *p);
665 if (*object == WL_ZOMBIE_OBJECT) {
666 /* references object we've already
667 * destroyed client side */
669 } else if (*object == NULL && *p != 0) {
670 printf("unknown object (%d), message %s(%s)\n",
671 *p, message->name, message->signature);
677 if (*object != NULL && message->types[i-2] != NULL &&
678 (*object)->interface != message->types[i-2]) {
679 printf("invalid object (%d), type (%s), "
681 *p, (*object)->interface->name,
682 message->name, message->signature);
690 closure->types[i] = &ffi_type_uint32;
691 closure->args[i] = p;
692 object = wl_map_lookup(objects, *p);
693 if (object != NULL) {
694 printf("not a new object (%d), "
696 *p, message->name, message->signature);
703 closure->types[i] = &ffi_type_pointer;
706 next = p + DIV_ROUNDUP(length, sizeof *p);
708 printf("message too short, "
709 "object (%d), message %s(%s)\n",
710 *p, message->name, message->signature);
715 array = (struct wl_array **) extra;
716 extra += sizeof *array;
717 closure->args[i] = array;
719 *array = (struct wl_array *) extra;
720 extra += sizeof **array;
722 (*array)->size = length;
728 closure->types[i] = &ffi_type_sint;
732 closure->args[i] = fd;
734 wl_buffer_copy(&connection->fds_in, fd, sizeof *fd);
735 connection->fds_in.tail += sizeof *fd;
738 printf("unknown type\n");
746 ffi_prep_cif(&closure->cif, FFI_DEFAULT_ABI,
747 closure->count, &ffi_type_void, closure->types);
749 wl_connection_consume(connection, size);
755 wl_closure_destroy(closure);
756 wl_connection_consume(connection, size);
762 wl_closure_invoke(struct wl_closure *closure,
763 struct wl_object *target, void (*func)(void), void *data)
767 closure->args[0] = &data;
768 closure->args[1] = ⌖
770 ffi_call(&closure->cif, func, &result, closure->args);
774 copy_fds_to_connection(struct wl_closure *closure,
775 struct wl_connection *connection)
777 const struct wl_message *message = closure->message;
781 count = strlen(message->signature) + 2;
782 for (i = 2; i < count; i++) {
783 if (message->signature[i - 2] != 'h')
786 fd = closure->args[i];
787 if (wl_connection_put_fd(connection, *fd)) {
788 fprintf(stderr, "request could not be marshaled: "
789 "can't send file descriptor");
798 wl_closure_send(struct wl_closure *closure, struct wl_connection *connection)
802 if (copy_fds_to_connection(closure, connection))
805 size = closure->start[1] >> 16;
807 return wl_connection_write(connection, closure->start, size);
811 wl_closure_queue(struct wl_closure *closure, struct wl_connection *connection)
815 if (copy_fds_to_connection(closure, connection))
818 size = closure->start[1] >> 16;
820 return wl_connection_queue(connection, closure->start, size);
824 wl_closure_print(struct wl_closure *closure, struct wl_object *target, int send)
826 union wl_value *value;
832 clock_gettime(CLOCK_REALTIME, &tp);
833 time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
835 fprintf(stderr, "[%10.3f] %s%s@%u.%s(",
838 target->interface->name, target->id,
839 closure->message->name);
841 for (i = 2; i < closure->count; i++) {
843 fprintf(stderr, ", ");
845 value = closure->args[i];
846 switch (closure->message->signature[i - 2]) {
848 fprintf(stderr, "%u", value->uint32);
851 si = (int32_t) value->uint32;
852 fprintf(stderr, "%d", si);
855 si = (int32_t) value->uint32;
856 fprintf(stderr, "%f", (double) si / 256.0);
859 fprintf(stderr, "\"%s\"", value->string);
863 fprintf(stderr, "%s@%u",
864 value->object->interface->name,
867 fprintf(stderr, "nil");
870 fprintf(stderr, "new id %u", value->uint32);
873 fprintf(stderr, "array");
876 fprintf(stderr, "fd %d", value->uint32);
881 fprintf(stderr, ")\n");
885 wl_closure_destroy(struct wl_closure *closure)