xref: /freebsd/lib/libutil/uucplock.3 (revision bc89b58def9c028d29a5faf3a03eee2b8b5337ae)
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.\"
267f3dea24SPeter Wemm.\" $FreeBSD$
27568b59b9SBrian Somers.\" "
28*bc89b58dSGordon Bergling.Dd May 10, 2020
2974804d58SMike Pritchard.Dt UUCPLOCK 3
30aa12cea2SUlrich Spörlein.Os
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
363dc329d1SAlexey Zelkin.Sh LIBRARY
373dc329d1SAlexey Zelkin.Lb libutil
38568b59b9SBrian Somers.Sh SYNOPSIS
3932eef9aeSRuslan Ermilov.In sys/types.h
4032eef9aeSRuslan Ermilov.In libutil.h
41568b59b9SBrian Somers.Ft int
4285b0d75dSBrian Somers.Fn uu_lock "const char *ttyname"
43568b59b9SBrian Somers.Ft int
4446cf264aSBrian Somers.Fn uu_lock_txfr "const char *ttyname" "pid_t pid"
4546cf264aSBrian Somers.Ft int
4685b0d75dSBrian Somers.Fn uu_unlock "const char *ttyname"
4785b0d75dSBrian Somers.Ft const char *
48687d0cdeSBrian Somers.Fn uu_lockerr "int uu_lockresult"
49568b59b9SBrian Somers.Sh DESCRIPTION
50568b59b9SBrian SomersThe
51568b59b9SBrian Somers.Fn uu_lock
52568b59b9SBrian Somersfunction attempts to create a lock file called
53b5ebf1f5SMike Pritchard.Pa /var/spool/lock/LCK..
54568b59b9SBrian Somerswith a suffix given by the passed
55568b59b9SBrian Somers.Fa ttyname .
56568b59b9SBrian SomersIf the file already exists, it is expected to contain the process
57568b59b9SBrian Somersid of the locking program.
58568b59b9SBrian Somers.Pp
59568b59b9SBrian SomersIf the file does not already exist, or the owning process given by
60568b59b9SBrian Somersthe process id found in the lock file is no longer running,
61568b59b9SBrian Somers.Fn uu_lock
62568b59b9SBrian Somerswill write its own process id into the file and return success.
63568b59b9SBrian Somers.Pp
6446cf264aSBrian Somers.Fn uu_lock_txfr
6546cf264aSBrian Somerstransfers lock ownership to another process.
6646cf264aSBrian Somers.Fn uu_lock
6746cf264aSBrian Somersmust have previously been successful.
6846cf264aSBrian Somers.Pp
69568b59b9SBrian Somers.Fn uu_unlock
70568b59b9SBrian Somersremoves the lockfile created by
71568b59b9SBrian Somers.Fn uu_lock
72568b59b9SBrian Somersfor the given
73568b59b9SBrian Somers.Fa ttyname .
74568b59b9SBrian SomersCare should be taken that
75568b59b9SBrian Somers.Fn uu_lock
76568b59b9SBrian Somerswas successful before calling
77568b59b9SBrian Somers.Fn uu_unlock .
78687d0cdeSBrian Somers.Pp
79687d0cdeSBrian Somers.Fn uu_lockerr
80687d0cdeSBrian Somersreturns an error string representing the error
81687d0cdeSBrian Somers.Fa uu_lockresult ,
82687d0cdeSBrian Somersas returned from
83687d0cdeSBrian Somers.Fn uu_lock .
84568b59b9SBrian Somers.Sh RETURN VALUES
85687d0cdeSBrian Somers.Fn uu_unlock
86687d0cdeSBrian Somersreturns 0 on success and -1 on failure.
87687d0cdeSBrian Somers.Pp
88687d0cdeSBrian Somers.Fn uu_lock
89687d0cdeSBrian Somersmay return any of the following values:
90687d0cdeSBrian Somers.Pp
91687d0cdeSBrian Somers.Dv UU_LOCK_INUSE :
92687d0cdeSBrian SomersThe lock is in use by another process.
93687d0cdeSBrian Somers.Pp
94687d0cdeSBrian Somers.Dv UU_LOCK_OK :
95687d0cdeSBrian SomersThe lock was successfully created.
96687d0cdeSBrian Somers.Pp
97687d0cdeSBrian Somers.Dv UU_LOCK_OPEN_ERR :
98687d0cdeSBrian SomersThe lock file could not be opened via
99687d0cdeSBrian Somers.Xr open 2 .
100687d0cdeSBrian Somers.Pp
101687d0cdeSBrian Somers.Dv UU_LOCK_READ_ERR :
102687d0cdeSBrian SomersThe lock file could not be read via
103687d0cdeSBrian Somers.Xr read 2 .
104687d0cdeSBrian Somers.Pp
10584dc2299SAndrey A. Chernov.Dv UU_LOCK_CREAT_ERR :
1060227791bSRuslan ErmilovCannot create temporary lock file via
10784dc2299SAndrey A. Chernov.Xr creat 2 .
108687d0cdeSBrian Somers.Pp
109687d0cdeSBrian Somers.Dv UU_LOCK_WRITE_ERR :
110687d0cdeSBrian SomersThe current process id could not be written to the lock file via a call to
111687d0cdeSBrian Somers.Xr write 2 .
112687d0cdeSBrian Somers.Pp
11384dc2299SAndrey A. Chernov.Dv UU_LOCK_LINK_ERR :
1140227791bSRuslan ErmilovCannot link temporary lock file via
11584dc2299SAndrey A. Chernov.Xr link 2 .
11684dc2299SAndrey A. Chernov.Pp
11784dc2299SAndrey A. Chernov.Dv UU_LOCK_TRY_ERR :
11884dc2299SAndrey A. ChernovLocking attempts are failed after 5 tries.
11984dc2299SAndrey A. Chernov.Pp
120687d0cdeSBrian SomersIf a value of
121687d0cdeSBrian Somers.Dv UU_LOCK_OK
122687d0cdeSBrian Somersis passed to
123687d0cdeSBrian Somers.Fn uu_lockerr ,
124134970f6SMike Pritchardan empty string is returned.
12528754192SAndrey A. ChernovOtherwise, a string specifying
126687d0cdeSBrian Somersthe reason for failure is returned.
127687d0cdeSBrian Somers.Fn uu_lockerr
128687d0cdeSBrian Somersuses the current value of
129b5ebf1f5SMike Pritchard.Va errno
1301a0a9345SRuslan Ermilovto determine the exact error.
1311a0a9345SRuslan ErmilovCare should be made not to allow
132b5ebf1f5SMike Pritchard.Va errno
133687d0cdeSBrian Somersto be changed between calls to
134568b59b9SBrian Somers.Fn uu_lock
135568b59b9SBrian Somersand
136687d0cdeSBrian Somers.Fn uu_lockerr .
13746cf264aSBrian Somers.Pp
13846cf264aSBrian Somers.Fn uu_lock_txfr
13946cf264aSBrian Somersmay return any of the following values:
14046cf264aSBrian Somers.Pp
14146cf264aSBrian Somers.Dv UU_LOCK_OK :
1421a0a9345SRuslan ErmilovThe transfer was successful.
1431a0a9345SRuslan ErmilovThe specified process now holds the device
14446cf264aSBrian Somerslock.
14546cf264aSBrian Somers.Pp
14646cf264aSBrian Somers.Dv UU_LOCK_OWNER_ERR :
14746cf264aSBrian SomersThe current process does not already own a lock on the specified device.
14846cf264aSBrian Somers.Pp
14946cf264aSBrian Somers.Dv UU_LOCK_WRITE_ERR :
15046cf264aSBrian SomersThe new process id could not be written to the lock file via a call to
15146cf264aSBrian Somers.Xr write 2 .
152568b59b9SBrian Somers.Sh ERRORS
153687d0cdeSBrian SomersIf
154568b59b9SBrian Somers.Fn uu_lock
15546cf264aSBrian Somersreturns one of the error values above, the global value
156b5ebf1f5SMike Pritchard.Va errno
1571a0a9345SRuslan Ermilovcan be used to determine the cause.
1581a0a9345SRuslan ErmilovRefer to the respective manual pages
159687d0cdeSBrian Somersfor further details.
160568b59b9SBrian Somers.Pp
161568b59b9SBrian Somers.Fn uu_unlock
162568b59b9SBrian Somerswill set the global variable
163b5ebf1f5SMike Pritchard.Va errno
164568b59b9SBrian Somersto reflect the reason that the lock file could not be removed.
165568b59b9SBrian SomersRefer to the description of
166568b59b9SBrian Somers.Xr unlink 2
167568b59b9SBrian Somersfor further details.
168687d0cdeSBrian Somers.Sh SEE ALSO
169bf5cbf35SWolfram Schneider.Xr lseek 2 ,
170687d0cdeSBrian Somers.Xr open 2 ,
171687d0cdeSBrian Somers.Xr read 2 ,
172687d0cdeSBrian Somers.Xr write 2
173*bc89b58dSGordon Bergling.Sh HISTORY
174*bc89b58dSGordon BerglingThe functions
175*bc89b58dSGordon Bergling.Fn uu_lock ,
176*bc89b58dSGordon Bergling.Fn uu_unlock
177*bc89b58dSGordon Berglingand
178*bc89b58dSGordon Bergling.Fn uu_lockerr
179*bc89b58dSGordon Berglingfirst appeared in
180*bc89b58dSGordon Bergling.Fx 2.0.5 .
181568b59b9SBrian Somers.Sh BUGS
182568b59b9SBrian SomersIt is possible that a stale lock is not recognised as such if a new
183568b59b9SBrian Somersprocesses is assigned the same processes id as the program that left
184568b59b9SBrian Somersthe stale lock.
185568b59b9SBrian Somers.Pp
186568b59b9SBrian SomersThe calling process must have write permissions to the
187b5ebf1f5SMike Pritchard.Pa /var/spool/lock
1881a0a9345SRuslan Ermilovdirectory.
1891a0a9345SRuslan ErmilovThere is no mechanism in place to ensure that the
190568b59b9SBrian Somerspermissions of this directory are the same as those of the
191568b59b9SBrian Somersserial devices that might be locked.
192