plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * DESCRIPTION |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 22 | * Test that closing a regular file and a pipe works correctly |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <errno.h> |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | |
| 31 | void cleanup(void); |
| 32 | void setup(void); |
| 33 | |
plars | 48049f3 | 2003-07-05 03:12:42 +0000 | [diff] [blame] | 34 | char *TCID = "close01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | |
| 37 | char fname[40] = ""; |
| 38 | |
| 39 | int fild, newfd, pipefildes[2]; |
| 40 | |
| 41 | struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 42 | int *fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | char *type; |
| 44 | } TC[] = { |
| 45 | /* file descriptor for a regular file */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 46 | { |
| 47 | &newfd, "file"}, |
| 48 | /* file descriptor for a pipe */ |
| 49 | { |
| 50 | &pipefildes[0], "pipe"} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 53 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | { |
| 55 | |
| 56 | int i; |
Garrett Cooper | 7b98b69 | 2010-12-16 22:20:35 -0800 | [diff] [blame] | 57 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 58 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
Garrett Cooper | 7b98b69 | 2010-12-16 22:20:35 -0800 | [diff] [blame] | 60 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 61 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
Garrett Cooper | 7b98b69 | 2010-12-16 22:20:35 -0800 | [diff] [blame] | 63 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 66 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 67 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
Garrett Cooper | 7b98b69 | 2010-12-16 22:20:35 -0800 | [diff] [blame] | 69 | if ((fild = creat(fname, 0777)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 70 | tst_brkm(TBROK | TERRNO, cleanup, "can't open file %s", |
| 71 | fname); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
Garrett Cooper | 7b98b69 | 2010-12-16 22:20:35 -0800 | [diff] [blame] | 73 | if ((newfd = dup(fild)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 74 | tst_brkm(TBROK | TERRNO, cleanup, |
| 75 | "can't dup the file des"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | |
| 77 | if (pipe(pipefildes) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 78 | tst_brkm(TBROK | TERRNO, cleanup, "can't open pipe"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | } |
| 80 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | for (i = 0; i < TST_TOTAL; i++) { |
| 82 | |
| 83 | TEST(close(*TC[i].fd)); |
| 84 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | if (TEST_RETURN == -1) { |
| 86 | tst_resm(TFAIL, "call failed unexpectedly"); |
| 87 | continue; |
| 88 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 90 | if (close(*TC[i].fd) == -1) { |
| 91 | tst_resm(TPASS, "%s appears closed", |
| 92 | TC[i].type); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 94 | tst_resm(TFAIL, "%s close succeeded on" |
| 95 | "second attempt", TC[i].type); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 100 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 101 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 102 | tst_exit(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 103 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | { |
| 107 | int mypid; |
| 108 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 110 | |
| 111 | umask(0); |
| 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | TEST_PAUSE; |
| 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | tst_tmpdir(); |
| 116 | |
| 117 | mypid = getpid(); |
Garrett Cooper | 7b98b69 | 2010-12-16 22:20:35 -0800 | [diff] [blame] | 118 | sprintf(fname, "fname.%d", mypid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | } |
| 120 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | close(fild); |
mridge | cfb2c95 | 2004-08-25 15:12:37 +0000 | [diff] [blame] | 124 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | tst_rmdir(); |
| 126 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 127 | } |