summaryrefslogtreecommitdiffstats
path: root/upstream/archlinux/man3p/dup.3p
blob: 59444c3f0171590be4b198b6a00ac0d5abb5ca73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
'\" et
.TH DUP "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.\"
.SH NAME
dup,
dup2
\(em duplicate an open file descriptor
.SH SYNOPSIS
.LP
.nf
#include <unistd.h>
.P
int dup(int \fIfildes\fP);
int dup2(int \fIfildes\fP, int \fIfildes2\fP);
.fi
.SH DESCRIPTION
The
\fIdup\fR()
function provides an alternative interface to the service provided by
\fIfcntl\fR()
using the F_DUPFD command. The call
.IR dup ( fildes )
shall be equivalent to:
.sp
.RS 4
.nf

fcntl(fildes, F_DUPFD, 0);
.fi
.P
.RE
.P
The
\fIdup2\fR()
function shall cause the file descriptor
.IR fildes2
to refer to the same open file description as the file descriptor
.IR fildes
and to share any locks, and shall return
.IR fildes2 .
If
.IR fildes2
is already a valid open file descriptor, it shall be closed first, unless
.IR fildes
is equal to
.IR fildes2
in which case
\fIdup2\fR()
shall return
.IR fildes2
without closing it. If the close operation fails to close
.IR fildes2 ,
\fIdup2\fR()
shall return \-1 without changing the open file description to which
.IR fildes2
refers. If
.IR fildes
is not a valid file descriptor,
\fIdup2\fR()
shall return \-1 and shall not close
.IR fildes2 .
If
.IR fildes2
is less than 0 or greater than or equal to
{OPEN_MAX},
\fIdup2\fR()
shall return \-1 with
.IR errno
set to
.BR [EBADF] .
.P
Upon successful completion, if
.IR fildes
is not equal to
.IR fildes2 ,
the FD_CLOEXEC flag associated with
.IR fildes2
shall be cleared. If
.IR fildes
is equal to
.IR fildes2 ,
the FD_CLOEXEC flag associated with
.IR fildes2
shall not be changed.
.P
If
.IR fildes
refers to a typed memory object, the result of the
\fIdup2\fR()
function is unspecified.
.SH "RETURN VALUE"
Upon successful completion a non-negative integer, namely the file
descriptor, shall be returned; otherwise, \-1 shall be returned
and
.IR errno
set to indicate the error.
.SH ERRORS
The
\fIdup\fR()
function shall fail if:
.TP
.BR EBADF
The
.IR fildes
argument is not a valid open file descriptor.
.TP
.BR EMFILE
All file descriptors available to the process are currently open.
.P
The
\fIdup2\fR()
function shall fail if:
.TP
.BR EBADF
The
.IR fildes
argument is not a valid open file descriptor or the argument
.IR fildes2
is negative or greater than or equal to
{OPEN_MAX}.
.TP
.BR EINTR
The
\fIdup2\fR()
function was interrupted by a signal.
.P
The
\fIdup2\fR()
function may fail if:
.TP
.BR EIO
An I/O error occurred while attempting to close
.IR fildes2 .
.LP
.IR "The following sections are informative."
.SH EXAMPLES
.SS "Redirecting Standard Output to a File" S
.P
The following example closes standard output for the current processes,
re-assigns standard output to go to the file referenced by
.IR pfd ,
and closes the original file descriptor to clean up.
.sp
.RS 4
.nf

#include <unistd.h>
\&...
int pfd;
\&...
close(1);
dup(pfd);
close(pfd);
\&...
.fi
.P
.RE
.SS "Redirecting Error Messages"
.P
The following example redirects messages from
.IR stderr
to
.IR stdout .
.sp
.RS 4
.nf

#include <unistd.h>
\&...
dup2(1, 2);
\&...
.fi
.P
.RE
.SH "APPLICATION USAGE"
Implementations may use file descriptors that must be inherited into
child processes for the child process to remain conforming, such as for
message catalog or tracing purposes. Therefore, an application that calls
\fIdup2\fR()
with an arbitrary integer for
.IR fildes2
risks non-conforming behavior, and
\fIdup2\fR()
can only portably be used to overwrite file descriptor values that the
application has obtained through explicit actions, or for the three file
descriptors corresponding to the standard file streams. In order to avoid
a race condition of leaking an unintended file descriptor into a child
process, an application should consider opening all file descriptors
with the FD_CLOEXEC bit set unless the file descriptor is intended to
be inherited across
.IR exec .
.SH RATIONALE
The
\fIdup\fR()
function is redundant. Its services are also provided by the
\fIfcntl\fR()
function. It has been included in this volume of POSIX.1\(hy2017 primarily for historical reasons,
since many existing applications use it. On the other hand, the
\fIdup2\fR()
function provides unique services, as no other interface is able to
atomically replace an existing file descriptor.
.P
The
\fIdup2\fR()
function is not marked obsolescent because it presents a type-safe
version of functionality provided in a type-unsafe version by
\fIfcntl\fR().
It is used in the POSIX Ada binding.
.P
The
\fIdup2\fR()
function is not intended for use in critical regions as a
synchronization mechanism.
.P
In the description of
.BR [EBADF] ,
the case of
.IR fildes
being out of range is covered by the given case of
.IR fildes
not being valid. The descriptions for
.IR fildes
and
.IR fildes2
are different because the only kind of invalidity that is relevant for
.IR fildes2
is whether it is out of range; that is, it does not matter whether
.IR fildes2
refers to an open file when the
\fIdup2\fR()
call is made.
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
.IR "\fIclose\fR\^(\|)",
.IR "\fIfcntl\fR\^(\|)",
.IR "\fIopen\fR\^(\|)"
.P
The Base Definitions volume of POSIX.1\(hy2017,
.IR "\fB<unistd.h>\fP"
.\"
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1-2017, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 7, 2018 Edition,
Copyright (C) 2018 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group.
In the event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .
.PP
Any typographical or formatting errors that appear
in this page are most likely
to have been introduced during the conversion of the source files to
man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .