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