FREEBSD::STRINGF(3)
π NAME π
okay so bestie, welcome to the FREEBSD::STRINGF(3) page from the Library Functions Manual β and this one is all about freebsd::pidfile. iconic.
freebsd::pidfile β own a PID file handle.
π LIBRARY π
library βlibutil++β β yes, that's the whole library section, and yes it's giving double-plus slay.
β¨ 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). that's the whole vibe: one class instance, one PID-file-handle situation, no cap.
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) functions. so it's unique_ptr-coded, but make it PID-file-coded, iykyk.
The currently-owned PID file is removed by invoking pidfile_remove(3) 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. so yeah, cleanup is literally part of the main character energy.
The release() method relinquishes ownership of the current PID file handle and returns the value of the previously-owned PID file handle. it's giving βhand it over, bestie.β
The write() method writes out the PID of the current process to the PID file via pidfile_write(3). very demure, very PID-pilled.
The close() method closes the current PID file without removing it via pidfile_close(3). If the close succeeds, the PID file handle is no longer valid.
π¨ real-talk caveat: if close() succeeds, the PID file handle is no longer valid. not me forgetting that. π
The fileno() method returns the underlying file descriptor for the current PID file via pidfile_fileno(3). fd reveal, slay.
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. so if (!pf) is a whole vibe check, fr fr.
π bestie note: there is no get() method here β the raw pointer does not get exported, periodt.
πΈ 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;
}
this example is lowkey giving full daemon main-character energy: open the PID file, vibe check it, daemonize, write the PID, then loop forever like the icon it is. π
π«Ά SEE ALSO π«Ά
pidfile(3)β the other half of the lore.
xoxo, Debian π July 31, 2025 β FREEBSD::STRINGF(3) forever π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π the page's license: BSD-2-Clause
- Copyright (c) 2025 Chelsio Communications, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms