---
name: "freebsd::pidfile"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/freebsd::pidfile"
---

# freebsd::pidfile(3)

own a PID file handle

## NAME

**freebsd::pidfile** – own a PID file handle

## LIBRARY

library “libutil++”

## SYNOPSIS

**#include \<libutil++.hh\>**

*class* *freebsd::pidfile* {

**pidfile**();

**pidfile**(*struct pidfh \*pfh*);

**pidfile**(*pidfile &&other*);

**~pidfile**();

*struct* *pidfh* *\**\

**release**();

*void*\

**reset**(*struct pidfh \*newpfh = nullptr*);

*int*\

**write**();

*int*\

**close**();

*int*\

**fileno**();

*pidfile &*\

**operator=**(*pidfile &&other*);

*pidfile &*\

**operator=**(*struct pidfh \*pfh*);

**explicit operator bool**();

};

## DESCRIPTION

Each instance of this class owns a PID file handle created by [pidfile\_open(3)](/pidfile_open/3.md). This class is patterned on std::unique\_ptr; however, rather than exporting the raw pointer via a **get**() method, this class provides wrapper methods for each of the other [pidfile(3)](/pidfile/3.md) functions. The currently-owned PID file is removed by invoking [pidfile\_remove(3)](/pidfile_remove/3.md) when an instance of this class is destroyed. The currently-owned PID file is also removed if it is replaced by the **reset**() method or assignment operators.

The **release**() method relinquishes ownership of the current PID file handle and returns the value of the previously-owned PID file handle.

The **write**() method writes out the PID of the current process to the PID file via [pidfile\_write(3)](/pidfile_write/3.md).

The **close**() method closes the current PID file without removing it via [pidfile\_close(3)](/pidfile_close/3.md). If the close succeeds, the PID file handle is no longer valid.

The **fileno**() method returns the underlying file descriptor for the current PID file via [pidfile\_fileno(3)](/pidfile_fileno/3.md).

The explicit *bool* conversion operator permits testing the validity of an object. The operator returns true if the instance owns a valid PID file handle.

## EXAMPLES

```
int
main()
{
	freebsd::pidfile pf(pidfile_open("/var/run/daemon.pid",
	    0600, NULL));
	if (!pf)
		err(1, "pidfile_open");
	if (daemon(0, 0) == -1) {
		warn("daemon");
		return 1;
	}
	pf->write();
	for (;;) {
		/* Do Work */
	}
	return 0;
}
```

## SEE ALSO

[pidfile(3)](/pidfile/3.md)
