annotate tests/test-dup-safer.c @ 40196:e63f5d3edab5

relocatable-prog: Update documentation. * doc/relocatable-maint.texi (Supporting Relocation): Update to match the recent changes.
author Bruno Haible <bruno@clisp.org>
date Sun, 24 Feb 2019 01:49:15 +0100
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12399
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* Test that dup_safer leaves standard fds alone.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19595
diff changeset
2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
12399
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
7 (at your option) any later version.
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 GNU General Public License for more details.
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18883
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
12399
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19 #include <config.h>
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21 #include "unistd--.h"
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
22
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
23 #include <fcntl.h>
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
24 #include <errno.h>
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
25 #include <stdbool.h>
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
26 #include <stdio.h>
17373
160b88fd4691 tests: don't assume getdtablesize () <= 10000000
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
27 #include <unistd.h>
12399
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
28
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
29 #include "binary-io.h"
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
30 #include "cloexec.h"
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
31
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
32 #if defined _WIN32 && ! defined __CYGWIN__
16214
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
33 /* Get declarations of the native Windows API functions. */
12399
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
34 # define WIN32_LEAN_AND_MEAN
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
35 # include <windows.h>
15752
b86e9061a6d0 New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
36 /* Get _get_osfhandle. */
18883
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
37 # if GNULIB_MSVC_NOTHROW
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
38 # include "msvc-nothrow.h"
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
39 # else
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
40 # include <io.h>
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
41 # endif
12399
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42 #endif
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
43
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
44 #if !O_BINARY
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
45 # define setmode(f,m) zero ()
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
46 static int zero (void) { return 0; }
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
47 #endif
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
48
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
49 /* This test intentionally closes stderr. So, we arrange to have fd 10
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
50 (outside the range of interesting fd's during the test) set up to
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
51 duplicate the original stderr. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
52
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
53 #define BACKUP_STDERR_FILENO 10
12510
618282a72d9b tests: use macros.h in more places
Eric Blake <ebb9@byu.net>
parents: 12399
diff changeset
54 #define ASSERT_STREAM myerr
618282a72d9b tests: use macros.h in more places
Eric Blake <ebb9@byu.net>
parents: 12399
diff changeset
55 #include "macros.h"
12399
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
56
12510
618282a72d9b tests: use macros.h in more places
Eric Blake <ebb9@byu.net>
parents: 12399
diff changeset
57 static FILE *myerr;
12399
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
58
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
59 /* Return true if FD is open. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
60 static bool
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
61 is_open (int fd)
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
62 {
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
63 #if defined _WIN32 && ! defined __CYGWIN__
16214
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
64 /* On native Windows, the initial state of unassigned standard file
12399
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
65 descriptors is that they are open but point to an
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
66 INVALID_HANDLE_VALUE, and there is no fcntl. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
67 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
68 #else
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
69 # ifndef F_GETFL
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
70 # error Please port fcntl to your platform
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
71 # endif
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
72 return 0 <= fcntl (fd, F_GETFL);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
73 #endif
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
74 }
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
75
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
76 /* Return true if FD is open and inheritable across exec/spawn. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
77 static bool
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
78 is_inheritable (int fd)
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
79 {
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
80 #if defined _WIN32 && ! defined __CYGWIN__
16214
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
81 /* On native Windows, the initial state of unassigned standard file
12399
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
82 descriptors is that they are open but point to an
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
83 INVALID_HANDLE_VALUE, and there is no fcntl. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
84 HANDLE h = (HANDLE) _get_osfhandle (fd);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
85 DWORD flags;
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
86 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
87 return 0;
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
88 return (flags & HANDLE_FLAG_INHERIT) != 0;
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
89 #else
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
90 # ifndef F_GETFD
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
91 # error Please port fcntl to your platform
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
92 # endif
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
93 int i = fcntl (fd, F_GETFD);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
94 return 0 <= i && (i & FD_CLOEXEC) == 0;
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
95 #endif
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
96 }
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
97
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
98 /* Return true if FD is open in the given MODE, which is either
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
99 O_TEXT or O_BINARY. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
100 static bool
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
101 is_mode (int fd, int mode)
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
102 {
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
103 int value = setmode (fd, O_BINARY);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
104 setmode (fd, value);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
105 return mode == value;
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
106 }
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
107
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
108 #define witness "test-dup-safer.txt"
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
109
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
110 int
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
111 main (void)
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
112 {
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
113 int i;
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
114 int fd;
17373
160b88fd4691 tests: don't assume getdtablesize () <= 10000000
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
115 int bad_fd = getdtablesize ();
12399
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
116
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
117 /* We close fd 2 later, so save it in fd 10. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
118 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
119 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
120 return 2;
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
121
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
122 /* Create file for later checks. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
123 fd = creat (witness, 0600);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
124 ASSERT (STDERR_FILENO < fd);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
125
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
126 /* Four iterations, with progressively more standard descriptors
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
127 closed. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
128 for (i = -1; i <= STDERR_FILENO; i++)
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
129 {
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
130 if (0 <= i)
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
131 ASSERT (close (i) == 0);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
132
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
133 /* Detect errors. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
134 errno = 0;
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
135 ASSERT (dup (-1) == -1);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
136 ASSERT (errno == EBADF);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
137 errno = 0;
17373
160b88fd4691 tests: don't assume getdtablesize () <= 10000000
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
138 ASSERT (dup (bad_fd) == -1);
12399
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
139 ASSERT (errno == EBADF);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
140 close (fd + 1);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
141 errno = 0;
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
142 ASSERT (dup (fd + 1) == -1);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
143 ASSERT (errno == EBADF);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
144
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
145 /* Preserve text vs. binary. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
146 setmode (fd, O_BINARY);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
147 ASSERT (dup (fd) == fd + 1);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
148 ASSERT (is_open (fd + 1));
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
149 ASSERT (is_inheritable (fd + 1));
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
150 ASSERT (is_mode (fd + 1, O_BINARY));
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
151
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
152 ASSERT (close (fd + 1) == 0);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
153 setmode (fd, O_TEXT);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
154 ASSERT (dup (fd) == fd + 1);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
155 ASSERT (is_open (fd + 1));
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
156 ASSERT (is_inheritable (fd + 1));
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
157 ASSERT (is_mode (fd + 1, O_TEXT));
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
158
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
159 /* Create cloexec copy. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
160 ASSERT (close (fd + 1) == 0);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
161 ASSERT (fd_safer_flag (dup_cloexec (fd), O_CLOEXEC) == fd + 1);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
162 ASSERT (set_cloexec_flag (fd + 1, true) == 0);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
163 ASSERT (is_open (fd + 1));
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
164 ASSERT (!is_inheritable (fd + 1));
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
165 ASSERT (close (fd) == 0);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
166
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
167 /* dup always creates inheritable copies. Also, check that
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
168 earliest slot past std fds is used. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
169 ASSERT (dup (fd + 1) == fd);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
170 ASSERT (is_open (fd));
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
171 ASSERT (is_inheritable (fd));
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
172 ASSERT (close (fd + 1) == 0);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
173 }
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
174
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
175 /* Cleanup. */
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
176 ASSERT (close (fd) == 0);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
177 ASSERT (unlink (witness) == 0);
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
178
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
179 return 0;
7b2940180b02 unistd-safer: add unit test
Eric Blake <ebb9@byu.net>
parents:
diff changeset
180 }