Pidfile single process instance mechanism I'm writing a system daemon in Rust that may be started by the user manually. Only one instance of the daemon should be running at a time
so I have created a pidfile locking mechanism to prevent multiple instances. This is my first time doing any kind of unix style file locking
and I would appreciate another pair of eyes to check for anything I may have missed.
Style pointers would also be great as I am new to Rust.

use nix::fcntl::{flock, FlockArg};
use nix::sys::signal::kill;
use nix::unistd::Pid;
use std::fs::File;
use std::fs::OpenOptions;
use std::io;
use std::io::{Error, ErrorKind, Read, Seek, SeekFrom, Write};
use std::os::unix::io::AsRawFd;
use std::path;
use std::process;

fn write_pid(mut file: &amp;File) -&gt; Result&lt;(), io::Error&gt; {
    let id = process::id().to_string() + "\n";
    file.write_all(id.as_bytes())?;
    Ok(())
}

fn lock_pidfile(file: &amp;File, pidfile: &amp;path::Path) -&gt; Result&lt;(), io::Error&gt; {
    flock(file.as_raw_fd(), FlockArg::LockExclusiveNonblock).map_err(|_| {
        Error::new(
            ErrorKind::Other,
            format!("Failed to lock pidfile: {}", pidfile.display()),
        )
    })
}

pub fn exclusive(pidfile: &amp;path::Path) -&gt; Result&lt;bool, io::Error&gt; {
    let pf = OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(pidfile);

    match pf {
        Ok(file) =&gt; {
            lock_pidfile(&amp;file, pidfile)?;
            write_pid(&amp;file)?;
            Ok(true)
        }
        Err(err) =&gt; {
            match err.kind() {
                ErrorKind::AlreadyExists =&gt; {
                    let mut file = OpenOptions::new().read(true).write(true).open(pidfile)?;
                    lock_pidfile(&amp;file, pidfile)?;
                    let mut id_str = String::new();
                    file.read_to_string(&amp;mut id_str)?;
                    let id: u32 = id_str.trim().parse().map_err(|_| {
                        Error::new(
                            ErrorKind::Other,
                            format!("Failed to parse pidfile: {}", pidfile.display()),
                        )
                    })?;
                    // Kill None just checks if process exists.
                    // Same as kill(pid, 0); in C
                    if kill(Pid::from_raw(id as i32), None).is_ok() {
                        Ok(false)
                    } else {
                        file.seek(SeekFrom::Start(0))?;
                        write_pid(&amp;file)?;
                        Ok(true)
                    }
                }
                _ =&gt; Err(err),
            }
        }
    }
}
```
I don't know much about file locking, but I can guide you in Rust.

I'd suggest changing your write_pid to use writeln!. I think you can simplify it to this.

fn write_pid(mut file: &amp;File) -&gt; Result&lt;(), io::Error&gt; {
    writeln!(file, "{}", process::id())
}


I'd also prefer to reduce the nesting in exclusive, perhaps something like this:

pub fn exclusive(pidfile: &amp;path::Path) -&gt; Result&lt;bool, io::Error&gt; {
    let pf = OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(pidfile);

    match pf {
        Ok(file) =&gt; {
            lock_pidfile(&amp;file, pidfile)?;
            write_pid(&amp;file)?;
            return Ok(true);
        }
        Err(ref e) if e.kind() == ErrorKind::AlreadyExists =&gt; {}
        Err(e) =&gt; return Err(e),
    }

    let mut file = OpenOptions::new().read(true).write(true).open(pidfile)?;
    lock_pidfile(&amp;file, pidfile)?;
    let mut id_str = String::new();
    file.read_to_string(&amp;mut id_str)?;
    let id: u32 = id_str.trim().parse().map_err(|_| {
        Error::new(
            ErrorKind::Other,
            format!("Failed to parse pidfile: {}", pidfile.display()),
        )
    })?;
    // Kill None just checks if process exists.
    // Same as kill(pid, 0); in C
    if kill(Pid::from_raw(id as i32), None).is_ok() {
        Ok(false)
    } else {
        file.seek(SeekFrom::Start(0))?;
        write_pid(&amp;file)?;
        Ok(true)
    }
}