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