lockf(), lockf64()

lock sections of a file 

Function


SYNOPSIS

#include <unistd.h>

int lockf(int fildes, int cmd, off_t size);

int lockf64(int fildes, int cmd, off64_t size);


DESCRIPTION

The lockf() function allows sections of a file to be locked. Locks may be advisory or mandatory; refer to Porting Issues for more information. Calls to lockf() from other threads which attempt to lock the locked file section either returns with an error value or block until the section becomes unlocked. All locks for a process are removed when the process terminates. Record locking with lockf() is supported only for regular files.

The following lock commands are available:

F_ULOCK 

Unlock a previously locked section of the file.

F_LOCK 

Lock a section for exclusive use. If the section is already locked by another process, wait until it is available, or until lockf() is interrupted by a signal.

F_TLOCK 

Test a section for another process' locks. If the section is already locked, return a failure rather than blocking until the lock becomes available

F_TEST 

Test a section for another process' locks.

The section to be locked or unlocked starts at the current offset in the file and extends forward for a positive size or backward for a negative size (the preceding bytes up to but not including the current offset). If the specified lock size is 0, the section from the current offset through the largest possible file offset is locked. An area need not be allocated to the file to be locked because locks may exist past the end-of-file.

The sections locked with F_LOCK or F_TLOCK may, in whole or in part, contain or be contained by a previously locked section for the same process. When this occurs, or if adjacent locked sections would occur, the sections are combined into a single locked section. If the request would cause the number of locks to exceed a system-imposed limit, the request fails.

File locks are released on the first close by the locking process of any file descriptor for the file.

The F_ULOCK request may release (wholly or in part) one or more locked sections controlled by the process. Locked sections are unlocked starting at the current file offset through the specified size bytes , or to the end of file if the size is 0. When all of a locked section is not released, the remaining portions of that section are still locked by the process. Releasing the center portion of a locked section causes the remaining locked beginning and end portions to become two separate locked sections. If the request would cause the number of locks in the system to exceed a system-imposed limit, the request fails.

A potential for deadlock occurs if the threads of a process controlling a locked section are blocked by accessing another process' locked section. If the system detects that deadlock would occur, lockf() returns -1 with errno set to EDEADLK.

The lockf64() function is identical to the lockf() function except that it is capable of working with files that are larger than 2 gigabytes. The lockf64() function is a part of the large file extensions.


PARAMETERS

fildes 

Is an open file descriptor. It must have O_WRONLY or O_RDWR permission for a successful locking call.

cmd 

Is a lock command, as described in the DESCRIPTION section.

size 

Is the number of contiguous bytes to be locked or unlocked.


RETURN VALUES

If successful, lockf() and lockf64() return a value of zero. On failure, they return a value of -1 and set errno to one of the following values:

EACCES  

The cmd parameter is F_TLOCK or F_TEST and the section is already locked by another process.

EBADF  

The fildes parameter is not a valid open descriptor. Or, the cmd parameter is F_LOCK or F_TLOCK and the fildes argument is not a valid file descriptor open for writing.

EDEADLK 

The cmd parameter is F_LOCK, the lock is blocked by some lock from another process, and putting the calling process to sleep, waiting for that lock to become free would cause a deadlock.

EINTR 

The cmd parameter is F_LOCK and a signal interrupted the process while it was waiting to complete the lock.

EINVAL 

The cmd parameter is not one of F_LOCK, F_TLOCK, F_TEST, or F_ULOCK.

The size parameter plus the current file offset is less than 0.

The fildes parameter refers to a file that does not support locking.

ENOLCK 

The cmd parameter is F_LOCK, F_TLOCK, or F_ULOCK and the number of locked regions available in the system would be exceeded by the request.

EOVERFLOW 

The offset of the first, or if size is not 0 then the last byte in the requested section cannot be represented correctly in an object of type off_t.


MULTITHREAD SAFETY LEVEL

MT-Safe.


PORTING ISSUES

lockf() is implemented as a wrapper around fcntl(), using the F_GETLK, F_SETLK, and F_SETLKW commands.

Standard locks apply only to the local system. Locks are, by default, advisory. The NuTCRACKER Platform also supports a form of mandatory locking on 2012/8.1/2012R2/10/2016/2019. If the S_ISGID access mode bit is set for a file, and S_IXGRP is not set, then the NuTCRACKER Platform enables mandatory locking for the file using Win32 APIs. Mandatory locks are not inherited across exec(). Refer to chmod() and struct stat for more information on setting mode bits for a file.

The NuTCRACKER Platform also supports an advisory locking mode that uses the native Win32 locking APIs (LockFileEx() on 2012/8.1/2012R2/10/2016/2019). This mode can be set with the _NC_SET_NT_LIMITED_LOCKING parameter to _NutConf(), or via the NUT_NT_LIMITED_LOCKING environment variable. This mode supports network locking, but does not have normal UNIX semantics (for example, locks cannot be read back, lock operations cannot be interrupted, and locks are not inherited across exec()). Refer to _NutConf(), and to Environment Variables in the PTC MKS Toolkit UNIX to Windows Porting Guide for more information.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
_NutConf(), _NutForkExecl(), _NutForkExecle(), _NutForkExeclp(), _NutForkExeclpe(), _NutForkExecv(), _NutForkExecve(), _NutForkExecvp(), _NutForkExecvpe(), chmod(), execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), fcntl(), fork()

Miscellaneous:
lf64, struct stat


PTC MKS Toolkit 10.3 Documentation Build 39.