Mercurial > hg > octave-kai > gnulib-hg
annotate tests/test-dup2.c @ 12391:5d8d7c606ce6
test-dup2: enhance test
Ensure that dup2(cloexec_fd, target) returns an inheritable fd.
* modules/dup2-tests (Depends-on): Add cloexec.
* tests/test-dup2.c (main): Enhance test.
Signed-off-by: Eric Blake <ebb9@byu.net>
author | Eric Blake <ebb9@byu.net> |
---|---|
date | Sat, 05 Dec 2009 06:19:01 -0700 |
parents | e45d9bb2233e |
children | 8700bea78e67 |
rev | line source |
---|---|
11727 | 1 /* Test duplicating 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 <unistd.h> | |
22 | |
23 #include <errno.h> | |
24 #include <fcntl.h> | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 | |
12391 | 28 #include "cloexec.h" |
29 | |
11727 | 30 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
31 /* Get declarations of the Win32 API functions. */ | |
32 # define WIN32_LEAN_AND_MEAN | |
33 # include <windows.h> | |
34 #endif | |
35 | |
36 #define ASSERT(expr) \ | |
12391 | 37 do \ |
38 { \ | |
39 if (!(expr)) \ | |
40 { \ | |
11727 | 41 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ |
12391 | 42 fflush (stderr); \ |
43 abort (); \ | |
44 } \ | |
45 } \ | |
11727 | 46 while (0) |
47 | |
48 /* Return non-zero if FD is open. */ | |
49 static int | |
50 is_open (int fd) | |
51 { | |
52 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
53 /* On Win32, the initial state of unassigned standard file | |
54 descriptors is that they are open but point to an | |
55 INVALID_HANDLE_VALUE, and there is no fcntl. */ | |
56 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE; | |
57 #else | |
58 # ifndef F_GETFL | |
59 # error Please port fcntl to your platform | |
60 # endif | |
61 return 0 <= fcntl (fd, F_GETFL); | |
62 #endif | |
63 } | |
64 | |
12391 | 65 /* Return non-zero if FD is open and inheritable across exec/spawn. */ |
66 static int | |
67 is_inheritable (int fd) | |
68 { | |
69 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
70 /* On Win32, the initial state of unassigned standard file | |
71 descriptors is that they are open but point to an | |
72 INVALID_HANDLE_VALUE, and there is no fcntl. */ | |
73 HANDLE h = (HANDLE) _get_osfhandle (fd); | |
74 DWORD flags; | |
75 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0) | |
76 return 0; | |
77 return (flags & HANDLE_FLAG_INHERIT) != 0; | |
78 #else | |
79 # ifndef F_GETFD | |
80 # error Please port fcntl to your platform | |
81 # endif | |
82 int i = fcntl (fd, F_GETFD); | |
83 return 0 <= i && (i & FD_CLOEXEC) == 0; | |
84 #endif | |
85 } | |
86 | |
11727 | 87 int |
12197
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
12154
diff
changeset
|
88 main (void) |
11727 | 89 { |
90 const char *file = "test-dup2.tmp"; | |
91 char buffer[1]; | |
11885 | 92 int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600); |
11727 | 93 |
11825 | 94 /* Assume std descriptors were provided by invoker. */ |
95 ASSERT (STDERR_FILENO < fd); | |
11727 | 96 ASSERT (is_open (fd)); |
11825 | 97 /* Ignore any other fd's leaked into this process. */ |
98 close (fd + 1); | |
99 close (fd + 2); | |
11727 | 100 ASSERT (!is_open (fd + 1)); |
101 ASSERT (!is_open (fd + 2)); | |
102 | |
103 /* Assigning to self must be a no-op. */ | |
104 ASSERT (dup2 (fd, fd) == fd); | |
105 ASSERT (is_open (fd)); | |
106 | |
12154 | 107 /* The source must be valid. */ |
108 errno = 0; | |
109 ASSERT (dup2 (-1, fd) == -1); | |
110 ASSERT (errno == EBADF); | |
111 errno = 0; | |
112 ASSERT (dup2 (AT_FDCWD, fd) == -1); | |
113 ASSERT (errno == EBADF); | |
114 ASSERT (is_open (fd)); | |
115 | |
11727 | 116 /* If the source is not open, then the destination is unaffected. */ |
117 errno = 0; | |
118 ASSERT (dup2 (fd + 1, fd + 1) == -1); | |
119 ASSERT (errno == EBADF); | |
120 ASSERT (!is_open (fd + 1)); | |
121 errno = 0; | |
122 ASSERT (dup2 (fd + 1, fd) == -1); | |
123 ASSERT (errno == EBADF); | |
124 ASSERT (is_open (fd)); | |
125 | |
126 /* The destination must be valid. */ | |
127 errno = 0; | |
128 ASSERT (dup2 (fd, -2) == -1); | |
129 ASSERT (errno == EBADF); | |
11885 | 130 errno = 0; |
131 ASSERT (dup2 (fd, 10000000) == -1); | |
132 ASSERT (errno == EBADF); | |
11727 | 133 |
134 /* Using dup2 can skip fds. */ | |
135 ASSERT (dup2 (fd, fd + 2) == fd + 2); | |
136 ASSERT (is_open (fd)); | |
137 ASSERT (!is_open (fd + 1)); | |
138 ASSERT (is_open (fd + 2)); | |
139 | |
11885 | 140 /* Verify that dup2 closes the previous occupant of a fd. */ |
11727 | 141 ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1); |
142 ASSERT (dup2 (fd + 1, fd) == fd); | |
143 ASSERT (close (fd + 1) == 0); | |
144 ASSERT (write (fd, "1", 1) == 1); | |
145 ASSERT (dup2 (fd + 2, fd) == fd); | |
11885 | 146 ASSERT (lseek (fd, 0, SEEK_END) == 0); |
11727 | 147 ASSERT (write (fd + 2, "2", 1) == 1); |
11885 | 148 ASSERT (lseek (fd, 0, SEEK_SET) == 0); |
11727 | 149 ASSERT (read (fd, buffer, 1) == 1); |
150 ASSERT (*buffer == '2'); | |
151 | |
12391 | 152 /* Any new fd created by dup2 must not be cloexec. */ |
153 ASSERT (close (fd + 2) == 0); | |
154 ASSERT (dup_cloexec (fd) == fd + 1); | |
155 ASSERT (!is_inheritable (fd + 1)); | |
156 ASSERT (dup2 (fd + 1, fd + 1) == fd + 1); | |
157 ASSERT (!is_inheritable (fd + 1)); | |
158 ASSERT (dup2 (fd + 1, fd + 2) == fd + 2); | |
159 ASSERT (is_inheritable (fd + 2)); | |
160 | |
11727 | 161 /* Clean up. */ |
162 ASSERT (close (fd + 2) == 0); | |
12391 | 163 ASSERT (close (fd + 1) == 0); |
11727 | 164 ASSERT (close (fd) == 0); |
165 ASSERT (unlink (file) == 0); | |
166 | |
167 return 0; | |
168 } |