.\" $NetBSD: pidfile.3,v 1.17 2026/05/30 10:09:47 roy Exp $ .\" .\" Copyright (c) 1999, 2016, 2026 The NetBSD Foundation, Inc. .\" All rights reserved. .\" .\" This code is derived from software contributed to The NetBSD Foundation .\" by Jason R. Thorpe and Roy Marples. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .Dd May 29, 2026 .Dt PIDFILE 3 .Os .Sh NAME .Nm pidfile , .Nm pidfile_lock , .Nm pidfile_read , .Nm pidfile_clean , .Nm pidfile_unlock , .Nm pidfile_fd , .Nm pidfile_path , .Nm pidfile_unremoveable .Nd write a daemon pid file .Sh LIBRARY .Lb libutil .Sh SYNOPSIS .In util.h .Ft int .Fn pidfile "const char *path" .Ft pid_t .Fn pidfile_lock "const char *path" .Ft pid_t .Fn pidfile_read "const char *path" .Ft int .Fn pidfile_clean "void" .Ft int .Fn pidfile_unlock "void" .Ft int .Fn pidfile_fd "void" .Ft const char * .Fn pidfile_path "void" .Ft void .Fn pidfile_unremoveable "void" .Sh DESCRIPTION .Fn pidfile and .Fn pidfile_lock create and lock a file containing the process ID of the calling program. The pid file can be used as a quick reference if the process needs to be sent a signal. The pid file is truncated and removed automatically when the program exits, unless the program receives a fatal signal. .Pp If .Ar path is .Dv NULL or a plain basename (a name containing no directory components), the pid file is created in the .Pa /var/run directory. The file name has the form .Pa /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. .Pp 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. .Pp If called with a new .Ar path , .Fn pidfile and .Fn pidfile_lock will remove the old pid file. .Pp The pid file is truncated, so these functions can be called multiple times and allow a child process to take over the lock. .Pp .Fn pidfile_read will read the last pid file created, or specified by .Ar path , and return the process ID it contains. .Pp .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. .Pp .Fn pidfile_unlock will release the resources held by the pid file, keeping it intact. This is should be called if the process holding the pid file forks another without the intent of taking it over. .Pp .Fn pidfile_fd returns the file descriptor holding the lock for the pid file. .Pp .Fn pidfile_path returns the full path of the pid file. .Pp .Fn pidfile_unremoveable should be called when it's not possible for the process holding the pid file lock to unlink the pid file. For example, the process could be in a .Xr chroot 2 or a sandbox environment which denies the ability to either read or unlink the pid file. .Sh RETURN VALUES .Fn pidfile and .Fn pidfile_clean and .Fn pidfile_unlock return 0 on success and \-1 on failure. .Pp .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. .Pp .Fn pidfile_read returns the process ID if known, otherwise \-1. .Pp .Fn pidfile_fd returns the file descriptor of the locked pid file, otherwise \-1. .Pp .Fn pidfile_path returns a pointer to the path of the locked pid file, otherwise NULL. .Sh ERRORS The .Fn pidfile and .Fn pidfile_lock functions will fail if: .Bl -tag -width Er .It Bq Er EBADF The pid file either hasn't been locked or has already been unlocked. .It Bq Er EEXIST Some process already holds the lock on the given pid file, meaning that a daemon is already running. .El .Pp The .Fn pidfile and .Fn pidfile_lock functions may also fail and set .Va errno for any errors specified by the routines .Xr malloc 3 , .Xr open 2 .Xr lseek 2 .Xr ftruncate 2 and .Xr dprintf 3 . .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 . .Pp The .Fn pidfile_lock , .Fn pidfile_read , and .Fn pidfile_clean function calls appeared in .Nx 8 . .Pp The .Fn pidfile_fd , .Fn pidfile_path , .Fn pidfile_unlock and .Fn pidfile_unremoveable function calls appeared in .Nx 12 . .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 .