---
name: "uu_lockerr"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/uu_lock"
---

# uu\_lockerr(3)

acquire and release control of a serial device

Part of [uu\_lock(3)](/uu_lock/3.md).

## NAME

**uu\_lock**, **uu\_lock\_txfr**, **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 calling **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)](/open/2.md).

UU\_LOCK\_READ\_ERR: The lock file could not be read via [read(2)](/read/2.md).

UU\_LOCK\_CREAT\_ERR: Cannot create temporary lock file via [creat(2)](/creat/2.md).

UU\_LOCK\_WRITE\_ERR: The current process id could not be written to the lock file via a call to [write(2)](/write/2.md).

UU\_LOCK\_LINK\_ERR: Cannot link temporary lock file via [link(2)](/link/2.md).

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)](/write/2.md).

## 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 manual 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)](/unlink/2.md) for further details.

## SEE ALSO

[lseek(2)](/lseek/2.md), [open(2)](/open/2.md), [read(2)](/read/2.md), [write(2)](/write/2.md)

## HISTORY

The functions **uu\_lock**(), **uu\_unlock**() and **uu\_lockerr**() first appeared in FreeBSD 2.0.5.

## 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 permissions of this directory are the same as those of the serial devices that might be locked.
