Skip to content

Commit b6396ca

Browse files
authored
Merge pull request #1792 from jasonbking/cmsg
2 parents 5062033 + 40c46f4 commit b6396ca

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

libc-test/build.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ use std::env;
88
fn do_cc() {
99
let target = env::var("TARGET").unwrap();
1010
if cfg!(unix) {
11-
let exclude = ["wasi", "solaris", "illumos"];
11+
let exclude = ["wasi"];
1212
if !exclude.iter().any(|x| target.contains(x)) {
13-
cc::Build::new().file("src/cmsg.c").compile("cmsg");
13+
let mut cmsg = cc::Build::new();
14+
15+
cmsg.file("src/cmsg.c");
16+
17+
if target.contains("solaris") || target.contains("illumos") {
18+
cmsg.define("_XOPEN_SOURCE", "700");
19+
}
20+
cmsg.compile("cmsg");
1421
}
1522
}
1623
if target.contains("android") || target.contains("linux") {

libc-test/test/cmsg.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
extern crate libc;
55

66
#[cfg(unix)]
7-
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
87
mod t {
98

109
use libc::{self, c_uchar, c_uint, c_void, cmsghdr, msghdr};

src/unix/solarish/mod.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,7 +1998,63 @@ pub const PRIO_PROCESS: ::c_int = 0;
19981998
pub const PRIO_PGRP: ::c_int = 1;
19991999
pub const PRIO_USER: ::c_int = 2;
20002000

2001+
// As per sys/socket.h, header alignment must be 8 bytes on SPARC
2002+
// and 4 bytes everywhere else:
2003+
#[cfg(target_arch = "sparc64")]
2004+
const _CMSG_HDR_ALIGNMENT: usize = 8;
2005+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
2006+
const _CMSG_HDR_ALIGNMENT: usize = 4;
2007+
2008+
const _CMSG_DATA_ALIGNMENT: usize = ::mem::size_of::<::c_int>();
2009+
2010+
fn _CMSG_HDR_ALIGN(p: usize) -> usize {
2011+
(p + _CMSG_HDR_ALIGNMENT - 1) & !(_CMSG_HDR_ALIGNMENT - 1)
2012+
}
2013+
2014+
fn _CMSG_DATA_ALIGN(p: usize) -> usize {
2015+
(p + _CMSG_DATA_ALIGNMENT - 1) & !(_CMSG_DATA_ALIGNMENT - 1)
2016+
}
2017+
20012018
f! {
2019+
pub fn CMSG_DATA(cmsg: *const ::cmsghdr) -> *mut ::c_uchar {
2020+
_CMSG_DATA_ALIGN(cmsg.offset(1) as usize) as *mut ::c_uchar
2021+
}
2022+
2023+
pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
2024+
_CMSG_DATA_ALIGN(::mem::size_of::<::cmsghdr>()) as ::c_uint + length
2025+
}
2026+
2027+
pub fn CMSG_FIRSTHDR(mhdr: *const ::msghdr) -> *mut ::cmsghdr {
2028+
if ((*mhdr).msg_controllen as usize) < ::mem::size_of::<::cmsghdr>() {
2029+
0 as *mut ::cmsghdr
2030+
} else {
2031+
(*mhdr).msg_control as *mut ::cmsghdr
2032+
}
2033+
}
2034+
2035+
pub fn CMSG_NXTHDR(mhdr: *const ::msghdr, cmsg: *const ::cmsghdr)
2036+
-> *mut ::cmsghdr
2037+
{
2038+
if cmsg.is_null() {
2039+
return ::CMSG_FIRSTHDR(mhdr);
2040+
};
2041+
let next = _CMSG_HDR_ALIGN(cmsg as usize + (*cmsg).cmsg_len as usize
2042+
+ ::mem::size_of::<::cmsghdr>());
2043+
let max = (*mhdr).msg_control as usize
2044+
+ (*mhdr).msg_controllen as usize;
2045+
if next > max {
2046+
0 as *mut ::cmsghdr
2047+
} else {
2048+
_CMSG_HDR_ALIGN(cmsg as usize + (*cmsg).cmsg_len as usize)
2049+
as *mut ::cmsghdr
2050+
}
2051+
}
2052+
2053+
pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
2054+
_CMSG_HDR_ALIGN(::mem::size_of::<::cmsghdr>() as usize
2055+
+ length as usize) as ::c_uint
2056+
}
2057+
20022058
pub fn FD_CLR(fd: ::c_int, set: *mut fd_set) -> () {
20032059
let bits = ::mem::size_of_val(&(*set).fds_bits[0]) * 8;
20042060
let fd = fd as usize;

0 commit comments

Comments
 (0)