<s>
In	O
Unix	B-Application
and	O
Unix-like	B-Operating_System
computer	O
operating	O
systems	O
,	O
a	O
file	B-Operating_System
descriptor	O
(	O
FD	O
,	O
less	O
frequently	O
fildes	O
)	O
is	O
a	O
process-unique	O
identifier	O
(	O
handle	O
)	O
for	O
a	O
file	B-Operating_System
or	O
other	O
input/output	B-General_Concept
resource	B-General_Concept
,	O
such	O
as	O
a	O
pipe	B-Operating_System
or	O
network	B-Protocol
socket	I-Protocol
.	O
</s>
<s>
File	B-Operating_System
descriptors	O
typically	O
have	O
non-negative	O
integer	O
values	O
,	O
with	O
negative	O
values	O
being	O
reserved	O
to	O
indicate	O
"	O
no	O
value	O
"	O
or	O
error	O
conditions	O
.	O
</s>
<s>
File	B-Operating_System
descriptors	O
are	O
a	O
part	O
of	O
the	O
POSIX	O
API	B-Application
.	O
</s>
<s>
Each	O
Unix	B-Operating_System
process	I-Operating_System
(	O
except	O
perhaps	O
daemons	B-Operating_System
)	O
should	O
have	O
three	O
standard	O
POSIX	O
file	B-Operating_System
descriptors	O
,	O
corresponding	O
to	O
the	O
three	O
standard	B-Operating_System
streams	I-Operating_System
:	O
</s>
<s>
In	O
the	O
traditional	O
implementation	O
of	O
Unix	B-Application
,	O
file	B-Operating_System
descriptors	O
index	O
into	O
a	O
per-process	O
maintained	O
by	O
the	O
kernel	O
,	O
that	O
in	O
turn	O
indexes	O
into	O
a	O
system-wide	O
table	O
of	O
files	O
opened	O
by	O
all	O
processes	O
,	O
called	O
the	O
.	O
</s>
<s>
This	O
table	O
records	O
the	O
mode	O
with	O
which	O
the	O
file	B-Operating_System
(	O
or	O
other	O
resource	B-General_Concept
)	O
has	O
been	O
opened	O
:	O
for	O
reading	O
,	O
writing	O
,	O
appending	O
,	O
and	O
possibly	O
other	O
modes	O
.	O
</s>
<s>
It	O
also	O
indexes	O
into	O
a	O
third	O
table	O
called	O
the	O
inode	B-Application
table	I-Application
that	O
describes	O
the	O
actual	O
underlying	O
files	O
.	O
</s>
<s>
To	O
perform	O
input	O
or	O
output	O
,	O
the	O
process	B-Operating_System
passes	O
the	O
file	B-Operating_System
descriptor	O
to	O
the	O
kernel	O
through	O
a	O
system	B-Operating_System
call	I-Operating_System
,	O
and	O
the	O
kernel	O
will	O
access	O
the	O
file	B-Operating_System
on	O
behalf	O
of	O
the	O
process	B-Operating_System
.	O
</s>
<s>
The	O
process	B-Operating_System
does	O
not	O
have	O
direct	O
access	O
to	O
the	O
file	B-Operating_System
or	O
inode	B-Application
tables	O
.	O
</s>
<s>
On	O
Linux	B-Application
,	O
the	O
set	O
of	O
file	B-Operating_System
descriptors	O
open	B-Language
in	O
a	O
process	B-Operating_System
can	O
be	O
accessed	O
under	O
the	O
path	O
/proc/PID/fd/	O
,	O
where	O
PID	O
is	O
the	O
process	B-Operating_System
identifier	I-Operating_System
.	O
</s>
<s>
File	B-Operating_System
descriptor	O
/proc/PID/fd/0	O
is	O
stdin	O
,	O
/proc/PID/fd/1	O
is	O
stdout	O
,	O
and	O
/proc/PID/fd/2	O
is	O
stderr	O
.	O
</s>
<s>
As	O
a	O
shortcut	O
to	O
these	O
,	O
any	O
running	O
process	B-Operating_System
can	O
also	O
access	O
its	O
own	O
file	B-Operating_System
descriptors	O
through	O
the	O
folders	O
/proc/self/fd	O
and	O
/dev/fd	O
.	O
</s>
<s>
In	O
Unix-like	B-Operating_System
systems	I-Operating_System
,	O
file	B-Operating_System
descriptors	O
can	O
refer	O
to	O
any	O
Unix	B-Application
file	I-Application
type	I-Application
named	O
in	O
a	O
file	B-Operating_System
system	O
.	O
</s>
<s>
As	O
well	O
as	O
regular	O
files	O
,	O
this	O
includes	O
directories	B-Application
,	O
block	O
and	O
character	O
devices	O
(	O
also	O
called	O
"	O
special	O
files	O
"	O
)	O
,	O
Unix	B-Protocol
domain	I-Protocol
sockets	I-Protocol
,	O
and	O
named	B-Operating_System
pipes	I-Operating_System
.	O
</s>
<s>
File	B-Operating_System
descriptors	O
can	O
also	O
refer	O
to	O
other	O
objects	O
that	O
do	O
not	O
normally	O
exist	O
in	O
the	O
file	B-Operating_System
system	O
,	O
such	O
as	O
anonymous	B-Operating_System
pipes	I-Operating_System
and	O
network	B-Protocol
sockets	I-Protocol
.	O
</s>
<s>
The	O
FILE	B-Operating_System
data	O
structure	O
in	O
the	O
C	B-Language
standard	I-Language
I/O	I-Language
library	I-Language
usually	O
includes	O
a	O
low	O
level	O
file	B-Operating_System
descriptor	O
for	O
the	O
object	O
in	O
question	O
on	O
Unix-like	B-Operating_System
systems	I-Operating_System
.	O
</s>
<s>
The	O
overall	O
data	O
structure	O
provides	O
additional	O
abstraction	O
and	O
is	O
instead	O
known	O
as	O
a	O
file	B-Operating_System
handle	O
.	O
</s>
<s>
The	O
following	O
lists	O
typical	O
operations	O
on	O
file	B-Operating_System
descriptors	O
on	O
modern	O
Unix-like	B-Operating_System
systems	I-Operating_System
.	O
</s>
<s>
Most	O
of	O
these	O
functions	O
are	O
declared	O
in	O
the	O
<unistd.h> header, but some are in the <fcntl.h>	O
header	O
instead	O
.	O
</s>
<s>
The	O
function	O
is	O
used	O
to	O
perform	O
various	O
operations	O
on	O
a	O
file	B-Operating_System
descriptor	O
,	O
depending	O
on	O
the	O
command	O
argument	O
passed	O
to	O
it	O
.	O
</s>
<s>
There	O
are	O
commands	O
to	O
get	O
and	O
set	O
attributes	O
associated	O
with	O
a	O
file	B-Operating_System
descriptor	O
,	O
including	O
and	O
.	O
</s>
<s>
A	O
series	O
of	O
new	O
operations	O
on	O
file	B-Operating_System
descriptors	O
has	O
been	O
added	O
to	O
many	O
modern	O
Unix-like	B-Operating_System
systems	I-Operating_System
,	O
as	O
well	O
as	O
numerous	O
C	O
libraries	O
,	O
to	O
be	O
standardized	O
in	O
a	O
future	O
version	O
of	O
POSIX	O
.	O
</s>
<s>
The	O
at	O
suffix	O
signifies	O
that	O
the	O
function	O
takes	O
an	O
additional	O
first	O
argument	O
supplying	O
a	O
file	B-Operating_System
descriptor	O
from	O
which	O
relative	O
paths	O
are	O
resolved	O
,	O
the	O
forms	O
lacking	O
the	O
at	O
suffix	O
thus	O
becoming	O
equivalent	O
to	O
passing	O
a	O
file	B-Operating_System
descriptor	O
corresponding	O
to	O
the	O
current	B-Application
working	I-Application
directory	I-Application
.	O
</s>
<s>
The	O
purpose	O
of	O
these	O
new	O
operations	O
is	O
to	O
defend	O
against	O
a	O
certain	O
class	O
of	O
TOCTOU	B-Error_Name
attacks	O
.	O
</s>
<s>
Unix	B-Application
file	B-Operating_System
descriptors	O
behave	O
in	O
many	O
ways	O
as	O
capabilities	O
.	O
</s>
<s>
They	O
can	O
be	O
passed	O
between	O
processes	O
across	O
Unix	B-Protocol
domain	I-Protocol
sockets	I-Protocol
using	O
the	O
sendmsg( )	O
system	B-Operating_System
call	I-Operating_System
.	O
</s>
<s>
Note	O
,	O
however	O
,	O
that	O
what	O
is	O
actually	O
passed	O
is	O
a	O
reference	O
to	O
an	O
"	O
open	B-Language
file	B-Operating_System
description	O
"	O
that	O
has	O
mutable	O
state	O
(	O
the	O
file	B-Operating_System
offset	O
,	O
and	O
the	O
file	B-Operating_System
status	O
and	O
access	O
flags	O
)	O
.	O
</s>
<s>
This	O
complicates	O
the	O
secure	O
use	O
of	O
file	B-Operating_System
descriptors	O
as	O
capabilities	O
,	O
since	O
when	O
programs	O
share	O
access	O
to	O
the	O
same	O
open	B-Language
file	B-Operating_System
description	O
,	O
they	O
can	O
interfere	O
with	O
each	O
other	O
's	O
use	O
of	O
it	O
by	O
changing	O
its	O
offset	O
or	O
whether	O
it	O
is	O
blocking	O
or	O
non-blocking	O
,	O
for	O
example	O
.	O
</s>
<s>
A	O
Unix	B-Operating_System
process	I-Operating_System
 '	O
file	B-Operating_System
descriptor	O
table	O
is	O
an	O
example	O
of	O
a	O
C-list	B-Data_Structure
.	O
</s>
