Skip to content
This repository was archived by the owner on Apr 13, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
## Example

```rust

extern crate parity_daemonize;

use parity_daemonize::daemonize;
use parity_daemonize::{AsHandle, daemonize};
use std::{thread, time, process, io};
use io::Write;

fn main() {
match daemonize("pid_file.txt") {
// we are now in the daemon, use this handle to detach from the parent process
Ok(handle) => {
Ok(mut handle) => {
let mut count = 0;
loop {
// the daemon's output is piped to the parent process' stdout
Expand All @@ -35,7 +36,7 @@ fn main() {
// if this is the daemon, this is piped to the parent's stderr
eprintln!("{}", e);
// don't forget to flush
io::stderr().flush();
let _ = io::stderr().flush();
process::exit(1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub enum ErrorKind {
}

impl Fail for Error {
fn cause(&self) -> Option<&Fail> {
fn cause(&self) -> Option<&dyn Fail> {
self.inner.cause()
}

Expand All @@ -90,7 +90,7 @@ impl Fail for Error {
}

impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.inner, f)
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
Expand All @@ -14,9 +14,15 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Library for running a process as a daemon.
//! Currently, only Linux is supported.

#![warn(missing_docs, rust_2018_idioms)]

use crate::error::{Error, ErrorKind};
use std::path::PathBuf;

/// Error types
pub mod error;

mod platform;
Expand All @@ -29,6 +35,7 @@ type Result<T> = std::result::Result<T, Error>;
/// this can be useful, as the daemon will pipe it's stdout/stderr to the parent process
/// to communicate if start up was successful
pub trait AsHandle {
/// File descriptor type
type Fd;

/// Creates a `Handle` from a raw file descriptor
Expand All @@ -51,6 +58,10 @@ pub trait AsHandle {
}

#[macro_export]
#[doc(hidden)]
/// Macro for calling `c-style functions` and wrapping the return status in a `Result`
/// If the function return `-1` it will return `Err<$err:expr>` otherwise `Ok(int)`
// FIXME: this is not platform independent: `https://github.com/paritytech/parity-daemonize/issues/14`
macro_rules! map_err {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why do we expose this macro in the first place, can we move to unix submodule/or add #[doc(hidden)]?

Copy link
Copy Markdown
Contributor Author

@niklasad1 niklasad1 Feb 17, 2019

Choose a reason for hiding this comment

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

I don't know and I think we shouldn't export it because it is only used internally but I would prefer keep it out of this PR.
I can mark it as #[doc(hidden)] ok?

Also I think we could re-write the macro to get full failure case, such as Rust does it: https://github.com/rust-lang/rust/blob/master/src/libstd/sys/unix/mod.rs#L129-#L135

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I can mark it as #[doc(hidden)] ok?

yep

Also I think we could re-write the macro to get full failure case, such as Rust does it: https://github.com/rust-lang/rust/blob/master/src/libstd/sys/unix/mod.rs#L129-#L135

yeah, also it doesn't need to be a macro anymore, which would eliminate the need for #[doc(hidden)]

($e:expr, $err:expr) => {
match $e {
Expand Down
11 changes: 2 additions & 9 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
use std::{
process,
env,
thread,
time,
fs,
io::Read
};
use std::{fs, io::Read, process};

#[test]
fn test_simple() {
Expand All @@ -18,7 +11,7 @@ fn test_simple() {

let mut file = fs::File::open("pid_file").unwrap();
let mut pid = String::new();
file.read_to_string(&mut pid);
file.read_to_string(&mut pid).unwrap();

let _ = process::Command::new("kill")
.arg("-9")
Expand Down