Alyssa Ross <hi@alyssa.is> writes:
src/connection.c | 4 + src/meson.build | 16 +- src/virtio_wl.c | 344 +++++++++++++++++++++++ src/virtio_wl.h | 23 ++ src/wayland-os.c | 23 ++ src/wayland-os.h | 3 + src/wayland-server.c | 20 +- src/wayland-shm.c | 24 +- tests/meson.build | 1 + tests/virtio_wl-test.c | 615 +++++++++++++++++++++++++++++++++++++++++ 10 files changed, 1064 insertions(+), 9 deletions(-) create mode 100644 src/virtio_wl.c create mode 100644 src/virtio_wl.h create mode 100644 tests/virtio_wl-test.c
diff --git a/src/virtio_wl.c b/src/virtio_wl.c new file mode 100644 index 0000000..a1ee8ee --- /dev/null +++ b/src/virtio_wl.c
(snip)
+static size_t +fdbuf_to_cmsg(struct msghdr *msg, const int *buf, size_t buflen) +{ + // Check msg->msg_control is long enough to fit at least one fd. + if (msg->msg_controllen < CMSG_SPACE(sizeof(int))) { + + // If there's at least one fd in buf, set MSG_CTRUNC. + size_t i = 0; + while (i < buflen && !(msg->msg_flags & MSG_CTRUNC)) + if (buf[i++] != -1) + msg->msg_flags |= MSG_CTRUNC; + + return 0; + } + + // cmsg(3): + // > When initializing a buffer that will contain a series of cmsghdr + // > structures (e.g., to be sent with sendmsg(2)), that buffer should + // > first be zero-initialized to en‐ sure the correct operation of + // > CMSG_NXTHDR().
This comment looks weird -- both the misalignment (tabs vs spaces, it seems) and "en- sure" (probably reflow mistake).
+ memset(msg->msg_control, 0, msg->msg_controllen); + + // Set up the cmsg. + struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + + // Copy as many fds as fit into cmsg. + size_t len = 0; + for (size_t i = 0; i < buflen; i++) { + if (buf[i] == -1) + continue; + + if (CMSG_LEN((len + 1) * sizeof(int)) > msg->msg_controllen) { + msg->msg_flags |= MSG_CTRUNC; + break; + } + + memcpy(CMSG_DATA(cmsg) + sizeof(int) * len, &buf[i], sizeof(int)); + len++; + } + + cmsg->cmsg_len = CMSG_LEN(len * sizeof(int)); + return len; +}
I can't comment on the code itself, as I'm unfamiliar with both Wayland and C. :D Cole