blob: da14cdfa4f9277e965052054bd0a6cb2ced50d07 [file] [log] [blame]
robbiewa2d47322002-12-31 21:58:11 +00001/*
robbiewa2d47322002-12-31 21:58:11 +00002 * Copyright (c) International Business Machines Corp., 2002
Wanlong Gao4582b9c2013-02-04 17:11:18 +08003 * Copyright (c) 2013 Wanlong Gao <gaowanlong@cn.fujitsu.com>
robbiewa2d47322002-12-31 21:58:11 +00004 *
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewa2d47322002-12-31 21:58:11 +000018 */
19
Wanlong Gao4582b9c2013-02-04 17:11:18 +080020/*
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 */
robbiewa2d47322002-12-31 21:58:11 +000025
robbiewa7751102003-03-27 16:38:59 +000026#include <sys/types.h>
27#include <sys/stat.h>
robbiewa2d47322002-12-31 21:58:11 +000028#include <stdio.h>
robbiewa7751102003-03-27 16:38:59 +000029#include <fcntl.h>
robbiewa2d47322002-12-31 21:58:11 +000030#include "test.h"
robbiewa2d47322002-12-31 21:58:11 +000031
32char *TCID = "open09";
Wanlong Gao4582b9c2013-02-04 17:11:18 +080033int TST_TOTAL = 2;
robbiewa2d47322002-12-31 21:58:11 +000034
35#define PASSED 1
36#define FAILED 0
37
Wanlong Gao4582b9c2013-02-04 17:11:18 +080038static char tempfile[40] = "";
robbiewa2d47322002-12-31 21:58:11 +000039
Wanlong Gao4582b9c2013-02-04 17:11:18 +080040static void setup(void);
41static void cleanup(void);
42
robbiewa7751102003-03-27 16:38:59 +000043int main(int ac, char *av[])
robbiewa2d47322002-12-31 21:58:11 +000044{
subrata_modak56207ce2009-03-23 13:35:39 +000045 int fildes;
46 int ret = 0;
47 char pbuf[BUFSIZ];
robbiewa2d47322002-12-31 21:58:11 +000048
Cyril Hrubis89af32a2012-10-24 16:39:11 +020049 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020050 const char *msg;
robbiewa2d47322002-12-31 21:58:11 +000051
Wanlong Gao4582b9c2013-02-04 17:11:18 +080052 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
robbiewa2d47322002-12-31 21:58:11 +000058 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiewa2d47322002-12-31 21:58:11 +000059
Wanlong Gao4582b9c2013-02-04 17:11:18 +080060 fildes = open(tempfile, O_WRONLY);
61 if (fildes == -1)
62 tst_brkm(TFAIL, cleanup, "\t\t\topen failed");
63
subrata_modak56207ce2009-03-23 13:35:39 +000064 ret = read(fildes, pbuf, 1);
Wanlong Gao4582b9c2013-02-04 17:11:18 +080065 if (ret != -1)
subrata_modak56207ce2009-03-23 13:35:39 +000066 tst_resm(TFAIL, "read should not succeed");
Wanlong Gao4582b9c2013-02-04 17:11:18 +080067 else
68 tst_resm(TPASS, "Test passed in O_WRONLY.");
69
robbiewa2d47322002-12-31 21:58:11 +000070 close(fildes);
71
Wanlong Gao4582b9c2013-02-04 17:11:18 +080072 fildes = open(tempfile, O_RDONLY);
73 if (fildes == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +000074 tst_resm(TFAIL, "\t\t\topen failed");
robbiewa2d47322002-12-31 21:58:11 +000075 } else {
subrata_modak56207ce2009-03-23 13:35:39 +000076 ret = write(fildes, pbuf, 1);
Wanlong Gao4582b9c2013-02-04 17:11:18 +080077 if (ret != -1)
78 tst_resm(TFAIL, "write should not succeed");
79 else
80 tst_resm(TPASS, "Test passed in O_RDONLY.");
robbiewa2d47322002-12-31 21:58:11 +000081 }
82 close(fildes);
Wanlong Gao4582b9c2013-02-04 17:11:18 +080083 }
robbiewa2d47322002-12-31 21:58:11 +000084
Wanlong Gao4582b9c2013-02-04 17:11:18 +080085 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -080086 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -070087}
Wanlong Gao4582b9c2013-02-04 17:11:18 +080088
89static 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
108static void cleanup(void)
109{
Wanlong Gao4582b9c2013-02-04 17:11:18 +0800110 unlink(tempfile);
111 tst_rmdir();
112}