-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # .github/workflows/rust.yml # Cargo.toml # README.md # src/ffi.rs
- Loading branch information
Showing
13 changed files
with
668 additions
and
585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/target | ||
.idea/ | ||
Cargo.lock | ||
*.log | ||
*.pid | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,56 @@ | ||
extern crate daemonize_me; | ||
use daemonize_me::{Daemon, Group, User}; | ||
use nix::unistd::{getgid, getuid}; | ||
use std::convert::TryFrom; | ||
|
||
use std::any::Any; | ||
use std::fs::File; | ||
use std::process::exit; | ||
|
||
pub use daemonize_me::Daemon; | ||
|
||
|
||
fn post_fork_parent(ppid: i32, cpid: i32) -> ! { | ||
println!("Parent pid: {}, Child pid {}", ppid, cpid); | ||
println!("Parent will keep running after the child is forked, might even go do other tasks"); | ||
println!("Or quit like so, bye :)"); | ||
exit(0); | ||
} | ||
|
||
fn post_fork_child(ppid: i32, cpid: i32) { | ||
println!("Parent pid: {}, Child pid {}", ppid, cpid); | ||
println!("This hook is called in the child"); | ||
// Child hook must return | ||
return | ||
} | ||
|
||
fn after_init(_: Option<&dyn Any>) { | ||
println!("Initialized the daemon!"); | ||
return | ||
} | ||
|
||
fn main() { | ||
let stdout = File::create("info.log").unwrap(); | ||
let stderr = File::create("err.log").unwrap(); | ||
let uid = getuid(); | ||
let gid = getgid(); | ||
println!("sid: {}, pid: {}", uid, gid); | ||
let daemon = Daemon::new() | ||
.pid_file("example.pid", Some(false)) | ||
.user(User::try_from("daemon").unwrap()) | ||
.group(Group::try_from("daemon").unwrap()) | ||
.umask(0o000) | ||
.work_dir(".") | ||
.stdout(stdout) | ||
.stderr(stderr) | ||
// Hooks are optional | ||
.setup_post_fork_parent_hook(post_fork_parent) | ||
.setup_post_fork_child_hook(post_fork_child) | ||
.setup_post_init_hook(after_init, None) | ||
// | ||
.start(); | ||
|
||
match daemon { | ||
Ok(_) => println!("Daemonized with success"), | ||
Err(e) => eprintln!("Error, {}", e), | ||
Err(e) => { | ||
eprintln!("Error, {}", e); | ||
exit(-1); | ||
}, | ||
} | ||
|
||
for i in 0..=10000 { | ||
println!("{}", i); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.