1 /* 2 * Copyright (c) International Business Machines Corp., 2001 3 * 07/2001 Ported by Wayne Boyer 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 Foundation, 17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 /* 21 * DESCRIPTION 22 * Test that closing a regular file and a pipe works correctly 23 */ 24 25 #include <stdio.h> 26 #include <errno.h> 27 #include <fcntl.h> 28 #include <sys/stat.h> 29 #include "test.h" 30 31 void cleanup(void); 32 void setup(void); 33 34 char *TCID = "close01"; 35 int TST_TOTAL = 2; 36 37 char fname[40] = ""; 38 39 int fild, newfd, pipefildes[2]; 40 41 struct test_case_t { 42 int *fd; 43 char *type; 44 } TC[] = { 45 /* file descriptor for a regular file */ 46 { 47 &newfd, "file"}, 48 /* file descriptor for a pipe */ 49 { 50 &pipefildes[0], "pipe"} 51 }; 52 53 int main(int ac, char **av) 54 { 55 56 int i; 57 int lc; 58 59 tst_parse_opts(ac, av, NULL, NULL); 60 61 setup(); 62 63 for (lc = 0; TEST_LOOPING(lc); lc++) { 64 65 tst_count = 0; 66 67 if ((fild = creat(fname, 0777)) == -1) 68 tst_brkm(TBROK | TERRNO, cleanup, "can't open file %s", 69 fname); 70 71 if ((newfd = dup(fild)) == -1) 72 tst_brkm(TBROK | TERRNO, cleanup, 73 "can't dup the file des"); 74 75 if (pipe(pipefildes) == -1) { 76 tst_brkm(TBROK | TERRNO, cleanup, "can't open pipe"); 77 } 78 79 for (i = 0; i < TST_TOTAL; i++) { 80 81 TEST(close(*TC[i].fd)); 82 83 if (TEST_RETURN == -1) { 84 tst_resm(TFAIL, "call failed unexpectedly"); 85 continue; 86 } 87 88 if (close(*TC[i].fd) == -1) { 89 tst_resm(TPASS, "%s appears closed", 90 TC[i].type); 91 } else { 92 tst_resm(TFAIL, "%s close succeeded on" 93 "second attempt", TC[i].type); 94 } 95 } 96 97 } 98 99 cleanup(); 100 tst_exit(); 101 } 102 103 void setup(void) 104 { 105 int mypid; 106 107 tst_sig(FORK, DEF_HANDLER, cleanup); 108 109 umask(0); 110 111 TEST_PAUSE; 112 113 tst_tmpdir(); 114 115 mypid = getpid(); 116 sprintf(fname, "fname.%d", mypid); 117 } 118 119 void cleanup(void) 120 { 121 close(fild); 122 123 tst_rmdir(); 124 125 } 126