From 7226f462e4188a7a172afb906c550b104b4993e9 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Fri, 16 Feb 2018 14:35:14 +0100 Subject: [PATCH 1/2] Use errno from the standard library --- src/ffi.rs | 23 ----------------------- src/lib.rs | 11 ++++++++--- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index 1798b609b..b322a8835 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -37,29 +37,6 @@ extern { pub fn flock(fd: libc::c_int, operation: libc::c_int) -> libc::c_int; } -#[cfg(target_os = "linux")] -unsafe fn errno_location() -> *const libc::c_int { - extern { fn __errno_location() -> *const libc::c_int; } - __errno_location() -} - - -#[cfg(target_os = "openbsd")] -unsafe fn errno_location() -> *const libc::c_int { - extern { fn __errno() -> *const libc::c_int; } - __errno() -} - -#[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))] -unsafe fn errno_location() -> *const libc::c_int { - extern { fn __error() -> *const libc::c_int; } - __error() -} - -pub unsafe fn errno() -> libc::c_int { - *errno_location() -} - pub unsafe fn get_gid_by_name(name: &CString) -> Option { let ptr = getgrnam(name.as_ptr() as *const libc::c_char); if ptr.is_null() { diff --git a/src/lib.rs b/src/lib.rs index 2f366f972..314f34799 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,6 +45,7 @@ mod ffi; extern crate libc; use std::fmt; +use std::io; use std::env::{set_current_dir}; use std::ffi::{CString}; use std::os::unix::ffi::OsStringExt; @@ -53,9 +54,9 @@ use std::path::{Path, PathBuf}; use std::process::{exit}; pub use libc::{uid_t, gid_t, mode_t}; -use libc::{LOCK_EX, LOCK_NB, c_int, fopen, write, close, fileno, fork, getpid, setsid, setuid, setgid, dup2, umask}; +use libc::{LOCK_EX, LOCK_NB, fopen, write, close, fileno, fork, getpid, setsid, setuid, setgid, dup2, umask}; -use self::ffi::{errno, flock, get_gid_by_name, get_uid_by_name}; +use self::ffi::{flock, get_gid_by_name, get_uid_by_name}; macro_rules! tryret { ($expr:expr, $ret:expr, $err:expr) => ( @@ -67,7 +68,7 @@ macro_rules! tryret { ) } -pub type Errno = c_int; +pub type Errno = i32; /// This error type for `Daemonize` `start` method. #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)] @@ -110,6 +111,10 @@ pub enum DaemonizeError { __Nonexhaustive, } +fn errno() -> Errno { + io::Error::last_os_error().raw_os_error().unwrap() +} + impl DaemonizeError { fn __description(&self) -> &str { match *self { From d28093a844d9166fd7f181bac03f068979bf73b6 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Wed, 28 Feb 2018 18:07:08 +0100 Subject: [PATCH 2/2] Fix concerns --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 314f34799..826a86486 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -112,7 +112,8 @@ pub enum DaemonizeError { } fn errno() -> Errno { - io::Error::last_os_error().raw_os_error().unwrap() + io::Error::last_os_error().raw_os_error() + .expect("last_os_error produces only OS errors hence raw_os_error cannot be None") } impl DaemonizeError {