maint: update all copyright year number ranges
[gnulib.git] / tests / test-dup3.c
1 /* Test duplicating file descriptors.
2    Copyright (C) 2009-2013 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2009,
18    and Bruno Haible <bruno@clisp.org>, 2009.  */
19
20 #include <config.h>
21
22 #include <unistd.h>
23
24 #include "signature.h"
25 SIGNATURE_CHECK (dup3, int, (int, int, int));
26
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdbool.h>
30
31 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
32 /* Get declarations of the native Windows API functions.  */
33 # define WIN32_LEAN_AND_MEAN
34 # include <windows.h>
35 /* Get _get_osfhandle.  */
36 # include "msvc-nothrow.h"
37 #endif
38
39 #include "binary-io.h"
40 #include "macros.h"
41
42 /* Return true if FD is open.  */
43 static bool
44 is_open (int fd)
45 {
46 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
47   /* On native Windows, the initial state of unassigned standard file
48      descriptors is that they are open but point to an
49      INVALID_HANDLE_VALUE, and there is no fcntl.  */
50   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
51 #else
52 # ifndef F_GETFL
53 #  error Please port fcntl to your platform
54 # endif
55   return 0 <= fcntl (fd, F_GETFL);
56 #endif
57 }
58
59 /* Return true if FD is not inherited to child processes.  */
60 static bool
61 is_cloexec (int fd)
62 {
63 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
64   HANDLE h = (HANDLE) _get_osfhandle (fd);
65   DWORD flags;
66   ASSERT (GetHandleInformation (h, &flags));
67   return (flags & HANDLE_FLAG_INHERIT) == 0;
68 #else
69   int flags;
70   ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
71   return (flags & FD_CLOEXEC) != 0;
72 #endif
73 }
74
75 int
76 main ()
77 {
78   int use_cloexec;
79
80 #if O_CLOEXEC
81   for (use_cloexec = 0; use_cloexec <= 1; use_cloexec++)
82 #else
83   use_cloexec = 0;
84 #endif
85     {
86       const char *file = "test-dup3.tmp";
87       int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
88       int o_flags;
89       char buffer[1];
90
91       o_flags = 0;
92 #if O_CLOEXEC
93       if (use_cloexec)
94         o_flags |= O_CLOEXEC;
95 #endif
96
97       /* Assume std descriptors were provided by invoker.  */
98       ASSERT (STDERR_FILENO < fd);
99       ASSERT (is_open (fd));
100       /* Ignore any other fd's leaked into this process.  */
101       close (fd + 1);
102       close (fd + 2);
103       ASSERT (!is_open (fd + 1));
104       ASSERT (!is_open (fd + 2));
105
106       /* Assigning to self is invalid.  */
107       errno = 0;
108       ASSERT (dup3 (fd, fd, o_flags) == -1);
109       ASSERT (errno == EINVAL);
110       ASSERT (is_open (fd));
111
112       /* If the source is not open, then the destination is unaffected.  */
113       errno = 0;
114       ASSERT (dup3 (fd + 1, fd + 2, o_flags) == -1);
115       ASSERT (errno == EBADF);
116       ASSERT (!is_open (fd + 2));
117       errno = 0;
118       ASSERT (dup3 (fd + 1, fd, o_flags) == -1);
119       ASSERT (errno == EBADF);
120       ASSERT (is_open (fd));
121
122       /* The destination must be valid.  */
123       errno = 0;
124       ASSERT (dup3 (fd, -2, o_flags) == -1);
125       ASSERT (errno == EBADF);
126       errno = 0;
127       ASSERT (dup3 (fd, 10000000, o_flags) == -1);
128       ASSERT (errno == EBADF);
129
130       /* Using dup3 can skip fds.  */
131       ASSERT (dup3 (fd, fd + 2, o_flags) == fd + 2);
132       ASSERT (is_open (fd));
133       ASSERT (!is_open (fd + 1));
134       ASSERT (is_open (fd + 2));
135       if (use_cloexec)
136         ASSERT (is_cloexec (fd + 2));
137       else
138         ASSERT (!is_cloexec (fd + 2));
139
140       /* Verify that dup3 closes the previous occupant of a fd.  */
141       ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
142       ASSERT (dup3 (fd + 1, fd, o_flags) == fd);
143       ASSERT (close (fd + 1) == 0);
144       ASSERT (write (fd, "1", 1) == 1);
145       ASSERT (dup3 (fd + 2, fd, o_flags) == 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');
151
152       /* Clean up.  */
153       ASSERT (close (fd + 2) == 0);
154       ASSERT (close (fd) == 0);
155       ASSERT (unlink (file) == 0);
156     }
157
158   return 0;
159 }