2011-03-30 13:49:47 +00:00
|
|
|
.TH DUP 2
|
|
|
|
.SH NAME
|
|
|
|
dup \- duplicate an open file descriptor
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.B #include <u.h>
|
|
|
|
.br
|
|
|
|
.B #include <libc.h>
|
|
|
|
.PP
|
|
|
|
.B
|
|
|
|
int dup(int oldfd, int newfd)
|
|
|
|
.SH DESCRIPTION
|
|
|
|
Given a file descriptor,
|
|
|
|
.IR oldfd ,
|
|
|
|
referring to an open file,
|
|
|
|
.I dup
|
|
|
|
returns a new file descriptor referring to the same file.
|
|
|
|
.PP
|
|
|
|
If
|
|
|
|
.I newfd
|
|
|
|
is \-1 the system chooses the lowest available file descriptor.
|
|
|
|
Otherwise,
|
|
|
|
.I dup
|
|
|
|
will use
|
|
|
|
.I newfd
|
|
|
|
for the new file descriptor
|
|
|
|
(closing any old file associated with
|
|
|
|
.IR newfd ).
|
|
|
|
File descriptors are allocated dynamically,
|
|
|
|
so to prevent unwarranted growth of the file descriptor table,
|
|
|
|
.I dup
|
|
|
|
requires that
|
|
|
|
.I newfd
|
|
|
|
be no greater than 20 more than the highest file descriptor ever used by
|
|
|
|
the program.
|
kernel: implement per file descriptor OCEXEC flag, reject ORCLOSE when opening /fd, /srv and /shr
The OCEXEC flag used to be maintained per channel,
making it shared between all the file desciptors.
This has a unexpected side effects with regard to
channel passing drivers such as devdup (/fd),
devsrv (/srv) and devshr (/shr).
For example, opening a /srv file with OCEXEC
makes it impossible to be remounted by exportfs
as it internally does a exec() to mount and
re-export it. There is no way to reset the flag.
This change makes the OCEXEC flag per file descriptor,
so a open with the OCEXEC flag only affects the fd
group of the calling process, and not the channel
itself.
On rfork(RFFDG), the per file descriptor flags get
copied.
On dup(), the per file descriptor flags are reset.
The second modification is that /fd, /srv and /shr
should reject the ORCLOSE flag, as the files that
are returned have already been opend.
2020-12-13 15:04:09 +00:00
|
|
|
.PP
|
|
|
|
.I Dup
|
|
|
|
does not copy the per file descriptor
|
|
|
|
.B OCEXEC
|
|
|
|
flag,
|
|
|
|
meaning that
|
|
|
|
.I newfd
|
|
|
|
will not be closed on
|
2021-09-08 18:22:35 +00:00
|
|
|
.IR exec (2)
|
kernel: implement per file descriptor OCEXEC flag, reject ORCLOSE when opening /fd, /srv and /shr
The OCEXEC flag used to be maintained per channel,
making it shared between all the file desciptors.
This has a unexpected side effects with regard to
channel passing drivers such as devdup (/fd),
devsrv (/srv) and devshr (/shr).
For example, opening a /srv file with OCEXEC
makes it impossible to be remounted by exportfs
as it internally does a exec() to mount and
re-export it. There is no way to reset the flag.
This change makes the OCEXEC flag per file descriptor,
so a open with the OCEXEC flag only affects the fd
group of the calling process, and not the channel
itself.
On rfork(RFFDG), the per file descriptor flags get
copied.
On dup(), the per file descriptor flags are reset.
The second modification is that /fd, /srv and /shr
should reject the ORCLOSE flag, as the files that
are returned have already been opend.
2020-12-13 15:04:09 +00:00
|
|
|
syscall,
|
|
|
|
when
|
|
|
|
.I oldfd
|
|
|
|
had been previously opend with it.
|
2011-03-30 13:49:47 +00:00
|
|
|
.SH SOURCE
|
|
|
|
.B /sys/src/libc/9syscall
|
|
|
|
.SH SEE ALSO
|
|
|
|
.IR intro (2),
|
|
|
|
.IR dup (3)
|
|
|
|
.SH DIAGNOSTICS
|
|
|
|
Sets
|
|
|
|
.IR errstr .
|