📄 syscalls_12.html
字号:
<!-- This HTML file has been created by texi2html 1.29
from syscalls.texi on 4 June 1994 -->
<TITLE>Syscall specifications of Linux - fcntl</TITLE>
<P>Go to the <A HREF="syscalls_11.html" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_11.html">previous</A>, <A HREF="syscalls_13.html" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_13.html">next</A> section.<P>
<H2><A NAME="SEC12" HREF="syscalls_toc.html#SEC12" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_toc.html#SEC12">fcntl</A></H2>
<P>
<H3>SYNOPSIS</H3>
<P>
<CODE>int fcntl(int <VAR>fd</VAR>, int <VAR>cmd</VAR>);</CODE>
<P>
<CODE>int fcntl(int <VAR>fd</VAR>, int <VAR>cmd</VAR>, long <VAR>arg</VAR>);</CODE>
<P>
<H3>PARAMETERS</H3>
<P>
<VAR>fd</VAR>: [in] the file descriptor affected by the call.
<P>
<VAR>cmd</VAR>: [in] the operation to apply on the file descriptor.
<P>
<VAR>arg</VAR>: [in] an optionnal argument to the operation.
<P>
<H3>DESCRIPTION</H3>
<P>
This call directly applies an operation on a file descriptor. The
possible operations are:
<P>
<DL COMPACT>
<DT><CODE>F_DUPFD</CODE>
<DD>Duplicates <VAR>fd</VAR> the the new file descriptor specified by <VAR>arg</VAR>.
If <VAR>arg</VAR> specifies a file descriptor that was already opened, then
the file descriptor is first closed. It has the same effect has
<CODE>dup2</CODE>. See section <A HREF="syscalls_11.html#SEC11" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_11.html#SEC11">dup and dup2</A>
<P>
<DT><CODE>F_GETFD</CODE>
<DD>Returns the close-on-exec flag of <VAR>fd</VAR>.
<P>
<DT><CODE>F_SETFD</CODE>
<DD>Sets the close-on-exec flag of <VAR>fd</VAR>.
<P>
<DT><CODE>F_GETFL</CODE>
<DD>Returns the file descriptor flags (as specified by <CODE>open</CODE>).
<P>
<DT><CODE>F_SETFL</CODE>
<DD>Sets the file descriptor flags to <VAR>arg</VAR>. The only flags that can be
modified are <CODE>O_APPEND</CODE> and <CODE>O_NONBLOCK</CODE>. See section <A HREF="syscalls_10.html#SEC10" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_10.html#SEC10">creat and open</A>
<P>
<DT><CODE>F_GETLK</CODE>
<DD>Determine if the lock described by <VAR>arg</VAR> can be set on the file. If
so, the <CODE>l_type</CODE> member is set to <CODE>F_UNLCK</CODE>. Otherwise,
<VAR>arg</VAR> is modified to describe the lock preventing the set operation.
<DT><CODE>F_SETLK</CODE>
<DD>Set the lock described by <VAR>arg</VAR> on the file or release an already
existing lock.
<P>
<DT><CODE>F_SETLKW</CODE>
<DD>Same as <CODE>F_SETLK</CODE> but block if the lock can not be set.
<P>
<DT><CODE>F_GETOWN</CODE>
<DD>Returns the process id or the process group of a socket. A process group
is returned as a negative value.
<P>
<DT><CODE>F_SETOWN</CODE>
<DD>Sets the process id or the process group that owns a socket. The owner
of the socket receives the <CODE>SIGIO</CODE> and <CODE>SIGURG</CODE> signals. A
process group is specified by a negative value.
</DL>
When using the <CODE>F_GETLK</CODE>, <CODE>F_SETLK</CODE> or <CODE>F_SETLKW</CODE>
commands, the argument is a pointer to a <CODE>flock</CODE> structure. This
structure has the following layout:
<P>
<PRE>
struct flock {
short l_type; /* read, write or unlock */
short l_whence; /* how to interpret l_start */
off_t l_start; /* where to begin the locking area */
off_t l_len; /* the lenght of the area to lock */
pid_t l_pid; /* the pid of the task holding the lock:
returned by F_GETLK */
};
</PRE>
<P>
The <CODE>l_whence</CODE> member has the same meaning as for <CODE>lseek</CODE>.
See section <A HREF="syscalls_19.html#SEC19" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_19.html#SEC19">lseek</A> <CODE>l_type</CODE> can take one of the following values:
<P>
<DL COMPACT>
<DT><CODE>F_RDLCK</CODE>
<DD>for a shared read lock on.
<P>
<DT><CODE>F_WRLCK</CODE>
<DD>for an exclusive write lock.
<P>
<DT><CODE>F_UNLCK</CODE>
<DD>to unlock the region.
</DL>
<P>
The system merges adjacent locking regions of the same type and owned by
the same task. When a subregion inside a region is unlocked, the region
is split in two parts.
<P>
<H3>RETURN VALUE</H3>
<P>
On success, it depends on the <VAR>cmd</VAR> parameter:
<P>
<DL COMPACT>
<DT><CODE>F_DUPFD</CODE>
<DD>the new file descriptor.
<P>
<DT><CODE>F_GETFD</CODE>
<DD>the value of the close-on-exec flag.
<P>
<DT><CODE>F_GETFL</CODE>
<DD>the value of the file descriptor flags.
<P>
<DT><CODE>F_GETOWN</CODE>
<DD>the owner of the file descriptor.
<P>
</DL>
<P>
On error, the call returns -1 and <CODE>errno</CODE> is set to one of the
following values:
<P>
<UL>
<LI><CODE>EINVAL</CODE>: for <CODE>F_DUPFD</CODE>, <VAR>arg</VAR> is invalid or the
maximum number of opened file descriptor is reached. For <CODE>F_GETLK</CODE>,
specified a lock with <CODE>F_UNLCK</CODE>. Or unlock operation failed.
<LI><CODE>EBADF</CODE>: the file descriptor is invalid, or the task
requested a lock for reading or writing while it does not have the
corresponding read or write access right on the file.
<LI><CODE>EAGAIN</CODE>: impossible to set the lock.
<LI><CODE>ENOLCK</CODE>: impossible to allocate memory for the new lock.
<LI><CODE>EFAULT</CODE>, <CODE>ERESTARTSYS</CODE>.
</UL>
<P>
<P>Go to the <A HREF="syscalls_11.html" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_11.html">previous</A>, <A HREF="syscalls_13.html" tppabs="http://personal.xfol.com/%7erezaie/syscall/syscalls_13.html">next</A> section.<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -