1 .\" $NetBSD: pidlock.3,v 1.12 2009/03/09 19:24:27 joerg Exp $
3 .\" Copyright 1996, 1997 by Curt Sampson <cjs@NetBSD.org>
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
11 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
12 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
13 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
15 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 .\" POSSIBILITY OF SUCH DAMAGE.
30 .Nd locks based on files containing PIDs
36 .Fn pidlock "const char *lockfile" "int flags" "pid_t *locker" "const char *info"
38 .Fn ttylock "const char *tty" "int flags" "pid_t *locker"
40 .Fn ttyunlock "const char *tty"
47 functions attempt to create a lockfile for an arbitrary resource that
48 only one program may hold at a time.
51 this is access to a tty device.)
53 function succeeds in creating the lockfile, it will succeed for
54 no other program calling it with the same lockfile until the original
55 calling program has removed the lockfile or exited.
58 function will remove the lockfile created by
61 These functions use the method of creating a lockfile traditionally
62 used by UUCP software.
63 This is described as follows in the documentation for Taylor UUCP:
64 .Bd -filled -offset indent
65 The lock file normally contains the process ID of the locking process.
66 This makes it easy to determine whether a lock is still valid.
67 The algorithm is to create a temporary file and then link
68 it to the name that must be locked.
69 If the link fails because a file with that name already exists,
70 the existing file is read to get the process ID.
71 If the process still exists, the lock attempt fails.
72 Otherwise the lock file is deleted and the locking algorithm
76 The PID is stored in ASCII format, with leading spaces to pad it
77 out to ten characters, and a terminating newline.
78 This implementation has been extended to put the hostname
79 on the second line of the file, terminated with a newline, and
80 optionally an arbitrary comment on the third line of the file, also
81 terminated with a newline.
82 If a comment is given, but
84 is not, a blank line will be written as the second line of the file.
88 function will attempt to create the file
90 and put the current process's pid in it.
93 function will do the same, but should be passed only the base name
94 (with no leading directory prefix) of the
96 to be locked; it will test that the tty exists in
98 and is a character device, and then create
101 directory and prefix the filename with
105 function to remove this lock.
107 The following flags may be passed in
109 .Bl -tag -width Dv -offset indent
110 .It Dv PIDLOCK_NONBLOCK
111 The function should return immediately when a lock is held by another
113 Otherwise the function will wait (forever, if necessary)
114 for the lock to be freed.
115 .It Dv PIDLOCK_USEHOSTNAME
116 The hostname should be compared against the hostname in the second
117 line of the file (if present), and if they differ, no attempt at
118 checking for a living process holding the lock will be made, and
119 the lockfile will never be deleted.
120 (The process is assumed to be alive.)
121 This is used for locking on NFS or other remote filesystems.
122 (The function will never create a lock if
123 .Dv PIDLOCK_USEHOSTNAME
124 is specified and no hostname is present.)
129 is non-null, it will contain the PID of the locking process, if there
134 is non-null and the lock succeeds, the string it points to will be
135 written as the third line of the lock file.
137 Zero is returned if the operation was successful; on an error a -1
138 is returned and a standard error code is left in the global location
141 In addition to the errors that are returned from
153 to the following values on failure:
155 .It Bq Er EWOULDBLOCK
156 Another running process has a lock and the
164 is not a character special device.
172 functions appeared in
178 The lockfile format breaks if a pid is longer than ten digits when
179 printed in decimal form.
181 The PID returned will be the pid of the locker on the remote machine if
182 .Dv PIDLOCK_USEHOSTNAME
183 is specified, but there is no indication that this is not on the local