1 /* Test duplicating file descriptors.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
30 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
31 /* Get declarations of the Win32 API functions. */
32 # define WIN32_LEAN_AND_MEAN
36 #define ASSERT(expr) \
41 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
48 /* Return non-zero if FD is open. */
52 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
53 /* On Win32, the initial state of unassigned standard file
54 descriptors is that they are open but point to an
55 INVALID_HANDLE_VALUE, and there is no fcntl. */
56 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
59 # error Please port fcntl to your platform
61 return 0 <= fcntl (fd, F_GETFL);
65 /* Return non-zero if FD is open and inheritable across exec/spawn. */
67 is_inheritable (int fd)
69 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
70 /* On Win32, the initial state of unassigned standard file
71 descriptors is that they are open but point to an
72 INVALID_HANDLE_VALUE, and there is no fcntl. */
73 HANDLE h = (HANDLE) _get_osfhandle (fd);
75 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
77 return (flags & HANDLE_FLAG_INHERIT) != 0;
80 # error Please port fcntl to your platform
82 int i = fcntl (fd, F_GETFD);
83 return 0 <= i && (i & FD_CLOEXEC) == 0;
90 const char *file = "test-dup2.tmp";
92 int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
94 /* Assume std descriptors were provided by invoker. */
95 ASSERT (STDERR_FILENO < fd);
96 ASSERT (is_open (fd));
97 /* Ignore any other fd's leaked into this process. */
100 ASSERT (!is_open (fd + 1));
101 ASSERT (!is_open (fd + 2));
103 /* Assigning to self must be a no-op. */
104 ASSERT (dup2 (fd, fd) == fd);
105 ASSERT (is_open (fd));
107 /* The source must be valid. */
109 ASSERT (dup2 (-1, fd) == -1);
110 ASSERT (errno == EBADF);
112 ASSERT (dup2 (AT_FDCWD, fd) == -1);
113 ASSERT (errno == EBADF);
114 ASSERT (is_open (fd));
116 /* If the source is not open, then the destination is unaffected. */
118 ASSERT (dup2 (fd + 1, fd + 1) == -1);
119 ASSERT (errno == EBADF);
120 ASSERT (!is_open (fd + 1));
122 ASSERT (dup2 (fd + 1, fd) == -1);
123 ASSERT (errno == EBADF);
124 ASSERT (is_open (fd));
126 /* The destination must be valid. */
128 ASSERT (dup2 (fd, -2) == -1);
129 ASSERT (errno == EBADF);
131 ASSERT (dup2 (fd, 10000000) == -1);
132 ASSERT (errno == EBADF);
134 /* Using dup2 can skip fds. */
135 ASSERT (dup2 (fd, fd + 2) == fd + 2);
136 ASSERT (is_open (fd));
137 ASSERT (!is_open (fd + 1));
138 ASSERT (is_open (fd + 2));
140 /* Verify that dup2 closes the previous occupant of a fd. */
141 ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
142 ASSERT (dup2 (fd + 1, fd) == fd);
143 ASSERT (close (fd + 1) == 0);
144 ASSERT (write (fd, "1", 1) == 1);
145 ASSERT (dup2 (fd + 2, fd) == fd);
146 ASSERT (lseek (fd, 0, SEEK_END) == 0);
147 ASSERT (write (fd + 2, "2", 1) == 1);
148 ASSERT (lseek (fd, 0, SEEK_SET) == 0);
149 ASSERT (read (fd, buffer, 1) == 1);
150 ASSERT (*buffer == '2');
152 /* Any new fd created by dup2 must not be cloexec. */
153 ASSERT (close (fd + 2) == 0);
154 ASSERT (dup_cloexec (fd) == fd + 1);
155 ASSERT (!is_inheritable (fd + 1));
156 ASSERT (dup2 (fd + 1, fd + 1) == fd + 1);
157 ASSERT (!is_inheritable (fd + 1));
158 ASSERT (dup2 (fd + 1, fd + 2) == fd + 2);
159 ASSERT (is_inheritable (fd + 2));
162 ASSERT (close (fd + 2) == 0);
163 ASSERT (close (fd + 1) == 0);
164 ASSERT (close (fd) == 0);
165 ASSERT (unlink (file) == 0);