t_closefrom.c revision 1.5
11.5Sandvar/* $NetBSD: t_closefrom.c,v 1.5 2022/04/12 21:05:37 andvar Exp $ */ 21.1Sjruoho 31.1Sjruoho/*- 41.1Sjruoho * Copyright (c) 2011 The NetBSD Foundation, Inc. 51.1Sjruoho * All rights reserved. 61.1Sjruoho * 71.1Sjruoho * This code is derived from software contributed to The NetBSD Foundation 81.1Sjruoho * by Jukka Ruohonen. 91.1Sjruoho * 101.1Sjruoho * Redistribution and use in source and binary forms, with or without 111.1Sjruoho * modification, are permitted provided that the following conditions 121.1Sjruoho * are met: 131.1Sjruoho * 1. Redistributions of source code must retain the above copyright 141.1Sjruoho * notice, this list of conditions and the following disclaimer. 151.1Sjruoho * 2. Redistributions in binary form must reproduce the above copyright 161.1Sjruoho * notice, this list of conditions and the following disclaimer in the 171.1Sjruoho * documentation and/or other materials provided with the distribution. 181.1Sjruoho * 191.1Sjruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 201.1Sjruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 211.1Sjruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221.1Sjruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 231.1Sjruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 241.1Sjruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 251.1Sjruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 261.1Sjruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 271.1Sjruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 281.1Sjruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 291.1Sjruoho * POSSIBILITY OF SUCH DAMAGE. 301.1Sjruoho */ 311.1Sjruoho#include <sys/cdefs.h> 321.5Sandvar__RCSID("$NetBSD: t_closefrom.c,v 1.5 2022/04/12 21:05:37 andvar Exp $"); 331.4Sjruoho 341.4Sjruoho#include <sys/wait.h> 351.1Sjruoho 361.1Sjruoho#include <atf-c.h> 371.1Sjruoho#include <errno.h> 381.1Sjruoho#include <fcntl.h> 391.1Sjruoho#include <limits.h> 401.1Sjruoho#include <unistd.h> 411.1Sjruoho 421.1Sjruohostatic const char path[] = "closefrom"; 431.1Sjruoho 441.1SjruohoATF_TC_WITH_CLEANUP(closefrom_basic); 451.1SjruohoATF_TC_HEAD(closefrom_basic, tc) 461.1Sjruoho{ 471.1Sjruoho atf_tc_set_md_var(tc, "descr", "A basic test of closefrom(3), #1"); 481.1Sjruoho} 491.1Sjruoho 501.1SjruohoATF_TC_BODY(closefrom_basic, tc) 511.1Sjruoho{ 521.1Sjruoho int fd, cur1, cur2; 531.1Sjruoho 541.4Sjruoho (void)closefrom(STDERR_FILENO + 1); 551.4Sjruoho 561.1Sjruoho fd = open(path, O_RDONLY | O_CREAT, 0400); 571.1Sjruoho ATF_REQUIRE(fd >= 0); 581.1Sjruoho 591.1Sjruoho cur1 = fcntl(0, F_MAXFD); 601.1Sjruoho 611.4Sjruoho ATF_REQUIRE(cur1 == STDERR_FILENO + 1); 621.1Sjruoho ATF_REQUIRE(closefrom(cur1) == 0); 631.1Sjruoho 641.1Sjruoho cur2 = fcntl(0, F_MAXFD); 651.1Sjruoho 661.1Sjruoho ATF_REQUIRE(cur1 - 1 == cur2); 671.3Sjruoho ATF_REQUIRE(close(fd) == -1); 681.1Sjruoho ATF_REQUIRE(unlink(path) == 0); 691.1Sjruoho} 701.1Sjruoho 711.1SjruohoATF_TC_CLEANUP(closefrom_basic, tc) 721.1Sjruoho{ 731.1Sjruoho (void)unlink(path); 741.1Sjruoho} 751.1Sjruoho 761.1SjruohoATF_TC_WITH_CLEANUP(closefrom_buffer); 771.1SjruohoATF_TC_HEAD(closefrom_buffer, tc) 781.1Sjruoho{ 791.1Sjruoho atf_tc_set_md_var(tc, "descr", "A basic test of closefrom(3), #2"); 801.1Sjruoho} 811.1Sjruoho 821.1SjruohoATF_TC_BODY(closefrom_buffer, tc) 831.1Sjruoho{ 841.1Sjruoho int buf[16], cur, half; 851.1Sjruoho size_t i; 861.1Sjruoho 871.1Sjruoho /* 881.1Sjruoho * Open a buffer of descriptors, close the half of 891.1Sjruoho * these and verify that the result is consistent. 901.1Sjruoho */ 911.1Sjruoho ATF_REQUIRE(closefrom(STDERR_FILENO + 1) == 0); 921.1Sjruoho 931.1Sjruoho cur = fcntl(0, F_MAXFD); 941.1Sjruoho ATF_REQUIRE(cur == STDERR_FILENO); 951.1Sjruoho 961.1Sjruoho for (i = 0; i < __arraycount(buf); i++) { 971.1Sjruoho buf[i] = open(path, O_RDWR | O_CREAT, 0600); 981.1Sjruoho ATF_REQUIRE(buf[i] >= 0); 991.1Sjruoho } 1001.1Sjruoho 1011.1Sjruoho cur = fcntl(0, F_MAXFD); 1021.1Sjruoho ATF_REQUIRE(cur == __arraycount(buf) + STDERR_FILENO); 1031.1Sjruoho 1041.1Sjruoho half = STDERR_FILENO + __arraycount(buf) / 2; 1051.1Sjruoho ATF_REQUIRE(closefrom(half) == 0); 1061.1Sjruoho 1071.1Sjruoho cur = fcntl(0, F_MAXFD); 1081.1Sjruoho ATF_REQUIRE(cur == half - 1); 1091.1Sjruoho 1101.1Sjruoho for (i = 0; i < __arraycount(buf); i++) 1111.1Sjruoho (void)close(buf[i]); 1121.1Sjruoho} 1131.1Sjruoho 1141.1SjruohoATF_TC_CLEANUP(closefrom_buffer, tc) 1151.1Sjruoho{ 1161.1Sjruoho (void)unlink(path); 1171.1Sjruoho} 1181.1Sjruoho 1191.1SjruohoATF_TC(closefrom_err); 1201.1SjruohoATF_TC_HEAD(closefrom_err, tc) 1211.1Sjruoho{ 1221.1Sjruoho atf_tc_set_md_var(tc, "descr", "Test errors from closefrom(3)"); 1231.1Sjruoho} 1241.1Sjruoho 1251.1SjruohoATF_TC_BODY(closefrom_err, tc) 1261.1Sjruoho{ 1271.1Sjruoho 1281.1Sjruoho errno = 0; 1291.1Sjruoho ATF_REQUIRE_ERRNO(EBADF, closefrom(-INT_MAX) == -1); 1301.1Sjruoho} 1311.1Sjruoho 1321.4SjruohoATF_TC(closefrom_one); 1331.4SjruohoATF_TC_HEAD(closefrom_one, tc) 1341.4Sjruoho{ 1351.4Sjruoho atf_tc_set_md_var(tc, "descr", "Test closefrom(1)"); 1361.4Sjruoho} 1371.4Sjruoho 1381.4SjruohoATF_TC_BODY(closefrom_one, tc) 1391.4Sjruoho{ 1401.4Sjruoho pid_t pid; 1411.4Sjruoho int sta; 1421.4Sjruoho 1431.4Sjruoho pid = fork(); 1441.4Sjruoho ATF_REQUIRE(pid >= 0); 1451.4Sjruoho 1461.4Sjruoho if (pid == 0) { 1471.4Sjruoho 1481.4Sjruoho if (closefrom(1) != 0) 1491.4Sjruoho _exit(10); 1501.4Sjruoho 1511.4Sjruoho _exit(fcntl(0, F_MAXFD)); 1521.4Sjruoho } 1531.4Sjruoho 1541.4Sjruoho 1551.4Sjruoho (void)wait(&sta); 1561.4Sjruoho 1571.4Sjruoho /* 1581.5Sandvar * STDIN_FILENO should still be open; WEXITSTATUS(1) == 0. 1591.4Sjruoho */ 1601.4Sjruoho if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != 0) 1611.4Sjruoho atf_tc_fail("not all descriptors were closed"); 1621.4Sjruoho} 1631.4Sjruoho 1641.1SjruohoATF_TP_ADD_TCS(tp) 1651.1Sjruoho{ 1661.1Sjruoho 1671.1Sjruoho ATF_TP_ADD_TC(tp, closefrom_basic); 1681.1Sjruoho ATF_TP_ADD_TC(tp, closefrom_buffer); 1691.1Sjruoho ATF_TP_ADD_TC(tp, closefrom_err); 1701.4Sjruoho ATF_TP_ADD_TC(tp, closefrom_one); 1711.1Sjruoho 1721.1Sjruoho return atf_no_error(); 1731.1Sjruoho} 174