PIDFILE(3) NetBSD Library Functions Manual PIDFILE(3)
NAME
pidfile, pidfile_lock, pidfile_read, pidfile_clean, pidfile_unlock, pidfile_fd, pidfile_path, pidfile_unremoveable -- write a daemon pid file
LIBRARY
System Utilities Library (libutil, -lutil)
SYNOPSIS
#include <util.h> int pidfile(const char *path); pid_t pidfile_lock(const char *path); pid_t pidfile_read(const char *path); int pidfile_clean(void); int pidfile_unlock(void); int pidfile_fd(void); const char * pidfile_path(void); void pidfile_unremoveable(void);
DESCRIPTION
pidfile() and 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. If path is NULL or a plain basename (a name containing no directory com- ponents), the pid file is created in the /var/run directory. The file name has the form /var/run/basename.pid. The basename part is either the value of path if it was not NULL, or the program name as returned by getprogname(3) otherwise. If path is an absolute or relative path (i.e. it contains the `/' charac- ter), the pid file is created in the provided location. If called with a new path, pidfile() and pidfile_lock() will remove the old pid file. The pid file is truncated, so these functions can be called multiple times and allow a child process to take over the lock. pidfile_read() will read the last pid file created, or specified by path, and return the process ID it contains. pidfile_clean() will ftruncate(2), close(2), and unlink(2) the last open- ing pid file if, and only if, the current process wrote it. This func- tion should be called if the program needs to call _exit(2) (such as from a signal handler) and needs to clean up the pid file. 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. pidfile_fd() returns the file descriptor holding the lock for the pid file. pidfile_path() returns the full path of the pid file. 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 chroot(2) or a sandbox environment which denies the ability to either read or unlink the pid file.
RETURN VALUES
pidfile() and pidfile_clean() and pidfile_unlock() return 0 on success and -1 on failure. 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. pidfile_read() returns the process ID if known, otherwise -1. pidfile_fd() returns the file descriptor of the locked pid file, other- wise -1. pidfile_path() returns a pointer to the path of the locked pid file, oth- erwise NULL.
ERRORS
The pidfile() and pidfile_lock() functions will fail if: [EBADF] The pid file either hasn't been locked or has already been unlocked. [EEXIST] Some process already holds the lock on the given pid file and it contained a readable pid. [EAGAIN] Some process already holds the lock on the given pid file but a pid could not be read from it. The pidfile() and pidfile_lock() functions may also fail and set errno for any errors specified by the routines malloc(3), open(2) lseek(2) ftruncate(2) and dprintf(3).
SEE ALSO
flock(2), atexit(3)
HISTORY
The pidfile() function call appeared in NetBSD 1.5. Support for creating pid files in any arbitrary path was added in NetBSD 6.0. The pidfile_lock(), pidfile_read(), and pidfile_clean() function calls appeared in NetBSD 8. The pidfile_fd(), pidfile_path(), pidfile_unlock() and pidfile_unremoveable() function calls appeared in NetBSD 12.
CAVEATS
pidfile() and pidfile_lock() use atexit(3) to ensure the pid file is cleaned at program exit. However, programs that use the _exit(2) func- tion (for example, in signal handlers) will not trigger this behaviour and should call pidfile_clean(). NetBSD 11.99 June 11, 2026 NetBSD 11.99
Powered by man-cgi (2026-04-06). Maintained for NetBSD by Kimmo Suominen. Based on man-cgi by Panagiotis Christias.