robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 1 | /* |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2002 |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 3 | * Copyright (c) 2013 Wanlong Gao <gaowanlong@cn.fujitsu.com> |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 20 | /* |
| 21 | * Description: |
| 22 | * 1. open an O_WRONLY file, test if read failed. |
| 23 | * 2. open an O_RDONLY file, test if write failed. |
| 24 | */ |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 25 | |
robbiew | a775110 | 2003-03-27 16:38:59 +0000 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
robbiew | a775110 | 2003-03-27 16:38:59 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 30 | #include "test.h" |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 31 | |
| 32 | char *TCID = "open09"; |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 33 | int TST_TOTAL = 2; |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 34 | |
| 35 | #define PASSED 1 |
| 36 | #define FAILED 0 |
| 37 | |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 38 | static char tempfile[40] = ""; |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 39 | |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 40 | static void setup(void); |
| 41 | static void cleanup(void); |
| 42 | |
robbiew | a775110 | 2003-03-27 16:38:59 +0000 | [diff] [blame] | 43 | int main(int ac, char *av[]) |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 44 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 45 | int fildes; |
| 46 | int ret = 0; |
| 47 | char pbuf[BUFSIZ]; |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 48 | |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 49 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 50 | const char *msg; |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 51 | |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 52 | msg = parse_opts(ac, av, NULL, NULL); |
| 53 | if (msg != NULL) |
| 54 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 55 | |
| 56 | setup(); |
| 57 | |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 58 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 59 | |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 60 | fildes = open(tempfile, O_WRONLY); |
| 61 | if (fildes == -1) |
| 62 | tst_brkm(TFAIL, cleanup, "\t\t\topen failed"); |
| 63 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 64 | ret = read(fildes, pbuf, 1); |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 65 | if (ret != -1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | tst_resm(TFAIL, "read should not succeed"); |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 67 | else |
| 68 | tst_resm(TPASS, "Test passed in O_WRONLY."); |
| 69 | |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 70 | close(fildes); |
| 71 | |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 72 | fildes = open(tempfile, O_RDONLY); |
| 73 | if (fildes == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 74 | tst_resm(TFAIL, "\t\t\topen failed"); |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 75 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | ret = write(fildes, pbuf, 1); |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 77 | if (ret != -1) |
| 78 | tst_resm(TFAIL, "write should not succeed"); |
| 79 | else |
| 80 | tst_resm(TPASS, "Test passed in O_RDONLY."); |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 81 | } |
| 82 | close(fildes); |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 83 | } |
robbiew | a2d4732 | 2002-12-31 21:58:11 +0000 | [diff] [blame] | 84 | |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 85 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 86 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 87 | } |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 88 | |
| 89 | static void setup(void) |
| 90 | { |
| 91 | int fildes; |
| 92 | |
| 93 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 94 | TEST_PAUSE; |
| 95 | tst_tmpdir(); |
| 96 | |
| 97 | sprintf(tempfile, "open09.%d", getpid()); |
| 98 | |
| 99 | fildes = creat(tempfile, 0600); |
| 100 | if (fildes == -1) { |
| 101 | tst_brkm(TBROK, cleanup, "\t\t\tcan't create '%s'", |
| 102 | tempfile); |
| 103 | } else { |
| 104 | close(fildes); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void cleanup(void) |
| 109 | { |
Wanlong Gao | 4582b9c | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 110 | unlink(tempfile); |
| 111 | tst_rmdir(); |
| 112 | } |