1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! Easy to use daemonizing for rust programs in unix enviroment.
//!
//! ```
//! daemonize_redirect(Some("stdout.log"), Some("stderr.log"), ChdirMode::ChdirRoot).unwrap();
//! ```
//! See examples for sample program.

extern crate libc;

use std::{io, env, ffi, path, process};

/// The error type for daemonizing related operations.
///
/// Most variants holds `Option<i32>` value, where `i32`
/// stands for `errno` result for corresponding OS functions.
#[derive(Debug)]
pub enum Error {
    FirstFork(Option<i32>),
    SecondFork(Option<i32>),
    Setsid(Option<i32>),
    Chdir(io::Error),
    FilenameToStr(path::PathBuf),
    FilenameFFI(path::PathBuf, ffi::NulError),
    OpenStd(path::PathBuf, Option<i32>),
    Dup2(Option<i32>),
}

#[derive(Debug)]
pub enum ChdirMode {
    NoChdir,
    ChdirRoot,
}

struct Redirected(libc::c_int);

impl Drop for Redirected {
    fn drop(&mut self) {
        if self.0 >= 0 {
            unsafe { libc::close(self.0) };
            self.0 = -1;
        }
    }
}

fn to_path_buf<P>(path: &Option<P>) -> path::PathBuf where P: AsRef<path::Path> {
    if let &Some(ref p) = path {
        p.as_ref().to_owned()
    } else {
        let null: &path::Path = "/dev/null".as_ref();
        null.to_path_buf()
    }
}

fn redirect<P>(std: Option<P>) -> Result<Redirected, Error> where P: AsRef<path::Path> {
    let filename = std.as_ref()
        .map(|s| s.as_ref().to_str())
        .unwrap_or(Some("/dev/null"))
        .ok_or(Error::FilenameToStr(to_path_buf(&std)));
    let path = try!(ffi::CString::new(try!(filename)).map_err(|e| Error::FilenameFFI(to_path_buf(&std), e)));

    let fd = unsafe { libc::open(path.as_ptr(),
                                 libc::O_CREAT | libc::O_WRONLY | libc::O_APPEND,
                                 (libc::S_IRUSR | libc::S_IRGRP | libc::S_IWGRP | libc::S_IWUSR) as libc::c_uint) };
    if fd < 0 {
        Err(Error::OpenStd(to_path_buf(&std), io::Error::last_os_error().raw_os_error()))
    } else {
        Ok(Redirected(fd))
    }
}

/// Performs program daemonizing with optional redirection for STDIN and STDOUT.
/// If the redirection parameter is `None`, then stream will be redirected to `/dev/null`,
/// otherwise it will be redirected to the file provided.
///
/// Returns the new process id after all forks.
pub fn daemonize_redirect<PO, PE>(stdout: Option<PO>, stderr: Option<PE>, chdir: ChdirMode) -> Result<libc::pid_t, Error>
    where PO: AsRef<path::Path>, PE: AsRef<path::Path>
{
    daemonize(try!(redirect(stdout)), try!(redirect(stderr)), chdir)
}

fn daemonize(mut stdout_fd: Redirected, mut stderr_fd: Redirected, chdir: ChdirMode) -> Result<libc::pid_t, Error> {
    macro_rules! errno {
        ($err:ident) => ({ return Err(Error::$err(io::Error::last_os_error().raw_os_error())) })
    }

    let pid = unsafe { libc::fork() };
    if pid < 0 {
        errno!(FirstFork)
    } else if pid != 0 {
        process::exit(0)
    }

    if unsafe { libc::setsid() } < 0 {
        errno!(Setsid)
    }

    unsafe { libc::signal(libc::SIGHUP, libc::SIG_IGN); }
    let pid = unsafe { libc::fork() };
    if pid < 0 {
        errno!(SecondFork)
    } else if pid != 0 {
        process::exit(0)
    }

    if let ChdirMode::ChdirRoot = chdir {
        match env::set_current_dir("/") {
            Ok(()) => (),
            Err(e) => {
                return Err(Error::Chdir(e))
            },
        }
    }

    if unsafe { libc::dup2(stdout_fd.0, libc::STDOUT_FILENO) } < 0 {
        errno!(Dup2)
    } else {
        stdout_fd.0 = -1
    }

    if unsafe { libc::dup2(stderr_fd.0, libc::STDERR_FILENO) } < 0 {
        errno!(Dup2)
    } else {
        stderr_fd.0 = -1
    }

    Ok(unsafe { libc::getpid() })
}