xref: /minix3/lib/libutil/pidlock.3 (revision 0c3983b25a88161cf074524e5c94585a2582ae82)
1*0c3983b2SBen Gras.\"	$NetBSD: pidlock.3,v 1.12 2009/03/09 19:24:27 joerg Exp $
2*0c3983b2SBen Gras.\"
3*0c3983b2SBen Gras.\" Copyright 1996, 1997 by Curt Sampson <cjs@NetBSD.org>
4*0c3983b2SBen Gras.\"
5*0c3983b2SBen Gras.\" Redistribution and use in source and binary forms, with or without
6*0c3983b2SBen Gras.\" modification, are permitted provided that the following conditions
7*0c3983b2SBen Gras.\" are met:
8*0c3983b2SBen Gras.\" 1. Redistributions of source code must retain the above copyright
9*0c3983b2SBen Gras.\"    notice, this list of conditions and the following disclaimer.
10*0c3983b2SBen Gras.\"
11*0c3983b2SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
12*0c3983b2SBen Gras.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
13*0c3983b2SBen Gras.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14*0c3983b2SBen Gras.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
15*0c3983b2SBen Gras.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16*0c3983b2SBen Gras.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17*0c3983b2SBen Gras.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18*0c3983b2SBen Gras.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19*0c3983b2SBen Gras.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20*0c3983b2SBen Gras.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21*0c3983b2SBen Gras.\" POSSIBILITY OF SUCH DAMAGE.
22*0c3983b2SBen Gras.\"
23*0c3983b2SBen Gras.Dd March 19, 2006
24*0c3983b2SBen Gras.Dt PIDLOCK 3
25*0c3983b2SBen Gras.Os
26*0c3983b2SBen Gras.Sh NAME
27*0c3983b2SBen Gras.Nm pidlock ,
28*0c3983b2SBen Gras.Nm ttylock ,
29*0c3983b2SBen Gras.Nm ttyunlock
30*0c3983b2SBen Gras.Nd locks based on files containing PIDs
31*0c3983b2SBen Gras.Sh LIBRARY
32*0c3983b2SBen Gras.Lb libutil
33*0c3983b2SBen Gras.Sh SYNOPSIS
34*0c3983b2SBen Gras.In util.h
35*0c3983b2SBen Gras.Ft int
36*0c3983b2SBen Gras.Fn pidlock "const char *lockfile" "int flags" "pid_t *locker" "const char *info"
37*0c3983b2SBen Gras.Ft int
38*0c3983b2SBen Gras.Fn ttylock "const char *tty" "int flags" "pid_t *locker"
39*0c3983b2SBen Gras.Ft int
40*0c3983b2SBen Gras.Fn ttyunlock "const char *tty"
41*0c3983b2SBen Gras.Sh DESCRIPTION
42*0c3983b2SBen GrasThe
43*0c3983b2SBen Gras.Fn pidlock
44*0c3983b2SBen Gras.Fn ttylock ,
45*0c3983b2SBen Grasand
46*0c3983b2SBen Gras.Fn ttyunlock
47*0c3983b2SBen Grasfunctions attempt to create a lockfile for an arbitrary resource that
48*0c3983b2SBen Grasonly one program may hold at a time.
49*0c3983b2SBen Gras(In the case of
50*0c3983b2SBen Gras.Fn ttylock ,
51*0c3983b2SBen Grasthis is access to a tty device.)
52*0c3983b2SBen GrasIf the
53*0c3983b2SBen Grasfunction succeeds in creating the lockfile, it will succeed for
54*0c3983b2SBen Grasno other program calling it with the same lockfile until the original
55*0c3983b2SBen Grascalling program has removed the lockfile or exited.
56*0c3983b2SBen GrasThe
57*0c3983b2SBen Gras.Fn ttyunlock
58*0c3983b2SBen Grasfunction will remove the lockfile created by
59*0c3983b2SBen Gras.Fn ttylock .
60*0c3983b2SBen Gras.Pp
61*0c3983b2SBen GrasThese functions use the method of creating a lockfile traditionally
62*0c3983b2SBen Grasused by UUCP software.
63*0c3983b2SBen GrasThis is described as follows in the documentation for Taylor UUCP:
64*0c3983b2SBen Gras.Bd -filled -offset indent
65*0c3983b2SBen GrasThe lock file normally contains the process ID of the locking process.
66*0c3983b2SBen GrasThis makes it easy to determine whether a lock is still valid.
67*0c3983b2SBen GrasThe algorithm is to create a temporary file and then link
68*0c3983b2SBen Grasit to the name that must be locked.
69*0c3983b2SBen GrasIf the link fails because a file with that name already exists,
70*0c3983b2SBen Grasthe existing file is read to get the process ID.
71*0c3983b2SBen GrasIf the process still exists, the lock attempt fails.
72*0c3983b2SBen GrasOtherwise the lock file is deleted and the locking algorithm
73*0c3983b2SBen Grasis retried.
74*0c3983b2SBen Gras.Ed
75*0c3983b2SBen Gras.Pp
76*0c3983b2SBen GrasThe PID is stored in ASCII format, with leading spaces to pad it
77*0c3983b2SBen Grasout to ten characters, and a terminating newline.
78*0c3983b2SBen GrasThis implementation has been extended to put the hostname
79*0c3983b2SBen Grason the second line of the file, terminated with a newline, and
80*0c3983b2SBen Grasoptionally an arbitrary comment on the third line of the file, also
81*0c3983b2SBen Grasterminated with a newline.
82*0c3983b2SBen GrasIf a comment is given, but
83*0c3983b2SBen Gras.Dv PIDLOCK_NONBLOCK
84*0c3983b2SBen Grasis not, a blank line will be written as the second line of the file.
85*0c3983b2SBen Gras.Pp
86*0c3983b2SBen GrasThe
87*0c3983b2SBen Gras.Fn pidlock
88*0c3983b2SBen Grasfunction will attempt to create the file
89*0c3983b2SBen Gras.Fa lockfile
90*0c3983b2SBen Grasand put the current process's pid in it.
91*0c3983b2SBen GrasThe
92*0c3983b2SBen Gras.Fn ttylock
93*0c3983b2SBen Grasfunction will do the same, but should be passed only the base name
94*0c3983b2SBen Gras(with no leading directory prefix) of the
95*0c3983b2SBen Gras.Fa tty
96*0c3983b2SBen Grasto be locked; it will test that the tty exists in
97*0c3983b2SBen Gras.Pa /dev
98*0c3983b2SBen Grasand is a character device, and then create
99*0c3983b2SBen Grasthe file in the
100*0c3983b2SBen Gras.Pa /var/spool/lock
101*0c3983b2SBen Grasdirectory and prefix the filename with
102*0c3983b2SBen Gras.Pa LCK.. .
103*0c3983b2SBen GrasUse the
104*0c3983b2SBen Gras.Fn ttyunlock
105*0c3983b2SBen Grasfunction to remove this lock.
106*0c3983b2SBen Gras.Pp
107*0c3983b2SBen GrasThe following flags may be passed in
108*0c3983b2SBen Gras.Pa flags :
109*0c3983b2SBen Gras.Bl -tag -width Dv -offset indent
110*0c3983b2SBen Gras.It Dv PIDLOCK_NONBLOCK
111*0c3983b2SBen GrasThe function should return immediately when a lock is held by another
112*0c3983b2SBen Grasactive process.
113*0c3983b2SBen GrasOtherwise the function will wait (forever, if necessary)
114*0c3983b2SBen Grasfor the lock to be freed.
115*0c3983b2SBen Gras.It Dv PIDLOCK_USEHOSTNAME
116*0c3983b2SBen GrasThe hostname should be compared against the hostname in the second
117*0c3983b2SBen Grasline of the file (if present), and if they differ, no attempt at
118*0c3983b2SBen Graschecking for a living process holding the lock will be made, and
119*0c3983b2SBen Grasthe lockfile will never be deleted.
120*0c3983b2SBen Gras(The process is assumed to be alive.)
121*0c3983b2SBen GrasThis is used for locking on NFS or other remote filesystems.
122*0c3983b2SBen Gras(The function will never create a lock if
123*0c3983b2SBen Gras.Dv PIDLOCK_USEHOSTNAME
124*0c3983b2SBen Grasis specified and no hostname is present.)
125*0c3983b2SBen Gras.El
126*0c3983b2SBen Gras.Pp
127*0c3983b2SBen GrasIf
128*0c3983b2SBen Gras.Pa locker
129*0c3983b2SBen Grasis non-null, it will contain the PID of the locking process, if there
130*0c3983b2SBen Grasis one, on return.
131*0c3983b2SBen Gras.Pp
132*0c3983b2SBen GrasIf
133*0c3983b2SBen Gras.Pa info
134*0c3983b2SBen Grasis non-null and the lock succeeds, the string it points to will be
135*0c3983b2SBen Graswritten as the third line of the lock file.
136*0c3983b2SBen Gras.Sh RETURN VALUES
137*0c3983b2SBen GrasZero is returned if the operation was successful; on an error a -1
138*0c3983b2SBen Grasis returned and a standard error code is left in the global location
139*0c3983b2SBen Gras.Va errno .
140*0c3983b2SBen Gras.Sh ERRORS
141*0c3983b2SBen GrasIn addition to the errors that are returned from
142*0c3983b2SBen Gras.Xr stat 2 ,
143*0c3983b2SBen Gras.Xr open 2 ,
144*0c3983b2SBen Gras.Xr read 2 ,
145*0c3983b2SBen Gras.Xr write 2 ,
146*0c3983b2SBen Grasand
147*0c3983b2SBen Gras.Xr link 2 ,
148*0c3983b2SBen Gras.Fn pidlock
149*0c3983b2SBen Grasor
150*0c3983b2SBen Gras.Fn ttylock
151*0c3983b2SBen Grascan set
152*0c3983b2SBen Gras.Va errno
153*0c3983b2SBen Grasto the following values on failure:
154*0c3983b2SBen Gras.Bl -tag -width Er
155*0c3983b2SBen Gras.It Bq Er EWOULDBLOCK
156*0c3983b2SBen GrasAnother running process has a lock and the
157*0c3983b2SBen Gras.Dv PIDLOCK_NONBLOCK
158*0c3983b2SBen Grasflag was specified.
159*0c3983b2SBen Gras.It Bq Er EFTYPE
160*0c3983b2SBen GrasThe
161*0c3983b2SBen Gras.Fa tty
162*0c3983b2SBen Grasspecified in
163*0c3983b2SBen Gras.Fn ttylock
164*0c3983b2SBen Grasis not a character special device.
165*0c3983b2SBen Gras.El
166*0c3983b2SBen Gras.\" .Sh SEE ALSO
167*0c3983b2SBen Gras.Sh HISTORY
168*0c3983b2SBen GrasThe
169*0c3983b2SBen Gras.Fn pidlock
170*0c3983b2SBen Grasand
171*0c3983b2SBen Gras.Fn ttylock
172*0c3983b2SBen Grasfunctions appeared in
173*0c3983b2SBen Gras.Nx 1.3 .
174*0c3983b2SBen Gras.Sh AUTHORS
175*0c3983b2SBen Gras.An Curt Sampson
176*0c3983b2SBen Gras.Aq cjs@NetBSD.org .
177*0c3983b2SBen Gras.Sh BUGS
178*0c3983b2SBen GrasThe lockfile format breaks if a pid is longer than ten digits when
179*0c3983b2SBen Grasprinted in decimal form.
180*0c3983b2SBen Gras.Pp
181*0c3983b2SBen GrasThe PID returned will be the pid of the locker on the remote machine if
182*0c3983b2SBen Gras.Dv PIDLOCK_USEHOSTNAME
183*0c3983b2SBen Grasis specified, but there is no indication that this is not on the local
184*0c3983b2SBen Grasmachine.
185