close01.c revision aabb8340f63ed31afe995fd97795e542dc68b93c
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 31void cleanup(void); 32void setup(void); 33 34char *TCID = "close01"; 35int TST_TOTAL = 2; 36 37char fname[40] = ""; 38 39int fild, newfd, pipefildes[2]; 40 41struct 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 53int main(int ac, char **av) 54{ 55 56 int i; 57 int lc; 58 const char *msg; 59 60 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) 61 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 62 63 setup(); 64 65 for (lc = 0; TEST_LOOPING(lc); lc++) { 66 67 tst_count = 0; 68 69 if ((fild = creat(fname, 0777)) == -1) 70 tst_brkm(TBROK | TERRNO, cleanup, "can't open file %s", 71 fname); 72 73 if ((newfd = dup(fild)) == -1) 74 tst_brkm(TBROK | TERRNO, cleanup, 75 "can't dup the file des"); 76 77 if (pipe(pipefildes) == -1) { 78 tst_brkm(TBROK | TERRNO, cleanup, "can't open pipe"); 79 } 80 81 for (i = 0; i < TST_TOTAL; i++) { 82 83 TEST(close(*TC[i].fd)); 84 85 if (TEST_RETURN == -1) { 86 tst_resm(TFAIL, "call failed unexpectedly"); 87 continue; 88 } 89 90 if (close(*TC[i].fd) == -1) { 91 tst_resm(TPASS, "%s appears closed", 92 TC[i].type); 93 } else { 94 tst_resm(TFAIL, "%s close succeeded on" 95 "second attempt", TC[i].type); 96 } 97 } 98 99 } 100 101 cleanup(); 102 tst_exit(); 103} 104 105void setup(void) 106{ 107 int mypid; 108 109 tst_sig(FORK, DEF_HANDLER, cleanup); 110 111 umask(0); 112 113 TEST_PAUSE; 114 115 tst_tmpdir(); 116 117 mypid = getpid(); 118 sprintf(fname, "fname.%d", mypid); 119} 120 121void cleanup(void) 122{ 123 close(fild); 124 125 tst_rmdir(); 126 127} 128