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