Skip to content

Commit 291d618

Browse files
committed
Merge #814
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!`.
2 parents b957a7a + 111950d commit 291d618

File tree

10 files changed

+213
-136
lines changed

10 files changed

+213
-136
lines changed

CHANGELOG.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2222
([#739](https://github.com/nix-rust/nix/pull/739))
2323
- Expose `signalfd` module on Android as well.
2424
([#739](https://github.com/nix-rust/nix/pull/739))
25-
- Added nix::sys::ptrace::detach.
25+
- Added nix::sys::ptrace::detach.
2626
([#749](https://github.com/nix-rust/nix/pull/749))
2727
- Added timestamp socket control message variant:
2828
`nix::sys::socket::ControlMessage::ScmTimestamp`
@@ -48,6 +48,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4848
- Added the `from_raw()` method to `WaitStatus` for converting raw status values
4949
to `WaitStatus` independent of syscalls.
5050
([#741](https://github.com/nix-rust/nix/pull/741))
51+
- Added more standard trait implementations for various types.
52+
([#814](https://github.com/nix-rust/nix/pull/814))
5153

5254
### Changed
5355
- Use native `pipe2` on all BSD targets. Users should notice no difference.
@@ -75,11 +77,17 @@ This project adheres to [Semantic Versioning](http://semver.org/).
7577
([#731](https://github.com/nix-rust/nix/pull/731))
7678
- Marked `pty::ptsname` function as `unsafe`
7779
([#744](https://github.com/nix-rust/nix/pull/744))
78-
- Moved constants ptrace request, event and options to enums and updated ptrace functions and argument types accordingly.
80+
- Moved constants ptrace request, event and options to enums and updated ptrace functions and argument types accordingly.
7981
([#749](https://github.com/nix-rust/nix/pull/749))
8082
- `AioCb::Drop` will now panic if the `AioCb` is still in-progress ([#715](https://github.com/nix-rust/nix/pull/715))
8183
- Restricted `nix::sys::socket::UnixAddr::new_abstract` to Linux and Android only.
8284
([#785](https://github.com/nix-rust/nix/pull/785))
85+
- The `ucred` struct has been removed in favor of a `UserCredentials` struct that
86+
contains only getters for its fields.
87+
([#814](https://github.com/nix-rust/nix/pull/814))
88+
- Both `ip_mreq` and `ipv6_mreq` have been replaced with `IpMembershipRequest` and
89+
`Ipv6MembershipRequest`.
90+
([#814](https://github.com/nix-rust/nix/pull/814))
8391

8492

8593
### Fixed
@@ -95,6 +103,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
95103
([#747](https://github.com/nix-rust/nix/pull/747))
96104
- The `Errno` variants are no longer reexported from the `errno` module. `Errno` itself is no longer reexported from the
97105
crate root and instead must be accessed using the `errno` module. ([#696](https://github.com/nix-rust/nix/pull/696))
106+
- Removed `MS_VERBOSE`, `MS_NOSEC`, and `MS_BORN` from `MsFlags`. These
107+
are internal kernel flags and should never have been exposed.
108+
([#814](https://github.com/nix-rust/nix/pull/814))
109+
98110

99111
## [0.9.0] 2017-07-23
100112

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
[Documentation (Releases)](https://docs.rs/nix/)
77

8-
[Documentation (Development)](https://nix-rust.github.io/nix/nix/index.html)
9-
108
Nix seeks to provide friendly bindings to various *nix platform APIs (Linux, Darwin,
119
...). The goal is to not provide a 100% unified interface, but to unify
1210
what can be while still providing platform specific APIs.

src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ macro_rules! libc_enum {
9090
}
9191
) => {
9292
$($attrs)*
93-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
93+
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
9494
enum $BitFlags {
9595
$($entries)*
9696
}
@@ -106,7 +106,7 @@ macro_rules! libc_enum {
106106
}
107107
) => {
108108
$($attrs)*
109-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
109+
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
110110
pub enum $BitFlags {
111111
$($entries)*
112112
}

src/mount.rs

+40-32
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,47 @@ use libc;
33
use {Result, NixPath};
44
use errno::Errno;
55

6-
bitflags!(
6+
libc_bitflags!(
77
pub struct MsFlags: c_ulong {
8-
const MS_RDONLY = libc::MS_RDONLY; // Mount read-only
9-
const MS_NOSUID = libc::MS_NOSUID; // Ignore suid and sgid bits
10-
const MS_NODEV = libc::MS_NODEV; // Disallow access to device special files
11-
const MS_NOEXEC = libc::MS_NOEXEC; // Disallow program execution
12-
const MS_SYNCHRONOUS = libc::MS_SYNCHRONOUS; // Writes are synced at once
13-
const MS_REMOUNT = libc::MS_REMOUNT; // Alter flags of a mounted FS
14-
const MS_MANDLOCK = libc::MS_MANDLOCK; // Allow mandatory locks on a FS
15-
const MS_DIRSYNC = libc::MS_DIRSYNC; // Directory modifications are synchronous
16-
const MS_NOATIME = libc::MS_NOATIME; // Do not update access times
17-
const MS_NODIRATIME = libc::MS_NODIRATIME; // Do not update directory access times
18-
const MS_BIND = libc::MS_BIND; // Linux 2.4.0 - Bind directory at different place
19-
const MS_MOVE = libc::MS_MOVE;
20-
const MS_REC = libc::MS_REC;
21-
const MS_VERBOSE = 1 << 15; // Deprecated
22-
const MS_SILENT = libc::MS_SILENT;
23-
const MS_POSIXACL = libc::MS_POSIXACL;
24-
const MS_UNBINDABLE = libc::MS_UNBINDABLE;
25-
const MS_PRIVATE = libc::MS_PRIVATE;
26-
const MS_SLAVE = libc::MS_SLAVE;
27-
const MS_SHARED = libc::MS_SHARED;
28-
const MS_RELATIME = libc::MS_RELATIME;
29-
const MS_KERNMOUNT = libc::MS_KERNMOUNT;
30-
const MS_I_VERSION = libc::MS_I_VERSION;
31-
const MS_STRICTATIME = libc::MS_STRICTATIME;
32-
const MS_NOSEC = 1 << 28;
33-
const MS_BORN = 1 << 29;
34-
const MS_ACTIVE = libc::MS_ACTIVE;
35-
const MS_NOUSER = libc::MS_NOUSER;
36-
const MS_RMT_MASK = libc::MS_RMT_MASK;
37-
const MS_MGC_VAL = libc::MS_MGC_VAL;
38-
const MS_MGC_MSK = libc::MS_MGC_MSK;
8+
/// Mount read-only
9+
MS_RDONLY;
10+
/// Ignore suid and sgid bits
11+
MS_NOSUID;
12+
/// Disallow access to device special files
13+
MS_NODEV;
14+
/// Disallow program execution
15+
MS_NOEXEC;
16+
/// Writes are synced at once
17+
MS_SYNCHRONOUS;
18+
/// Alter flags of a mounted FS
19+
MS_REMOUNT;
20+
/// Allow mandatory locks on a FS
21+
MS_MANDLOCK;
22+
/// Directory modifications are synchronous
23+
MS_DIRSYNC;
24+
/// Do not update access times
25+
MS_NOATIME;
26+
/// Do not update directory access times
27+
MS_NODIRATIME;
28+
/// Linux 2.4.0 - Bind directory at different place
29+
MS_BIND;
30+
MS_MOVE;
31+
MS_REC;
32+
MS_SILENT;
33+
MS_POSIXACL;
34+
MS_UNBINDABLE;
35+
MS_PRIVATE;
36+
MS_SLAVE;
37+
MS_SHARED;
38+
MS_RELATIME;
39+
MS_KERNMOUNT;
40+
MS_I_VERSION;
41+
MS_STRICTATIME;
42+
MS_ACTIVE;
43+
MS_NOUSER;
44+
MS_RMT_MASK;
45+
MS_MGC_VAL;
46+
MS_MGC_MSK;
3947
}
4048
);
4149

src/sys/epoll.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ libc_bitflags!(
2828
}
2929
);
3030

31-
#[derive(Clone, Copy, Eq, PartialEq)]
31+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
3232
#[repr(i32)]
3333
pub enum EpollOp {
3434
EpollCtlAdd = libc::EPOLL_CTL_ADD,

src/sys/signalfd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub fn signalfd(fd: RawFd, mask: &SigSet, flags: SfdFlags) -> Result<RawFd> {
7979
/// Err(err) => (), // some error happend
8080
/// }
8181
/// ```
82-
#[derive(Debug)]
82+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
8383
pub struct SignalFd(RawFd);
8484

