From e8429cdc5ec2914e17116a3e6abf149f7dabc4f9 Mon Sep 17 00:00:00 2001 From: auronandace Date: Mon, 13 Apr 2026 10:18:40 +0100 Subject: [PATCH 1/4] move sys_socket bits from C to cbindgen --- include/bits/sys/socket.h | 26 -------------------------- src/header/sys_socket/cbindgen.toml | 28 +++++++++++++++++++++++++--- src/header/sys_socket/mod.rs | 10 ++++++++-- 3 files changed, 33 insertions(+), 31 deletions(-) delete mode 100644 include/bits/sys/socket.h diff --git a/include/bits/sys/socket.h b/include/bits/sys/socket.h deleted file mode 100644 index 99f0d9fe38..0000000000 --- a/include/bits/sys/socket.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef _BITS_SYS_SOCKET_H -#define _BITS_SYS_SOCKET_H - -struct sockaddr_storage { - sa_family_t ss_family; - char __ss_padding[128-sizeof(long)-sizeof(sa_family_t)]; - unsigned long __ss_align; -}; - -// These definitions were taken from musl, license MIT { -#define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1)) -#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg)) -#define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen) - -#define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1)) -#define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || \ - __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \ - ? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg)) -#define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0) - -#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) -#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr))) -#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) -// } from musl, license MIT - -#endif // _BITS_SYS_SOCKET_H diff --git a/src/header/sys_socket/cbindgen.toml b/src/header/sys_socket/cbindgen.toml index 53adbc0fe3..03207837a5 100644 --- a/src/header/sys_socket/cbindgen.toml +++ b/src/header/sys_socket/cbindgen.toml @@ -1,10 +1,32 @@ sys_includes = ["stddef.h", "stdint.h", "sys/types.h"] -include_guard = "_SYS_SOCKET_H" +include_guard = "_RELIBC_SYS_SOCKET_H" after_includes = """ -#include // for iovec +#include // for iovec from sys/uio.h #include // for socklen_t """ -trailer = "#include " +trailer = """ +struct sockaddr_storage { + sa_family_t ss_family; + char __ss_padding[128-sizeof(long)-sizeof(sa_family_t)]; + unsigned long __ss_align; +}; + +// These definitions were taken from musl, license MIT { +#define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1)) +#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg)) +#define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen) + +#define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1)) +#define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || \ + __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \ + ? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg)) +#define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0) + +#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) +#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr))) +#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) +// } from musl, license MIT +""" language = "C" style = "Tag" no_includes = true diff --git a/src/header/sys_socket/mod.rs b/src/header/sys_socket/mod.rs index 89c6b29986..6863c867e4 100644 --- a/src/header/sys_socket/mod.rs +++ b/src/header/sys_socket/mod.rs @@ -76,8 +76,10 @@ pub struct sockaddr { } // Max size of [`sockaddr_storage`] +/// cbindgen:ignore const _SS_MAXSIZE: usize = 128; // Align to pointer width +/// cbindgen:ignore const _SS_PADDING: usize = _SS_MAXSIZE - mem::size_of::() - mem::size_of::(); /// See . @@ -91,22 +93,26 @@ const _SS_PADDING: usize = _SS_MAXSIZE - mem::size_of::() - mem::si /// from protocol structs in C #[repr(C)] //#[derive(CheckVsLibcCrate)] FIXME: can't ignore private fields yet +/// cbindgen:ignore pub struct sockaddr_storage { pub ss_family: sa_family_t, __ss_pad2: [u8; _SS_PADDING], __ss_align: usize, } -// These must match C macros in include/bits/sys/socket.h { +// These must match C macros in sys_socket/cbindgen.toml { +/// cbindgen:ignore pub unsafe extern "C" fn __CMSG_LEN(cmsg: *const cmsghdr) -> ssize_t { ((unsafe { (*cmsg).cmsg_len as size_t } + mem::size_of::() - 1) & !(mem::size_of::() - 1)) as ssize_t } +/// cbindgen:ignore pub unsafe extern "C" fn __CMSG_NEXT(cmsg: *const cmsghdr) -> *mut c_uchar { unsafe { (cmsg as *mut c_uchar).offset(__CMSG_LEN(cmsg)) } } +/// cbindgen:ignore pub unsafe extern "C" fn __MHDR_END(mhdr: *const msghdr) -> *mut c_uchar { unsafe { ((*mhdr).msg_control.cast::()).add((*mhdr).msg_controllen) } } @@ -165,7 +171,7 @@ pub unsafe extern "C" fn CMSG_SPACE(len: c_uint) -> c_uint { pub unsafe extern "C" fn CMSG_LEN(length: c_uint) -> c_uint { (unsafe { CMSG_ALIGN(mem::size_of::()) } + length as usize) as c_uint } -// } These must match C macros in include/bits/sys/socket.h +// } These must match C macros in sys_socket/cbindgen.toml /// See . #[unsafe(no_mangle)] From f0e3574f307c792ee88526554febc0e874a9db41 Mon Sep 17 00:00:00 2001 From: auronandace Date: Mon, 13 Apr 2026 10:59:23 +0100 Subject: [PATCH 2/4] remove sys_socket include from ifaddrs and netdb --- src/header/ifaddrs/cbindgen.toml | 3 ++- src/header/netdb/cbindgen.toml | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/header/ifaddrs/cbindgen.toml b/src/header/ifaddrs/cbindgen.toml index f7d97cd169..b507c6f75f 100644 --- a/src/header/ifaddrs/cbindgen.toml +++ b/src/header/ifaddrs/cbindgen.toml @@ -1,4 +1,5 @@ -sys_includes = ["features.h", "netinet/in.h", "sys/socket.h"] +# netinet/in.h brings in sys/socket.h +sys_includes = ["features.h", "netinet/in.h"] include_guard = "_RELIBC_IFADDRS_H" language = "C" style = "Tag" diff --git a/src/header/netdb/cbindgen.toml b/src/header/netdb/cbindgen.toml index acba8b8749..e5767e164f 100644 --- a/src/header/netdb/cbindgen.toml +++ b/src/header/netdb/cbindgen.toml @@ -1,4 +1,5 @@ -sys_includes = ["sys/socket.h", "netinet/in.h", "features.h"] +# netinet/in.h brings in sys/socket.h +sys_includes = ["netinet/in.h", "features.h"] include_guard = "_RELIBC_NETDB_H" trailer = """ #define h_errno (*__h_errno_location()) From 7cf6933f81e1fff9fe5e3e52efeb1b5a9d5aedcd Mon Sep 17 00:00:00 2001 From: auronandace Date: Mon, 13 Apr 2026 11:14:11 +0100 Subject: [PATCH 3/4] remove sys_socket include from netdb tests --- tests/netdb/getaddrinfo.c | 1 - tests/netdb/getaddrinfo_null.c | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/netdb/getaddrinfo.c b/tests/netdb/getaddrinfo.c index 9c4968d560..e1bb13562f 100644 --- a/tests/netdb/getaddrinfo.c +++ b/tests/netdb/getaddrinfo.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include "test_helpers.h" diff --git a/tests/netdb/getaddrinfo_null.c b/tests/netdb/getaddrinfo_null.c index b874a9270a..01b69cb860 100644 --- a/tests/netdb/getaddrinfo_null.c +++ b/tests/netdb/getaddrinfo_null.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include From 8945b07415161a7f3faa94978e621e99735d1da2 Mon Sep 17 00:00:00 2001 From: auronandace Date: Mon, 13 Apr 2026 11:29:13 +0100 Subject: [PATCH 4/4] add include guard for the moved bits --- src/header/sys_socket/cbindgen.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/header/sys_socket/cbindgen.toml b/src/header/sys_socket/cbindgen.toml index 03207837a5..42474483a2 100644 --- a/src/header/sys_socket/cbindgen.toml +++ b/src/header/sys_socket/cbindgen.toml @@ -5,6 +5,9 @@ after_includes = """ #include // for socklen_t """ trailer = """ +#ifndef _RELIBC_BITS_SYS_SOCKET_H +#define _RELIBC_BITS_SYS_SOCKET_H + struct sockaddr_storage { sa_family_t ss_family; char __ss_padding[128-sizeof(long)-sizeof(sa_family_t)]; @@ -26,6 +29,8 @@ struct sockaddr_storage { #define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr))) #define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) // } from musl, license MIT + +#endif // _RELIBC_BITS_SYS_SOCKET_H """ language = "C" style = "Tag"