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

Misc cleanup #814

Merged
merged 8 commits into from
Dec 11, 2017
Merged

Misc cleanup #814

merged 8 commits into from
Dec 11, 2017

Conversation

Susurrus
Copy link
Contributor

@Susurrus Susurrus commented Dec 6, 2017

Add more traits to the various datatypes in nix. Also try to utilize more libc types where appropriate.

This is still WIP because:

  • Looking for feedback on ba2d85b. Does it make sense for bitflags structs to have Ord & PartialOrd derived for them?
  • Need to implement a newtype wrapper around libc::linger with both a new() constructor and getters. Additionally this needs manual implementations of Eq, PartialEq, Debug (I've been skipping manually implementing Hash for now.
  • In 2b3fb11 I need to implement newtype wrappers around ip_mreq and ipv6_mreq that have new() constructors and field getters. Additionally they need manual implementations of Eq, PartialEq, Debug (I've been skipping manually implementing Hash for now. This commit also needs a CHANGELOG entry.
  • ed79cfb needs a CHANGELOG entry because its variants names have changed with the switch to using libc_bitflags!.

@Susurrus
Copy link
Contributor Author

Susurrus commented Dec 6, 2017

@asomers It looks like ucred isn't in libc for FreeBSD, but I did find that it should exist here. I am a little confused though on whether this is a similar interface on Linux and FreeBSD or if this should be restricted to Linux, as I'm not too familiar with the whole ucred API.

@Susurrus
Copy link
Contributor Author

Susurrus commented Dec 6, 2017

For example, I'm not certain if we should be using ucred on FreeBSD or should be using xucred instead.

@Susurrus
Copy link
Contributor Author

Susurrus commented Dec 6, 2017

For the BSDs, it's looks like NetBSD uses the sockcred struct, which is missing from libc and FreeBSD and DragonFlyBSD uses the cmsgcred struct, also missing from libc. I don't believe OpenBSD has this interface as SCM_CREDS is missing from their socket.h.

Next steps are to add those structs to libc. I'm not certain how I want to abstract this, if I want to hide all these structs behind the new UserCredentials datatype with appropriate accessor methods or have separate types for each different platform. I'm leaning towards abstrating it all away behind UserCredentials, but we'll see how the implementation goes.

@asomers
Copy link
Member

asomers commented Dec 7, 2017

@Susurrus The answer lies here. Despite the doc comments, it looks like Nix is using UnixCredentials with Linux's SO_PEERCRED socket option. The analogous socket option on FreeBSD is LOCAL_PEERCRED, which returns a struct xucred. struct xucred, however, has different fields than Linux's struct ucred, so we would need to conditionally compile the various accessors. Linux also has an SCM_CREDENTIALS message type for use with sendmsg. FreeBSD uses a struct cmsgcred for that. However, Nix doesn't seem to support that yet.

@@ -173,6 +170,47 @@ libc_bitflags!{
}
}

/// Unix credentials of the sending process.
///
/// This struct is used with the `SCM_CREDENTIALS` ancillary message for UNIX sockets.
Copy link
Member

Choose a reason for hiding this comment

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

This message, while true, is red herring because Nix doesn't support SCM_CREDENTIALS. It should read "This struct is used with the SO_PEERCRED/LOCAL_PEERCRED socket options for UNIX sockets.

/// This struct is used with the `SCM_CREDENTIALS` ancillary message for UNIX sockets.
#[repr(C)]
#[derive(Clone, Copy)]
pub struct UnixCredentials(libc::ucred);
Copy link
Member

Choose a reason for hiding this comment

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

should be libc::xucred on FreeBSD.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a very different looking struct, I'm really surprised that this is correct. I would have thought it'd be the struct I mentioned above, cmsgcred, which does have pid, uid, and gid fields.

Copy link
Member

Choose a reason for hiding this comment

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

Except there are two problems. If UnixCredentials were actually used with SCM_CREDENTIALS, then you would be correct. struct cmsgcred would be the struct to wrap. However, the SCM_CREDENTIALS comment is red herring. UnixCredentials is actually used as a socket option, with SO_PEERCRED. That's why I say that it should wrap xucred.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From this SO question it seems like SCM_CREDENTIALS should be what we offer as an API and we abandon the SO_PEERCRED implementation, since one looks to be a newer/better version of the other.

@@ -151,7 +152,7 @@ sockopt_impl!(Both, OobInline, libc::SOL_SOCKET, libc::SO_OOBINLINE, bool);
sockopt_impl!(GetOnly, SocketError, libc::SOL_SOCKET, libc::SO_ERROR, i32);
sockopt_impl!(Both, KeepAlive, libc::SOL_SOCKET, libc::SO_KEEPALIVE, bool);
#[cfg(all(target_os = "linux", not(target_arch="arm")))]
sockopt_impl!(GetOnly, PeerCredentials, libc::SOL_SOCKET, libc::SO_PEERCRED, super::ucred);
sockopt_impl!(GetOnly, PeerCredentials, libc::SOL_SOCKET, libc::SO_PEERCRED, super::UnixCredentials);
Copy link
Member

Choose a reason for hiding this comment

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

On FreeBSD, this should be LOCAL_PEERCRED instead of SO_PEERCRED.

@Susurrus Susurrus force-pushed the misc_cleanup branch 2 times, most recently from 3462db7 to 3a741ed Compare December 8, 2017 05:11
@Susurrus
Copy link
Contributor Author

Susurrus commented Dec 8, 2017

Note that all Linux MIPS build jobs will fail until rust-lang/libc#867 is merged. Once that's merged, this can go in.

I'm going to back off expanding APIs to any other platforms as it's just too much work. I'd really like to get all this libc cleanup stuff wrapper up, and it's just taking way too long! I keep finding new constants or types we have squirreled away that should be in libc instead. Hopefully this gets us pretty damn close to the end of it.

@Susurrus Susurrus changed the title WIP: Misc cleanup Misc cleanup Dec 8, 2017
@Susurrus
Copy link
Contributor Author

Susurrus commented Dec 8, 2017

Should go green once this runs through. @asomers Let me know if you have any feedback here, hopefully these changes are uncontroversial at this point.

@Susurrus Susurrus force-pushed the misc_cleanup branch 4 times, most recently from e15e5dc to 7c387b6 Compare December 9, 2017 19:01
@Susurrus
Copy link
Contributor Author

Susurrus commented Dec 9, 2017

@asomers Can I get a final check with you on this before I merge it?

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.

It all looks good, but I think you should group the CHANGELOG entries with the changes that made them.

CHANGELOG.md Outdated
@@ -83,6 +85,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- The `ucred` struct has been removed in favor of a `UserCredentials` struct that
contains only getters for its fields.
([#814](https://github.com/nix-rust/nix/pull/814))
- Both `ip_mreq` and `ipv6_mreq` have been replaced with `IpMembershipRequest` and
Copy link
Member

Choose a reason for hiding this comment

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

This bit should be squashed with a9022b5

@@ -98,6 +103,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#747](https://github.com/nix-rust/nix/pull/747))
- The `Errno` variants are no longer reexported from the `errno` module. `Errno` itself is no longer reexported from the
crate root and instead must be accessed using the `errno` module. ([#696](https://github.com/nix-rust/nix/pull/696))
- Removed `MS_VERBOSE`, `MS_NOSEC`, and `MS_BORN` from `MsFlags`. These
Copy link
Member

Choose a reason for hiding this comment

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

This part should be squashed with 5837c6d

@@ -48,6 +48,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Added the `from_raw()` method to `WaitStatus` for converting raw status values
to `WaitStatus` independent of syscalls.
([#741](https://github.com/nix-rust/nix/pull/741))
- Added more standard trait implementations for various types.
Copy link
Member

Choose a reason for hiding this comment

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

This bit should be squashed with e59c72b and 310d205

- The `ucred` struct has been removed in favor of a `UserCredentials` struct that
contains only getters for its fields.
([#814](https://github.com/nix-rust/nix/pull/814))
- Both `ip_mreq` and `ipv6_mreq` have been replaced with `IpMembershipRequest` and
Copy link
Member

Choose a reason for hiding this comment

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

This bit should be squashed wit a9022b5

([#749](https://github.com/nix-rust/nix/pull/749))
- `AioCb::Drop` will now panic if the `AioCb` is still in-progress ([#715](https://github.com/nix-rust/nix/pull/715))
- Restricted `nix::sys::socket::UnixAddr::new_abstract` to Linux and Android only.
([#785](https://github.com/nix-rust/nix/pull/785))
- The `ucred` struct has been removed in favor of a `UserCredentials` struct that
Copy link
Member

Choose a reason for hiding this comment

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

This bit should be squashed with c25f551

@Susurrus
Copy link
Contributor Author

You're right, I was just being lazy about it.

Bors r+ asomers

bors bot added a commit that referenced this pull request Dec 11, 2017
814: Misc cleanup r=Susurrus a=Susurrus

Add more traits to the various datatypes in `nix`. Also try to utilize more `libc` types where appropriate.

This is still WIP because:
  * Looking for feedback on ba2d85b. Does it make sense for bitflags structs to have `Ord` & `PartialOrd` derived for them?
  * Need to implement a newtype wrapper around `libc::linger` with both a `new()` constructor and getters. Additionally this needs manual implementations of `Eq`, `PartialEq`, `Debug` (I've been skipping manually implementing `Hash` for now.
  * In 2b3fb11 I need to implement newtype wrappers around `ip_mreq` and `ipv6_mreq` that have `new()` constructors and field getters. Additionally they need manual implementations of `Eq`, `PartialEq`, `Debug` (I've been skipping manually implementing `Hash` for now. This commit also needs a CHANGELOG entry.
  * ed79cfb needs a CHANGELOG entry because its variants names have changed with the switch to using `libc_bitflags!`.
@bors
Copy link
Contributor

bors bot commented Dec 11, 2017

@bors bors bot merged commit 111950d into nix-rust:master Dec 11, 2017
@Susurrus Susurrus deleted the misc_cleanup branch December 11, 2017 19:08
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.

2 participants