1*7be913e0SJohn Baldwin.\" 2*7be913e0SJohn Baldwin.\" SPDX-License-Identifier: BSD-2-Clause 3*7be913e0SJohn Baldwin.\" 4*7be913e0SJohn Baldwin.\" Copyright (c) 2025 Chelsio Communications, Inc. 5*7be913e0SJohn Baldwin.\" Written by: John Baldwin <jhb@FreeBSD.org> 6*7be913e0SJohn Baldwin.\" 7*7be913e0SJohn Baldwin.Dd July 31, 2025 8*7be913e0SJohn Baldwin.Dt FREEBSD::STRINGF 3 9*7be913e0SJohn Baldwin.Os 10*7be913e0SJohn Baldwin.Sh NAME 11*7be913e0SJohn Baldwin.Nm freebsd::pidfile 12*7be913e0SJohn Baldwin.Nd own a PID file handle 13*7be913e0SJohn Baldwin.Sh LIBRARY 14*7be913e0SJohn Baldwin.Lb libutil++ 15*7be913e0SJohn Baldwin.Sh SYNOPSIS 16*7be913e0SJohn Baldwin.In libutil++.hh 17*7be913e0SJohn Baldwin.Pp 18*7be913e0SJohn Baldwin.Vt class freebsd::pidfile 19*7be913e0SJohn Baldwin{ 20*7be913e0SJohn Baldwin.Bd -ragged -offset indent 21*7be913e0SJohn Baldwin.Fn pidfile 22*7be913e0SJohn Baldwin.Fn pidfile "struct pidfh *pfh" 23*7be913e0SJohn Baldwin.Fn pidfile "pidfile &&other" 24*7be913e0SJohn Baldwin.Fn ~pidfile 25*7be913e0SJohn Baldwin.Ft struct pidfh * 26*7be913e0SJohn Baldwin.Fn release 27*7be913e0SJohn Baldwin.Ft void 28*7be913e0SJohn Baldwin.Fn reset "struct pidfh *newpfh = nullptr" 29*7be913e0SJohn Baldwin.Ft int 30*7be913e0SJohn Baldwin.Fn write 31*7be913e0SJohn Baldwin.Ft int 32*7be913e0SJohn Baldwin.Fn close 33*7be913e0SJohn Baldwin.Ft int 34*7be913e0SJohn Baldwin.Fn fileno 35*7be913e0SJohn Baldwin.Ft "pidfile &" 36*7be913e0SJohn Baldwin.Fn operator= "pidfile &&other" 37*7be913e0SJohn Baldwin.Ft "pidfile &" 38*7be913e0SJohn Baldwin.Fn operator= "struct pidfh *pfh" 39*7be913e0SJohn Baldwin.Fn "explicit operator bool" 40*7be913e0SJohn Baldwin.Ed 41*7be913e0SJohn Baldwin}; 42*7be913e0SJohn Baldwin.Sh DESCRIPTION 43*7be913e0SJohn BaldwinEach instance of this class owns a PID file handle created by 44*7be913e0SJohn Baldwin.Xr pidfile_open 3 . 45*7be913e0SJohn BaldwinThis class is patterned on std::unique_ptr; 46*7be913e0SJohn Baldwinhowever, 47*7be913e0SJohn Baldwinrather than exporting the raw pointer via a 48*7be913e0SJohn Baldwin.Fn get 49*7be913e0SJohn Baldwinmethod, 50*7be913e0SJohn Baldwinthis class provides wrapper methods for each of the other 51*7be913e0SJohn Baldwin.Xr pidfile 3 52*7be913e0SJohn Baldwinfunctions. 53*7be913e0SJohn BaldwinThe currently-owned PID file is removed by invoking 54*7be913e0SJohn Baldwin.Xr pidfile_remove 3 55*7be913e0SJohn Baldwinwhen an instance of this class is destroyed. 56*7be913e0SJohn BaldwinThe currently-owned PID file is also removed if it is replaced by the 57*7be913e0SJohn Baldwin.Fn reset 58*7be913e0SJohn Baldwinmethod or assignment operators. 59*7be913e0SJohn Baldwin.Pp 60*7be913e0SJohn BaldwinThe 61*7be913e0SJohn Baldwin.Fn release 62*7be913e0SJohn Baldwinmethod relinquishes ownership of the current PID file handle and returns the 63*7be913e0SJohn Baldwinvalue of the previously-owned PID file handle. 64*7be913e0SJohn Baldwin.Pp 65*7be913e0SJohn BaldwinThe 66*7be913e0SJohn Baldwin.Fn write 67*7be913e0SJohn Baldwinmethod writes out the PID of the current process to the PID file via 68*7be913e0SJohn Baldwin.Xr pidfile_write 3 . 69*7be913e0SJohn Baldwin.Pp 70*7be913e0SJohn BaldwinThe 71*7be913e0SJohn Baldwin.Fn close 72*7be913e0SJohn Baldwinmethod closes the current PID file without removing it via 73*7be913e0SJohn Baldwin.Xr pidfile_close 3 . 74*7be913e0SJohn BaldwinIf the close succeeds, the PID file handle is no longer valid. 75*7be913e0SJohn Baldwin.Pp 76*7be913e0SJohn BaldwinThe 77*7be913e0SJohn Baldwin.Fn fileno 78*7be913e0SJohn Baldwinmethod returns the underlying file descriptor for the current PID file via 79*7be913e0SJohn Baldwin.Xr pidfile_fileno 3 . 80*7be913e0SJohn Baldwin.Pp 81*7be913e0SJohn BaldwinThe explicit 82*7be913e0SJohn Baldwin.Vt bool 83*7be913e0SJohn Baldwinconversion operator permits testing the validity of an object. 84*7be913e0SJohn BaldwinThe operator returns true if the instance owns a valid PID file handle. 85*7be913e0SJohn Baldwin.Sh EXAMPLES 86*7be913e0SJohn Baldwin.Bd -literal -offset indent 87*7be913e0SJohn Baldwinint 88*7be913e0SJohn Baldwinmain() 89*7be913e0SJohn Baldwin{ 90*7be913e0SJohn Baldwin freebsd::pidfile pf(pidfile_open("/var/run/daemon.pid", 91*7be913e0SJohn Baldwin 0600, NULL)); 92*7be913e0SJohn Baldwin if (!pf) 93*7be913e0SJohn Baldwin err(1, "pidfile_open"); 94*7be913e0SJohn Baldwin 95*7be913e0SJohn Baldwin if (daemon(0, 0) == -1) { 96*7be913e0SJohn Baldwin warn("daemon"); 97*7be913e0SJohn Baldwin return 1; 98*7be913e0SJohn Baldwin } 99*7be913e0SJohn Baldwin 100*7be913e0SJohn Baldwin pf->write(); 101*7be913e0SJohn Baldwin 102*7be913e0SJohn Baldwin for (;;) { 103*7be913e0SJohn Baldwin /* Do Work */ 104*7be913e0SJohn Baldwin } 105*7be913e0SJohn Baldwin 106*7be913e0SJohn Baldwin return 0; 107*7be913e0SJohn Baldwin} 108*7be913e0SJohn Baldwin.Ed 109*7be913e0SJohn Baldwin.Sh SEE ALSO 110*7be913e0SJohn Baldwin.Xr pidfile 3 111