maint: update copyright
[gnulib.git] / tests / test-cloexec.c
1 /* Test duplicating non-inheritable file descriptors.
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 "cloexec.h"
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26
27 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
28 /* Get declarations of the native Windows API functions.  */
29 # define WIN32_LEAN_AND_MEAN
30 # include <windows.h>
31 /* Get _get_osfhandle.  */
32 # include "msvc-nothrow.h"
33 #endif
34
35 #include "binary-io.h"
36 #include "macros.h"
37
38 /* Return non-zero if FD is open and inheritable across exec/spawn.  */
39 static int
40 is_inheritable (int fd)
41 {
42 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
43   /* On native Windows, the initial state of unassigned standard file
44      descriptors is that they are open but point to an
45      INVALID_HANDLE_VALUE, and there is no fcntl.  */
46   HANDLE h = (HANDLE) _get_osfhandle (fd);
47   DWORD flags;
48   if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
49     return 0;
50   return (flags & HANDLE_FLAG_INHERIT) != 0;
51 #else
52 # ifndef F_GETFD
53 #  error Please port fcntl to your platform
54 # endif
55   int i = fcntl (fd, F_GETFD);
56   return 0 <= i && (i & FD_CLOEXEC) == 0;
57 #endif
58 }
59
60 #if !O_BINARY
61 # define setmode(f,m) zero ()
62 static int zero (void) { return 0; }
63 #endif
64
65 /* Return non-zero if FD is open in the given MODE, which is either
66    O_TEXT or O_BINARY.  */
67 static int
68 is_mode (int fd, int mode)
69 {
70   int value = setmode (fd, O_BINARY);
71   setmode (fd, value);
72   return mode == value;
73 }
74
75 int
76 main (void)
77 {
78   const char *file = "test-cloexec.tmp";
79   int fd = creat (file, 0600);
80   int fd2;
81   int bad_fd = getdtablesize ();
82
83   /* Assume std descriptors were provided by invoker.  */
84   ASSERT (STDERR_FILENO < fd);
85   ASSERT (is_inheritable (fd));
86
87   /* Normal use of set_cloexec_flag.  */
88   ASSERT (set_cloexec_flag (fd, true) == 0);
89 #if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
90   ASSERT (!is_inheritable (fd));
91 #endif
92   ASSERT (set_cloexec_flag (fd, false) == 0);
93   ASSERT (is_inheritable (fd));
94
95   /* Normal use of dup_cloexec.  */
96   fd2 = dup_cloexec (fd);
97   ASSERT (fd < fd2);
98   ASSERT (!is_inheritable (fd2));
99   ASSERT (close (fd) == 0);
100   ASSERT (dup_cloexec (fd2) == fd);
101   ASSERT (!is_inheritable (fd));
102   ASSERT (close (fd2) == 0);
103
104   /* On systems that distinguish between text and binary mode,
105      dup_cloexec reuses the mode of the source.  */
106   setmode (fd, O_BINARY);
107   ASSERT (is_mode (fd, O_BINARY));
108   fd2 = dup_cloexec (fd);
109   ASSERT (fd < fd2);
110   ASSERT (is_mode (fd2, O_BINARY));
111   ASSERT (close (fd2) == 0);
112   setmode (fd, O_TEXT);
113   ASSERT (is_mode (fd, O_TEXT));
114   fd2 = dup_cloexec (fd);
115   ASSERT (fd < fd2);
116   ASSERT (is_mode (fd2, O_TEXT));
117   ASSERT (close (fd2) == 0);
118
119   /* Test error handling.  */
120   errno = 0;
121   ASSERT (set_cloexec_flag (-1, false) == -1);
122   ASSERT (errno == EBADF);
123   errno = 0;
124   ASSERT (set_cloexec_flag (bad_fd, false) == -1);
125   ASSERT (errno == EBADF);
126   errno = 0;
127   ASSERT (set_cloexec_flag (fd2, false) == -1);
128   ASSERT (errno == EBADF);
129   errno = 0;
130   ASSERT (dup_cloexec (-1) == -1);
131   ASSERT (errno == EBADF);
132   errno = 0;
133   ASSERT (dup_cloexec (bad_fd) == -1);
134   ASSERT (errno == EBADF);
135   errno = 0;
136   ASSERT (dup_cloexec (fd2) == -1);
137   ASSERT (errno == EBADF);
138
139   /* Clean up.  */
140   ASSERT (close (fd) == 0);
141   ASSERT (unlink (file) == 0);
142
143   return 0;
144 }