Skip to content

Commit 40c46f4

Browse files
committed
Add ancillary socket data accessor functions for solarish OSes
1 parent 0a60b22 commit 40c46f4

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
@@ -1995,7 +1995,63 @@ pub const PRIO_PROCESS: ::c_int = 0;
19951995
pub const PRIO_PGRP: ::c_int = 1;
19961996
pub const PRIO_USER: ::c_int = 2;
19971997

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

0 commit comments

Comments
 (0)