p If .Ar path is .Dv NULL or a plain basename (a name containing no directory components), the pid file is created in the
a /var/run directory. The file name has the form
a /var/run/basename.pid . The basename part is either the value of .Ar path if it was not .Dv NULL , or the program name as returned by .Xr getprogname 3 otherwise.
p If .Ar path is an absolute or relative path (i.e. it contains the .Sq / character), the pid file is created in the provided location.
p If called with a new .Ar path , .Fn pidfile and .Fn pidfile_lock will remove the old pid file.
p The pid file is truncated, so these functions can be called multiple times and allow a child process to take over the lock.
p .Fn pidfile_read will read the last pid file created, or specified by .Ar path , and return the process ID it contains.
p .Fn pidfile_clean will .Xr ftruncate 2 , .Xr close 2 , and .Xr unlink 2 the last opening pid file if, and only if, the current process wrote it. This function should be called if the program needs to call .Xr _exit 2 (such as from a signal handler) and needs to clean up the pid file. .Sh RETURN VALUES .Fn pidfile and .Fn pidfile_clean returns 0 on success and -1 on failure.
p .Fn pidfile_lock returns 0 on success. Otherwise, the process ID who owns the lock is returned and if that cannot be derived then -1 is returned.
p .Fn pidfile_read returns the process ID if known, otherwise -1. .Sh ERRORS The .Fn pidfile and .Fn pidfile_lock functions will fail if: l -tag -width Er t Bq Er EEXIST Some process already holds the lock on the given pid file, meaning that a daemon is already running. t Bq Er ENAMETOOLONG Specified pidfile's name is too long. .El .Sh SEE ALSO .Xr flock 2 , .Xr atexit 3 .Sh HISTORY The .Fn pidfile function call appeared in .Nx 1.5 . Support for creating pid files in any arbitrary path was added in .Nx 6.0 .
p The .Fn pidfile_lock , .Fn pidfile_read , and .Fn pidfile_clean function calls appeared in .Nx 8 . .Sh CAVEATS .Fn pidfile and .Fn pidfile_lock use .Xr atexit 3 to ensure the pid file is cleaned at program exit. However, programs that use the .Xr _exit 2 function (for example, in signal handlers) will not trigger this behaviour and should call .Fn pidfile_clean . Like-wise, if the program creates a pid file before .Xr fork 2 Ns ing a child to take over, it should use the .Xr _exit 2 function instead of returning or using the .Xr exit 3 function to ensure the pid file is not cleaned.