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: src/lib/libutil/pidfile.3,v 1.5 2006/03/04 15:20:28 keramida Exp $
35 .Nd "library for PID files handling"
42 .Fn pidfile_open "const char *path" "mode_t mode" "pid_t *pidptr"
44 .Fn pidfile_write "struct pidfh *pfh"
46 .Fn pidfile_close "struct pidfh *pfh"
48 .Fn pidfile_remove "struct pidfh *pfh"
52 family of functions allows daemons to handle PID files.
55 to lock a pidfile and detect already running daemons.
59 function opens (or creates) a file specified by the
61 argument and locks it with the
64 If a file can not be locked, a PID of an already running daemon is returned in
67 argument (if it is not
69 The function does not write process' PID into the file here, so it can be
72 and exit with a proper error message when needed.
77 .Pa /var/run/ Ns Ao Va progname Ac Ns Pa .pid
82 function writes process' PID into a previously opened file.
86 function closes a pidfile.
87 It should be used after daemon
89 to start a child process.
93 function closes and removes a pidfile.
97 function returns a valid pointer to a
99 structure on success, or
105 .Rv -std pidfile_write pidfile_close pidfile_remove
107 The following example shows in which order these functions should be used.
108 Note that it is safe to pass
118 pid_t otherpid, childpid;
120 pfh = pidfile_open("/var/run/daemon.pid", 0600, &otherpid);
122 if (errno == EEXIST) {
123 errx(EXIT_FAILURE, "Daemon already running, pid: %jd.",
126 /* If we cannot create pidfile from other reasons, only warn. */
127 warn("Cannot open or create pidfile");
130 if (daemon(0, 0) == -1) {
131 warn("Cannot daemonize");
143 syslog(LOG_ERR, "Cannot fork(): %s.", strerror(errno));
150 syslog(LOG_INFO, "Child %jd started.", (intmax_t)childpid);
161 function will fail if:
164 Some process already holds the lock on the given pidfile, meaning that a
165 daemon is already running.
166 .It Bq Er ENAMETOOLONG
167 Specified pidfile's name is too long.
169 Some process already holds the lock on the given pidfile, but PID read
170 from there is invalid.
175 function may also fail and set
177 for any errors specified for the
186 function will fail if:
189 Improper function use.
190 Probably called before
196 function may also fail and set
198 for any errors specified for the
207 function may fail and set
209 for any errors specified for the
217 function will fail if:
220 Improper function use.
221 Probably called not from the process which made
227 function may also fail and set
229 for any errors specified for the
245 functionality is based on ideas from
246 .An John-Mark Gurney Aq jmg@FreeBSD.org .
248 The code and manual page was written by
249 .An Pawel Jakub Dawidek Aq pjd@FreeBSD.org .