Skip to content

Commit

Permalink
Add OS specific types
Browse files Browse the repository at this point in the history
Add OS specific types that address alignment problems and their
respective libc definitions.
  • Loading branch information
dlrobertson committed Sep 30, 2016
1 parent fcb80e0 commit 7e2c42e
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/platform/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use bincode::serde::DeserializeError;
use libc::{self, MAP_FAILED, MAP_SHARED, POLLIN, PROT_READ, PROT_WRITE, SOCK_SEQPACKET, SOL_SOCKET};
use libc::{SO_LINGER, S_IFMT, S_IFSOCK, c_char, c_int, c_short, c_ushort, c_void, getsockopt};
use libc::{SO_LINGER, S_IFMT, S_IFSOCK, c_char, c_int, c_short, c_void, getsockopt};
use libc::{iovec, mkstemp, mode_t, msghdr, nfds_t, off_t, poll, pollfd, recvmsg, sendmsg};
use libc::{setsockopt, size_t, sockaddr, sockaddr_un, socketpair, socklen_t};
use std::cmp;
Expand Down Expand Up @@ -39,11 +39,25 @@ const TEMP_FILE_TEMPLATE: &'static str = "/sdcard/servo/ipc-channel-shared-memor
#[cfg(not(target_os="android"))]
const TEMP_FILE_TEMPLATE: &'static str = "/tmp/ipc-channel-shared-memory.XXXXXX";

#[cfg(target_os = "linux")]
type IovLen = usize;
#[cfg(target_os = "linux")]
type MsgControlLen = size_t;
#[cfg(target_os = "linux")]
type SunFamily = u16;

#[cfg(target_os = "freebsd")]
type IovLen = i32;
#[cfg(target_os = "freebsd")]
type MsgControlLen = socklen_t;
#[cfg(target_os = "freebsd")]
type SunFamily = u8;

unsafe fn new_sockaddr_un(path: *const c_char) -> (sockaddr_un, usize) {
let mut sockaddr: sockaddr_un = mem::zeroed();
libc::strncpy(sockaddr.sun_path.as_mut_ptr(),
path, sockaddr.sun_path.len() - 1);
sockaddr.sun_family = libc::AF_UNIX as u16;
sockaddr.sun_family = libc::AF_UNIX as SunFamily;
let len = mem::size_of::<c_short>() + (libc::strlen(sockaddr.sun_path.as_ptr())
as usize);
(sockaddr, len)
Expand Down Expand Up @@ -211,7 +225,7 @@ impl OsIpcSender {
let cmsg_length = mem::size_of_val(fds);
let (cmsg_buffer, cmsg_space) = if cmsg_length > 0 {
let cmsg_buffer = libc::malloc(CMSG_SPACE(cmsg_length)) as *mut cmsghdr;
(*cmsg_buffer).cmsg_len = CMSG_LEN(cmsg_length);
(*cmsg_buffer).cmsg_len = CMSG_LEN(cmsg_length) as MsgControlLen;
(*cmsg_buffer).cmsg_level = libc::SOL_SOCKET;
(*cmsg_buffer).cmsg_type = SCM_RIGHTS;

Expand Down Expand Up @@ -243,9 +257,9 @@ impl OsIpcSender {
msg_name: ptr::null_mut(),
msg_namelen: 0,
msg_iov: iovec.as_mut_ptr(),
msg_iovlen: iovec.len(),
msg_iovlen: iovec.len() as IovLen,
msg_control: cmsg_buffer as *mut c_void,
msg_controllen: cmsg_space,
msg_controllen: cmsg_space as MsgControlLen,
msg_flags: 0,
};

Expand Down Expand Up @@ -898,9 +912,9 @@ impl UnixCmsg {
msg_name: ptr::null_mut(),
msg_namelen: 0,
msg_iov: iovec.as_mut_ptr(),
msg_iovlen: iovec.len(),
msg_iovlen: iovec.len() as IovLen,
msg_control: cmsg_buffer as *mut c_void,
msg_controllen: cmsg_length,
msg_controllen: cmsg_length as MsgControlLen,
msg_flags: 0,
},
}
Expand Down Expand Up @@ -932,7 +946,7 @@ impl UnixCmsg {
}

unsafe fn cmsg_len(&self) -> size_t {
(*(self.msghdr.msg_control as *const cmsghdr)).cmsg_len
(*(self.msghdr.msg_control as *const cmsghdr)).cmsg_len as size_t
}
}

Expand Down Expand Up @@ -981,7 +995,7 @@ extern {

#[repr(C)]
struct cmsghdr {
cmsg_len: size_t,
cmsg_len: MsgControlLen,
cmsg_level: c_int,
cmsg_type: c_int,
}
Expand Down

0 comments on commit 7e2c42e

Please sign in to comment.