VirtualBox

source: kBuild/vendor/grep/2.12/gnulib-tests/test-cloexec.c@ 3627

Last change on this file since 3627 was 2595, checked in by bird, 13 years ago

gnu grep version 2.12 (grep-2.12.tar.xz, md5sum=8d2f0346d08b13c18afb81f0e8aa1e2f)

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1/* Test duplicating non-inheritable file descriptors.
2 Copyright (C) 2009-2012 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. */
39static int
40is_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 ()
62static 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. */
67static int
68is_mode (int fd, int mode)
69{
70 int value = setmode (fd, O_BINARY);
71 setmode (fd, value);
72 return mode == value;
73}
74
75int
76main (void)
77{
78 const char *file = "test-cloexec.tmp";
79 int fd = creat (file, 0600);
80 int fd2;
81
82 /* Assume std descriptors were provided by invoker. */
83 ASSERT (STDERR_FILENO < fd);
84 ASSERT (is_inheritable (fd));
85
86 /* Normal use of set_cloexec_flag. */
87 ASSERT (set_cloexec_flag (fd, true) == 0);
88#if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
89 ASSERT (!is_inheritable (fd));
90#endif
91 ASSERT (set_cloexec_flag (fd, false) == 0);
92 ASSERT (is_inheritable (fd));
93
94 /* Normal use of dup_cloexec. */
95 fd2 = dup_cloexec (fd);
96 ASSERT (fd < fd2);
97 ASSERT (!is_inheritable (fd2));
98 ASSERT (close (fd) == 0);
99 ASSERT (dup_cloexec (fd2) == fd);
100 ASSERT (!is_inheritable (fd));
101 ASSERT (close (fd2) == 0);
102
103 /* On systems that distinguish between text and binary mode,
104 dup_cloexec reuses the mode of the source. */
105 setmode (fd, O_BINARY);
106 ASSERT (is_mode (fd, O_BINARY));
107 fd2 = dup_cloexec (fd);
108 ASSERT (fd < fd2);
109 ASSERT (is_mode (fd2, O_BINARY));
110 ASSERT (close (fd2) == 0);
111 setmode (fd, O_TEXT);
112 ASSERT (is_mode (fd, O_TEXT));
113 fd2 = dup_cloexec (fd);
114 ASSERT (fd < fd2);
115 ASSERT (is_mode (fd2, O_TEXT));
116 ASSERT (close (fd2) == 0);
117
118 /* Test error handling. */
119 errno = 0;
120 ASSERT (set_cloexec_flag (-1, false) == -1);
121 ASSERT (errno == EBADF);
122 errno = 0;
123 ASSERT (set_cloexec_flag (10000000, false) == -1);
124 ASSERT (errno == EBADF);
125 errno = 0;
126 ASSERT (set_cloexec_flag (fd2, false) == -1);
127 ASSERT (errno == EBADF);
128 errno = 0;
129 ASSERT (dup_cloexec (-1) == -1);
130 ASSERT (errno == EBADF);
131 errno = 0;
132 ASSERT (dup_cloexec (10000000) == -1);
133 ASSERT (errno == EBADF);
134 errno = 0;
135 ASSERT (dup_cloexec (fd2) == -1);
136 ASSERT (errno == EBADF);
137
138 /* Clean up. */
139 ASSERT (close (fd) == 0);
140 ASSERT (unlink (file) == 0);
141
142 return 0;
143}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette