close_fd

Function close_fd 

Source
pub fn close_fd() -> Result<()>
Expand description

Close file descriptors stdin, stdout, stderr

Warning: This function closes the file descriptors, making them available for reuse. If your daemon opens files after calling this, those files may get fd 0, 1, or 2, causing println!, eprintln!, or panic output to corrupt them.

Use redirect_stdio() instead, which is safer and follows industry best practices by redirecting stdio to /dev/null instead of closing. This keeps fd 0, 1, 2 occupied, ensuring subsequent files get fd >= 3, preventing silent corruption.

§Errors

Returns an io::Error if any of the file descriptors fail to close. Already-closed descriptors (EBADF) are treated as success so the function is idempotent.

§Example

use fork::close_fd;

// Warning: Files opened after this may get fd 0,1,2!
close_fd()?;