blob: 08a909a852fefcb23e84565e3317e0c676360e61 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubise38b9612014-06-02 17:20:57 +02002 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubise38b9612014-06-02 17:20:57 +02005 * 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.
plars865695b2001-08-27 22:15:12 +00009 *
Cyril Hrubise38b9612014-06-02 17:20:57 +020010 * 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.
plars865695b2001-08-27 22:15:12 +000014 *
Cyril Hrubise38b9612014-06-02 17:20:57 +020015 * 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
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
plars865695b2001-08-27 22:15:12 +000021 * DESCRIPTION
Cyril Hrubise38b9612014-06-02 17:20:57 +020022 * Test that closing a regular file and a pipe works correctly
plars865695b2001-08-27 22:15:12 +000023 */
24
25#include <stdio.h>
26#include <errno.h>
robbiew2c945242002-11-11 19:01:25 +000027#include <fcntl.h>
28#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000029#include "test.h"
plars865695b2001-08-27 22:15:12 +000030
31void cleanup(void);
32void setup(void);
33
plars48049f32003-07-05 03:12:42 +000034char *TCID = "close01";
plars865695b2001-08-27 22:15:12 +000035int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000036
37char fname[40] = "";
38
39int fild, newfd, pipefildes[2];
40
41struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000042 int *fd;
plars865695b2001-08-27 22:15:12 +000043 char *type;
44} TC[] = {
45 /* file descriptor for a regular file */
Wanlong Gao354ebb42012-12-07 10:10:04 +080046 {
47 &newfd, "file"},
48 /* file descriptor for a pipe */
49 {
50 &pipefildes[0], "pipe"}
plars865695b2001-08-27 22:15:12 +000051};
52
subrata_modak56207ce2009-03-23 13:35:39 +000053int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000054{
55
56 int i;
Garrett Cooper7b98b692010-12-16 22:20:35 -080057 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020058 const char *msg;
plars865695b2001-08-27 22:15:12 +000059
Garrett Cooper7b98b692010-12-16 22:20:35 -080060 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080061 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000062
Garrett Cooper7b98b692010-12-16 22:20:35 -080063 setup();
plars865695b2001-08-27 22:15:12 +000064
plars865695b2001-08-27 22:15:12 +000065 for (lc = 0; TEST_LOOPING(lc); lc++) {
66
Caspar Zhangd59a6592013-03-07 14:59:12 +080067 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000068
Garrett Cooper7b98b692010-12-16 22:20:35 -080069 if ((fild = creat(fname, 0777)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080070 tst_brkm(TBROK | TERRNO, cleanup, "can't open file %s",
71 fname);
plars865695b2001-08-27 22:15:12 +000072
Garrett Cooper7b98b692010-12-16 22:20:35 -080073 if ((newfd = dup(fild)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080074 tst_brkm(TBROK | TERRNO, cleanup,
75 "can't dup the file des");
plars865695b2001-08-27 22:15:12 +000076
77 if (pipe(pipefildes) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080078 tst_brkm(TBROK | TERRNO, cleanup, "can't open pipe");
plars865695b2001-08-27 22:15:12 +000079 }
80
plars865695b2001-08-27 22:15:12 +000081 for (i = 0; i < TST_TOTAL; i++) {
82
83 TEST(close(*TC[i].fd));
84
subrata_modak56207ce2009-03-23 13:35:39 +000085 if (TEST_RETURN == -1) {
86 tst_resm(TFAIL, "call failed unexpectedly");
87 continue;
88 }
plars865695b2001-08-27 22:15:12 +000089
Cyril Hrubise38b9612014-06-02 17:20:57 +020090 if (close(*TC[i].fd) == -1) {
91 tst_resm(TPASS, "%s appears closed",
92 TC[i].type);
plars865695b2001-08-27 22:15:12 +000093 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +020094 tst_resm(TFAIL, "%s close succeeded on"
95 "second attempt", TC[i].type);
plars865695b2001-08-27 22:15:12 +000096 }
97 }
98
99 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000100
Cyril Hrubise38b9612014-06-02 17:20:57 +0200101 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800102 tst_exit();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103}
plars865695b2001-08-27 22:15:12 +0000104
subrata_modak56207ce2009-03-23 13:35:39 +0000105void setup(void)
plars865695b2001-08-27 22:15:12 +0000106{
107 int mypid;
108
plars865695b2001-08-27 22:15:12 +0000109 tst_sig(FORK, DEF_HANDLER, cleanup);
110
111 umask(0);
112
plars865695b2001-08-27 22:15:12 +0000113 TEST_PAUSE;
114
plars865695b2001-08-27 22:15:12 +0000115 tst_tmpdir();
116
117 mypid = getpid();
Garrett Cooper7b98b692010-12-16 22:20:35 -0800118 sprintf(fname, "fname.%d", mypid);
plars865695b2001-08-27 22:15:12 +0000119}
120
subrata_modak56207ce2009-03-23 13:35:39 +0000121void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000122{
subrata_modak56207ce2009-03-23 13:35:39 +0000123 close(fild);
mridgecfb2c952004-08-25 15:12:37 +0000124
plars865695b2001-08-27 22:15:12 +0000125 tst_rmdir();
126
Chris Dearmanec6edca2012-10-17 19:54:01 -0700127}