close01.c revision d59a659cd639ca2780b00049d102acd2a783d585
1/* 2 * 3 * Copyright (c) International Business Machines Corp., 2001 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13 * the GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20/* 21 * NAME 22 * close01.c 23 * 24 * DESCRIPTION 25 * Test that closing a regular file and a pipe works correctly 26 * 27 * ALGORITHM 28 * Creat a file, and dup() a fildes 29 * Open a pipe 30 * call close() using the TEST macro 31 * if the call fails 32 * issue a FAIL message and continue 33 * else if STD_FUNCTIONAL_TEST 34 * attempt to close the file/pipe again 35 * if there is an error 36 * issue a PASS message 37 * else 38 * issue a FAIL message 39 * else 40 * issue a PASS message 41 * 42 * 43 * USAGE: <for command-line> 44 * close01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] 45 * where, -c n : Run n copies concurrently. 46 * -f : Turn off functionality Testing. 47 * -i n : Execute test n times. 48 * -I x : Execute test for x seconds. 49 * -P x : Pause for x seconds between iterations. 50 * -t : Turn on syscall timing. 51 * 52 * HISTORY 53 * 07/2001 Ported by Wayne Boyer 54 * 55 * RESTRICTIONS 56 * None 57 */ 58 59#include <stdio.h> 60#include <errno.h> 61#include <fcntl.h> 62#include <sys/stat.h> 63#include "test.h" 64#include "usctest.h" 65 66void cleanup(void); 67void setup(void); 68 69char *TCID = "close01"; 70int TST_TOTAL = 2; 71 72char fname[40] = ""; 73 74int fild, newfd, pipefildes[2]; 75 76struct test_case_t { 77 int *fd; 78 char *type; 79} TC[] = { 80 /* file descriptor for a regular file */ 81 { 82 &newfd, "file"}, 83 /* file descriptor for a pipe */ 84 { 85 &pipefildes[0], "pipe"} 86}; 87 88int main(int ac, char **av) 89{ 90 91 int i; 92 int lc; 93 char *msg; 94 95 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) 96 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 97 98 setup(); 99 100 for (lc = 0; TEST_LOOPING(lc); lc++) { 101 102 tst_count = 0; 103 104 if ((fild = creat(fname, 0777)) == -1) 105 tst_brkm(TBROK | TERRNO, cleanup, "can't open file %s", 106 fname); 107 108 if ((newfd = dup(fild)) == -1) 109 tst_brkm(TBROK | TERRNO, cleanup, 110 "can't dup the file des"); 111 112 if (pipe(pipefildes) == -1) { 113 tst_brkm(TBROK | TERRNO, cleanup, "can't open pipe"); 114 } 115 116 for (i = 0; i < TST_TOTAL; i++) { 117 118 TEST(close(*TC[i].fd)); 119 120 if (TEST_RETURN == -1) { 121 tst_resm(TFAIL, "call failed unexpectedly"); 122 continue; 123 } 124 125 if (STD_FUNCTIONAL_TEST) { 126 if (close(*TC[i].fd) == -1) { 127 tst_resm(TPASS, "%s appears closed", 128 TC[i].type); 129 } else { 130 tst_resm(TFAIL, "%s close succeeded on" 131 "second attempt", TC[i].type); 132 } 133 } else { 134 tst_resm(TPASS, "call succeeded"); 135 } 136 } 137 138 } 139 cleanup(); 140 141 tst_exit(); 142} 143 144void setup(void) 145{ 146 int mypid; 147 148 tst_sig(FORK, DEF_HANDLER, cleanup); 149 150 umask(0); 151 152 TEST_PAUSE; 153 154 tst_tmpdir(); 155 156 mypid = getpid(); 157 sprintf(fname, "fname.%d", mypid); 158} 159 160void cleanup(void) 161{ 162 close(fild); 163 164 TEST_CLEANUP; 165 166 tst_rmdir(); 167 168} 169