Skip to content

Commit e9e4e18

Browse files
committed
Consolidate CMSG_* implementations
The original intent was to simplify the reasoning for how they differ across platforms. It should as a consequence also make it easier to extend target support. Changes: - CMSG_* functions which can be const are all marked const. - Removes unsafe from `CMSG_ALIGN`, `CMSG_SPACE`, `CMSG_LEN`. Fixes: Custom CMSG_FIRSTHDR implementation for VxWorks had missed to check that `mhdr.msg_controllen >= size_of::<cmsghdr>()`, as per POSIX 1003.1-2024. Documents: - Usage and safety requirements. - Adds missing references to upstream headers.
1 parent ab195eb commit e9e4e18

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+771
-688
lines changed

libc-test/tests/cmsg.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
44
#[cfg(unix)]
55
mod t {
6-
76
use std::mem;
87

98
use libc::{
10-
self,
119
c_uchar,
1210
c_uint,
1311
c_void,
@@ -50,9 +48,7 @@ mod t {
5048
#[test]
5149
fn test_cmsg_len() {
5250
for l in 0..128 {
53-
unsafe {
54-
assert_eq!(libc::CMSG_LEN(l) as usize, cmsg_len(l));
55-
}
51+
assert_eq!(libc::CMSG_LEN(l) as usize, unsafe { cmsg_len(l) });
5652
}
5753
}
5854

@@ -108,10 +104,8 @@ mod t {
108104

109105
#[test]
110106
fn test_cmsg_space() {
111-
unsafe {
112-
for l in 0..128 {
113-
assert_eq!(libc::CMSG_SPACE(l) as usize, cmsg_space(l));
114-
}
107+
for l in 0..128 {
108+
assert_eq!(libc::CMSG_SPACE(l) as usize, unsafe { cmsg_space(l) });
115109
}
116110
}
117111
}

src/fuchsia/mod.rs

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3000,6 +3000,8 @@ pub const O_NOFOLLOW: c_int = 0x00000080;
30003000
pub const HUGETLB_FLAG_ENCODE_SHIFT: u32 = 26;
30013001
pub const MAP_HUGE_SHIFT: u32 = 26;
30023002

3003+
// END_PUB_CONST
3004+
30033005
// intentionally not public, only used for fd_set
30043006
cfg_if! {
30053007
if #[cfg(target_pointer_width = "32")] {
@@ -3011,8 +3013,6 @@ cfg_if! {
30113013
}
30123014
}
30133015

3014-
// END_PUB_CONST
3015-
30163016
f! {
30173017
pub fn FD_CLR(fd: c_int, set: *mut fd_set) -> () {
30183018
let fd = fd as usize;
@@ -3069,40 +3069,6 @@ f! {
30693069
pub fn CPU_EQUAL(set1: &cpu_set_t, set2: &cpu_set_t) -> bool {
30703070
set1.bits == set2.bits
30713071
}
3072-
3073-
pub fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar {
3074-
cmsg.offset(1) as *mut c_uchar
3075-
}
3076-
3077-
pub fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
3078-
if ((*cmsg).cmsg_len as size_t) < size_of::<cmsghdr>() {
3079-
core::ptr::null_mut::<cmsghdr>()
3080-
} else if __CMSG_NEXT(cmsg).add(size_of::<cmsghdr>()) >= __MHDR_END(mhdr) {
3081-
core::ptr::null_mut::<cmsghdr>()
3082-
} else {
3083-
__CMSG_NEXT(cmsg).cast()
3084-
}
3085-
}
3086-
3087-
pub fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
3088-
if (*mhdr).msg_controllen as size_t >= size_of::<cmsghdr>() {
3089-
(*mhdr).msg_control.cast()
3090-
} else {
3091-
core::ptr::null_mut::<cmsghdr>()
3092-
}
3093-
}
3094-
3095-
pub const fn CMSG_ALIGN(len: size_t) -> size_t {
3096-
(len + size_of::<size_t>() - 1) & !(size_of::<size_t>() - 1)
3097-
}
3098-
3099-
pub const fn CMSG_SPACE(len: c_uint) -> c_uint {
3100-
(CMSG_ALIGN(len as size_t) + CMSG_ALIGN(size_of::<cmsghdr>())) as c_uint
3101-
}
3102-
3103-
pub const fn CMSG_LEN(len: c_uint) -> c_uint {
3104-
(CMSG_ALIGN(size_of::<cmsghdr>()) + len as size_t) as c_uint
3105-
}
31063072
}
31073073

31083074
safe_f! {
@@ -3168,19 +3134,6 @@ safe_f! {
31683134
}
31693135
}
31703136

3171-
fn __CMSG_LEN(cmsg: *const cmsghdr) -> ssize_t {
3172-
((unsafe { (*cmsg).cmsg_len as size_t } + size_of::<c_long>() - 1) & !(size_of::<c_long>() - 1))
3173-
as ssize_t
3174-
}
3175-
3176-
fn __CMSG_NEXT(cmsg: *const cmsghdr) -> *mut c_uchar {
3177-
(unsafe { cmsg.offset(__CMSG_LEN(cmsg)) }) as *mut c_uchar
3178-
}
3179-
3180-
fn __MHDR_END(mhdr: *const msghdr) -> *mut c_uchar {
3181-
unsafe { (*mhdr).msg_control.offset((*mhdr).msg_controllen as isize) }.cast()
3182-
}
3183-
31843137
// EXTERN_FN
31853138

31863139
#[link(name = "c")]

src/new/aix/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
//! * Headers are not public
44
//! * Manual pages: <https://www.ibm.com/docs/en/aix> (under "Technical reference" for that version)
55
6+
pub(crate) mod sys;
67
pub(crate) mod unistd;

src/new/aix/sys/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! Directory: `sys/`
2+
3+
pub(crate) mod socket;

src/new/aix/sys/socket.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Header: `sys/socket.h`
2+
3+
pub use crate::new::common::posix::sys::socket::{
4+
CMSG_DATA,
5+
CMSG_FIRSTHDR,
6+
CMSG_LEN,
7+
CMSG_NXTHDR,
8+
CMSG_SPACE,
9+
};

src/new/apple/libc/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! Entrypoint for Apple headers, usually found as part of the Xcode SDK.
2+
//!
3+
//! <https://github.com/apple-oss-distributions/Libc/tree/main/include>
4+
5+
pub(crate) mod signal;
6+
pub(crate) mod sys;
7+
pub(crate) mod unistd;

src/new/apple/libc/sys/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! Directory: `sys/`
2+
3+
pub(crate) mod socket;

src/new/apple/libc/sys/socket.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Header: `sys/socket.h`
2+
//!
3+
//! <https://github.com/freebsd/freebsd-src/blob/main/sys/sys/socket.h>
4+
5+
pub(crate) type __ALIGN_BOUNDARY = c_long;
6+
7+
pub use crate::new::common::posix::sys::socket::{
8+
CMSG_DATA,
9+
CMSG_FIRSTHDR,
10+
CMSG_LEN,
11+
CMSG_SPACE,
12+
};
13+
14+
pub unsafe fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const crate::cmsghdr) -> *mut crate::cmsghdr {
15+
if cmsg.is_null() {
16+
return CMSG_FIRSTHDR(mhdr);
17+
}
18+
19+
crate::new::common::posix::sys::socket::CMSG_NXTHDR(mhdr, cmsg)
20+
}

src/new/apple/xnu/sys/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! <https://github.com/apple-oss-distributions/xnu/tree/main/bsd/sys>
44
55
pub(crate) mod signal;
6+
pub(crate) mod socket;
67

78
/// Directory: `sys/_types`
89
///

src/new/apple/xnu/sys/socket.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! Header: `sys/socket.h`
2+
//!
3+
//! <https://github.com/apple-oss-distributions/xnu/blob/main/bsd/sys/socket.h>
4+
5+
pub use crate::new::common::bsd::sys::socket::CMSG_NXTHDR;
6+
pub use crate::new::common::posix::sys::socket::{
7+
CMSG_DATA,
8+
CMSG_FIRSTHDR,
9+
CMSG_LEN,
10+
CMSG_SPACE,
11+
};

0 commit comments

Comments
 (0)