NAME

zkFcntl - file control

SYNOPSIS

#include "zkFcntl.h"

int zkFcntl(int fildes, int cmd, ...);

DESCRIPTION

The zkFcntl() function provides for control over open files. The fildes argument is a file descriptor.

The available values for cmd are defined in the header "zkFcntl.h", which include:

F_GETFL
Get the file status flags and file access modes, defined in "zkFcntl.h", for the file description associated with fildes. The file access modes can be extracted from the return value using the mask O_ACCMODE, which is defined in "zkFcntl.h". File status flags and file access modes are associated with the file descriptor and do not affect other file descriptors that refer to the same file with different open file descriptions.

The following values for cmd are available for advisory record locking. Record locking is supported for regular files, and may be supported for other files.

F_SETLK
Set or clear a file segment lock according to the lock description pointed to by the third argument, arg, taken as a pointer to type struct flock, defined in "zkFcntl.h". F_SETLK is used to establish exclusive (or write) locks (F_WRLCK), as well as to remove locks (F_UNLCK). F_WRLCK and F_UNLCK are defined in "zkFcntl.h". If an exclusive lock cannot be set, zkFcntl() will return immediately with a return value of -1.

Additional implementation-dependent values for cmd may be defined in "zkFcntl.h". Their names will start with F_.

An exclusive lock will prevent any other process from setting a shared lock or an exclusive lock on any portion of the protected area. A request for an exclusive lock will fail if the file descriptor was not opened with write access.

The structure flock describes the type (l_type), starting offset (l_whence), relative offset (l_start), and size (l_len) of the segment of the file to be affected.

The value of l_whence is SEEK_SET, SEEK_CUR or SEEK_END, to indicate that the relative offset l_start bytes will be measured from the start of the file, current position or end of the file, respectively. The value of l_len is the number of consecutive bytes to be locked. The value of l_len may be negative (where the definition of off_t permits negative values of l_len).

If l_len is positive, the area affected starts at l_start and ends at l_start+l_len-1. If l_len is negative, the area affected starts at l_start+l_len and ends at l_start-1. Locks may start and extend beyond the current end of a file, but must not be negative relative to the beginning of the file. A lock will be set to extend to the largest possible value of the file offset for that file by setting l_len to 0. If such a lock also has l_start set to 0 and l_whence is set to SEEK_SET, the whole file will be locked.

The behaviour when an F_SETLK request overlaps previously locked segments is implementation-defined. To maintain portability, applications should only lock mutually disjoint (i.e., non-overlapping) file segments.

All locks associated with a file for a given process are removed when a file descriptor for that file is closed by that process or the process holding that file descriptor terminates. Locks are not inherited by a child process created using fork().

A potential for deadlock occurs if a process controlling a locked region is put to sleep by attempting to lock another process' locked region. If the system detects that sleeping until a locked region is unlocked would cause a deadlock, zkFcntl() will fail with an [EDEADLK] error.

An unlock (F_UNLCK) request in which l_len is non-zero and the offset of the last byte of the requested segment is the maximum value for an object of type off_t, when the process has an existing lock in which l_len is 0 and which includes the last byte of the requested segment, will be treated as a request to unlock from the start of the requested segment with an l_len equal to 0. Otherwise an unlock (F_UNLCK) request will attempt to unlock only the requested segment.

RETURN VALUE

Upon successful completion, the value returned depends on cmd as follows:

F_GETFL
Value of file status flags and access modes. The return value will not be negative.
F_SETLK
Value other than -1.

Otherwise, -1 is returned and errno is set to indicate the error.

ERRORS

The zkFcntl() function will fail if:

[EACCES] or [EAGAIN]
The cmd argument is F_SETLK; the type of lock (l_type) is a shared (F_RDLCK) or exclusive (F_WRLCK) lock and the segment of a file to be locked is already exclusive-locked by another process, or the type is an exclusive lock and some portion of the segment of a file to be locked is already shared-locked or exclusive-locked by another process.
[EBADF]
The fildes argument is not a valid open file descriptor, or the argument cmd is F_SETLK or F_SETLKW, the type of lock, l_type, is a shared lock (F_RDLCK), and fildes is not a valid file descriptor open for reading, or the type of lock l_type, is an exclusive lock (F_WRLCK), and fildes is not a valid file descriptor open for writing.
[EINTR]
The cmd argument is F_SETLKW and the function was interrupted by a signal.
[EINVAL]
The cmd argument is invalid, or the cmd argument is F_DUPFD and arg is negative or greater than or equal to {OPEN_MAX}, or the cmd argument is F_GETLK, F_SETLK or F_SETLKW and the data pointed to by arg is not valid, or fildes refers to a file that does not support locking.
[EMFILE]
The argument cmd is F_DUPFD and {OPEN_MAX} file descriptors are currently open in the calling process, or no file descriptors greater than or equal to arg are available.
[ENOLCK]
The argument cmd is F_SETLK or F_SETLKW and satisfying the lock or unlock request would result in the number of locked regions in the system exceeding a system-imposed limit.
[EOVERFLOW]
One of the values to be returned cannot be represented correctly.
[EOVERFLOW]
The cmd argument is F_GETLK, F_SETLK or F_SETLKW and the smallest or, if l_len is non-zero, the largest offset of any byte in the requested segment cannot be represented correctly in an object of type off_t.

The zkFcntl() function may fail if:

[EDEADLK]
The cmd argument is F_SETLKW, 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.

PORTABILITY

The zkFcntl() function may portably be used only with file descriptors representing regular files. Any attempt to use it on a socket, directory, FIFO, STREAMS file, terminal device file, character special file, or block special file is non-portable.

Depending upon the platform, additional cmd arguments may be passed to the function. These extra commands include:

F_DUPFD
F_GETFD
F_SETFD
F_SETFL
F_GETLK
F_SETLKW

However, these commands aren't supported on all target platforms.

Depending upon the platform, the record locking commands (F_GETLK, F_SETLK, and F_SETLKW) may allow for shared (or read) locks in addition to exclusive (or write) locks. However, shared locks aren't supported on all target platforms.