xref: /freebsd/lib/libutil/uucplock.3 (revision 84dc22996da39f0d729487f9223011ec189d020f)
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.\"
2684dc2299SAndrey A. Chernov.\" $Id: uucplock.3,v 1.7 1997/05/12 10:36:14 brian Exp $
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 ,
33b5ebf1f5SMike Pritchard.Nm uu_unlock ,
34b5ebf1f5SMike Pritchard.Nm uu_lockerr
35568b59b9SBrian Somers.Nd acquire and release control of a serial device
36568b59b9SBrian Somers.Sh SYNOPSIS
37210793f7SDavid Nugent.Fd #include <sys/types.h>
38568b59b9SBrian Somers.Fd #include <libutil.h>
39568b59b9SBrian Somers.Ft int
4085b0d75dSBrian Somers.Fn uu_lock "const char *ttyname"
41568b59b9SBrian Somers.Ft int
4285b0d75dSBrian Somers.Fn uu_unlock "const char *ttyname"
4385b0d75dSBrian Somers.Ft const char *
44687d0cdeSBrian Somers.Fn uu_lockerr "int uu_lockresult"
45568b59b9SBrian Somers.Pp
46568b59b9SBrian SomersLink with
47568b59b9SBrian Somers.Va -lutil
48568b59b9SBrian Somerson the
49568b59b9SBrian Somers.Xr cc 1
50568b59b9SBrian Somerscommand line.
51568b59b9SBrian Somers.Sh DESCRIPTION
52568b59b9SBrian SomersThe
53568b59b9SBrian Somers.Fn uu_lock
54568b59b9SBrian Somersfunction attempts to create a lock file called
55b5ebf1f5SMike Pritchard.Pa /var/spool/lock/LCK..
56568b59b9SBrian Somerswith a suffix given by the passed
57568b59b9SBrian Somers.Fa ttyname .
58568b59b9SBrian SomersIf the file already exists, it is expected to contain the process
59568b59b9SBrian Somersid of the locking program.
60568b59b9SBrian Somers.Pp
61568b59b9SBrian SomersIf the file does not already exist, or the owning process given by
62568b59b9SBrian Somersthe process id found in the lock file is no longer running,
63568b59b9SBrian Somers.Fn uu_lock
64568b59b9SBrian Somerswill write its own process id into the file and return success.
65568b59b9SBrian Somers.Pp
66568b59b9SBrian Somers.Fn uu_unlock
67568b59b9SBrian Somersremoves the lockfile created by
68568b59b9SBrian Somers.Fn uu_lock
69568b59b9SBrian Somersfor the given
70568b59b9SBrian Somers.Fa ttyname .
71568b59b9SBrian SomersCare should be taken that
72568b59b9SBrian Somers.Fn uu_lock
73568b59b9SBrian Somerswas successful before calling
74568b59b9SBrian Somers.Fn uu_unlock .
75687d0cdeSBrian Somers.Pp
76687d0cdeSBrian Somers.Fn uu_lockerr
77687d0cdeSBrian Somersreturns an error string representing the error
78687d0cdeSBrian Somers.Fa uu_lockresult ,
79687d0cdeSBrian Somersas returned from
80687d0cdeSBrian Somers.Fn uu_lock .
81568b59b9SBrian Somers.Sh RETURN VALUES
82687d0cdeSBrian Somers.Fn uu_unlock
83687d0cdeSBrian Somersreturns 0 on success and -1 on failure.
84687d0cdeSBrian Somers.Pp
85687d0cdeSBrian Somers.Fn uu_lock
86687d0cdeSBrian Somersmay return any of the following values:
87687d0cdeSBrian Somers.Pp
88687d0cdeSBrian Somers.Dv UU_LOCK_INUSE:
89687d0cdeSBrian SomersThe lock is in use by another process.
90687d0cdeSBrian Somers.Pp
91687d0cdeSBrian Somers.Dv UU_LOCK_OK:
92687d0cdeSBrian SomersThe lock was successfully created.
93687d0cdeSBrian Somers.Pp
94687d0cdeSBrian Somers.Dv UU_LOCK_OPEN_ERR:
95687d0cdeSBrian SomersThe lock file could not be opened via
96687d0cdeSBrian Somers.Xr open 2 .
97687d0cdeSBrian Somers.Pp
98687d0cdeSBrian Somers.Dv UU_LOCK_READ_ERR:
99687d0cdeSBrian SomersThe lock file could not be read via
100687d0cdeSBrian Somers.Xr read 2 .
101687d0cdeSBrian Somers.Pp
10284dc2299SAndrey A. Chernov.Dv UU_LOCK_CREAT_ERR:
10384dc2299SAndrey A. ChernovCan't create temporary lock file via
10484dc2299SAndrey A. Chernov.Xr creat 2 .
105687d0cdeSBrian Somers.Pp
106687d0cdeSBrian Somers.Dv UU_LOCK_WRITE_ERR:
107687d0cdeSBrian SomersThe current process id could not be written to the lock file via a call to
108687d0cdeSBrian Somers.Xr write 2 .
109687d0cdeSBrian Somers.Pp
11084dc2299SAndrey A. Chernov.Dv UU_LOCK_LINK_ERR:
11184dc2299SAndrey A. ChernovCan't link temporary lock file via
11284dc2299SAndrey A. Chernov.Xr link 2 .
11384dc2299SAndrey A. Chernov.Pp
11484dc2299SAndrey A. Chernov.Dv UU_LOCK_TRY_ERR:
11584dc2299SAndrey A. ChernovLocking attempts are failed after 5 tries.
11684dc2299SAndrey A. Chernov.Pp
117687d0cdeSBrian SomersIf a value of
118687d0cdeSBrian Somers.Dv UU_LOCK_OK
119687d0cdeSBrian Somersis passed to
120687d0cdeSBrian Somers.Fn uu_lockerr ,
121134970f6SMike Pritchardan empty string is returned.
12228754192SAndrey A. ChernovOtherwise, a string specifying
123687d0cdeSBrian Somersthe reason for failure is returned.
124687d0cdeSBrian Somers.Fn uu_lockerr
125687d0cdeSBrian Somersuses the current value of
126b5ebf1f5SMike Pritchard.Va errno
127687d0cdeSBrian Somersto determine the exact error.  Care should be made not to allow
128b5ebf1f5SMike Pritchard.Va errno
129687d0cdeSBrian Somersto be changed between calls to
130568b59b9SBrian Somers.Fn uu_lock
131568b59b9SBrian Somersand
132687d0cdeSBrian Somers.Fn uu_lockerr .
133568b59b9SBrian Somers.Sh ERRORS
134687d0cdeSBrian SomersIf
135568b59b9SBrian Somers.Fn uu_lock
136687d0cdeSBrian Somersreturns one of the four error values above, the global value
137b5ebf1f5SMike Pritchard.Va errno
138687d0cdeSBrian Somerscan be used to determine the cause.  Refer to the respective manual pages
139687d0cdeSBrian Somersfor further details.
140568b59b9SBrian Somers.Pp
141568b59b9SBrian Somers.Fn uu_unlock
142568b59b9SBrian Somerswill set the global variable
143b5ebf1f5SMike Pritchard.Va errno
144568b59b9SBrian Somersto reflect the reason that the lock file could not be removed.
145568b59b9SBrian SomersRefer to the description of
146568b59b9SBrian Somers.Xr unlink 2
147568b59b9SBrian Somersfor further details.
148687d0cdeSBrian Somers.Sh SEE ALSO
149687d0cdeSBrian Somers.Xr open 2 ,
150687d0cdeSBrian Somers.Xr read 2 ,
151687d0cdeSBrian Somers.Xr lseek 2 ,
152687d0cdeSBrian Somers.Xr write 2
153568b59b9SBrian Somers.Sh BUGS
154687d0cdeSBrian SomersLocking is not atomic.  Should a race condition occur, it's
155687d0cdeSBrian Somerspossible that the
156687d0cdeSBrian Somers.Dq losing
157687d0cdeSBrian Somersprocess fails to identify the
158687d0cdeSBrian Somers.Dq winning
159687d0cdeSBrian Somersprocess.  If this happens,
160687d0cdeSBrian Somers.Fn uu_lock
161687d0cdeSBrian Somersreturns
162687d0cdeSBrian Somers.Dv UU_LOCK_READ_ERR
163687d0cdeSBrian Somersand errno is set to
164b5ebf1f5SMike Pritchard.Er EINVAL .
165568b59b9SBrian Somers.Pp
166568b59b9SBrian SomersIt is possible that a stale lock is not recognised as such if a new
167568b59b9SBrian Somersprocesses is assigned the same processes id as the program that left
168568b59b9SBrian Somersthe stale lock.
169568b59b9SBrian Somers.Pp
170568b59b9SBrian SomersThe calling process must have write permissions to the
171b5ebf1f5SMike Pritchard.Pa /var/spool/lock
172568b59b9SBrian Somersdirectory.  There is no mechanism in place to ensure that the
173568b59b9SBrian Somerspermissions of this directory are the same as those of the
174568b59b9SBrian Somersserial devices that might be locked.
175