Skip to content

Commit

Permalink
Add support for compiling on macOS
Browse files Browse the repository at this point in the history
Dubiously useful, but doesn't take much, and it's a useful reference point
for portability to different Unix systems.
  • Loading branch information
ids1024 committed Jan 20, 2024
1 parent 15a897e commit 31bab14
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
9 changes: 6 additions & 3 deletions wayland-backend/src/rs/server_impl/common_poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use rustix::event::epoll;
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
target_os = "openbsd",
target_os = "macos"
))]
use rustix::event::kqueue::*;
use smallvec::SmallVec;
Expand All @@ -43,7 +44,8 @@ impl<D> InnerBackend<D> {
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
target_os = "openbsd",
target_os = "macos"
))]
let poll_fd = kqueue().map_err(Into::into).map_err(InitError::Io)?;

Expand Down Expand Up @@ -108,7 +110,8 @@ impl<D> InnerBackend<D> {
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
target_os = "openbsd",
target_os = "macos"
))]
pub fn dispatch_all_clients(&self, data: &mut D) -> std::io::Result<usize> {
use std::time::Duration;
Expand Down
3 changes: 2 additions & 1 deletion wayland-backend/src/rs/server_impl/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ impl<D> ErasedState for State<D> {
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
target_os = "openbsd",
target_os = "macos"
))]
let ret = {
use rustix::event::kqueue::*;
Expand Down
24 changes: 18 additions & 6 deletions wayland-backend/src/rs/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ impl Socket {
/// slice should not be longer than `MAX_BYTES_OUT` otherwise the receiving
/// end may lose some data.
pub fn send_msg(&self, bytes: &[u8], fds: &[OwnedFd]) -> IoResult<usize> {
#[cfg(not(target_os = "macos"))]
let flags = SendFlags::DONTWAIT | SendFlags::NOSIGNAL;
#[cfg(target_os = "macos")]
let flags = SendFlags::DONTWAIT;

if !fds.is_empty() {
let iov = [IoSlice::new(bytes)];
Expand All @@ -66,15 +69,15 @@ impl Socket {
/// slice `MAX_FDS_OUT` long, otherwise some data of the received message may
/// be lost.
pub fn rcv_msg(&self, buffer: &mut [u8], fds: &mut VecDeque<OwnedFd>) -> IoResult<usize> {
#[cfg(not(target_os = "macos"))]
let flags = RecvFlags::DONTWAIT | RecvFlags::CMSG_CLOEXEC;
#[cfg(target_os = "macos")]
let flags = RecvFlags::DONTWAIT;

let mut cmsg_space = vec![0; rustix::cmsg_space!(ScmRights(MAX_FDS_OUT))];
let mut cmsg_buffer = RecvAncillaryBuffer::new(&mut cmsg_space);
let mut iov = [IoSliceMut::new(buffer)];
let msg = recvmsg(
&self.stream,
&mut iov[..],
&mut cmsg_buffer,
RecvFlags::DONTWAIT | RecvFlags::CMSG_CLOEXEC,
)?;
let msg = recvmsg(&self.stream, &mut iov[..], &mut cmsg_buffer, flags)?;

let received_fds = cmsg_buffer
.drain()
Expand All @@ -84,12 +87,21 @@ impl Socket {
})
.flatten();
fds.extend(received_fds);
#[cfg(target_os = "macos")]
for fd in fds.iter() {
if let Ok(flags) = rustix::io::fcntl_getfd(fd) {
let _ = rustix::io::fcntl_setfd(fd, flags | rustix::io::FdFlags::CLOEXEC);
}
}
Ok(msg.bytes)
}
}

impl From<UnixStream> for Socket {
fn from(stream: UnixStream) -> Self {
// macOS doesn't have MSG_NOSIGNAL, but has SO_NOSIGPIPE instead
#[cfg(target_os = "macos")]
let _ = rustix::net::sockopt::set_socket_nosigpipe(&stream, true);
Self { stream }
}
}
Expand Down

0 comments on commit 31bab14

Please sign in to comment.