maint: update copyright
[gnulib.git] / tests / test-dup-safer.c
1 /* Test that dup_safer leaves standard fds alone.
2    Copyright (C) 2009-2014 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
19 #include <config.h>
20
21 #include "unistd--.h"
22
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <unistd.h>
28
29 #include "binary-io.h"
30 #include "cloexec.h"
31
32 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
33 /* Get declarations of the native Windows API functions.  */
34 # define WIN32_LEAN_AND_MEAN
35 # include <windows.h>
36 /* Get _get_osfhandle.  */
37 # include "msvc-nothrow.h"
38 #endif
39
40 #if !O_BINARY
41 # define setmode(f,m) zero ()
42 static int zero (void) { return 0; }
43 #endif
44
45 /* This test intentionally closes stderr.  So, we arrange to have fd 10
46    (outside the range of interesting fd's during the test) set up to
47    duplicate the original stderr.  */
48
49 #define BACKUP_STDERR_FILENO 10
50 #define ASSERT_STREAM myerr
51 #include "macros.h"
52
53 static FILE *myerr;
54
55 /* Return true if FD is open.  */
56 static bool
57 is_open (int fd)
58 {
59 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
60   /* On native Windows, the initial state of unassigned standard file
61      descriptors is that they are open but point to an
62      INVALID_HANDLE_VALUE, and there is no fcntl.  */
63   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
64 #else
65 # ifndef F_GETFL
66 #  error Please port fcntl to your platform
67 # endif
68   return 0 <= fcntl (fd, F_GETFL);
69 #endif
70 }
71
72 /* Return true if FD is open and inheritable across exec/spawn.  */
73 static bool
74 is_inheritable (int fd)
75 {
76 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
77   /* On native Windows, the initial state of unassigned standard file
78      descriptors is that they are open but point to an
79      INVALID_HANDLE_VALUE, and there is no fcntl.  */
80   HANDLE h = (HANDLE) _get_osfhandle (fd);
81   DWORD flags;
82   if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
83     return 0;
84   return (flags & HANDLE_FLAG_INHERIT) != 0;
85 #else
86 # ifndef F_GETFD
87 #  error Please port fcntl to your platform
88 # endif
89   int i = fcntl (fd, F_GETFD);
90   return 0 <= i && (i & FD_CLOEXEC) == 0;
91 #endif
92 }
93
94 /* Return true if FD is open in the given MODE, which is either
95    O_TEXT or O_BINARY.  */
96 static bool
97 is_mode (int fd, int mode)
98 {
99   int value = setmode (fd, O_BINARY);
100   setmode (fd, value);
101   return mode == value;
102 }
103
104 #define witness "test-dup-safer.txt"
105
106 int
107 main (void)
108 {
109   int i;
110   int fd;
111   int bad_fd = getdtablesize ();
112
113   /* We close fd 2 later, so save it in fd 10.  */
114   if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
115       || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
116     return 2;
117
118   /* Create file for later checks.  */
119   fd = creat (witness, 0600);
120   ASSERT (STDERR_FILENO < fd);
121
122   /* Four iterations, with progressively more standard descriptors
123      closed.  */
124   for (i = -1; i <= STDERR_FILENO; i++)
125     {
126       if (0 <= i)
127         ASSERT (close (i) == 0);
128
129       /* Detect errors.  */
130       errno = 0;
131       ASSERT (dup (-1) == -1);
132       ASSERT (errno == EBADF);
133       errno = 0;
134       ASSERT (dup (bad_fd) == -1);
135       ASSERT (errno == EBADF);
136       close (fd + 1);
137       errno = 0;
138       ASSERT (dup (fd + 1) == -1);
139       ASSERT (errno == EBADF);
140
141       /* Preserve text vs. binary.  */
142       setmode (fd, O_BINARY);
143       ASSERT (dup (fd) == fd + 1);
144       ASSERT (is_open (fd + 1));
145       ASSERT (is_inheritable (fd + 1));
146       ASSERT (is_mode (fd + 1, O_BINARY));
147
148       ASSERT (close (fd + 1) == 0);
149       setmode (fd, O_TEXT);
150       ASSERT (dup (fd) == fd + 1);
151       ASSERT (is_open (fd + 1));
152       ASSERT (is_inheritable (fd + 1));
153       ASSERT (is_mode (fd + 1, O_TEXT));
154
155       /* Create cloexec copy.  */
156       ASSERT (close (fd + 1) == 0);
157       ASSERT (fd_safer_flag (dup_cloexec (fd), O_CLOEXEC) == fd + 1);
158       ASSERT (set_cloexec_flag (fd + 1, true) == 0);
159       ASSERT (is_open (fd + 1));
160       ASSERT (!is_inheritable (fd + 1));
161       ASSERT (close (fd) == 0);
162
163       /* dup always creates inheritable copies.  Also, check that
164          earliest slot past std fds is used.  */
165       ASSERT (dup (fd + 1) == fd);
166       ASSERT (is_open (fd));
167       ASSERT (is_inheritable (fd));
168       ASSERT (close (fd + 1) == 0);
169     }
170
171   /* Cleanup.  */
172   ASSERT (close (fd) == 0);
173   ASSERT (unlink (witness) == 0);
174
175   return 0;
176 }