Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

close(2)

exec(2)

lockf(2)

open(2)

fcntl(5)

FCNTL(2)  —  HP-UX

NAME

fcntl − file control

SYNOPSIS

#include <fcntl.h>

int fcntl (fildes, cmd, arg)
int fildes, cmd;

union {
int val;
struct flock *lockdes;
} arg;

DESCRIPTION

Fcntl provides for control over open files.  Fildes is an open file descriptor obtained from a creat, open, dup, fcntl, or pipe system call. 

The cmds available are:

F_DUPFD Return a new file descriptor that has the following characteristics:

Lowest numbered available file descriptor greater than or equal to arg.val.

Same open file (or pipe) as the original file. 

Same file pointer as the original file (i.e., both file descriptors share one file pointer). 

Same access mode (read, write or read/write). 

Same file status flags (i.e., both file descriptors share the same file status flags). 

The close-on-exec flag associated with the new file descriptor is set to remain open across exec(2) system calls.

F_GETFD Get the close-on-exec flag associated with the file descriptor fildes. If the low-order bit is 0 the file will remain open across exec(2), otherwise the file will be closed upon execution of exec(2).

F_SETFD Set the close-on-exec flag associated with fildes to the low-order bit of arg.val (see F_GETFD). 

F_GETFL Get file status flags; see fcntl(5).

F_SETFL Set file status flags to arg.val. Only certain flags can be set; see fcntl(5).

F_GETLK Get the first lock which blocks the lock description given by the variable of type struct flock pointed to by arg.  The information retrieved overwrites the information passed to fcntl in the flock structure.  If no lock is found that would prevent this lock from being created, the structure is passed back unchanged except for the lock type which will be set to F_UNLCK. 

F_SETLK Set or clear a file segment lock according to the variable of type struct flock pointed to by arg.lockdes (see fcntl(5)).  The cmd F_SETLK is used to establish read (F_RDLCK) and write (F_WRLCK) locks, as well as remove either type of lock (F_UNLCK).  If a read or write lock cannot be set, fcntl will return immediately with an error value of −1. 

F_SETLKW This cmd is the same as F_SETLK except that if a read or write lock is blocked by other locks, the process will sleep until the segment is free to be locked. 

A read lock prevents any process from write-locking the protected area.  More than one read lock can exist for a given segment of a file at a given time.  The file descriptor on which a read lock is being placed must have been opened with read access. 

A write lock prevents any process from read-locking or write-locking the protected area.  Only one write lock may exist for a given segment of a file at a given time.  The file descriptor on which a write lock is being placed must have been opened with write access. 

The structure flock describes the type (l_type), starting offset (l_whence), relative offset (l_start), size (l_len), and process id (l_pid) of the segment of the file to be affected.  The process id field is only used with the F_GETLK cmd to return the value for a block in lock.  Locks may start and extend beyond the current end of a file, but may not be negative relative to the beginning of the file.  A lock may be set to always extend to the end of file by setting l_len to zero (0).  If such a lock also has l_start set to zero (0), the whole file will be locked.  Changing or unlocking a segment from the middle of a larger locked segment leaves two smaller segments for either end.  Locking a segment that is already locked by the calling process causes the old lock type to be removed and the new lock type to take effect.  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 in a fork(2) system call. 

When enforcement-mode file and record locking is active on a file, see chmod(2), future read and write system calls on the file will be affected by the record locks in effect. 

ERRORS

Fcntl will fail if one or more of the following conditions are true, thus errno is set accordingly:

­[EBADF] Fildes is not a valid open file descriptor. 

­[EMFILE] Cmd is F_DUPFD and the maximum number of file descriptors is currently open. 

[EMFILE] Cmd is F_SETLK or F_SETLKW, the type of lock is a read or write lock and there are no more file-locking headers available (too many files have segments locked). 

­[EINVAL] Cmd is F_DUPFD and arg.val is negative, greater than or equal to the maximum number of file descriptors. 

[EINVAL] Cmd is F_GETLK, F_SETLK, or F_SETLKW and arg.lockdes or the data it points to is not valid. 

[EINVAL] Cmd is not a valid command. 

[EACCES] Cmd is F_SETLK, the type of lock (l_type) is a read (F_RDLCK) lock or write (F_WRLCK) lock and the segment of a file to be locked is already write-locked by another process, or the type is a write lock (F-WRLCK) and the segment of a file to be locked is already read- or write-locked by another process. 

[ENOSPC] Cmd is F_SETLK or F_SETLKW, the type of lock is a read or write lock and there are no more file-locking headers available (too many files have segments locked), or there are no more record locks available (too many file segments locked). 

­[EDEADLK] Cmd is F_SETLKW, when the lock is blocked by some lock from another process and sleeping (waiting) for that lock to become free.  This causes a deadlock situation. 

­[EFAULT] Cmd is either F_GETLK, F_SETLK or F_SETLKW, and arg points to an illegal address. 

RETURN VALUE

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

F_DUPFD A new file descriptor. 

F_GETFD Value of close-on-exec flag (only the low-order bit is defined). 

F_SETFD Value other than −1. 

F_GETFL Value of file status flags. 

F_SETFL Value other than −1. 

F_GETLK Value other that −1. 

F_SETLK Value other than −1. 

F_SETLKW Value other than −1. 

Otherwise, a value of −1 is returned and errno is set to indicate the error. 

HARDWARE DEPENDENCIES

Series 200, 500
The F_GETLK, F_SETLK, and F_SETLKW commands are not supported.

The EFAULT error "Cmd is either F_GETLK, F_SETLK or F_SETLKW, and arg points to an illegal address" is not supported. 

AUTHOR

Fcntl was developed by the Hewlett-Packard Company, AT&T Bell Laboratories, and The University of California, Berkeley California, Computer Science Division, Department of Electrical Engineering and Computer Science. 

APPLICATION USAGE

Because in the future the variable errno will be set to EAGAIN rather than EACCES when a section of a file is already locked by another process, portable application programs should expect and test for either value, for example:

flk->l_type = F_RDLCK;
if (fcntl(fd, F_SETLK, flk) == -1)
if ((errno == EACCES) || (errno == EAGAIN))
/*
* section locked by another process,
* check for either EAGAIN or EACCES
* due to different implementations
*/
else if ...
/*
* check for other errors
*/

SEE ALSO

close(2), exec(2), lockf(2), open(2), fcntl(5). 

FUTURE DIRECTIONS

The error condition which currently sets errno to EACCES will instead set errno to EAGAIN [see also APPLICATION USAGE above]. 

Hewlett-Packard Company  —  Version B.1,  May 11, 2021

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026