-
Notifications
You must be signed in to change notification settings - Fork 67
Make sock_ctrl_msg work on unix #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@uran0sH please check the CI |
2784a19 to
82a42b6
Compare
Fixed |
stefano-garzarella
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of macos can we refer to unix in the changelog/pr title/comments/etc.
|
We can do later (e.g. in a simple freebsd vm), but yeah, please use |
|
Because I am not sure about this commit 7d8683c, I think we should discuss here. Below it's what i found. mac The difference is the value of align: linux uses the size of usize, and macos uses the size of u32. This is also for |
What are your concerns about that patch? |
|
@uran0sH could you clarify what exactly test failure you were seeing with that gets fixed by 7d8683c? I had @ShadowCurse run the tests without that commit on his mac, and they still passed 🤔 |
Sorry for replying so late. It doesn't fail every time. There is a probability that it will fail the test. Run this command multiple times. When it fails, it reports: (I print which fd will be closed) It can always pass on linux. |
c429fa6 to
eaeed1b
Compare
Changed |
Can be a race between the 2 tests? |
I think the strange thing here is have 0 as fd to close (multiple times), which is really strange. |
In Linux I see that |
|
Mhh, the IO violation then probably comes not from this function, but rather from the Rust "runtime" shutting down and dropping its |
Agree, but we should fix in another PR, see #245 (comment) for quick fix for this PR. |
|
@uran0sH please revert 6a4b213 and apply the following patch: From f0bccfe6479937f18d231be613794d01c4f15d6f Mon Sep 17 00:00:00 2001
From: Stefano Garzarella <sgarzare@redhat.com>
Date: Fri, 18 Jul 2025 11:36:24 +0200
Subject: [PATCH] sock_ctrl_msg: fix raw_recvmsg() on macOS
From: Stefano Garzarella <sgarzare@redhat.com>
Limit cmsg_len to the size of cmsg_buffer, since it seems that in macOS,
when `MSG_CTRUNC` is set, `cmsg.cmsg_len` is not updated with the
current size, but left with the size of the sender (this is not
happening on Linux).
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
src/unix/sock_ctrl_msg.rs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/unix/sock_ctrl_msg.rs b/src/unix/sock_ctrl_msg.rs
index af1cebf..f2021f3 100644
--- a/src/unix/sock_ctrl_msg.rs
+++ b/src/unix/sock_ctrl_msg.rs
@@ -224,8 +224,8 @@ unsafe fn raw_recvmsg(
// read.
let cmsg = (cmsg_ptr as *mut cmsghdr).read_unaligned();
if cmsg.cmsg_level == SOL_SOCKET && cmsg.cmsg_type == SCM_RIGHTS {
- let fds_count: usize =
- (cmsg.cmsg_len as usize - CMSG_LEN(0) as usize) / size_of::<RawFd>();
+ let cmsg_len: usize = std::cmp::min(cmsg.cmsg_len as usize, cmsg_capacity);
+ let fds_count: usize = (cmsg_len - CMSG_LEN(0) as usize) / size_of::<c_int>();
// The sender can transmit more data than we can buffer. If a message is too long to
// fit in the supplied buffer, excess bytes may be discarded depending on the type of
// socket the message is received from.
--
2.50.1
|
Yeah, fair enough, I'll submit something after this is merged, there's a few other things I'd like to have a look at anyway :) |
|
@uran0sH okay, about this PR, the next steps are:
If we fix those, I think we are ready to merge, so @roypat can rebase the #246 on top if this |
Eventfd is linux-specific. In order to support unix, we need to replace it with pipefd. Signed-off-by: Wenyu Huang <huangwenyuu@outlook.com>
Fixed these. |
|
@uran0sH great thanks! The final result LGTM, but please move the commit where to update the libc version before the commit where you use CMSG_ macros, otherwise, if in the future we will bisect this crate, we can have a build issue. |
Update libc version to fix:
```
error[E0015]: cannot call non-const function `CMSG_SPACE` in constants
--> src/unix/sock_ctrl_msg.rs:73:14
|
73 | unsafe { CMSG_SPACE(size_of::<RawFd>() as u32 * 32) as usize };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
```
Signed-off-by: Wenyu Huang <huangwenyuu@outlook.com>
libc also provides functions with the same functionality and supports multiple platforms, so we can replace CMSG_*! with libc equivalent. The test `buffer_len` doesn't need, if we use libc. Signed-off-by: Wenyu Huang <huangwenyuu@outlook.com>
Make some modifications to sock_ctl_msg to support Unix systems. Including adjusting the size of cmsg_len for different platforms. Signed-off-by: Wenyu Huang <huangwenyuu@outlook.com>
Limit cmsg_len to the size of cmsg_buffer, since it seems that in macOS, when `MSG_CTRUNC` is set, `cmsg.cmsg_len` is not updated with the current size, but left with the size of the sender (this is not happening on Linux). Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Signed-off-by: Wenyu Huang <huangwenyuu@outlook.com>
Signed-off-by: Wenyu Huang <huangwenyuu@outlook.com>
done |
Sorry I'm a bit late, I wanted to comment about this last week, I also think it is incorrect to close FDs in that function, if the process reaches its NOFILE limit the received FDs are already closed, so close() will fire the io safety violation (since 1.80 [0]), or worse closing a reused FD. |
Summary of the PR
Make sock_ctrl_msg work on unix. Split to these commits:
Requirements
Before submitting your PR, please make sure you addressed the following
requirements:
git commit -s), and the commit message has max 60 characters for thesummary and max 75 characters for each description line.
test.
Release" section of CHANGELOG.md (if no such section exists, please create one).
unsafecode is properly documented.