diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-08-27 09:02:43 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-08-27 09:03:21 +0200 |
| commit | 8a7392dea729d3ed49a8bf8eee25906c4fd616ac (patch) | |
| tree | 09e0b2fa96c60a2e0c8b0cae9e631bd2dc66f52f /src/ancillary-data.zig | |
| parent | c217e7ec5cddfc002c4582fb5d52727aee843a7d (diff) | |
Add ancillary data mechanism to send fds to compositor.
Currently we are just attaching the fds to the object id.
In theory this is not a valid implementation, since if we
have more than MAX_FD file descriptors this will not work.
But since this wont be the case in basically all cases of
the wayland protocol, we can just ignore that for now.
Diffstat (limited to 'src/ancillary-data.zig')
| -rw-r--r-- | src/ancillary-data.zig | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/src/ancillary-data.zig b/src/ancillary-data.zig index 0754720..667ba1f 100644 --- a/src/ancillary-data.zig +++ b/src/ancillary-data.zig @@ -18,47 +18,47 @@ const ControlMessage = extern struct { }; header: Header, - payload: union { + payload: extern union { fd: [255]std.posix.fd_t, }, pub fn alignment(length: usize) usize { - return (length + @sizeOf(usize) - 1) & ~(@sizeOf(usize) - 1); + return (length + @sizeOf(usize) - 1) & ~(@as(usize, @sizeOf(usize) - 1)); } - pub fn space(self: @This()) usize { - return self.alignment(self.length) + self.alignment(@sizeOf(Header)); + pub fn space(length: usize) usize { + return alignment(length) + alignment(@sizeOf(Header)); } pub fn from_fds(fds: []std.posix.fd_t) @This() { - var self: @This() = .{ - }; + var self: @This() = undefined; - self.level = .socket; - self.kind = .rights; - self.length = self.alignment(@sizeOf(Header)) + (fds.len * @sizeOf(std.posix.fd_t)); + self.header.level = .socket; + self.header.kind = .rights; + self.header.length = alignment(@sizeOf(Header)) + (fds.len * @sizeOf(std.posix.fd_t)); - @memcpy(self.payload, fds); + @memcpy(self.payload.fd[0..fds.len], fds); + + return self; } }; -pub fn send_fds(socket: std.posix.socket_t, fds: []std.posix.fd_t) !void { - var iobuf: [1]u8 = std.mem.zeroes([1]u8); +pub fn send_fds(socket: std.posix.socket_t, fds: []std.posix.fd_t, data: []const u8) !void { + var cmsg: ControlMessage = .from_fds(fds); const io = std.posix.iovec { - .base = &iobuf, - .len = iobuf.len, + .base = @constCast(@ptrCast(data)), + .len = data.len, }; - var cmsg: ControlMessage = .from_fds(fds); - const msghdr = std.posix.msghdr_const { .name = null, .namelen = 0, - .iov = &io, - .iolen = 1, + .iov = @ptrCast(&io), + .iovlen = 1, .control = &cmsg, - .controllen = cmsg.control_length(), + .controllen = @intCast(ControlMessage.space(fds.len)), + .flags = 0, }; _ = try std.posix.sendmsg(socket, &msghdr, 0); |