Skip to content

Commit 2e6c75d

Browse files
committed
Some changes for modern Rust
1 parent 5cf0867 commit 2e6c75d

File tree

7 files changed

+15
-34
lines changed

7 files changed

+15
-34
lines changed

src/error.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(unused)]
2+
13
use std::{any::Any, str::Utf8Error};
24

35
#[derive(Debug)]
@@ -14,6 +16,7 @@ pub enum Error {
1416
Daemonize {
1517
error: daemonize::Error,
1618
},
19+
#[allow(unused)]
1720
Unicode {
1821
error: Utf8Error,
1922
},
@@ -27,9 +30,6 @@ pub enum Error {
2730
UnknownMlockall,
2831
UnknownKill,
2932
UnknownGetpguid,
30-
Thread {
31-
error: Box<dyn Any + Send + 'static>,
32-
},
3333

3434
#[cfg(feature = "glob-ignore")]
3535
GlobPattern {
@@ -80,12 +80,6 @@ impl From<std::str::Utf8Error> for Error {
8080
}
8181
}
8282

83-
impl From<Box<dyn Any + Send + 'static>> for Error {
84-
fn from(error: Box<dyn Any + Send + 'static>) -> Self {
85-
Self::Thread { error }
86-
}
87-
}
88-
8983
#[cfg(feature = "glob-ignore")]
9084
impl From<glob::PatternError> for Error {
9185
fn from(error: glob::PatternError) -> Self {

src/kill.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::fs;
22
use std::time::Duration;
3-
use std::{ffi::OsStr, time::Instant};
3+
use std::time::Instant;
44

55
use libc::kill;
66
use libc::{EINVAL, EPERM, ESRCH, SIGKILL, SIGTERM};
77

88
use crate::errno::errno;
99
use crate::error::{Error, Result};
1010
use crate::process::Process;
11-
use crate::{checked_ffi, cli, utils};
11+
use crate::{cli, utils};
1212

1313
pub fn choose_victim(
1414
proc_buf: &mut [u8],
@@ -90,7 +90,7 @@ pub fn choose_victim(
9090
}
9191

9292
pub fn kill_process(pid: i32, signal: i32) -> Result<()> {
93-
let res = checked_ffi! { kill(pid, signal) };
93+
let res = unsafe { kill(pid, signal) };
9494

9595
if res == -1 {
9696
return Err(match errno() {

src/memory/mem_info.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::{fmt, mem};
33
use libc::sysinfo;
44

55
use crate::{
6-
checked_ffi,
76
error::{Error, Result},
87
utils::bytes_to_megabytes,
98
};
@@ -24,7 +23,7 @@ fn sys_info() -> Result<sysinfo> {
2423
let mut sys_info: sysinfo = unsafe { mem::zeroed() };
2524

2625
// Safety: sysinfo() is safe and must not fail when passed a valid reference
27-
let ret_val = checked_ffi! { libc::sysinfo(&mut sys_info) };
26+
let ret_val = unsafe { libc::sysinfo(&mut sys_info) };
2827

2928
if ret_val != 0 {
3029
// The only error that sysinfo() can have happens when

src/memory/mem_lock.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use libc::{c_int, mlockall};
22
use libc::{EAGAIN, EINVAL, ENOMEM, EPERM};
33
use libc::{MCL_CURRENT, MCL_FUTURE};
44

5-
use crate::checked_ffi;
65
use crate::errno::errno;
76
use crate::error::{Error, Result};
87

@@ -11,7 +10,7 @@ extern "C" {
1110
}
1211

1312
pub fn _mlockall_wrapper(flags: c_int) -> Result<()> {
14-
let err = checked_ffi! { mlockall(flags) };
13+
let err = unsafe { mlockall(flags) };
1514
if err == 0 {
1615
return Ok(());
1716
}
@@ -38,7 +37,7 @@ pub fn lock_memory_pages() -> Result<()> {
3837
// TODO: check for _MCL_ONFAULT == -1
3938

4039
#[allow(non_snake_case)]
41-
let MCL_ONFAULT: c_int = checked_ffi! { _MCL_ONFAULT };
40+
let MCL_ONFAULT: c_int = unsafe { _MCL_ONFAULT };
4241
match _mlockall_wrapper(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) {
4342
Err(err) => {
4443
eprintln!("First try at mlockall failed: {:?}", err);

src/process.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::io::Write;
33

44
use libc::getpgid;
55

6-
use crate::checked_ffi;
76
use crate::{
87
error::{Error, Result},
98
utils::{self, str_from_bytes},
@@ -26,7 +25,7 @@ impl Process {
2625
/// Returns the current process represented as a Process struct
2726
/// Unused in the actual code but very often used when debugging
2827
pub fn this(buf: &mut [u8]) -> Result<Self> {
29-
let pid = checked_ffi! { libc::getpid() } as u32;
28+
let pid = unsafe { libc::getpid() } as u32;
3029

3130
Self::from_pid(pid, buf)
3231
}
@@ -36,7 +35,7 @@ impl Process {
3635
/// TODO: would it be better to check for /proc/<PID>/ in here?
3736
pub fn is_alive_from_pid(pid: u32) -> bool {
3837
// Safety: `getpgid` is memory safe
39-
let group_id = checked_ffi! { getpgid(pid as i32) };
38+
let group_id = unsafe { getpgid(pid as i32) };
4039

4140
group_id > 0
4241
}

src/uname.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::ffi::CStr;
22
use std::mem;
33

4-
use crate::checked_ffi;
54
use crate::error::{Error, Result};
65
use crate::linux_version::LinuxVersion;
76
use libc::{uname, utsname};
@@ -16,7 +15,7 @@ impl Uname {
1615
// can be safely zeroed.
1716
let mut uts_struct: utsname = unsafe { mem::zeroed() };
1817

19-
let ret_val = checked_ffi! { uname(&mut uts_struct) };
18+
let ret_val = unsafe { uname(&mut uts_struct) };
2019

2120
// uname returns a negative number upon failure
2221
if ret_val < 0 {

src/utils.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,17 @@ use memchr::memchr;
1212
use crate::errno::errno;
1313
use crate::error::{Error, Result};
1414

15-
/// This macro is used whenever we call a C function but
16-
/// strongly believe that it cannot cause any memory unsafety.
17-
#[macro_export]
18-
macro_rules! checked_ffi {
19-
($e: expr) => {
20-
unsafe { $e }
21-
};
22-
}
23-
2415
/// Gets the effective user ID of the calling process
2516
fn effective_user_id() -> u32 {
2617
// Safety: the POSIX Programmer's Manual states that
2718
// geteuid will always be successful.
28-
checked_ffi! { libc::geteuid() }
19+
unsafe { libc::geteuid() }
2920
}
3021

3122
/// Gets the process group of the process
3223
/// with the given PID.
3324
pub fn get_process_group(pid: i32) -> Result<i32> {
34-
let pgid = checked_ffi! { getpgid(pid) };
25+
let pgid = unsafe { getpgid(pid) };
3526
if pgid == -1 {
3627
return Err(match errno() {
3728
EPERM => Error::NoPermission,
@@ -53,7 +44,7 @@ pub fn running_as_sudo() -> bool {
5344
pub fn page_size() -> Result<i64> {
5445
// _SC_PAGESIZE is defined in POSIX.1
5546
// Safety: no memory unsafety can arise from `sysconf`
56-
let page_size = checked_ffi! { sysconf(_SC_PAGESIZE) };
47+
let page_size = unsafe { sysconf(_SC_PAGESIZE) };
5748
if page_size == -1 {
5849
return Err(Error::SysConfFailed);
5950
}

0 commit comments

Comments
 (0)