2006-03-23 22:30:27 +03:00
|
|
|
.\" $NetBSD: pidlock.3,v 1.11 2006/03/23 19:30:27 wiz Exp $
|
1997-10-11 06:56:22 +04:00
|
|
|
.\"
|
2003-02-14 19:29:11 +03:00
|
|
|
.\" Copyright 1996, 1997 by Curt Sampson <cjs@NetBSD.org>
|
1997-10-11 06:56:22 +04:00
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
.\" POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
2006-03-20 00:56:04 +03:00
|
|
|
.Dd March 19, 2006
|
1997-10-11 06:56:22 +04:00
|
|
|
.Os
|
|
|
|
.Dt PIDLOCK 3
|
|
|
|
.Sh NAME
|
|
|
|
.Nm pidlock ,
|
|
|
|
.Nm ttylock ,
|
|
|
|
.Nm ttyunlock
|
|
|
|
.Nd locks based on files containing PIDs
|
1998-02-05 21:45:17 +03:00
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libutil
|
1997-10-11 06:56:22 +04:00
|
|
|
.Sh SYNOPSIS
|
2003-04-16 17:34:34 +04:00
|
|
|
.In util.h
|
1997-10-11 06:56:22 +04:00
|
|
|
.Ft int
|
|
|
|
.Fn pidlock "const char *lockfile" "int flags" "pid_t *locker" "const char *info"
|
|
|
|
.Ft int
|
|
|
|
.Fn ttylock "const char *tty" "int flags" "pid_t *locker"
|
|
|
|
.Ft int
|
|
|
|
.Fn ttyunlock "const char *tty"
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Fn pidlock
|
|
|
|
.Fn ttylock ,
|
|
|
|
and
|
|
|
|
.Fn ttyunlock
|
|
|
|
functions attempt to create a lockfile for an arbitrary resource that
|
2002-10-01 23:36:30 +04:00
|
|
|
only one program may hold at a time.
|
|
|
|
(In the case of
|
1997-10-11 06:56:22 +04:00
|
|
|
.Fn ttylock ,
|
2002-10-01 23:36:30 +04:00
|
|
|
this is access to a tty device.)
|
|
|
|
If the
|
1997-10-11 06:56:22 +04:00
|
|
|
function succeeds in creating the lockfile, it will succeed for
|
|
|
|
no other program calling it with the same lockfile until the original
|
2002-10-01 23:36:30 +04:00
|
|
|
calling program has removed the lockfile or exited.
|
|
|
|
The
|
1997-10-11 06:56:22 +04:00
|
|
|
.Fn ttyunlock
|
|
|
|
function will remove the lockfile created by
|
|
|
|
.Fn ttylock .
|
|
|
|
.Pp
|
|
|
|
These functions use the method of creating a lockfile traditionally
|
2002-10-01 23:36:30 +04:00
|
|
|
used by UUCP software.
|
|
|
|
This is described as follows in the documentation for Taylor UUCP:
|
1997-10-11 06:56:22 +04:00
|
|
|
.Bd -filled -offset indent
|
2002-10-01 23:36:30 +04:00
|
|
|
The lock file normally contains the process ID of the locking process.
|
|
|
|
This makes it easy to determine whether a lock is still valid.
|
|
|
|
The algorithm is to create a temporary file and then link
|
|
|
|
it to the name that must be locked.
|
|
|
|
If the link fails because a file with that name already exists,
|
|
|
|
the existing file is read to get the process ID.
|
|
|
|
If the process still exists, the lock attempt fails.
|
|
|
|
Otherwise the lock file is deleted and the locking algorithm
|
1997-10-11 06:56:22 +04:00
|
|
|
is retried.
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
The PID is stored in ASCII format, with leading spaces to pad it
|
2002-10-01 23:36:30 +04:00
|
|
|
out to ten characters, and a terminating newline.
|
|
|
|
This implementation has been extended to put the hostname
|
1997-10-11 06:56:22 +04:00
|
|
|
on the second line of the file, terminated with a newline, and
|
|
|
|
optionally an arbitrary comment on the third line of the file, also
|
2002-10-01 23:36:30 +04:00
|
|
|
terminated with a newline.
|
|
|
|
If a comment is given, but
|
1997-10-11 06:56:22 +04:00
|
|
|
.Dv PIDLOCK_NONBLOCK
|
|
|
|
is not, a blank line will be written as the second line of the file.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn pidlock
|
|
|
|
function will attempt to create the file
|
|
|
|
.Fa lockfile
|
2002-10-01 23:36:30 +04:00
|
|
|
and put the current process's pid in it.
|
|
|
|
The
|
1997-10-11 06:56:22 +04:00
|
|
|
.Fn ttylock
|
|
|
|
function will do the same, but should be passed only the base name
|
2006-03-23 22:30:27 +03:00
|
|
|
(with no leading directory prefix) of the
|
2006-03-20 00:56:04 +03:00
|
|
|
.Fa tty
|
|
|
|
to be locked; it will test that the tty exists in
|
1997-10-11 06:56:22 +04:00
|
|
|
.Pa /dev
|
|
|
|
and is a character device, and then create
|
|
|
|
the file in the
|
|
|
|
.Pa /var/spool/lock
|
|
|
|
directory and prefix the filename with
|
|
|
|
.Pa LCK.. .
|
|
|
|
Use the
|
|
|
|
.Fn ttyunlock
|
|
|
|
function to remove this lock.
|
|
|
|
.Pp
|
|
|
|
The following flags may be passed in
|
|
|
|
.Pa flags :
|
|
|
|
.Bl -tag -width Dv -offset indent
|
|
|
|
.It Dv PIDLOCK_NONBLOCK
|
|
|
|
The function should return immediately when a lock is held by another
|
2002-10-01 23:36:30 +04:00
|
|
|
active process.
|
|
|
|
Otherwise the function will wait (forever, if necessary)
|
1997-10-11 06:56:22 +04:00
|
|
|
for the lock to be freed.
|
|
|
|
.It Dv PIDLOCK_USEHOSTNAME
|
|
|
|
The hostname should be compared against the hostname in the second
|
|
|
|
line of the file (if present), and if they differ, no attempt at
|
|
|
|
checking for a living process holding the lock will be made, and
|
2002-10-01 23:36:30 +04:00
|
|
|
the lockfile will never be deleted.
|
|
|
|
(The process is assumed to be alive.)
|
|
|
|
This is used for locking on NFS or other remote filesystems.
|
1997-10-11 06:56:22 +04:00
|
|
|
(The function will never create a lock if
|
|
|
|
.Dv PIDLOCK_USEHOSTNAME
|
|
|
|
is specified and no hostname is present.)
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
If
|
|
|
|
.Pa locker
|
|
|
|
is non-null, it will contain the PID of the locking process, if there
|
|
|
|
is one, on return.
|
|
|
|
.Pp
|
|
|
|
If
|
|
|
|
.Pa info
|
|
|
|
is non-null and the lock succeeds, the string it points to will be
|
|
|
|
written as the third line of the lock file.
|
|
|
|
.Sh RETURN VALUES
|
|
|
|
Zero is returned if the operation was successful; on an error a -1
|
2006-03-20 00:56:04 +03:00
|
|
|
is returned and a standard error code is left in the global location
|
|
|
|
.Va errno .
|
1997-10-11 06:56:22 +04:00
|
|
|
.Sh ERRORS
|
2006-03-20 00:56:04 +03:00
|
|
|
In addition to the errors that are returned from
|
|
|
|
.Xr stat 2 ,
|
|
|
|
.Xr open 2 ,
|
|
|
|
.Xr read 2 ,
|
|
|
|
.Xr write 2 ,
|
2006-03-23 22:30:27 +03:00
|
|
|
and
|
2006-03-20 00:56:04 +03:00
|
|
|
.Xr link 2 ,
|
1997-10-11 06:56:22 +04:00
|
|
|
.Fn pidlock
|
|
|
|
or
|
|
|
|
.Fn ttylock
|
2006-03-20 00:56:04 +03:00
|
|
|
can set
|
|
|
|
.Va errno
|
|
|
|
to the following values on failure:
|
1997-10-11 06:56:22 +04:00
|
|
|
.Bl -tag -width Er
|
2006-03-20 00:56:04 +03:00
|
|
|
.It Bq Er EWOULDBLOCK
|
2003-03-31 22:49:45 +04:00
|
|
|
Another running process has a lock and the
|
1997-10-11 06:56:22 +04:00
|
|
|
.Dv PIDLOCK_NONBLOCK
|
|
|
|
flag was specified.
|
2006-03-20 00:56:04 +03:00
|
|
|
.It Bq Er EFTYPE
|
2006-03-23 22:30:27 +03:00
|
|
|
The
|
2006-03-20 00:56:04 +03:00
|
|
|
.Fa tty
|
2006-03-23 22:30:27 +03:00
|
|
|
specified in
|
2006-03-20 00:56:04 +03:00
|
|
|
.Fn ttylock
|
|
|
|
is not a character special device.
|
1997-10-11 06:56:22 +04:00
|
|
|
.El
|
|
|
|
.\" .Sh SEE ALSO
|
|
|
|
.Sh HISTORY
|
|
|
|
The
|
|
|
|
.Fn pidlock
|
|
|
|
and
|
|
|
|
.Fn ttylock
|
|
|
|
functions appeared in
|
|
|
|
.Nx 1.3 .
|
|
|
|
.Sh AUTHORS
|
2003-02-14 19:29:11 +03:00
|
|
|
.An Curt Sampson
|
2003-07-26 23:24:24 +04:00
|
|
|
.Aq cjs@NetBSD.org .
|
1997-10-11 06:56:22 +04:00
|
|
|
.Sh BUGS
|
|
|
|
The lockfile format breaks if a pid is longer than ten digits when
|
|
|
|
printed in decimal form.
|
|
|
|
.Pp
|
|
|
|
The PID returned will be the pid of the locker on the remote machine if
|
|
|
|
.Dv PIDLOCK_USEHOSTNAME
|
|
|
|
is specified, but there is no indication that this is not on the local
|
|
|
|
machine.
|