Added
Link Here
|
1 |
/*- |
2 |
* Copyright (c) 2012 Jukka A. Ukkonen |
3 |
* All rights reserved. |
4 |
* |
5 |
* This software was developed by Jukka Ukkonen for FreeBSD. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or without |
8 |
* modification, are permitted provided that the following conditions |
9 |
* are met: |
10 |
* 1. Redistributions of source code must retain the above copyright |
11 |
* notice, this list of conditions and the following disclaimer. |
12 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
* notice, this list of conditions and the following disclaimer in the |
14 |
* documentation and/or other materials provided with the distribution. |
15 |
* |
16 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
17 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
20 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
21 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
22 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
23 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
24 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
25 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
26 |
* SUCH DAMAGE. |
27 |
*/ |
28 |
|
29 |
#include <sys/cdefs.h> |
30 |
__FBSDID("$FreeBSD$"); |
31 |
|
32 |
#include "namespace.h" |
33 |
#include <unistd.h> |
34 |
#include <fcntl.h> |
35 |
#include <errno.h> |
36 |
#include "un-namespace.h" |
37 |
|
38 |
/* |
39 |
* A bunch of checks just in case someone |
40 |
* tries to compile this on a too old system. |
41 |
*/ |
42 |
|
43 |
#if !defined(F_DUP2FD_CLOEXEC) && defined(_F_DUP2FD_CLOEXEC) |
44 |
# define F_DUP2FD_CLOEXEC _F_DUP2FD_CLOEXEC |
45 |
#endif |
46 |
|
47 |
#if !defined(F_DUP2FD_CLOEXEC) |
48 |
# error Neither F_DUP2FD_CLOEXEC nor _F_DUP2FD_CLOEXEC defined! |
49 |
#endif |
50 |
|
51 |
#if !defined(F_DUP2FD) |
52 |
# error F_DUP2FD not defined! |
53 |
#endif |
54 |
|
55 |
#if !defined(O_CLOEXEC) |
56 |
# error O_CLOEXEC not defined! |
57 |
#endif |
58 |
|
59 |
/* |
60 |
* In case FreeBSD ever becomes O_NOSIGPIPE aware, |
61 |
* we will instantly start using it. |
62 |
*/ |
63 |
|
64 |
#if !defined(O_NOSIGPIPE) |
65 |
# define O_NOSIGPIPE 0 |
66 |
#endif |
67 |
|
68 |
int |
69 |
__dup3 (oldfd, newfd, flags) |
70 |
int oldfd; |
71 |
int newfd; |
72 |
int flags; |
73 |
{ |
74 |
int how; |
75 |
int ret; |
76 |
|
77 |
#if 0 |
78 |
/* |
79 |
* This would be how Linux does it |
80 |
* in pointlessly restrictive style. |
81 |
* NetBSD does not do this. |
82 |
* FreeBSD can also handle this case |
83 |
* properly. |
84 |
* ==> Anything which works on Linux |
85 |
* will work also on {Free,Net}BSD. |
86 |
* BSD code on Linux may experience |
87 |
* hiccups, if someone relies on |
88 |
* oldfd == newfd. |
89 |
*/ |
90 |
|
91 |
if (oldfd == newfd) { |
92 |
errno = EINVAL; |
93 |
return (-1); |
94 |
} |
95 |
#endif |
96 |
|
97 |
if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_NOSIGPIPE)) { |
98 |
errno = EINVAL; |
99 |
return (-1); |
100 |
} |
101 |
|
102 |
how = (flags & O_CLOEXEC) ? F_DUP2FD_CLOEXEC : F_DUP2FD; |
103 |
|
104 |
ret = _fcntl (oldfd, how, newfd); |
105 |
|
106 |
if (ret < 0) |
107 |
return (-1); |
108 |
|
109 |
newfd = ret; |
110 |
|
111 |
if (flags & (O_NONBLOCK | O_NOSIGPIPE)) { |
112 |
int flags2; |
113 |
|
114 |
flags2 = _fcntl (newfd, F_GETFL, 0); |
115 |
flags2 |= (flags & (O_NONBLOCK | O_NOSIGPIPE)); |
116 |
(void) _fcntl (newfd, F_SETFL, flags2); |
117 |
} |
118 |
|
119 |
return (newfd); |
120 |
} |
121 |
|
122 |
__weak_reference(__dup3, dup3); |
123 |
__weak_reference(__dup3, _dup3); |