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