t_pipe2.c revision 1.10 1 1.10 kre /* $NetBSD: t_pipe2.c,v 1.10 2025/07/17 19:50:40 kre Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos #include <sys/cdefs.h>
39 1.10 kre __RCSID("$NetBSD: t_pipe2.c,v 1.10 2025/07/17 19:50:40 kre Exp $");
40 1.1 christos
41 1.1 christos #include <atf-c.h>
42 1.1 christos #include <fcntl.h>
43 1.1 christos #include <unistd.h>
44 1.1 christos #include <stdlib.h>
45 1.1 christos #include <errno.h>
46 1.5 martin #include <sys/resource.h>
47 1.1 christos
48 1.10 kre #ifndef FD_CLOFORK
49 1.10 kre #define FD_CLOFORK 0
50 1.10 kre #endif
51 1.10 kre #ifndef O_CLOFORK
52 1.10 kre #define O_CLOFORK 0
53 1.10 kre #endif
54 1.10 kre
55 1.1 christos static void
56 1.1 christos run(int flags)
57 1.1 christos {
58 1.1 christos int fd[2], i;
59 1.1 christos
60 1.2 christos while ((i = open("/", O_RDONLY)) < 3)
61 1.1 christos ATF_REQUIRE(i != -1);
62 1.1 christos
63 1.9 christos ATF_REQUIRE_MSG(closefrom(3) != -1, "closefrom failed: %s",
64 1.9 christos strerror(errno));
65 1.1 christos
66 1.2 christos ATF_REQUIRE(pipe2(fd, flags) == 0);
67 1.1 christos
68 1.1 christos ATF_REQUIRE(fd[0] == 3);
69 1.1 christos ATF_REQUIRE(fd[1] == 4);
70 1.1 christos
71 1.1 christos if (flags & O_CLOEXEC) {
72 1.1 christos ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOEXEC) != 0);
73 1.1 christos ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOEXEC) != 0);
74 1.1 christos } else {
75 1.1 christos ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOEXEC) == 0);
76 1.1 christos ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOEXEC) == 0);
77 1.1 christos }
78 1.1 christos
79 1.10 kre if (flags & O_CLOFORK) {
80 1.10 kre ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOFORK) != 0);
81 1.10 kre ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOFORK) != 0);
82 1.10 kre } else {
83 1.10 kre ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOFORK) == 0);
84 1.10 kre ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOFORK) == 0);
85 1.10 kre }
86 1.10 kre
87 1.2 christos if (flags & O_NONBLOCK) {
88 1.1 christos ATF_REQUIRE((fcntl(fd[0], F_GETFL) & O_NONBLOCK) != 0);
89 1.1 christos ATF_REQUIRE((fcntl(fd[1], F_GETFL) & O_NONBLOCK) != 0);
90 1.1 christos } else {
91 1.1 christos ATF_REQUIRE((fcntl(fd[0], F_GETFL) & O_NONBLOCK) == 0);
92 1.1 christos ATF_REQUIRE((fcntl(fd[1], F_GETFL) & O_NONBLOCK) == 0);
93 1.1 christos }
94 1.1 christos
95 1.3 christos if (flags & O_NOSIGPIPE) {
96 1.3 christos ATF_REQUIRE(fcntl(fd[0], F_GETNOSIGPIPE) != 0);
97 1.3 christos ATF_REQUIRE(fcntl(fd[1], F_GETNOSIGPIPE) != 0);
98 1.3 christos } else {
99 1.3 christos ATF_REQUIRE(fcntl(fd[0], F_GETNOSIGPIPE) == 0);
100 1.3 christos ATF_REQUIRE(fcntl(fd[1], F_GETNOSIGPIPE) == 0);
101 1.3 christos }
102 1.3 christos
103 1.1 christos ATF_REQUIRE(close(fd[0]) != -1);
104 1.1 christos ATF_REQUIRE(close(fd[1]) != -1);
105 1.1 christos }
106 1.1 christos
107 1.1 christos ATF_TC(pipe2_basic);
108 1.1 christos ATF_TC_HEAD(pipe2_basic, tc)
109 1.1 christos {
110 1.1 christos atf_tc_set_md_var(tc, "descr", "A basic test of pipe2(2)");
111 1.1 christos }
112 1.1 christos
113 1.1 christos ATF_TC_BODY(pipe2_basic, tc)
114 1.1 christos {
115 1.1 christos run(0);
116 1.1 christos }
117 1.1 christos
118 1.4 jruoho ATF_TC(pipe2_consume);
119 1.4 jruoho ATF_TC_HEAD(pipe2_consume, tc)
120 1.4 jruoho {
121 1.4 jruoho atf_tc_set_md_var(tc, "descr", "Test that consuming file descriptors "
122 1.4 jruoho "with pipe2(2) does not crash the system (PR kern/46457)");
123 1.4 jruoho }
124 1.4 jruoho
125 1.4 jruoho ATF_TC_BODY(pipe2_consume, tc)
126 1.4 jruoho {
127 1.5 martin struct rlimit rl;
128 1.7 martin int err, filedes[2];
129 1.9 christos int old;
130 1.4 jruoho
131 1.9 christos ATF_REQUIRE_MSG(closefrom(4) != -1, "closefrom failed: %s",
132 1.9 christos strerror(errno));
133 1.7 martin
134 1.7 martin err = getrlimit(RLIMIT_NOFILE, &rl);
135 1.7 martin ATF_REQUIRE(err == 0);
136 1.5 martin /*
137 1.7 martin * The heart of this test is to run against the number of open
138 1.7 martin * file descriptor limit in the middle of a pipe2() call - i.e.
139 1.7 martin * before the call only a single descriptor may be openend.
140 1.5 martin */
141 1.9 christos old = rl.rlim_cur;
142 1.7 martin rl.rlim_cur = 4;
143 1.7 martin err = setrlimit(RLIMIT_NOFILE, &rl);
144 1.7 martin ATF_REQUIRE(err == 0);
145 1.5 martin
146 1.7 martin err = pipe2(filedes, O_CLOEXEC);
147 1.7 martin ATF_REQUIRE(err == -1);
148 1.9 christos rl.rlim_cur = old;
149 1.9 christos err = setrlimit(RLIMIT_NOFILE, &rl);
150 1.4 jruoho }
151 1.4 jruoho
152 1.1 christos ATF_TC(pipe2_nonblock);
153 1.1 christos ATF_TC_HEAD(pipe2_nonblock, tc)
154 1.1 christos {
155 1.1 christos atf_tc_set_md_var(tc, "descr", "A non-blocking test of pipe2(2)");
156 1.1 christos }
157 1.1 christos
158 1.1 christos ATF_TC_BODY(pipe2_nonblock, tc)
159 1.1 christos {
160 1.1 christos run(O_NONBLOCK);
161 1.1 christos }
162 1.1 christos
163 1.1 christos ATF_TC(pipe2_cloexec);
164 1.1 christos ATF_TC_HEAD(pipe2_cloexec, tc)
165 1.1 christos {
166 1.3 christos atf_tc_set_md_var(tc, "descr", "A close-on-exec test of pipe2(2)");
167 1.1 christos }
168 1.1 christos
169 1.1 christos ATF_TC_BODY(pipe2_cloexec, tc)
170 1.1 christos {
171 1.1 christos run(O_CLOEXEC);
172 1.1 christos }
173 1.1 christos
174 1.10 kre ATF_TC(pipe2_clofork);
175 1.10 kre ATF_TC_HEAD(pipe2_clofork, tc)
176 1.10 kre {
177 1.10 kre atf_tc_set_md_var(tc, "descr", "A close-on-fork test of pipe2(2)");
178 1.10 kre }
179 1.10 kre
180 1.10 kre ATF_TC_BODY(pipe2_clofork, tc)
181 1.10 kre {
182 1.10 kre #if defined(O_CLOFORK) && O_CLOFORK != 0
183 1.10 kre run(O_CLOFORK);
184 1.10 kre #else
185 1.10 kre atf_tc_skip("O_CLOFORK not yet implemented");
186 1.10 kre #endif
187 1.10 kre }
188 1.10 kre
189 1.3 christos ATF_TC(pipe2_nosigpipe);
190 1.3 christos ATF_TC_HEAD(pipe2_nosigpipe, tc)
191 1.3 christos {
192 1.3 christos atf_tc_set_md_var(tc, "descr", "A no sigpipe test of pipe2(2)");
193 1.3 christos }
194 1.3 christos
195 1.3 christos ATF_TC_BODY(pipe2_nosigpipe, tc)
196 1.3 christos {
197 1.3 christos run(O_NOSIGPIPE);
198 1.3 christos }
199 1.3 christos
200 1.1 christos ATF_TC(pipe2_einval);
201 1.1 christos ATF_TC_HEAD(pipe2_einval, tc)
202 1.1 christos {
203 1.1 christos atf_tc_set_md_var(tc, "descr", "A error check of pipe2(2)");
204 1.1 christos }
205 1.1 christos
206 1.1 christos ATF_TC_BODY(pipe2_einval, tc)
207 1.1 christos {
208 1.1 christos int fd[2];
209 1.1 christos ATF_REQUIRE_ERRNO(EINVAL, pipe2(fd, O_ASYNC) == -1);
210 1.1 christos }
211 1.1 christos
212 1.1 christos ATF_TP_ADD_TCS(tp)
213 1.1 christos {
214 1.1 christos
215 1.1 christos ATF_TP_ADD_TC(tp, pipe2_basic);
216 1.4 jruoho ATF_TP_ADD_TC(tp, pipe2_consume);
217 1.1 christos ATF_TP_ADD_TC(tp, pipe2_nonblock);
218 1.1 christos ATF_TP_ADD_TC(tp, pipe2_cloexec);
219 1.10 kre ATF_TP_ADD_TC(tp, pipe2_clofork);
220 1.3 christos ATF_TP_ADD_TC(tp, pipe2_nosigpipe);
221 1.1 christos ATF_TP_ADD_TC(tp, pipe2_einval);
222 1.1 christos
223 1.1 christos return atf_no_error();
224 1.1 christos }
225