8585
impl SignalFd {

src/sys/socket/mod.rs

+123-30
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
use {Error, Result};
55
use errno::Errno;
66
use features;
7-
use libc::{self, c_void, c_int, socklen_t, size_t, pid_t, uid_t, gid_t};
8-
use std::{mem, ptr, slice};
7+
use libc::{self, c_void, c_int, socklen_t, size_t};
8+
use std::{fmt, mem, ptr, slice};
99
use std::os::unix::io::RawFd;
1010
use sys::time::TimeVal;
1111
use sys::uio::IoVec;
1212

1313
mod addr;
1414
mod ffi;
15-
mod multicast;
1615
pub mod sockopt;
1716

1817
/*
@@ -34,22 +33,14 @@ pub use self::addr::{
3433
pub use ::sys::socket::addr::netlink::NetlinkAddr;
3534

3635
pub use libc::{
37-
in_addr,
38-
in6_addr,
36+
sa_family_t,
3937
sockaddr,
4038
sockaddr_in,
4139
sockaddr_in6,
40+
sockaddr_storage,
4241
sockaddr_un,
43-
sa_family_t,
44-
};
45-
46-
pub use self::multicast::{
47-
ip_mreq,
48-
ipv6_mreq,
4942
};
5043

51-
pub use libc::sockaddr_storage;
52-
5344
/// These constants are used to specify the communication semantics
5445
/// when creating a socket with [`socket()`](fn.socket.html)
5546
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
@@ -77,6 +68,7 @@ pub enum SockType {
7768
/// Constants used in [`socket`](fn.socket.html) and [`socketpair`](fn.socketpair.html)
7869
/// to specify the protocol to use.
7970
#[repr(i32)]
71+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
8072
pub enum SockProtocol {
8173
/// TCP protocol ([ip(7)](http://man7.org/linux/man-pages/man7/ip.7.html))
8274
Tcp = libc::IPPROTO_TCP,
@@ -174,6 +166,121 @@ libc_bitflags!{
174166
}
175167
}
176168

169+
cfg_if! {
170+
if #[cfg(all(target_os = "linux", not(target_arch = "arm")))] {
171+
/// Unix credentials of the sending process.
172+
///
173+
/// This struct is used with the `SO_PEERCRED` ancillary message for UNIX sockets.
174+
#[repr(C)]
175+
#[derive(Clone, Copy)]
176+
pub struct UnixCredentials(libc::ucred);
177+
178+
impl UnixCredentials {
179+
/// Returns the process identifier
180+
pub fn pid(&self) -> libc::pid_t {
181+
self.0.pid
182+
}
183+
184+
/// Returns the user identifier
185+
pub fn uid(&self) -> libc::uid_t {
186+
self.0.uid
187+
}
188+
189+
/// Returns the group identifier
190+
pub fn gid(&self) -> libc::gid_t {
191+
self.0.gid
192+
}
193+
}
194+
195+
impl PartialEq for UnixCredentials {
196+
fn eq(&self, other: &Self) -> bool {
197+
self.0.pid == other.0.pid && self.0.uid == other.0.uid && self.0.gid == other.0.gid
198+
}
199+
}
200+
impl Eq for UnixCredentials {}
201+
202+
impl fmt::Debug for UnixCredentials {
203+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
204+
f.debug_struct("UnixCredentials")
205+
.field("pid", &self.0.pid)
206+
.field("uid", &self.0.uid)
207+
.field("gid", &self.0.gid)
208+
.finish()
209+
}
210+
}
211+
}
212+
}
213+
214+
/// Request for multicast socket operations
215+
///
216+
/// This is a wrapper type around `ip_mreq`.
217+
#[repr(C)]
218+
#[derive(Clone, Copy)]
219+
pub struct IpMembershipRequest(libc::ip_mreq);
220+
221+
impl IpMembershipRequest {
222+
/// Instantiate a new `IpMembershipRequest`
223+
///
224+
/// If `interface` is `None`, then `Ipv4Addr::any()` will be used for the interface.
225+
pub fn new(group: Ipv4Addr, interface: Option<Ipv4Addr>) -> Self {
226+
IpMembershipRequest(libc::ip_mreq {
227+
imr_multiaddr: group.0,
228+
imr_interface: interface.unwrap_or(Ipv4Addr::any()).0,
229+
})
230+
}
231+
}
232+
233+
impl PartialEq for IpMembershipRequest {
234+
fn eq(&self, other: &Self) -> bool {
235+
self.0.imr_multiaddr.s_addr == other.0.imr_multiaddr.s_addr
236+
&& self.0.imr_interface.s_addr == other.0.imr_interface.s_addr
237+
}
238+
}
239+
impl Eq for IpMembershipRequest {}
240+
241+
impl fmt::Debug for IpMembershipRequest {
242+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
243+
f.debug_struct("IpMembershipRequest")
244+
.field("imr_multiaddr", &self.0.imr_multiaddr.s_addr)
245+
.field("imr_interface", &self.0.imr_interface.s_addr)
246+
.finish()
247+
}
248+
}
249+
250+
/// Request for ipv6 multicast socket operations
251+
///
252+
/// This is a wrapper type around `ipv6_mreq`.
253+
#[repr(C)]
254+
#[derive(Clone, Copy)]
255+
pub struct Ipv6MembershipRequest(libc::ipv6_mreq);
256+
257+
impl Ipv6MembershipRequest {
258+
/// Instantiate a new `Ipv6MembershipRequest`
259+
pub fn new(group: Ipv6Addr) -> Self {
260+
Ipv6MembershipRequest(libc::ipv6_mreq {
261+
ipv6mr_multiaddr: group.0,
262+
ipv6mr_interface: 0,
263+
})
264+
}
265+
}
266+
267+
impl PartialEq for Ipv6MembershipRequest {
268+
fn eq(&self, other: &Self) -> bool {
269+
self.0.ipv6mr_multiaddr.s6_addr == other.0.ipv6mr_multiaddr.s6_addr &&
270+
self.0.ipv6mr_interface == other.0.ipv6mr_interface
271+
}
272+
}
273+
impl Eq for Ipv6MembershipRequest {}
274+
275+
impl fmt::Debug for Ipv6MembershipRequest {
276+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
277+
f.debug_struct("Ipv6MembershipRequest")
278+
.field("ipv6mr_multiaddr", &self.0.ipv6mr_multiaddr.s6_addr)
279+
.field("ipv6mr_interface", &self.0.ipv6mr_interface)
280+
.finish()
281+
}
282+
}
283+
177284
/// Copy the in-memory representation of src into the byte slice dst,
178285
/// updating the slice to point to the remainder of dst only. Unsafe
179286
/// because it exposes all bytes in src, which may be UB if some of them
@@ -799,21 +906,6 @@ pub fn send(fd: RawFd, buf: &[u8], flags: MsgFlags) -> Result<usize> {
799906
Errno::result(ret).map(|r| r as usize)
800907
}
801908

802-
#[repr(C)]
803-
#[derive(Clone, Copy, Debug)]
804-
pub struct linger {
805-
pub l_onoff: c_int,
806-
pub l_linger: c_int
807-
}
808-
809-
#[repr(C)]
810-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
811-
pub struct ucred {
812-
pid: pid_t,
813-
uid: uid_t,
814-
gid: gid_t,
815-
}
816-
817909
/*
818910
*
819911
* ===== Socket Options =====
@@ -825,13 +917,14 @@ pub struct ucred {
825917
///
826918
/// [Further reading](http://pubs.opengroup.org/onlinepubs/9699919799/functions/setsockopt.html)
827919
#[repr(i32)]
920+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
828921
pub enum SockLevel {
829922
Socket = libc::SOL_SOCKET,
830923
Tcp = libc::IPPROTO_TCP,
831924
Ip = libc::IPPROTO_IP,
832925
Ipv6 = libc::IPPROTO_IPV6,
833926
Udp = libc::IPPROTO_UDP,
834-
#[cfg(any(target_os = "linux", target_os = "android"))]
927+
#[cfg(any(target_os = "android", target_os = "linux"))]
835928
Netlink = libc::SOL_NETLINK,
836929
}
837930

@@ -938,7 +1031,7 @@ pub unsafe fn sockaddr_storage_to_addr(
9381031
}
9391032

9401033

941-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
1034+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
9421035
pub enum Shutdown {
9431036
/// Further receptions will be disallowed.
9441037
Read,

0 commit comments

Comments
 (0)