Skip to content

Commit 6e37afc

Browse files
author
xadaemon
committed
chore: code reorganization, changes should be fully backwards compatible
1 parent ddf719a commit 6e37afc

File tree

5 files changed

+61
-293
lines changed

5 files changed

+61
-293
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "daemonize-me"
3-
version = "2.0.1"
3+
version = "2.0.2"
44
authors = ["Matheus Xavier <[email protected]>"]
55
edition = "2021"
66
license = "BSD-3-Clause/Apache-2.0"

ffi.rs

Lines changed: 0 additions & 235 deletions
This file was deleted.

src/daemon.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ impl<'a> Daemon<'a> {
230230
None => return Err(DaemonError::InvalidUmaskBits),
231231
};
232232
umask(umask_mode);
233+
233234
// Set the sid so the process isn't session orphan
234235
if let Err(_) = setsid() {
235236
return Err(DaemonError::SetSid);
@@ -238,6 +239,7 @@ impl<'a> Daemon<'a> {
238239
return Err(DaemonError::ChDir);
239240
};
240241
pid = getpid();
242+
241243
// create pid file and if configured to, chmod it
242244
if has_pid_file {
243245
// chmod of the pid file is deferred to after checking for the presence of the user and group
@@ -251,6 +253,7 @@ impl<'a> Daemon<'a> {
251253
Err(_) => return Err(DaemonError::WritePid),
252254
};
253255
}
256+
254257
// Drop privileges and chown the requested files
255258
if self.user.is_some() && self.group.is_some() {
256259
let user = match self.user {

src/errors.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use thiserror::Error;
2+
3+
#[derive(Error, Debug)]
4+
pub enum DaemonError {
5+
#[error("This feature is unavailable, or not implemented for your target os")]
6+
UnsupportedOnOS,
7+
#[error("Unable to fork")]
8+
Fork,
9+
#[error("Failed to chdir")]
10+
ChDir,
11+
#[error("Failed to open dev null")]
12+
OpenDevNull,
13+
#[error("Failed to close the file pointer of a stdio stream")]
14+
CloseFp,
15+
#[error("Invalid or nonexistent user")]
16+
InvalidUser,
17+
#[error("Invalid or nonexistent group")]
18+
InvalidGroup,
19+
#[error("Either group or user was specified but not the other")]
20+
InvalidUserGroupPair,
21+
#[error("The specified cstr is invalid")]
22+
InvalidCstr,
23+
#[error("Failed to execute initgroups")]
24+
InitGroups,
25+
#[error("Failed to set uid")]
26+
SetUid,
27+
#[error("Failed to set gid")]
28+
SetGid,
29+
#[error("Failed to chown the pid file")]
30+
ChownPid,
31+
#[error("Failed to create the pid file")]
32+
OpenPid,
33+
#[error("Failed to write to the pid file")]
34+
WritePid,
35+
#[error("Failed to redirect the standard streams")]
36+
RedirectStream,
37+
#[error("Umask bits are invalid")]
38+
InvalidUmaskBits,
39+
#[error("Failed to set sid")]
40+
SetSid,
41+
#[error("Failed to get groups record")]
42+
GetGrRecord,
43+
#[error("Failed to get passwd record")]
44+
GetPasswdRecord,
45+
#[error("Failed to set proc name")]
46+
SetProcName,
47+
#[error("Failed to set proc name")]
48+
InvalidProcName,
49+
}
50+
51+
pub type Result<T> = std::result::Result<T, DaemonError>;

src/lib.rs

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,19 @@
66
#![deny(clippy::needless_pass_by_value)]
77
#![deny(clippy::trivially_copy_pass_by_ref)]
88

9-
109
extern crate libc;
1110
extern crate nix;
1211

13-
use thiserror::Error;
14-
1512
mod stdio;
1613

17-
mod group;
18-
mod user;
1914
mod daemon;
2015
mod ffi;
16+
mod group;
17+
mod user;
2118

19+
mod errors;
20+
21+
pub use crate::daemon::Daemon;
22+
pub use crate::errors::{DaemonError, Result};
2223
pub use crate::group::Group;
2324
pub use crate::user::User;
24-
pub use crate::daemon::Daemon;
25-
26-
27-
#[derive(Error, Debug)]
28-
pub enum DaemonError {
29-
#[error("This feature is unavailable, or not implemented for your target os")]
30-
UnsupportedOnOS,
31-
#[error("Unable to fork")]
32-
Fork,
33-
#[error("Failed to chdir")]
34-
ChDir,
35-
#[error("Failed to open dev null")]
36-
OpenDevNull,
37-
#[error("Failed to close the file pointer of a stdio stream")]
38-
CloseFp,
39-
#[error("Invalid or nonexistent user")]
40-
InvalidUser,
41-
#[error("Invalid or nonexistent group")]
42-
InvalidGroup,
43-
#[error("Either group or user was specified but not the other")]
44-
InvalidUserGroupPair,
45-
#[error("The specified cstr is invalid")]
46-
InvalidCstr,
47-
#[error("Failed to execute initgroups")]
48-
InitGroups,
49-
#[error("Failed to set uid")]
50-
SetUid,
51-
#[error("Failed to set gid")]
52-
SetGid,
53-
#[error("Failed to chown the pid file")]
54-
ChownPid,
55-
#[error("Failed to create the pid file")]
56-
OpenPid,
57-
#[error("Failed to write to the pid file")]
58-
WritePid,
59-
#[error("Failed to redirect the standard streams")]
60-
RedirectStream,
61-
#[error("Umask bits are invalid")]
62-
InvalidUmaskBits,
63-
#[error("Failed to set sid")]
64-
SetSid,
65-
#[error("Failed to get groups record")]
66-
GetGrRecord,
67-
#[error("Failed to get passwd record")]
68-
GetPasswdRecord,
69-
#[error("Failed to set proc name")]
70-
SetProcName,
71-
#[error("Failed to set proc name")]
72-
InvalidProcName,
73-
}
74-
75-
pub type Result<T> = std::result::Result<T, DaemonError>;

0 commit comments

Comments
 (0)