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
#![deny(warnings)]
#![deny(clippy::complexity)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![deny(clippy::panic)]
#![deny(clippy::needless_pass_by_value)]
#![deny(clippy::trivially_copy_pass_by_ref)]


extern crate libc;
extern crate nix;

use thiserror::Error;

mod stdio;

mod group;
mod user;
mod daemon;
mod ffi;

pub use crate::group::Group;
pub use crate::user::User;
pub use crate::daemon::Daemon;


#[derive(Error, Debug)]
pub enum DaemonError {
    #[error("This feature is unavailable, or not implemented for your target os")]
    UnsupportedOnOS,
    #[error("Unable to fork")]
    Fork,
    #[error("Failed to chdir")]
    ChDir,
    #[error("Failed to open dev null")]
    OpenDevNull,
    #[error("Failed to close the file pointer of a stdio stream")]
    CloseFp,
    #[error("Invalid or nonexistent user")]
    InvalidUser,
    #[error("Invalid or nonexistent group")]
    InvalidGroup,
    #[error("Either group or user was specified but not the other")]
    InvalidUserGroupPair,
    #[error("The specified cstr is invalid")]
    InvalidCstr,
    #[error("Failed to execute initgroups")]
    InitGroups,
    #[error("Failed to set uid")]
    SetUid,
    #[error("Failed to set gid")]
    SetGid,
    #[error("Failed to chown the pid file")]
    ChownPid,
    #[error("Failed to create the pid file")]
    OpenPid,
    #[error("Failed to write to the pid file")]
    WritePid,
    #[error("Failed to redirect the standard streams")]
    RedirectStream,
    #[error("Umask bits are invalid")]
    InvalidUmaskBits,
    #[error("Failed to set sid")]
    SetSid,
    #[error("Failed to get groups record")]
    GetGrRecord,
    #[error("Failed to get passwd record")]
    GetPasswdRecord,
    #[error("Failed to set proc name")]
    SetProcName,
    #[error("Failed to set proc name")]
    InvalidProcName,
}

pub type Result<T> = std::result::Result<T, DaemonError>;