FREEBSD::STRINGF(3)

NAME
freebsd::pidfile - own a PID file handle

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). 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. 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.

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).

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.

The fileno() method returns the underlying file descriptor for the current PID file via pidfile_fileno(3).

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)