Skip to content

Commit

Permalink
Use safe_f! consistently across platforms
Browse files Browse the repository at this point in the history
The pr rust-lang#1870 introduced safe_f! macro, which made some functions like
WIFEXITED and WEXITSTATUS const and safe on linux_like platform only,
which causes inconsistency when trying to use those functions in crates
compiled across multiple platforms, as using unsafe on those functions
will generate unused_unsafe warning on linux platforms and lack of
unsafe block will fail compilation on non-linux platforms.

To avoid the inconsistency, this commit applies the same macro for all
the same functions on other platforms too.
  • Loading branch information
xonatius committed Sep 13, 2020
1 parent c2a184b commit 773f556
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 183 deletions.
79 changes: 40 additions & 39 deletions src/fuchsia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3158,42 +3158,6 @@ f! {
}
}

pub fn WIFSTOPPED(status: ::c_int) -> bool {
(status & 0xff) == 0x7f
}

pub fn WSTOPSIG(status: ::c_int) -> ::c_int {
(status >> 8) & 0xff
}

pub fn WIFCONTINUED(status: ::c_int) -> bool {
status == 0xffff
}

pub fn WIFSIGNALED(status: ::c_int) -> bool {
((status & 0x7f) + 1) as i8 >= 2
}

pub fn WTERMSIG(status: ::c_int) -> ::c_int {
status & 0x7f
}

pub fn WIFEXITED(status: ::c_int) -> bool {
(status & 0x7f) == 0
}

pub fn WEXITSTATUS(status: ::c_int) -> ::c_int {
(status >> 8) & 0xff
}

pub fn WCOREDUMP(status: ::c_int) -> bool {
(status & 0x80) != 0
}

pub fn QCMD(cmd: ::c_int, type_: ::c_int) -> ::c_int {
(cmd << 8) | (type_ & 0x00ff)
}

pub fn CPU_ZERO(cpuset: &mut cpu_set_t) -> () {
for slot in cpuset.bits.iter_mut() {
*slot = 0;
Expand Down Expand Up @@ -3291,6 +3255,44 @@ f! {
}
}

safe_f! {
pub {const} fn WIFSTOPPED(status: ::c_int) -> bool {
(status & 0xff) == 0x7f
}

pub {const} fn WSTOPSIG(status: ::c_int) -> ::c_int {
(status >> 8) & 0xff
}

pub {const} fn WIFCONTINUED(status: ::c_int) -> bool {
status == 0xffff
}

pub {const} fn WIFSIGNALED(status: ::c_int) -> bool {
((status & 0x7f) + 1) as i8 >= 2
}

pub {const} fn WTERMSIG(status: ::c_int) -> ::c_int {
status & 0x7f
}

pub {const} fn WIFEXITED(status: ::c_int) -> bool {
(status & 0x7f) == 0
}

pub {const} fn WEXITSTATUS(status: ::c_int) -> ::c_int {
(status >> 8) & 0xff
}

pub {const} fn WCOREDUMP(status: ::c_int) -> bool {
(status & 0x80) != 0
}

pub {const} fn QCMD(cmd: ::c_int, type_: ::c_int) -> ::c_int {
(cmd << 8) | (type_ & 0x00ff)
}
}

fn __CMSG_LEN(cmsg: *const cmsghdr) -> ::ssize_t {
((unsafe { (*cmsg).cmsg_len as ::size_t } + ::mem::size_of::<::c_long>()
- 1)
Expand All @@ -3302,9 +3304,8 @@ fn __CMSG_NEXT(cmsg: *const cmsghdr) -> *mut c_uchar {
}

fn __MHDR_END(mhdr: *const msghdr) -> *mut c_uchar {
unsafe {
(*mhdr).msg_control.offset((*mhdr).msg_controllen as isize)
}.cast()
unsafe { (*mhdr).msg_control.offset((*mhdr).msg_controllen as isize) }
.cast()
}

// EXTERN_FN
Expand Down
12 changes: 7 additions & 5 deletions src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3241,24 +3241,26 @@ f! {
(__DARWIN_ALIGN32(::mem::size_of::<::cmsghdr>()) + length as usize)
as ::c_uint
}
}

pub fn WSTOPSIG(status: ::c_int) -> ::c_int {
safe_f! {
pub {const} fn WSTOPSIG(status: ::c_int) -> ::c_int {
status >> 8
}

pub fn _WSTATUS(status: ::c_int) -> ::c_int {
pub {const} fn _WSTATUS(status: ::c_int) -> ::c_int {
status & 0x7f
}

pub fn WIFCONTINUED(status: ::c_int) -> bool {
pub {const} fn WIFCONTINUED(status: ::c_int) -> bool {
_WSTATUS(status) == _WSTOPPED && WSTOPSIG(status) == 0x13
}

pub fn WIFSIGNALED(status: ::c_int) -> bool {
pub {const} fn WIFSIGNALED(status: ::c_int) -> bool {
_WSTATUS(status) != _WSTOPPED && _WSTATUS(status) != 0
}

pub fn WIFSTOPPED(status: ::c_int) -> bool {
pub {const} fn WIFSTOPPED(status: ::c_int) -> bool {
_WSTATUS(status) == _WSTOPPED && WSTOPSIG(status) != 0x13
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/unix/bsd/freebsdlike/dragonfly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,8 +1037,10 @@ f! {
(_CMSG_ALIGN(::mem::size_of::<::cmsghdr>()) +
_CMSG_ALIGN(length as usize)) as ::c_uint
}
}

pub fn WIFSIGNALED(status: ::c_int) -> bool {
safe_f! {
pub {const} fn WIFSIGNALED(status: ::c_int) -> bool {
(status & 0o177) != 0o177 && (status & 0o177) != 0
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/unix/bsd/freebsdlike/freebsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,15 +1195,17 @@ f! {
::mem::size_of::<sockcred>() + ::mem::size_of::<::gid_t>() * ngrps
}

pub fn WIFSIGNALED(status: ::c_int) -> bool {
(status & 0o177) != 0o177 && (status & 0o177) != 0 && status != 0x13
}

pub fn uname(buf: *mut ::utsname) -> ::c_int {
__xuname(256, buf as *mut ::c_void)
}
}

safe_f! {
pub {const} fn WIFSIGNALED(status: ::c_int) -> bool {
(status & 0o177) != 0o177 && (status & 0o177) != 0 && status != 0x13
}
}

extern "C" {
pub fn __error() -> *mut ::c_int;

Expand Down
8 changes: 4 additions & 4 deletions src/unix/bsd/freebsdlike/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,16 +1220,16 @@ pub const TIME_ERROR: ::c_int = 5;
pub const REG_ENOSYS: ::c_int = -1;
pub const REG_ILLSEQ: ::c_int = 17;

f! {
pub fn WIFCONTINUED(status: ::c_int) -> bool {
safe_f! {
pub {const} fn WIFCONTINUED(status: ::c_int) -> bool {
status == 0x13
}

pub fn WSTOPSIG(status: ::c_int) -> ::c_int {
pub {const} fn WSTOPSIG(status: ::c_int) -> ::c_int {
status >> 8
}

pub fn WIFSTOPPED(status: ::c_int) -> bool {
pub {const} fn WIFSTOPPED(status: ::c_int) -> bool {
(status & 0o177) == 0o177
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/unix/bsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,24 +544,26 @@ f! {
*slot = 0;
}
}
}

pub fn WTERMSIG(status: ::c_int) -> ::c_int {
safe_f! {
pub {const} fn WTERMSIG(status: ::c_int) -> ::c_int {
status & 0o177
}

pub fn WIFEXITED(status: ::c_int) -> bool {
pub {const} fn WIFEXITED(status: ::c_int) -> bool {
(status & 0o177) == 0
}

pub fn WEXITSTATUS(status: ::c_int) -> ::c_int {
pub {const} fn WEXITSTATUS(status: ::c_int) -> ::c_int {
status >> 8
}

pub fn WCOREDUMP(status: ::c_int) -> bool {
pub {const} fn WCOREDUMP(status: ::c_int) -> bool {
(status & 0o200) != 0
}

pub fn QCMD(cmd: ::c_int, type_: ::c_int) -> ::c_int {
pub {const} fn QCMD(cmd: ::c_int, type_: ::c_int) -> ::c_int {
(cmd << 8) | (type_ & 0x00ff)
}
}
Expand Down
34 changes: 18 additions & 16 deletions src/unix/bsd/netbsdlike/netbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1699,29 +1699,13 @@ f! {
as ::c_uint
}

pub fn WSTOPSIG(status: ::c_int) -> ::c_int {
status >> 8
}

pub fn WIFSIGNALED(status: ::c_int) -> bool {
(status & 0o177) != 0o177 && (status & 0o177) != 0
}

pub fn WIFSTOPPED(status: ::c_int) -> bool {
(status & 0o177) == 0o177
}

// dirfd() is a macro on netbsd to access
// the first field of the struct where dirp points to:
// http://cvsweb.netbsd.org/bsdweb.cgi/src/include/dirent.h?rev=1.36
pub fn dirfd(dirp: *mut ::DIR) -> ::c_int {
*(dirp as *const ::c_int)
}

pub fn WIFCONTINUED(status: ::c_int) -> bool {
status == 0xffff
}

pub fn SOCKCREDSIZE(ngrps: usize) -> usize {
let ngrps = if ngrps > 0 {
ngrps - 1
Expand All @@ -1732,6 +1716,24 @@ f! {
}
}

safe_f! {
pub {const} fn WSTOPSIG(status: ::c_int) -> ::c_int {
status >> 8
}

pub {const} fn WIFSIGNALED(status: ::c_int) -> bool {
(status & 0o177) != 0o177 && (status & 0o177) != 0
}

pub {const} fn WIFSTOPPED(status: ::c_int) -> bool {
(status & 0o177) == 0o177
}

pub {const} fn WIFCONTINUED(status: ::c_int) -> bool {
status == 0xffff
}
}

extern "C" {
pub fn ntp_adjtime(buf: *mut timex) -> ::c_int;
pub fn ntp_gettime(buf: *mut ntptimeval) -> ::c_int;
Expand Down
10 changes: 6 additions & 4 deletions src/unix/bsd/netbsdlike/openbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ fn _ALIGN(p: usize) -> usize {
}

f! {
pub fn WIFCONTINUED(status: ::c_int) -> bool {
pub {const} fn WIFCONTINUED(status: ::c_int) -> bool {
status & 0o177777 == 0o177777
}

Expand Down Expand Up @@ -1375,16 +1375,18 @@ f! {
(_ALIGN(::mem::size_of::<::cmsghdr>()) + _ALIGN(length as usize))
as ::c_uint
}
}

pub fn WSTOPSIG(status: ::c_int) -> ::c_int {
safe_f! {
pub {const} fn WSTOPSIG(status: ::c_int) -> ::c_int {
status >> 8
}

pub fn WIFSIGNALED(status: ::c_int) -> bool {
pub {const} fn WIFSIGNALED(status: ::c_int) -> bool {
(status & 0o177) != 0o177 && (status & 0o177) != 0
}

pub fn WIFSTOPPED(status: ::c_int) -> bool {
pub {const} fn WIFSTOPPED(status: ::c_int) -> bool {
(status & 0xff) == 0o177
}
}
Expand Down
24 changes: 15 additions & 9 deletions src/unix/haiku/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,37 +1254,39 @@ f! {
*slot = 0;
}
}
}

pub fn WIFEXITED(status: ::c_int) -> bool {
safe_f! {
pub {const} fn WIFEXITED(status: ::c_int) -> bool {
(status & !0xff) == 0
}

pub fn WEXITSTATUS(status: ::c_int) -> ::c_int {
pub {const} fn WEXITSTATUS(status: ::c_int) -> ::c_int {
status & 0xff
}

pub fn WIFSIGNALED(status: ::c_int) -> bool {
pub {const} fn WIFSIGNALED(status: ::c_int) -> bool {
((status >> 8) & 0xff) != 0
}

pub fn WTERMSIG(status: ::c_int) -> ::c_int {
pub {const} fn WTERMSIG(status: ::c_int) -> ::c_int {
(status >> 8) & 0xff
}

pub fn WIFSTOPPED(status: ::c_int) -> bool {
pub {const} fn WIFSTOPPED(status: ::c_int) -> bool {
((status >> 16) & 0xff) != 0
}

pub fn WSTOPSIG(status: ::c_int) -> ::c_int {
pub {const} fn WSTOPSIG(status: ::c_int) -> ::c_int {
(status >> 16) & 0xff
}

// actually WIFCORED, but this is used everywhere else
pub fn WCOREDUMP(status: ::c_int) -> bool {
pub {const} fn WCOREDUMP(status: ::c_int) -> bool {
(status & 0x10000) != 0
}

pub fn WIFCONTINUED(status: ::c_int) -> bool {
pub {const} fn WIFCONTINUED(status: ::c_int) -> bool {
(status & 0x20000) != 0
}
}
Expand All @@ -1293,7 +1295,11 @@ extern "C" {
pub fn getrlimit(resource: ::c_int, rlim: *mut ::rlimit) -> ::c_int;
pub fn setrlimit(resource: ::c_int, rlim: *const ::rlimit) -> ::c_int;
pub fn getpriority(which: ::c_int, who: id_t) -> ::c_int;
pub fn setpriority(which: ::c_int, who: id_t, priority: ::c_int) -> ::c_int;
pub fn setpriority(
which: ::c_int,
who: id_t,
priority: ::c_int,
) -> ::c_int;

pub fn utimensat(
fd: ::c_int,
Expand Down
8 changes: 4 additions & 4 deletions src/unix/hermit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,16 +943,16 @@ pub const PRIO_PROCESS: ::c_int = 0;
pub const PRIO_PGRP: ::c_int = 1;
pub const PRIO_USER: ::c_int = 2;

f! {
pub fn WEXITSTATUS(status: ::c_int) -> ::c_int {
safe_f! {
pub {const} fn WEXITSTATUS(status: ::c_int) -> ::c_int {
(status >> 8) & 0xff
}

pub fn WIFEXITED(status: ::c_int) -> bool {
pub {const} fn WIFEXITED(status: ::c_int) -> bool {
(status & 0xff) == 0
}

pub fn WTERMSIG(status: ::c_int) -> ::c_int {
pub {const} fn WTERMSIG(status: ::c_int) -> ::c_int {
status & 0x7f
}
}
Expand Down
Loading

0 comments on commit 773f556

Please sign in to comment.