2 * Copyright (c) 2013 Jilles Tjoelker
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Limited test program for popen() as specified by IEEE Std. 1003.1-2008,
29 * with BSD extensions.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
46 static volatile sig_atomic_t got_sigpipe;
49 sigpipe_handler(int sig __unused)
55 check_cloexec(FILE *fp, const char *mode)
59 flags = fcntl(fileno(fp), F_GETFD);
60 ATF_CHECK_MSG(flags != -1, "fcntl(F_GETFD) failed; errno=%d", errno);
63 if (strchr(mode, 'e') != NULL)
64 exp_flags = FD_CLOEXEC;
67 ATF_CHECK_MSG((flags & FD_CLOEXEC) == exp_flags,
68 "bad cloexec flag; %d != %d", flags, exp_flags);
71 ATF_TC_WITHOUT_HEAD(popen_all_modes_test);
72 ATF_TC_BODY(popen_all_modes_test, tc)
77 const char *allmodes[] = { "r", "w", "r+", "re", "we", "r+e", "re+" };
79 for (i = 0; i < nitems(allmodes); i++) {
81 fp = popen("exit 7", mode);
82 ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode);
85 check_cloexec(fp, mode);
87 ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 7,
88 "bad exit status (no I/O)");
92 ATF_TC_WITHOUT_HEAD(popen_rmodes_test);
93 ATF_TC_BODY(popen_rmodes_test, tc)
96 const char *rmodes[] = { "r", "r+", "re", "r+e", "re+" };
101 for (i = 0; i < nitems(rmodes); i++) {
103 fp = popen("exit 9", mode);
104 ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode);
107 check_cloexec(fp, mode);
108 bool input_error_1 = !(fgetc(fp) != EOF || !feof(fp) || !ferror(fp));
109 ATF_CHECK_MSG(!input_error_1, "input error 1");
113 ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 9,
114 "bad exit status (input)");
117 for (i = 0; i < nitems(rmodes); i++) {
120 fp = popen("echo hi there", mode);
121 ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode);
124 check_cloexec(fp, mode);
125 ATF_CHECK_MSG((sres = fgets(buf, sizeof(buf), fp)) != NULL,
128 ATF_CHECK_MSG(strcmp(buf, "hi there\n") == 0,
131 ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
132 "Bad exit status (input)");
136 ATF_TC_WITHOUT_HEAD(popen_wmodes_test);
137 ATF_TC_BODY(popen_wmodes_test, tc)
140 const char *wmodes[] = { "w", "r+", "we", "r+e", "re+" };
142 struct sigaction act, oact;
145 for (i = 0; i < nitems(wmodes); i++) {
147 fp = popen("read x && [ \"$x\" = abcd ]", mode);
148 ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode);
151 check_cloexec(fp, mode);
152 ATF_CHECK_MSG(fputs("abcd\n", fp) != EOF,
155 ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
156 "Bad exit status (output)");
159 act.sa_handler = sigpipe_handler;
160 act.sa_flags = SA_RESTART;
161 sigemptyset(&act.sa_mask);
162 ATF_CHECK_MSG(sigaction(SIGPIPE, &act, &oact) != -1,
163 "sigaction() failed");
164 for (i = 0; i < nitems(wmodes); i++) {
166 fp = popen("exit 88", mode);
167 ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode);
170 check_cloexec(fp, mode);
172 while (fputs("abcd\n", fp) != EOF)
174 ATF_CHECK_MSG(ferror(fp) && errno == EPIPE, "Expected EPIPE");
175 ATF_CHECK_MSG(got_sigpipe, "Expected SIGPIPE");
177 ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 88,
178 "Bad exit status (EPIPE)");
180 ATF_CHECK_MSG(sigaction(SIGPIPE, &oact, NULL) != -1,
181 "sigaction() failed");
183 for (i = 0; i < nitems(wmodes); i++) {
184 for (j = 0; j < nitems(wmodes); j++) {
186 fp = popen("read x", mode);
187 ATF_CHECK_MSG(fp != NULL,
188 "popen(, \"%s\") failed", mode);
192 fp2 = popen("read x", mode);
193 ATF_CHECK_MSG(fp2 != NULL,
194 "popen(, \"%s\") failed", mode);
199 /* If fp2 inherits fp's pipe, we will deadlock here. */
201 ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 1,
202 "bad exit status (2 pipes)");
203 status = pclose(fp2);
204 ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 1,
205 "bad exit status (2 pipes)");
210 ATF_TC_WITHOUT_HEAD(popen_rwmodes_test);
211 ATF_TC_BODY(popen_rwmodes_test, tc)
213 const char *rwmodes[] = { "r+", "r+e", "re+" };
220 for (i = 0; i < nitems(rwmodes); i++) {
222 fp = popen("read x && printf '%s\\n' \"Q${x#a}\"", mode);
223 ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode);
226 check_cloexec(fp, mode);
227 ATF_CHECK_MSG((ires = fputs("abcd\n", fp)) != EOF,
230 sres = fgets(buf, sizeof(buf), fp);
231 ATF_CHECK_MSG(sres != NULL, "Input error 3");
233 ATF_CHECK_MSG(strcmp(buf, "Qbcd\n") == 0,
237 ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
238 "bad exit status (I/O)");
245 ATF_TP_ADD_TC(tp, popen_all_modes_test);
246 ATF_TP_ADD_TC(tp, popen_rmodes_test);
247 ATF_TP_ADD_TC(tp, popen_wmodes_test);
248 ATF_TP_ADD_TC(tp, popen_rwmodes_test);
250 return (atf_no_error());