UUCPLOCK(3) BSD Library Functions Manual UUCPLOCK(3)NAME
uu_lock, uu_unlock, uu_lockerr -- acquire and release control of a serial device
LIBRARY
System Utilities Library (libutil, -lutil)
SYNOPSIS
#include <sys/types.h>
#include <libutil.h>
int
uu_lock(const char *ttyname);
int
uu_lock_txfr(const char *ttyname, pid_t pid);
int
uu_unlock(const char *ttyname);
const char *
uu_lockerr(int uu_lockresult);
DESCRIPTION
The uu_lock() function attempts to create a lock file called /var/spool/lock/LCK.. with a suffix given by the passed ttyname. If the file
already exists, it is expected to contain the process id of the locking program.
If the file does not already exist, or the owning process given by the process id found in the lock file is no longer running, uu_lock() will
write its own process id into the file and return success.
uu_lock_txfr() transfers lock ownership to another process. uu_lock() must have previously been successful.
uu_unlock() removes the lockfile created by uu_lock() for the given ttyname. Care should be taken that uu_lock() was successful before call-
ing uu_unlock().
uu_lockerr() returns an error string representing the error uu_lockresult, as returned from uu_lock().
RETURN VALUES
uu_unlock() returns 0 on success and -1 on failure.
uu_lock() may return any of the following values:
UU_LOCK_INUSE: The lock is in use by another process.
UU_LOCK_OK: The lock was successfully created.
UU_LOCK_OPEN_ERR: The lock file could not be opened via open(2).
UU_LOCK_READ_ERR: The lock file could not be read via read(2).
UU_LOCK_CREAT_ERR: Cannot create temporary lock file via creat(2).
UU_LOCK_WRITE_ERR: The current process id could not be written to the lock file via a call to write(2).
UU_LOCK_LINK_ERR: Cannot link temporary lock file via link(2).
UU_LOCK_TRY_ERR: Locking attempts are failed after 5 tries.
If a value of UU_LOCK_OK is passed to uu_lockerr(), an empty string is returned. Otherwise, a string specifying the reason for failure is
returned. uu_lockerr() uses the current value of errno to determine the exact error. Care should be made not to allow errno to be changed
between calls to uu_lock() and uu_lockerr().
uu_lock_txfr() may return any of the following values:
UU_LOCK_OK: The transfer was successful. The specified process now holds the device lock.
UU_LOCK_OWNER_ERR: The current process does not already own a lock on the specified device.
UU_LOCK_WRITE_ERR: The new process id could not be written to the lock file via a call to write(2).
ERRORS
If uu_lock() returns one of the error values above, the global value errno can be used to determine the cause. Refer to the respective man-
ual pages for further details.
uu_unlock() will set the global variable errno to reflect the reason that the lock file could not be removed. Refer to the description of
unlink(2) for further details.
SEE ALSO lseek(2), open(2), read(2), write(2)BUGS
It is possible that a stale lock is not recognised as such if a new processes is assigned the same processes id as the program that left the
stale lock.
The calling process must have write permissions to the /var/spool/lock directory. There is no mechanism in place to ensure that the permis-
sions of this directory are the same as those of the serial devices that might be locked.
BSD March 30, 1997 BSD
Check Out this Related Man Page
PIDLOCK(3) BSD Library Functions Manual PIDLOCK(3)NAME
pidlock, ttylock, ttyunlock -- locks based on files containing PIDs
LIBRARY
System Utilities Library (libutil, -lutil)
SYNOPSIS
#include <util.h>
int
pidlock(const char *lockfile, int flags, pid_t *locker, const char *info);
int
ttylock(const char *tty, int flags, pid_t *locker);
int
ttyunlock(const char *tty);
DESCRIPTION
The pidlock() ttylock(), and ttyunlock() functions attempt to create a lockfile for an arbitrary resource that only one program may hold at a
time. (In the case of ttylock(), this is access to a tty device.) If the function succeeds in creating the lockfile, it will succeed for no
other program calling it with the same lockfile until the original calling program has removed the lockfile or exited. The ttyunlock() func-
tion will remove the lockfile created by ttylock().
These functions use the method of creating a lockfile traditionally used by UUCP software. This is described as follows in the documentation
for Taylor UUCP:
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. Oth-
erwise the lock file is deleted and the locking algorithm is retried.
The PID is stored in ASCII format, with leading spaces to pad it out to ten characters, and a terminating newline. This implementation has
been extended to put the hostname on the second line of the file, terminated with a newline, and optionally an arbitrary comment on the third
line of the file, also terminated with a newline. If a comment is given, but PIDLOCK_NONBLOCK is not, a blank line will be written as the
second line of the file.
The pidlock() function will attempt to create the file lockfile and put the current process's pid in it. The ttylock() function will do the
same, but should be passed only the base name (with no leading directory prefix) of the tty to be locked; it will test that the tty exists in
/dev and is a character device, and then create the file in the /var/spool/lock directory and prefix the filename with LCK... Use the
ttyunlock() function to remove this lock.
The following flags may be passed in flags:
PIDLOCK_NONBLOCK
The function should return immediately when a lock is held by another active process. Otherwise the function will wait
(forever, if necessary) for the lock to be freed.
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 the lockfile will never be deleted. (The
process is assumed to be alive.) This is used for locking on NFS or other remote filesystems. (The function will never
create a lock if PIDLOCK_USEHOSTNAME is specified and no hostname is present.)
If locker is non-null, it will contain the PID of the locking process, if there is one, on return.
If info is non-null and the lock succeeds, the string it points to will be written as the third line of the lock file.
RETURN VALUES
Zero is returned if the operation was successful; on an error a -1 is returned and a standard error code is left in the global location
errno.
ERRORS
In addition to the errors that are returned from stat(2), open(2), read(2), write(2), and link(2), pidlock() or ttylock() can set errno to
the following values on failure:
[EWOULDBLOCK] Another running process has a lock and the PIDLOCK_NONBLOCK flag was specified.
[EFTYPE] The tty specified in ttylock() is not a character special device.
HISTORY
The pidlock() and ttylock() functions appeared in NetBSD 1.3.
AUTHORS
Curt Sampson <cjs@NetBSD.org>.
BUGS
The lockfile format breaks if a pid is longer than ten digits when printed in decimal form.
The PID returned will be the pid of the locker on the remote machine if PIDLOCK_USEHOSTNAME is specified, but there is no indication that
this is not on the local machine.
BSD March 19, 2006 BSD