Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions libc-test/tests/cmsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@

#[cfg(unix)]
mod t {

use std::mem;

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

extern "C" {
pub fn cmsg_firsthdr(msgh: *const msghdr) -> *mut cmsghdr;
pub fn cmsg_nxthdr(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr;
pub fn cmsg_space(length: c_uint) -> usize;
pub fn cmsg_len(length: c_uint) -> usize;
pub fn cmsg_space(length: size_t) -> size_t;
pub fn cmsg_len(length: size_t) -> size_t;
pub fn cmsg_data(cmsg: *const cmsghdr) -> *mut c_uchar;
}

Expand Down Expand Up @@ -49,10 +47,8 @@ mod t {

#[test]
fn test_cmsg_len() {
for l in 0..128 {
unsafe {
assert_eq!(libc::CMSG_LEN(l) as usize, cmsg_len(l));
}
for l in 0..128usize {
assert_eq!(libc::CMSG_LEN(l), unsafe { cmsg_len(l) });
}
}

Expand Down Expand Up @@ -83,8 +79,7 @@ mod t {
// alignment and payload padding.
while !current_cmsghdr_ptr.is_null() {
unsafe {
(*current_cmsghdr_ptr).cmsg_len =
libc::CMSG_LEN(cmsg_payload_len as _) as _;
(*current_cmsghdr_ptr).cmsg_len = libc::CMSG_LEN(cmsg_payload_len) as _;

let libc_next = libc::CMSG_NXTHDR(&mhdr, current_cmsghdr_ptr);
let system_next = cmsg_nxthdr(&mhdr, current_cmsghdr_ptr);
Expand All @@ -108,10 +103,8 @@ mod t {

#[test]
fn test_cmsg_space() {
unsafe {
for l in 0..128 {
assert_eq!(libc::CMSG_SPACE(l) as usize, cmsg_space(l));
}
for l in 0..128 {
assert_eq!(libc::CMSG_SPACE(l), unsafe { cmsg_space(l) });
}
}
}
51 changes: 2 additions & 49 deletions src/fuchsia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3000,6 +3000,8 @@ pub const O_NOFOLLOW: c_int = 0x00000080;
pub const HUGETLB_FLAG_ENCODE_SHIFT: u32 = 26;
pub const MAP_HUGE_SHIFT: u32 = 26;

// END_PUB_CONST

// intentionally not public, only used for fd_set
cfg_if! {
if #[cfg(target_pointer_width = "32")] {
Expand All @@ -3011,8 +3013,6 @@ cfg_if! {
}
}

// END_PUB_CONST

f! {
pub fn FD_CLR(fd: c_int, set: *mut fd_set) -> () {
let fd = fd as usize;
Expand Down Expand Up @@ -3069,40 +3069,6 @@ f! {
pub fn CPU_EQUAL(set1: &cpu_set_t, set2: &cpu_set_t) -> bool {
set1.bits == set2.bits
}

pub fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar {
cmsg.offset(1) as *mut c_uchar
}

pub fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
if ((*cmsg).cmsg_len as size_t) < size_of::<cmsghdr>() {
core::ptr::null_mut::<cmsghdr>()
} else if __CMSG_NEXT(cmsg).add(size_of::<cmsghdr>()) >= __MHDR_END(mhdr) {
core::ptr::null_mut::<cmsghdr>()
} else {
__CMSG_NEXT(cmsg).cast()
}
}

pub fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
if (*mhdr).msg_controllen as size_t >= size_of::<cmsghdr>() {
(*mhdr).msg_control.cast()
} else {
core::ptr::null_mut::<cmsghdr>()
}
}

pub const fn CMSG_ALIGN(len: size_t) -> size_t {
(len + size_of::<size_t>() - 1) & !(size_of::<size_t>() - 1)
}

pub const fn CMSG_SPACE(len: c_uint) -> c_uint {
(CMSG_ALIGN(len as size_t) + CMSG_ALIGN(size_of::<cmsghdr>())) as c_uint
}

pub const fn CMSG_LEN(len: c_uint) -> c_uint {
(CMSG_ALIGN(size_of::<cmsghdr>()) + len as size_t) as c_uint
}
}

safe_f! {
Expand Down Expand Up @@ -3168,19 +3134,6 @@ safe_f! {
}
}

fn __CMSG_LEN(cmsg: *const cmsghdr) -> ssize_t {
((unsafe { (*cmsg).cmsg_len as size_t } + size_of::<c_long>() - 1) & !(size_of::<c_long>() - 1))
as ssize_t
}

fn __CMSG_NEXT(cmsg: *const cmsghdr) -> *mut c_uchar {
(unsafe { cmsg.offset(__CMSG_LEN(cmsg)) }) as *mut c_uchar
}

fn __MHDR_END(mhdr: *const msghdr) -> *mut c_uchar {
unsafe { (*mhdr).msg_control.offset((*mhdr).msg_controllen as isize) }.cast()
}

// EXTERN_FN

#[link(name = "c")]
Expand Down
1 change: 1 addition & 0 deletions src/new/aix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
//! * Headers are not public
//! * Manual pages: <https://www.ibm.com/docs/en/aix> (under "Technical reference" for that version)

pub(crate) mod sys;
pub(crate) mod unistd;
3 changes: 3 additions & 0 deletions src/new/aix/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Directory: `sys/`

pub(crate) mod socket;
9 changes: 9 additions & 0 deletions src/new/aix/sys/socket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! Header: `sys/socket.h`

pub use crate::new::common::posix::sys::socket::{
CMSG_DATA,
CMSG_FIRSTHDR,
CMSG_LEN,
CMSG_NXTHDR,
CMSG_SPACE,
};
7 changes: 7 additions & 0 deletions src/new/apple/libc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Entrypoint for Apple headers, usually found as part of the Xcode SDK.
//!
//! <https://github.com/apple-oss-distributions/Libc/tree/main/include>

pub(crate) mod signal;
pub(crate) mod sys;
pub(crate) mod unistd;
3 changes: 3 additions & 0 deletions src/new/apple/libc/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Directory: `sys/`

pub(crate) mod socket;
20 changes: 20 additions & 0 deletions src/new/apple/libc/sys/socket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Header: `sys/socket.h`
//!
//! <https://github.com/freebsd/freebsd-src/blob/main/sys/sys/socket.h>

pub(crate) type __ALIGN_BOUNDARY = c_long;

pub use crate::new::common::posix::sys::socket::{
CMSG_DATA,
CMSG_FIRSTHDR,
CMSG_LEN,
CMSG_SPACE,
};

pub unsafe fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const crate::cmsghdr) -> *mut crate::cmsghdr {
if cmsg.is_null() {
return CMSG_FIRSTHDR(mhdr);
}

crate::new::common::posix::sys::socket::CMSG_NXTHDR(mhdr, cmsg)
}
1 change: 1 addition & 0 deletions src/new/apple/xnu/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! <https://github.com/apple-oss-distributions/xnu/tree/main/bsd/sys>

pub(crate) mod signal;
pub(crate) mod socket;

/// Directory: `sys/_types`
///
Expand Down
11 changes: 11 additions & 0 deletions src/new/apple/xnu/sys/socket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! Header: `sys/socket.h`
//!
//! <https://github.com/apple-oss-distributions/xnu/blob/main/bsd/sys/socket.h>

pub use crate::new::common::bsd::sys::socket::CMSG_NXTHDR;
pub use crate::new::common::posix::sys::socket::{
CMSG_DATA,
CMSG_FIRSTHDR,
CMSG_LEN,
CMSG_SPACE,
};
11 changes: 11 additions & 0 deletions src/new/bionic_libc/sys/socket.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Header: `sys/socket.h`
//!
//! <https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/include/sys/socket.h>

use crate::prelude::*;

Expand Down Expand Up @@ -26,6 +28,15 @@ s! {
}
}

pub use crate::new::common::posix::sys::socket::{
CMSG_ALIGN,
CMSG_DATA,
CMSG_FIRSTHDR,
CMSG_LEN,
CMSG_NXTHDR,
CMSG_SPACE,
};

extern "C" {
pub fn recvmmsg(
sockfd: c_int,
Expand Down
16 changes: 16 additions & 0 deletions src/new/common/bsd.rs
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
//! Interfaces common across the BSD family.

pub(crate) mod sys {
pub(crate) mod socket {
#[cfg(not(target_os = "dragonfly"))]
pub unsafe fn CMSG_NXTHDR(
mhdr: *const crate::msghdr,
cmsg: *const crate::cmsghdr,
) -> *mut crate::cmsghdr {
if cmsg.is_null() {
return crate::CMSG_FIRSTHDR(mhdr);
}

crate::new::common::posix::sys::socket::CMSG_NXTHDR(mhdr, cmsg)
}
}
}
2 changes: 2 additions & 0 deletions src/new/common/posix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
))]
pub(crate) mod pthread;
pub(crate) mod unistd;

pub(crate) mod sys;
3 changes: 3 additions & 0 deletions src/new/common/posix/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Directory: `sys/`

pub(crate) mod socket;
Loading