Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-dup3.c
1 /* Test duplicating file descriptors.
2    Copyright (C) 2009 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 <errno.h>
25 #include <fcntl.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
31 /* Get declarations of the Win32 API functions.  */
32 # define WIN32_LEAN_AND_MEAN
33 # include <windows.h>
34 #endif
35
36 #include "binary-io.h"
37
38 #define ASSERT(expr) \
39   do                                                                         \
40     {                                                                        \
41       if (!(expr))                                                           \
42         {                                                                    \
43           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
44           fflush (stderr);                                                   \
45           abort ();                                                          \
46         }                                                                    \
47     }                                                                        \
48   while (0)
49
50 /* Return true if FD is open.  */
51 static bool
52 is_open (int fd)
53 {
54 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
55   /* On Win32, the initial state of unassigned standard file
56      descriptors is that they are open but point to an
57      INVALID_HANDLE_VALUE, and there is no fcntl.  */
58   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
59 #else
60 # ifndef F_GETFL
61 #  error Please port fcntl to your platform
62 # endif
63   return 0 <= fcntl (fd, F_GETFL);
64 #endif
65 }
66
67 /* Return true if FD is not inherited to child processes.  */
68 static bool
69 is_cloexec (int fd)
70 {
71 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
72   HANDLE h = (HANDLE) _get_osfhandle (fd);
73   DWORD flags;
74   ASSERT (GetHandleInformation (h, &flags));
75   return (flags & HANDLE_FLAG_INHERIT) == 0;
76 #else
77   int flags;
78   ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
79   return (flags & FD_CLOEXEC) != 0;
80 #endif
81 }
82
83 int
84 main ()
85 {
86   int use_cloexec;
87
88 #if defined O_CLOEXEC
89   for (use_cloexec = 0; use_cloexec <= 1; use_cloexec++)
90 #else
91   use_cloexec = 0;
92 #endif
93     {
94       const char *file = "test-dup3.tmp";
95       int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
96       int o_flags;
97       char buffer[1];
98
99       o_flags = 0;
100 #if defined O_CLOEXEC
101       if (use_cloexec)
102         o_flags |= O_CLOEXEC;
103 #endif
104
105       /* Assume std descriptors were provided by invoker.  */
106       ASSERT (STDERR_FILENO < fd);
107       ASSERT (is_open (fd));
108       /* Ignore any other fd's leaked into this process.  */
109       close (fd + 1);
110       close (fd + 2);
111       ASSERT (!is_open (fd + 1));
112       ASSERT (!is_open (fd + 2));
113
114       /* Assigning to self is invalid.  */
115       errno = 0;
116       ASSERT (dup3 (fd, fd, o_flags) == -1);
117       ASSERT (errno == EINVAL);
118       ASSERT (is_open (fd));
119
120       /* If the source is not open, then the destination is unaffected.  */
121       errno = 0;
122       ASSERT (dup3 (fd + 1, fd + 2, o_flags) == -1);
123       ASSERT (errno == EBADF);
124       ASSERT (!is_open (fd + 2));
125       errno = 0;
126       ASSERT (dup3 (fd + 1, fd, o_flags) == -1);
127       ASSERT (errno == EBADF);
128       ASSERT (is_open (fd));
129
130       /* The destination must be valid.  */
131       errno = 0;
132       ASSERT (dup3 (fd, -2, o_flags) == -1);
133       ASSERT (errno == EBADF);
134       errno = 0;
135       ASSERT (dup3 (fd, 10000000, o_flags) == -1);
136       ASSERT (errno == EBADF);
137
138       /* Using dup3 can skip fds.  */
139       ASSERT (dup3 (fd, fd + 2, o_flags) == fd + 2);
140       ASSERT (is_open (fd));
141       ASSERT (!is_open (fd + 1));
142       ASSERT (is_open (fd + 2));
143       if (use_cloexec)
144         ASSERT (is_cloexec (fd + 2));
145       else
146         ASSERT (!is_cloexec (fd + 2));
147
148       /* Verify that dup3 closes the previous occupant of a fd.  */
149       ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
150       ASSERT (dup3 (fd + 1, fd, o_flags) == fd);
151       ASSERT (close (fd + 1) == 0);
152       ASSERT (write (fd, "1", 1) == 1);
153       ASSERT (dup3 (fd + 2, fd, o_flags) == fd);
154       ASSERT (lseek (fd, 0, SEEK_END) == 0);
155       ASSERT (write (fd + 2, "2", 1) == 1);
156       ASSERT (lseek (fd, 0, SEEK_SET) == 0);
157       ASSERT (read (fd, buffer, 1) == 1);
158       ASSERT (*buffer == '2');
159
160       /* Clean up.  */
161       ASSERT (close (fd + 2) == 0);
162       ASSERT (close (fd) == 0);
163       ASSERT (unlink (file) == 0);
164     }
165
166   return 0;
167 }