Skip to content
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

Add better support for unnamed unix socket addrs #1857

Merged
merged 4 commits into from Nov 21, 2022

Conversation

stevenengler
Copy link
Contributor

@stevenengler stevenengler commented Nov 7, 2022

This adds the following 2 functions/methods: UnixAddr::new_unnamed and UnixAddr::is_unnamed.

Closes #1585

unix(7) on Linux:

unnamed: A stream socket that has not been bound to a pathname using bind(2) has no name. Likewise, the two sockets created by socketpair(2) are unnamed. When the address of an unnamed socket is returned, its length is sizeof(sa_family_t), and sun_path should not be inspected.

Edit: This currently isn't working on BSD, but I see why. Will fix it shortly.

@stevenengler stevenengler force-pushed the unnamed-unix-sockets branch 5 times, most recently from b1af529 to 60f17d7 Compare November 7, 2022 23:40
@stevenengler
Copy link
Contributor Author

stevenengler commented Nov 7, 2022

I decided to only enable these on Linux/Android, since unnamed unix sockets seem to behave differently on FreeBSD and I can't find any documentation for its behaviour. For example when running getsockname() on the fd returned from socketpair(), it returns an addrlen of 16 bytes while SUN_LEN(&addr) returns 2. I'm not sure that the existing code in UnixAddrKind::get() handles this correctly either on FreeBSD.

The current test failures seem to be unrelated, probably from a new clippy version.

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that defining this for Linux only is appropriate, because AFAIK no BSD OS documents such a thing. But don't forget the CHANGELOG. Also, I'd like to merge this before PR #1871.


let addr_1: UnixAddr = getsockname(fd_1).expect("getsockname failed");
assert!(addr_1.is_unnamed());
assert_eq!(addr_1, UnixAddr::new_unnamed());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part guaranteed to work, according to any Linux documentation, or is it just luck? I'm not sure we should have this assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linux says "When the address of an unnamed socket is returned, its length is sizeof(sa_family_t)", so the only possible unnamed unix socket address it could return would be sockaddr_un { sun_family: AF_UNIX, ... } with length 2. But I agree that the documentation around this still isn't great, so I removed this assertion.

@asomers
Copy link
Member

asomers commented Nov 19, 2022

Also, rebasing should fix the CI failures.

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I violated my own request and merged #1871 first. So I'll fix the subtle conflict myself.

bors r+

@bors bors bot merged commit 79f04fb into nix-rust:master Nov 21, 2022
@stevenengler stevenengler deleted the unnamed-unix-sockets branch November 21, 2022 18:07
asomers added a commit to asomers/nix that referenced this pull request Nov 21, 2022
Use it in the from_sockaddr_un_abstract_unnamed test.  That test and
this method were introduced by PRs nix-rust#1871 and nix-rust#1857, which crossed each
other.
bors bot added a commit that referenced this pull request Nov 21, 2022
1880: Use the new UnixAddr::new_unnamed in the unit tests r=rtzoeller a=asomers

Use it in the from_sockaddr_un_abstract_unnamed test.  That test and this method were introduced by PRs #1871 and #1857, which crossed each other.

Co-authored-by: Alan Somers <asomers@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No longer able to create an unnamed UnixAddr as of nix 23.0
2 participants