From 3efdc8a6cd2e0f184a5fb05b27e8c61f5d15cd4d Mon Sep 17 00:00:00 2001 From: Pekka Ristola Date: Sat, 25 Oct 2025 14:58:59 +0300 Subject: [PATCH 1/2] Fix cfgs in sockopt tests `rustix::time` and the txtime socket options are only available with the `time` feature, but they are unconditionally used in sockopt tests. Mark them with `cfg(feature = "time")` to enable compiling sockopt tests with just the `net` feature enabled. Otherwise `cargo test --features net` would end up with a compile error. Fixes: 65b04ae9fd18 ("Add support for SO_TXTIME / SCM_TXTIME (#1409)") --- tests/net/sockopt.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/net/sockopt.rs b/tests/net/sockopt.rs index 5177bafd8..bee072c2e 100644 --- a/tests/net/sockopt.rs +++ b/tests/net/sockopt.rs @@ -9,10 +9,10 @@ use rustix::io; target_env = "newlib" ))] use rustix::net::ipproto; -#[cfg(target_os = "linux")] +#[cfg(all(target_os = "linux", feature = "time"))] use rustix::net::TxTimeFlags; use rustix::net::{sockopt, AddressFamily, SocketType}; -#[cfg(target_os = "linux")] +#[cfg(all(target_os = "linux", feature = "time"))] use rustix::time::ClockId; use std::net::Ipv4Addr; use std::time::Duration; @@ -668,7 +668,7 @@ fn test_sockopts_multicast_ifv6() { } #[test] -#[cfg(target_os = "linux")] +#[cfg(all(target_os = "linux", feature = "time"))] fn test_sockopts_txtime() { crate::init(); From 4b757ca149ae368a0b75f66640c266d18aee0fc1 Mon Sep 17 00:00:00 2001 From: Pekka Ristola Date: Sat, 25 Oct 2025 15:20:28 +0300 Subject: [PATCH 2/2] Test `socket_passcred` only for Unix domain sockets Since Linux 6.16, `SO_PASSCRED` is allowed only for `AF_UNIX`, `AF_NETLINK` and `AF_BLUETOOTH` sockets. The tests used to get/set this option for IP sockets, which doesn't work with new kernels, so make the tests use a Unix domain socket instead. On new kernels, the tests fail with: ``` ---- sockopt::test_sockopts_ipv6 stdout ---- thread 'sockopt::test_sockopts_ipv6' panicked at tests/net/sockopt.rs:44:42: called `Result::unwrap()` on an `Err` value: Os { code: 95, kind: Unsupported, message: "Operation not supported" } ---- sockopt::test_sockopts_ipv4 stdout ---- thread 'sockopt::test_sockopts_ipv4' panicked at tests/net/sockopt.rs:44:42: called `Result::unwrap()` on an `Err` value: Os { code: 95, kind: Unsupported, message: "Operation not supported" } ``` The restriction was introduced to Linux in this commit: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7d8d93fdde50b86bbbf46a203c368ed320e729ab --- tests/net/sockopt.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/net/sockopt.rs b/tests/net/sockopt.rs index bee072c2e..d139d9a5c 100644 --- a/tests/net/sockopt.rs +++ b/tests/net/sockopt.rs @@ -40,8 +40,6 @@ fn test_sockopts_socket(s: &OwnedFd) { assert!(!sockopt::socket_broadcast(s).unwrap()); // On a new socket we shouldn't have a linger yet. assert!(sockopt::socket_linger(s).unwrap().is_none()); - #[cfg(linux_kernel)] - assert!(!sockopt::socket_passcred(s).unwrap()); // On a new socket we shouldn't have an error yet. assert_eq!(sockopt::socket_error(s).unwrap(), Ok(())); @@ -119,15 +117,6 @@ fn test_sockopts_socket(s: &OwnedFd) { // Check that we have a linger of at least the time we set. assert!(sockopt::socket_linger(s).unwrap().unwrap() >= Duration::new(1, 1)); - #[cfg(linux_kernel)] - { - // Set the passcred flag; - sockopt::set_socket_passcred(s, true).unwrap(); - - // Check that the passcred flag is set. - assert!(sockopt::socket_passcred(s).unwrap()); - } - // Set the receive buffer size. let size = sockopt::socket_recv_buffer_size(s).unwrap(); sockopt::set_socket_recv_buffer_size(s, size * 2).unwrap(); @@ -527,6 +516,18 @@ fn test_sockopts_ipv6() { test_sockopts_tcp(&s); } +#[cfg(linux_kernel)] +#[test] +fn test_socket_passcred() { + crate::init(); + + let s = rustix::net::socket(AddressFamily::UNIX, SocketType::STREAM, None).unwrap(); + + assert_eq!(sockopt::socket_passcred(&s), Ok(false)); + sockopt::set_socket_passcred(&s, true).unwrap(); + assert_eq!(sockopt::socket_passcred(&s), Ok(true)); +} + #[cfg(any(linux_kernel, target_os = "cygwin"))] #[test] fn test_socketopts_ip_mtu() {