xref: /netbsd/lib/libutil/pidlock.3 (revision c4a72b64)
1.\"	$NetBSD: pidlock.3,v 1.5 2002/10/01 19:36:30 wiz Exp $
2.\"
3.\" Copyright 1996, 1997 by Curt Sampson <cjs@netbsd.org>
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\"
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.
22.\"
23.Dd November 10, 1996
24.Os
25.Dt PIDLOCK 3
26.Sh NAME
27.Nm pidlock ,
28.Nm ttylock ,
29.Nm ttyunlock
30.Nd locks based on files containing PIDs
31.Sh LIBRARY
32.Lb libutil
33.Sh SYNOPSIS
34.Fd #include \*[Lt]util.h\*[Gt]
35.Ft int
36.Fn pidlock "const char *lockfile" "int flags" "pid_t *locker" "const char *info"
37.Ft int
38.Fn ttylock "const char *tty" "int flags" "pid_t *locker"
39.Ft int
40.Fn ttyunlock "const char *tty"
41.Sh DESCRIPTION
42The
43.Fn pidlock
44.Fn ttylock ,
45and
46.Fn ttyunlock
47functions attempt to create a lockfile for an arbitrary resource that
48only one program may hold at a time.
49(In the case of
50.Fn ttylock ,
51this is access to a tty device.)
52If the
53function succeeds in creating the lockfile, it will succeed for
54no other program calling it with the same lockfile until the original
55calling program has removed the lockfile or exited.
56The
57.Fn ttyunlock
58function will remove the lockfile created by
59.Fn ttylock .
60.Pp
61These functions use the method of creating a lockfile traditionally
62used by UUCP software.
63This is described as follows in the documentation for Taylor UUCP:
64.Bd -filled -offset indent
65The lock file normally contains the process ID of the locking process.
66This makes it easy to determine whether a lock is still valid.
67The algorithm is to create a temporary file and then link
68it to the name that must be locked.
69If the link fails because a file with that name already exists,
70the existing file is read to get the process ID.
71If the process still exists, the lock attempt fails.
72Otherwise the lock file is deleted and the locking algorithm
73is retried.
74.Ed
75.Pp
76The PID is stored in ASCII format, with leading spaces to pad it
77out to ten characters, and a terminating newline.
78This implementation has been extended to put the hostname
79on the second line of the file, terminated with a newline, and
80optionally an arbitrary comment on the third line of the file, also
81terminated with a newline.
82If a comment is given, but
83.Dv PIDLOCK_NONBLOCK
84is not, a blank line will be written as the second line of the file.
85.Pp
86The
87.Fn pidlock
88function will attempt to create the file
89.Fa lockfile
90and put the current process's pid in it.
91The
92.Fn ttylock
93function will do the same, but should be passed only the base name
94(with no leading directory prefix) of the tty to be locked; it will
95test that the tty exists in
96.Pa /dev
97and is a character device, and then create
98the file in the
99.Pa /var/spool/lock
100directory and prefix the filename with
101.Pa LCK.. .
102Use the
103.Fn ttyunlock
104function to remove this lock.
105.Pp
106The following flags may be passed in
107.Pa flags :
108.Bl -tag -width Dv -offset indent
109.It Dv PIDLOCK_NONBLOCK
110The function should return immediately when a lock is held by another
111active process.
112Otherwise the function will wait (forever, if necessary)
113for the lock to be freed.
114.It Dv PIDLOCK_USEHOSTNAME
115The hostname should be compared against the hostname in the second
116line of the file (if present), and if they differ, no attempt at
117checking for a living process holding the lock will be made, and
118the lockfile will never be deleted.
119(The process is assumed to be alive.)
120This is used for locking on NFS or other remote filesystems.
121(The function will never create a lock if
122.Dv PIDLOCK_USEHOSTNAME
123is specified and no hostname is present.)
124.El
125.Pp
126If
127.Pa locker
128is non-null, it will contain the PID of the locking process, if there
129is one, on return.
130.Pp
131If
132.Pa info
133is non-null and the lock succeeds, the string it points to will be
134written as the third line of the lock file.
135.Sh RETURN VALUES
136Zero is returned if the operation was successful; on an error a -1
137is returned and a standard error code is left in the global location errno.
138.Sh ERRORS
139These are among the values left in
140.Va errno
141if
142.Fn pidlock
143or
144.Fn ttylock
145returns a failure:
146.Bl -tag -width Er
147.It Bq Er EPERM
148The current process does not have some of the privileges necessary
149to perform the lock.
150These include read and write access to the lock
151directory, and read access to the current lockfile, if it exists.
152.It Bq Er ENOENT
153A component of a specified pathname did not exist, or the pathname
154was an empty string.
155.It Bq Er EWOULBLOCK
156Another runnning process has a lock and the
157.Dv PIDLOCK_NONBLOCK
158flag was specified.
159.It Bq Er ENAMETOOLONG
160A component of the path name exceeded 255 (MAXNAMELEN) characters,
161or an entire path name exceeded 1023 (MAXPATHLEN-1) characters.
162.El
163.\" .Sh SEE ALSO
164.Sh HISTORY
165The
166.Fn pidlock
167and
168.Fn ttylock
169functions appeared in
170.Nx 1.3 .
171.Sh AUTHORS
172Curt Sampson \*[Lt]cjs@netbsd.org\*[Gt]
173.Sh BUGS
174The lockfile format breaks if a pid is longer than ten digits when
175printed in decimal form.
176.Pp
177The PID returned will be the pid of the locker on the remote machine if
178.Dv PIDLOCK_USEHOSTNAME
179is specified, but there is no indication that this is not on the local
180machine.
181