-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a way to daemonize a process #15641
Comments
Hey Alex, I asked on the mailing list and IRC on how to do this, and I didn't get any responses, so I assumed it wasn't possible. Can you explain how I can achieve the same functionality as the C |
You'll want to spawn a new process with the use std::io::Command;
use std::os;
fn main() {
let args = os::args();
if args.len() == 1 {
let child = Command::new(args.get(0).as_slice())
.arg("child")
.detached().spawn().unwrap();
println!("child: {}", child.id());
child.forget();
} else {
println!("I'm a daemon!");
}
} This is all possible today, so I'm going to close this issue. |
So if I understand correctly, if I run this code with no arguments, I should see the process ID of the daemon and the daemon should print "I'm a daemon"? Because I only see the child process ID printed. |
I believe that is because the child has been detached so it did not inherit the same stdio file descriptors. |
I also added a 60 second sleep call after the "I'm a daemon!" |
Ah, that's because the stdout handle is closed so the call to |
Since std::io::Command is now defunct would someone be able to post a working example using std::process similar to the one that @alexcrichton posted above? |
This works for me. |
@somedude232 thanks |
The code above is not an acceptable solution, it's a backdoor. If anyone has copied the code above into a suid program, privilege escalation is trivial, since the args array is set by the calling process and args[0] cannot be trusted. Even if that wasn't true, a race condition would still exist since the path args[0] names could be replaced before the Command is executed. |
@Ofenhed do you have a safer alternative? |
The correct way would be to demonize the actual running process. I haven't tried it, but running the unsafe libc functions To slightly increase security with the examples above on systems with |
@alexcrichton Could we possibly reopen this thread (as it is not solved) or at least remove the backdoors as recommended solutions? |
…eykril fix: preceding QualifiedPathType for into_to_from assist fixes rust-lang#15598
How should I redirect stdout and stderr in this way? |
Being able to daemonize is a common thing for applications to do. There should be something in Rust similar to the C daemon function: http://man7.org/linux/man-pages/man3/daemon.3.html
The text was updated successfully, but these errors were encountered: