xref: /freebsd/lib/libutil/pidfile.3 (revision 52267f7411adcc76ede961420e08c0e42f42d415)
1.\" Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd October 20, 2008
28.Dt PIDFILE 3
29.Os
30.Sh NAME
31.Nm pidfile_open ,
32.Nm pidfile_write ,
33.Nm pidfile_close ,
34.Nm pidfile_remove
35.Nd "library for PID files handling"
36.Sh LIBRARY
37.Lb libutil
38.Sh SYNOPSIS
39.In sys/param.h
40.In libutil.h
41.Ft "struct pidfh *"
42.Fn pidfile_open "const char *path" "mode_t mode" "pid_t *pidptr"
43.Ft int
44.Fn pidfile_write "struct pidfh *pfh"
45.Ft int
46.Fn pidfile_close "struct pidfh *pfh"
47.Ft int
48.Fn pidfile_remove "struct pidfh *pfh"
49.Sh DESCRIPTION
50The
51.Nm pidfile
52family of functions allows daemons to handle PID files.
53It uses
54.Xr flopen 3
55to lock a pidfile and detect already running daemons.
56.Pp
57The
58.Fn pidfile_open
59function opens (or creates) a file specified by the
60.Fa path
61argument and locks it.
62If a file can not be locked, a PID of an already running daemon is returned in
63the
64.Fa pidptr
65argument (if it is not
66.Dv NULL ) .
67The function does not write process' PID into the file here, so it can be
68used before
69.Fn fork Ns ing
70and exit with a proper error message when needed.
71If the
72.Fa path
73argument is
74.Dv NULL ,
75.Pa /var/run/ Ns Ao Va progname Ac Ns Pa .pid
76file will be used.
77.Pp
78The
79.Fn pidfile_write
80function writes process' PID into a previously opened file.
81.Pp
82The
83.Fn pidfile_close
84function closes a pidfile.
85It should be used after daemon
86.Fn fork Ns s
87to start a child process.
88.Pp
89The
90.Fn pidfile_remove
91function closes and removes a pidfile.
92.Sh RETURN VALUES
93The
94.Fn pidfile_open
95function returns a valid pointer to a
96.Vt pidfh
97structure on success, or
98.Dv NULL
99if an error occurs.
100If an error occurs,
101.Va errno
102will be set.
103.Rv -std pidfile_write pidfile_close pidfile_remove
104.Sh EXAMPLES
105The following example shows in which order these functions should be used.
106Note that it is safe to pass
107.Dv NULL
108to
109.Fn pidfile_write ,
110.Fn pidfile_remove
111and
112.Fn pidfile_close
113functions.
114.Bd -literal
115struct pidfh *pfh;
116pid_t otherpid, childpid;
117
118pfh = pidfile_open("/var/run/daemon.pid", 0600, &otherpid);
119if (pfh == NULL) {
120	if (errno == EEXIST) {
121		errx(EXIT_FAILURE, "Daemon already running, pid: %jd.",
122		    (intmax_t)otherpid);
123	}
124	/* If we cannot create pidfile from other reasons, only warn. */
125	warn("Cannot open or create pidfile");
126}
127
128if (daemon(0, 0) == -1) {
129	warn("Cannot daemonize");
130	pidfile_remove(pfh);
131	exit(EXIT_FAILURE);
132}
133
134pidfile_write(pfh);
135
136for (;;) {
137	/* Do work. */
138	childpid = fork();
139	switch (childpid) {
140	case -1:
141		syslog(LOG_ERR, "Cannot fork(): %s.", strerror(errno));
142		break;
143	case 0:
144		pidfile_close(pfh);
145		/* Do child work. */
146		break;
147	default:
148		syslog(LOG_INFO, "Child %jd started.", (intmax_t)childpid);
149		break;
150	}
151}
152
153pidfile_remove(pfh);
154exit(EXIT_SUCCESS);
155.Ed
156.Sh ERRORS
157The
158.Fn pidfile_open
159function will fail if:
160.Bl -tag -width Er
161.It Bq Er EEXIST
162Some process already holds the lock on the given pidfile, meaning that a
163daemon is already running.
164.It Bq Er ENAMETOOLONG
165Specified pidfile's name is too long.
166.It Bq Er EINVAL
167Some process already holds the lock on the given pidfile, but PID read
168from there is invalid.
169.It Bq Er EAGAIN
170Some process already holds the lock on the given pidfile, but the file
171is truncated.
172Most likely, the existing daemon is writing new PID into
173the file.
174.El
175.Pp
176The
177.Fn pidfile_open
178function may also fail and set
179.Va errno
180for any errors specified for the
181.Xr fstat 2 ,
182.Xr open 2 ,
183and
184.Xr read 2
185calls.
186.Pp
187The
188.Fn pidfile_write
189function will fail if:
190.Bl -tag -width Er
191.It Bq Er EDOOFUS
192Improper function use.
193Probably called before
194.Fn pidfile_open .
195.El
196.Pp
197The
198.Fn pidfile_write
199function may also fail and set
200.Va errno
201for any errors specified for the
202.Xr fstat 2 ,
203.Xr ftruncate 2 ,
204and
205.Xr write 2
206calls.
207.Pp
208The
209.Fn pidfile_close
210function may fail and set
211.Va errno
212for any errors specified for the
213.Xr close 2
214and
215.Xr fstat 2
216calls.
217.Pp
218The
219.Fn pidfile_remove
220function will fail if:
221.Bl -tag -width Er
222.It Bq Er EDOOFUS
223Improper function use.
224Probably called not from the process which made
225.Fn pidfile_write .
226.El
227.Pp
228The
229.Fn pidfile_remove
230function may also fail and set
231.Va errno
232for any errors specified for the
233.Xr close 2 ,
234.Xr fstat 2 ,
235.Xr write 2 ,
236and
237.Xr unlink 2
238system calls and the
239.Xr flopen 3
240library function.
241.Sh SEE ALSO
242.Xr open 2 ,
243.Xr daemon 3 ,
244.Xr flopen 3
245.Sh AUTHORS
246.An -nosplit
247The
248.Nm pidfile
249functionality is based on ideas from
250.An John-Mark Gurney Aq jmg@FreeBSD.org .
251.Pp
252The code and manual page was written by
253.An Pawel Jakub Dawidek Aq pjd@FreeBSD.org .
254