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;
65 struct wl_object *object;
67 struct wl_array *array;
71 wl_buffer_put(struct wl_buffer *b, const void *data, size_t count)
76 if (head + count <= sizeof b->data) {
77 memcpy(b->data + head, data, count);
79 size = sizeof b->data - head;
80 memcpy(b->data + head, data, size);
81 memcpy(b->data, (const char *) data + size, count - size);
88 wl_buffer_put_iov(struct wl_buffer *b, struct iovec *iov, int *count)
95 iov[0].iov_base = b->data + head;
96 iov[0].iov_len = tail - head;
98 } else if (tail == 0) {
99 iov[0].iov_base = b->data + head;
100 iov[0].iov_len = sizeof b->data - head;
103 iov[0].iov_base = b->data + head;
104 iov[0].iov_len = sizeof b->data - head;
105 iov[1].iov_base = b->data;
106 iov[1].iov_len = tail;
112 wl_buffer_get_iov(struct wl_buffer *b, struct iovec *iov, int *count)
116 head = MASK(b->head);
117 tail = MASK(b->tail);
119 iov[0].iov_base = b->data + tail;
120 iov[0].iov_len = head - tail;
122 } else if (head == 0) {
123 iov[0].iov_base = b->data + tail;
124 iov[0].iov_len = sizeof b->data - tail;
127 iov[0].iov_base = b->data + tail;
128 iov[0].iov_len = sizeof b->data - tail;
129 iov[1].iov_base = b->data;
130 iov[1].iov_len = head;
136 wl_buffer_copy(struct wl_buffer *b, void *data, size_t count)
140 tail = MASK(b->tail);
141 if (tail + count <= sizeof b->data) {
142 memcpy(data, b->data + tail, count);
144 size = sizeof b->data - tail;
145 memcpy(data, b->data + tail, size);
146 memcpy((char *) data + size, b->data, count - size);
151 wl_buffer_size(struct wl_buffer *b)
153 return b->head - b->tail;
156 struct wl_connection *
157 wl_connection_create(int fd)
159 struct wl_connection *connection;
161 connection = malloc(sizeof *connection);
162 if (connection == NULL)
164 memset(connection, 0, sizeof *connection);
171 wl_connection_destroy(struct wl_connection *connection)
173 close(connection->fd);
178 wl_connection_copy(struct wl_connection *connection, void *data, size_t size)
180 wl_buffer_copy(&connection->in, data, size);
184 wl_connection_consume(struct wl_connection *connection, size_t size)
186 connection->in.tail += size;
190 build_cmsg(struct wl_buffer *buffer, char *data, int *clen)
192 struct cmsghdr *cmsg;
195 size = buffer->head - buffer->tail;
197 cmsg = (struct cmsghdr *) data;
198 cmsg->cmsg_level = SOL_SOCKET;
199 cmsg->cmsg_type = SCM_RIGHTS;
200 cmsg->cmsg_len = CMSG_LEN(size);
201 wl_buffer_copy(buffer, CMSG_DATA(cmsg), size);
202 *clen = cmsg->cmsg_len;
209 close_fds(struct wl_buffer *buffer)
211 int fds[MAX_FDS_OUT], i, count;
214 size = buffer->head - buffer->tail;
218 wl_buffer_copy(buffer, fds, size);
219 count = size / sizeof fds[0];
220 for (i = 0; i < count; i++)
222 buffer->tail += size;
226 decode_cmsg(struct wl_buffer *buffer, struct msghdr *msg)
228 struct cmsghdr *cmsg;
231 for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
232 cmsg = CMSG_NXTHDR(msg, cmsg)) {
233 if (cmsg->cmsg_level == SOL_SOCKET &&
234 cmsg->cmsg_type == SCM_RIGHTS) {
235 size = cmsg->cmsg_len - CMSG_LEN(0);
236 wl_buffer_put(buffer, CMSG_DATA(cmsg), size);
242 wl_connection_flush(struct wl_connection *connection)
247 int len = 0, count, clen;
250 if (!connection->want_flush)
253 tail = connection->out.tail;
254 while (connection->out.head - connection->out.tail > 0) {
255 wl_buffer_get_iov(&connection->out, iov, &count);
257 build_cmsg(&connection->fds_out, cmsg, &clen);
262 msg.msg_iovlen = count;
263 msg.msg_control = cmsg;
264 msg.msg_controllen = clen;
268 len = sendmsg(connection->fd, &msg,
269 MSG_NOSIGNAL | MSG_DONTWAIT);
270 } while (len == -1 && errno == EINTR);
275 close_fds(&connection->fds_out);
277 connection->out.tail += len;
280 connection->want_flush = 0;
282 return connection->out.head - tail;
286 wl_connection_read(struct wl_connection *connection)
293 wl_buffer_put_iov(&connection->in, iov, &count);
298 msg.msg_iovlen = count;
299 msg.msg_control = cmsg;
300 msg.msg_controllen = sizeof cmsg;
304 len = wl_os_recvmsg_cloexec(connection->fd, &msg, 0);
305 } while (len < 0 && errno == EINTR);
310 decode_cmsg(&connection->fds_in, &msg);
312 connection->in.head += len;
314 return connection->in.head - connection->in.tail;
318 wl_connection_write(struct wl_connection *connection,
319 const void *data, size_t count)
321 if (connection->out.head - connection->out.tail +
322 count > ARRAY_LENGTH(connection->out.data)) {
323 connection->want_flush = 1;
324 if (wl_connection_flush(connection) < 0)
328 wl_buffer_put(&connection->out, data, count);
329 connection->want_flush = 1;
335 wl_connection_queue(struct wl_connection *connection,
336 const void *data, size_t count)
338 if (connection->out.head - connection->out.tail +
339 count > ARRAY_LENGTH(connection->out.data)) {
340 connection->want_flush = 1;
341 if (wl_connection_flush(connection) < 0)
345 wl_buffer_put(&connection->out, data, count);
351 wl_message_size_extra(const struct wl_message *message)
355 for (i = 0, extra = 0; message->signature[i]; i++) {
357 switch (message->signature[i]) {
361 extra += sizeof (void *);
364 extra += sizeof (void *) + sizeof (struct wl_array);
367 extra += sizeof (int);
378 wl_connection_put_fd(struct wl_connection *connection, int32_t fd)
380 if (wl_buffer_size(&connection->fds_out) == MAX_FDS_OUT * sizeof fd) {
381 connection->want_flush = 1;
382 if (wl_connection_flush(connection) < 0)
386 wl_buffer_put(&connection->fds_out, &fd, sizeof fd);
392 get_next_argument(const char *signature, struct argument_details *details)
394 if (*signature == '?') {
395 details->nullable = 1;
398 details->nullable = 0;
400 details->type = *signature;
401 return signature + 1;
405 arg_count_for_signature(const char *signature)
409 if (*signature != '?')
417 wl_closure_vmarshal(struct wl_object *sender,
418 uint32_t opcode, va_list ap,
419 const struct wl_message *message)
421 struct wl_closure *closure;
422 struct wl_object **objectp, *object;
423 uint32_t length, aligned, *p, *start, size, *end;
425 struct wl_array **arrayp, *array;
427 const char *signature = message->signature;
428 struct argument_details arg;
430 int i, count, fd, extra_size, *fd_ptr;
432 /* FIXME: Match old fixed allocation for now */
433 closure = malloc(sizeof *closure + 1024);
437 extra_size = wl_message_size_extra(message);
438 count = arg_count_for_signature(signature) + 2;
439 extra = (char *) closure->buffer;
440 start = &closure->buffer[DIV_ROUNDUP(extra_size, sizeof *p)];
441 end = &closure->buffer[256];
444 closure->types[0] = &ffi_type_pointer;
445 closure->types[1] = &ffi_type_pointer;
447 for (i = 2; i < count; i++) {
448 signature = get_next_argument(signature, &arg);
452 closure->types[i] = &ffi_type_sint32;
453 closure->args[i] = p;
456 *p++ = va_arg(ap, wl_fixed_t);
459 closure->types[i] = &ffi_type_uint32;
460 closure->args[i] = p;
463 *p++ = va_arg(ap, uint32_t);
466 closure->types[i] = &ffi_type_sint32;
467 closure->args[i] = p;
470 *p++ = va_arg(ap, int32_t);
473 closure->types[i] = &ffi_type_pointer;
474 closure->args[i] = extra;
475 sp = (const char **) extra;
478 s = va_arg(ap, const char *);
480 if (!arg.nullable && s == NULL)
483 length = s ? strlen(s) + 1: 0;
484 aligned = (length + 3) & ~3;
485 if (p + aligned / sizeof *p + 1 > end)
490 *sp = (const char *) p;
494 memcpy(p, s, length);
495 memset((char *) p + length, 0, aligned - length);
496 p += aligned / sizeof *p;
499 closure->types[i] = &ffi_type_pointer;
500 closure->args[i] = extra;
501 objectp = (struct wl_object **) extra;
502 extra += sizeof *objectp;
504 object = va_arg(ap, struct wl_object *);
506 if (!arg.nullable && object == NULL)
512 *p++ = object ? object->id : 0;
516 closure->types[i] = &ffi_type_uint32;
517 closure->args[i] = p;
518 object = va_arg(ap, struct wl_object *);
522 if (!arg.nullable && object == NULL)
525 *p++ = object ? object->id : 0;
529 closure->types[i] = &ffi_type_pointer;
530 closure->args[i] = extra;
531 arrayp = (struct wl_array **) extra;
532 extra += sizeof *arrayp;
534 *arrayp = (struct wl_array *) extra;
535 extra += sizeof **arrayp;
537 array = va_arg(ap, struct wl_array *);
539 if (!arg.nullable && array == NULL)
542 if (array == NULL || array->size == 0) {
548 if (p + DIV_ROUNDUP(array->size, sizeof *p) + 1 > end)
551 memcpy(p, array->data, array->size);
553 (*arrayp)->size = array->size;
554 (*arrayp)->alloc = array->alloc;
557 p += DIV_ROUNDUP(array->size, sizeof *p);
561 closure->types[i] = &ffi_type_sint;
562 closure->args[i] = extra;
563 fd_ptr = (int *) extra;
564 extra += sizeof *fd_ptr;
566 fd = va_arg(ap, int);
567 dup_fd = wl_os_dupfd_cloexec(fd, 0);
569 fprintf(stderr, "dup failed: %m");
575 fprintf(stderr, "unhandled format code: '%c'\n",
582 size = (p - start) * sizeof *p;
583 start[0] = sender->id;
584 start[1] = opcode | (size << 16);
586 closure->start = start;
587 closure->message = message;
588 closure->count = count;
590 ffi_prep_cif(&closure->cif, FFI_DEFAULT_ABI,
591 closure->count, &ffi_type_void, closure->types);
596 printf("request too big to marshal, maximum size is %zu\n",
597 sizeof closure->buffer);
605 wl_log("error marshalling arguments for %s:%i.%s (signature %s): "
606 "null value passed for arg %i\n",
607 sender->interface->name, sender->id, message->name,
608 message->signature, i);
614 wl_connection_demarshal(struct wl_connection *connection,
616 struct wl_map *objects,
617 const struct wl_message *message)
619 uint32_t *p, *next, *end, length, **id;
622 unsigned int i, count, extra_space;
623 const char *signature = message->signature;
624 struct argument_details arg;
625 struct wl_object **object;
626 struct wl_array **array;
627 struct wl_closure *closure;
629 count = arg_count_for_signature(signature) + 2;
630 if (count > ARRAY_LENGTH(closure->types)) {
631 printf("too many args (%d)\n", count);
633 wl_connection_consume(connection, size);
637 extra_space = wl_message_size_extra(message);
638 closure = malloc(sizeof *closure + 8 + size + extra_space);
642 closure->message = message;
643 closure->types[0] = &ffi_type_pointer;
644 closure->types[1] = &ffi_type_pointer;
645 closure->start = closure->buffer;
647 wl_connection_copy(connection, closure->buffer, size);
648 p = &closure->buffer[2];
649 end = (uint32_t *) ((char *) p + size);
650 extra = (char *) end;
651 for (i = 2; i < count; i++) {
652 signature = get_next_argument(signature, &arg);
655 printf("message too short, "
656 "object (%d), message %s(%s)\n",
657 *p, message->name, message->signature);
664 closure->types[i] = &ffi_type_uint32;
665 closure->args[i] = p++;
668 closure->types[i] = &ffi_type_sint32;
669 closure->args[i] = p++;
672 closure->types[i] = &ffi_type_sint32;
673 closure->args[i] = p++;
676 closure->types[i] = &ffi_type_pointer;
679 next = p + DIV_ROUNDUP(length, sizeof *p);
681 printf("message too short, "
682 "object (%d), message %s(%s)\n",
683 *p, message->name, message->signature);
690 closure->args[i] = s;
698 if (length > 0 && (*s)[length - 1] != '\0') {
699 printf("string not nul-terminated, "
701 message->name, message->signature);
708 closure->types[i] = &ffi_type_pointer;
709 id = (uint32_t **) extra;
711 closure->args[i] = id;
714 if (*id == 0 && !arg.nullable) {
715 printf("NULL object received on non-nullable "
716 "type, message %s(%s)\n", message->name,
726 closure->types[i] = &ffi_type_pointer;
727 id = (uint32_t **) extra;
729 closure->args[i] = id;
732 if (*id == 0 && !arg.nullable) {
733 printf("NULL new ID received on non-nullable "
734 "type, message %s(%s)\n", message->name,
740 if (wl_map_reserve_new(objects, *p) < 0) {
741 printf("not a valid new object id (%d), "
743 *p, message->name, message->signature);
751 closure->types[i] = &ffi_type_pointer;
754 next = p + DIV_ROUNDUP(length, sizeof *p);
756 printf("message too short, "
757 "object (%d), message %s(%s)\n",
758 *p, message->name, message->signature);
763 array = (struct wl_array **) extra;
764 extra += sizeof *array;
765 closure->args[i] = array;
767 *array = (struct wl_array *) extra;
768 extra += sizeof **array;
770 (*array)->size = length;
776 closure->types[i] = &ffi_type_sint;
780 closure->args[i] = fd;
782 wl_buffer_copy(&connection->fds_in, fd, sizeof *fd);
783 connection->fds_in.tail += sizeof *fd;
786 printf("unknown type\n");
794 ffi_prep_cif(&closure->cif, FFI_DEFAULT_ABI,
795 closure->count, &ffi_type_void, closure->types);
797 wl_connection_consume(connection, size);
803 wl_closure_destroy(closure);
804 wl_connection_consume(connection, size);
810 interface_equal(const struct wl_interface *a, const struct wl_interface *b)
812 /* In most cases the pointer equality test is sufficient.
813 * However, in some cases, depending on how things are split
814 * across shared objects, we can end up with multiple
815 * instances of the interface metadata constants. So if the
816 * pointers match, the interfaces are equal, if they don't
817 * match we have to compare the interface names. */
819 return a == b || strcmp(a->name, b->name) == 0;
823 wl_closure_lookup_objects(struct wl_closure *closure, struct wl_map *objects)
825 struct wl_object **object;
826 const struct wl_message *message;
827 const char *signature;
828 struct argument_details arg;
832 message = closure->message;
833 signature = message->signature;
834 count = arg_count_for_signature(signature) + 2;
835 for (i = 2; i < count; i++) {
836 signature = get_next_argument(signature, &arg);
839 id = **(uint32_t **) closure->args[i];
840 object = closure->args[i];
841 *object = wl_map_lookup(objects, id);
842 if (*object == WL_ZOMBIE_OBJECT) {
843 /* references object we've already
844 * destroyed client side */
846 } else if (*object == NULL && id != 0) {
847 printf("unknown object (%u), message %s(%s)\n",
848 id, message->name, message->signature);
854 if (*object != NULL && message->types[i-2] != NULL &&
855 !interface_equal((*object)->interface,
856 message->types[i-2])) {
857 printf("invalid object (%u), type (%s), "
859 id, (*object)->interface->name,
860 message->name, message->signature);
871 wl_closure_invoke(struct wl_closure *closure,
872 struct wl_object *target, void (*func)(void), void *data)
876 closure->args[0] = &data;
877 closure->args[1] = ⌖
879 ffi_call(&closure->cif, func, &result, closure->args);
883 copy_fds_to_connection(struct wl_closure *closure,
884 struct wl_connection *connection)
886 const struct wl_message *message = closure->message;
888 struct argument_details arg;
889 const char *signature = message->signature;
892 count = arg_count_for_signature(signature) + 2;
893 for (i = 2; i < count; i++) {
894 signature = get_next_argument(signature, &arg);
898 fd = closure->args[i];
899 if (wl_connection_put_fd(connection, *fd)) {
900 fprintf(stderr, "request could not be marshaled: "
901 "can't send file descriptor");
910 wl_closure_send(struct wl_closure *closure, struct wl_connection *connection)
914 if (copy_fds_to_connection(closure, connection))
917 size = closure->start[1] >> 16;
919 return wl_connection_write(connection, closure->start, size);
923 wl_closure_queue(struct wl_closure *closure, struct wl_connection *connection)
927 if (copy_fds_to_connection(closure, connection))
930 size = closure->start[1] >> 16;
932 return wl_connection_queue(connection, closure->start, size);
936 wl_closure_print(struct wl_closure *closure, struct wl_object *target, int send)
938 union wl_value *value;
941 struct argument_details arg;
942 const char *signature = closure->message->signature;
946 clock_gettime(CLOCK_REALTIME, &tp);
947 time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
949 fprintf(stderr, "[%10.3f] %s%s@%u.%s(",
952 target->interface->name, target->id,
953 closure->message->name);
955 for (i = 2; i < closure->count; i++) {
956 signature = get_next_argument(signature, &arg);
958 fprintf(stderr, ", ");
960 value = closure->args[i];
963 fprintf(stderr, "%u", value->uint32);
966 si = (int32_t) value->uint32;
967 fprintf(stderr, "%d", si);
970 si = (int32_t) value->uint32;
971 fprintf(stderr, "%f", wl_fixed_to_double(si));
974 fprintf(stderr, "\"%s\"", value->string);
978 fprintf(stderr, "%s@%u",
979 value->object->interface->name,
982 fprintf(stderr, "nil");
985 fprintf(stderr, "new id %s@",
986 (closure->message->types[i - 2]) ?
987 closure->message->types[i - 2]->name :
989 if (send && value->new_id != 0)
990 fprintf(stderr, "%u", value->new_id);
991 else if (!send && value->object != NULL)
992 fprintf(stderr, "%u", value->object->id);
994 fprintf(stderr, "nil");
997 fprintf(stderr, "array");
1000 fprintf(stderr, "fd %d", value->uint32);
1005 fprintf(stderr, ")\n");
1009 wl_closure_destroy(struct wl_closure *closure)