1 .\" Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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
25 .\" $FreeBSD: head/lib/libutil/pidfile.3 231193 2012-02-08 08:49:30Z pjd $
35 .Nd "library for PID files handling"
41 .Fn pidfile_open "const char *path" "mode_t mode" "pid_t *pidptr"
43 .Fn pidfile_write "struct pidfh *pfh"
45 .Fn pidfile_close "struct pidfh *pfh"
47 .Fn pidfile_remove "struct pidfh *pfh"
49 .Fn pidfile_fileno "const struct pidfh *pfh"
53 family of functions allows daemons to handle PID files.
56 to lock a pidfile and detect already running daemons.
60 function opens (or creates) a file specified by the
62 argument and locks it.
67 and file can not be locked, the function will use it to store a PID of an
68 already running daemon or
70 in case daemon did not write its PID yet.
71 The function does not write process' PID into the file here, so it can be
74 and exit with a proper error message when needed.
79 .Pa /var/run/ Ns Ao Va progname Ac Ns Pa .pid
83 function sets the O_CLOEXEC close-on-exec flag when opening the pidfile.
87 function writes process' PID into a previously opened file.
88 The file is truncated before write, so calling the
90 function multiple times is supported.
94 function closes a pidfile.
95 It should be used after daemon
97 to start a child process.
101 function closes and removes a pidfile.
105 function returns the file descriptor for the open pidfile.
109 function returns a valid pointer to a
111 structure on success, or
118 .Rv -std pidfile_write pidfile_close pidfile_remove
122 function returns the low-level file descriptor.
129 is specified, or if the pidfile is no longer open.
131 The following example shows in which order these functions should be used.
132 Note that it is safe to pass
143 pid_t otherpid, childpid;
145 pfh = pidfile_open("/var/run/daemon.pid", 0600, &otherpid);
147 if (errno == EEXIST) {
148 errx(EXIT_FAILURE, "Daemon already running, pid: %jd.",
151 /* If we cannot create pidfile from other reasons, only warn. */
152 warn("Cannot open or create pidfile");
154 * Even though pfh is NULL we can continue, as the other pidfile_*
155 * function can handle such situation by doing nothing except setting
160 if (daemon(0, 0) == -1) {
161 warn("Cannot daemonize");
173 syslog(LOG_ERR, "Cannot fork(): %s.", strerror(errno));
180 syslog(LOG_INFO, "Child %jd started.", (intmax_t)childpid);
191 function will fail if:
194 Some process already holds the lock on the given pidfile, meaning that a
195 daemon is already running.
200 the function will use it to store a PID of an already running daemon or
202 in case daemon did not write its PID yet.
203 .It Bq Er ENAMETOOLONG
204 Specified pidfile's name is too long.
206 Some process already holds the lock on the given pidfile, but PID read
207 from there is invalid.
212 function may also fail and set
214 for any errors specified for the
223 function will fail if:
226 Improper function use.
227 Probably called before
233 function may also fail and set
235 for any errors specified for the
244 function may fail and set
246 for any errors specified for the
254 function will fail if:
257 Improper function use.
258 Probably called not from the process which made
264 function may also fail and set
266 for any errors specified for the
278 function will fail if:
281 Improper function use.
282 Probably called not from the process which used
293 functionality is based on ideas from
294 .An John-Mark Gurney Aq Mt jmg@FreeBSD.org .
296 The code and manual page was written by
297 .An Pawel Jakub Dawidek Aq Mt pjd@FreeBSD.org .