xref: /freebsd/lib/libutil/uucplock.3 (revision 568b59b9df9cbf2d11dda28cefc9fc105d6ff8c6)
1568b59b9SBrian Somers.\"
2568b59b9SBrian Somers.\" Copyright (c) 1996 Brian Somers <brian@awfulhak.demon.co.uk>
3568b59b9SBrian Somers.\"
4568b59b9SBrian Somers.\" All rights reserved.
5568b59b9SBrian Somers.\"
6568b59b9SBrian Somers.\" Redistribution and use in source and binary forms, with or without
7568b59b9SBrian Somers.\" modification, are permitted provided that the following conditions
8568b59b9SBrian Somers.\" are met:
9568b59b9SBrian Somers.\" 1. Redistributions of source code must retain the above copyright
10568b59b9SBrian Somers.\"    notice, this list of conditions and the following disclaimer.
11568b59b9SBrian Somers.\" 2. Redistributions in binary form must reproduce the above copyright
12568b59b9SBrian Somers.\"    notice, this list of conditions and the following disclaimer in the
13568b59b9SBrian Somers.\"    documentation and/or other materials provided with the distribution.
14568b59b9SBrian Somers.\"
15568b59b9SBrian Somers.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16568b59b9SBrian Somers.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17568b59b9SBrian Somers.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18568b59b9SBrian Somers.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19568b59b9SBrian Somers.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20568b59b9SBrian Somers.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21568b59b9SBrian Somers.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22568b59b9SBrian Somers.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23568b59b9SBrian Somers.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24568b59b9SBrian Somers.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25568b59b9SBrian Somers.\"
26568b59b9SBrian Somers.\" $Id$
27568b59b9SBrian Somers.\" "
28568b59b9SBrian Somers.Dd March 30, 1997
29568b59b9SBrian Somers.Os
30568b59b9SBrian Somers.Dt uucplock 3
31568b59b9SBrian Somers.Sh NAME
32568b59b9SBrian Somers.Nm uu_lock ,
33568b59b9SBrian Somers.Nm uu_unlock
34568b59b9SBrian Somers.Nd acquire and release control of a serial device
35568b59b9SBrian Somers.Sh SYNOPSIS
36568b59b9SBrian Somers.Fd #include <libutil.h>
37568b59b9SBrian Somers.Ft int
38568b59b9SBrian Somers.Fn uu_lock "char *ttyname"
39568b59b9SBrian Somers.Ft int
40568b59b9SBrian Somers.Fn uu_unlock "char *ttyname"
41568b59b9SBrian Somers.Pp
42568b59b9SBrian SomersLink with
43568b59b9SBrian Somers.Va -lutil
44568b59b9SBrian Somerson the
45568b59b9SBrian Somers.Xr cc 1
46568b59b9SBrian Somerscommand line.
47568b59b9SBrian Somers.Sh DESCRIPTION
48568b59b9SBrian SomersThe
49568b59b9SBrian Somers.Fn uu_lock
50568b59b9SBrian Somersfunction attempts to create a lock file called
51568b59b9SBrian Somers.Dq /var/spool/lock/LCK..
52568b59b9SBrian Somerswith a suffix given by the passed
53568b59b9SBrian Somers.Fa ttyname .
54568b59b9SBrian SomersIf the file already exists, it is expected to contain the process
55568b59b9SBrian Somersid of the locking program.
56568b59b9SBrian Somers.Pp
57568b59b9SBrian SomersIf the file does not already exist, or the owning process given by
58568b59b9SBrian Somersthe process id found in the lock file is no longer running,
59568b59b9SBrian Somers.Fn uu_lock
60568b59b9SBrian Somerswill write its own process id into the file and return success.
61568b59b9SBrian Somers.Pp
62568b59b9SBrian Somers.Fn uu_unlock
63568b59b9SBrian Somersremoves the lockfile created by
64568b59b9SBrian Somers.Fn uu_lock
65568b59b9SBrian Somersfor the given
66568b59b9SBrian Somers.Fa ttyname .
67568b59b9SBrian SomersCare should be taken that
68568b59b9SBrian Somers.Fn uu_lock
69568b59b9SBrian Somerswas successful before calling
70568b59b9SBrian Somers.Fn uu_unlock .
71568b59b9SBrian Somers.Sh RETURN VALUES
72568b59b9SBrian SomersBoth
73568b59b9SBrian Somers.Fn uu_lock
74568b59b9SBrian Somersand
75568b59b9SBrian Somers.Fn uu_unlock
76568b59b9SBrian Somersreturn 0 on success and -1 on failure.
77568b59b9SBrian Somers.Sh ERRORS
78568b59b9SBrian SomersOn failure,
79568b59b9SBrian Somers.Fn uu_lock
80568b59b9SBrian Somerswill log any unexpected errors using
81568b59b9SBrian Somers.Xr syslog 2 .
82568b59b9SBrian SomersIf the lock already exists and contains the process id of a running
83568b59b9SBrian Somersprocess,
84568b59b9SBrian Somers.Fn uu_lock
85568b59b9SBrian Somerswill silently fail.  The value of
86568b59b9SBrian Somers.Dv errno
87568b59b9SBrian Somerscan not be used to determine the cause of failure.
88568b59b9SBrian Somers.Pp
89568b59b9SBrian Somers.Fn uu_unlock
90568b59b9SBrian Somerswill set the global variable
91568b59b9SBrian Somers.Dv errno
92568b59b9SBrian Somersto reflect the reason that the lock file could not be removed.
93568b59b9SBrian SomersRefer to the description of
94568b59b9SBrian Somers.Xr unlink 2
95568b59b9SBrian Somersfor further details.
96568b59b9SBrian Somers.Sh BUGS
97568b59b9SBrian SomersLocking is not atomic.  Should a race condition occur, it's entirely
98568b59b9SBrian Somerspossible that both processes obtain the lock.
99568b59b9SBrian Somers.Pp
100568b59b9SBrian SomersIt is possible that a stale lock is not recognised as such if a new
101568b59b9SBrian Somersprocesses is assigned the same processes id as the program that left
102568b59b9SBrian Somersthe stale lock.
103568b59b9SBrian Somers.Pp
104568b59b9SBrian SomersThe calling process must have write permissions to the
105568b59b9SBrian Somers.Dq /var/spool/lock
106568b59b9SBrian Somersdirectory.  There is no mechanism in place to ensure that the
107568b59b9SBrian Somerspermissions of this directory are the same as those of the
108568b59b9SBrian Somersserial devices that might be locked.
109