fcntl |
Prototype: |
#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd, int cmd);
int fcntl(int fd, int cmd, long arg);
int fcntl(int fd, int cmd, struct flock *flock);
|
General Description: |
Manipulate the file or socket handle. |
Return Value: |
On error, -1 is returned, and errno is set appropriately. For a successful call, the return value depends on the operation: F_DUPFD - The new descriptor. F_GETFD - Value of flag. F_GETFL - Value of flags. F_GETOWN - Value of descriptor owner. F_GETSIG - Value of signal sent when read or write becomes possible, or zero for traditional SIGIO behavior. All other commands return zero. |
Parameters |
fd |
The descriptor to manipulate. |
cmd |
The operation to perform. Some operations are duplicates to existing functions. Some operation require an operand (arg or flock). Each operation is grouped into specific functions:Duplicate descriptor (F_DUPFD) - Same as dup2(arg, fd), this operation replaces fd with a copy of the descriptor in arg.Manipulate close-on-exec (F_GETFD, F_SETFD) - The kernel does not pass all file descriptors to the exec-child process. With this parameter you can test or set the close-on-exec.Manipulate descriptor flags (F_GETFL, F_SETFL) - Using these commands you can get the flags (set by the open() system call) of the descriptor. You can only set O_APPEND, O_NONBLOCK and O_ASYNC.Manipulate file locks (F_GETLK, F_SETLK, F_SETLKW) - GETLK retrieves the lock structure that currently holds the file. If the file is not locked,Determine who owns I/O signals (F_GETOWN, F_SETOWN) - Return or set the PID of the current owner of the SIGIO signal.Determine the kind of signal to send (F_GETSIG, F_SETSIG) - Gets or sets the signal type when more I/O operations can be performed. Default is SIGIO. |
arg |
The value to set. |
flock |
The locking key. |
Possible Errors |
EACCES |
Operation is prohibited by locks held by other processes. |
EAGAIN |
Operation is prohibited because the file has been memory-mapped by another process. |
EBADF |
fd is not an open file descriptor. |
EDEADLK |
It was detected that the specified F_SETLKW command would cause a deadlock. |
EFAULT |
lock is outside your accessible address space. |
EINTR |
For F_SETLKW, the command was interrupted by a signal. For F_GETLK and F_SETLK, the command was interrupted by a signal before the lock was checked or acquired. Most likely when locking a remote file (e.g. locking over NFS), but can sometimes happen locally. |
EINVAL |
For F_DUPFD, arg is negative or is greater than the maximum allowable value. For F_SETSIG, arg is not an allowable signal number. |
EMFILE |
For F_DUPFD, the process already has the maximum number of file descriptors open. |
ENOLCK |
Too many segment locks open, lock table is full, or a remote locking protocol failed (e.g. locking over NFS). |
EPERM |
Attempted to clear the O_APPEND flag on a file that has the append-only attribute set. |
Examples |
#include <unistd.h>
#include <fnctl.h>
...
printf("PID which owns SIGIO: %d",
fnctl(fd, F_GETOWN));
|
#include <unistd.h>
#include <fnctl.h>
...
if ( fnctl(fd, F_SETSIG, SIGKILL) != 0 )
perror("Can't set signal");
|
#include <unistd.h>
#include <fnctl.h>
...
if ( (fd_copy = fcntl(fd, F_DUPFD)) < 0 )
perror("Can't dup fd");
|