• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   Copyright (c) International Business Machines  Corp., 2002
3  *   Copyright (c) 2013 Wanlong Gao <gaowanlong@cn.fujitsu.com>
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
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
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  */
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include "test.h"
31 
32 char *TCID = "open09";
33 int TST_TOTAL = 2;
34 
35 #define PASSED 1
36 #define FAILED 0
37 
38 static char tempfile[40] = "";
39 
40 static void setup(void);
41 static void cleanup(void);
42 
main(int ac,char * av[])43 int main(int ac, char *av[])
44 {
45 	int fildes;
46 	int ret = 0;
47 	char pbuf[BUFSIZ];
48 
49 	int lc;
50 
51 	tst_parse_opts(ac, av, NULL, NULL);
52 
53 	setup();
54 
55 	for (lc = 0; TEST_LOOPING(lc); lc++) {
56 
57 		fildes = open(tempfile, O_WRONLY);
58 		if (fildes == -1)
59 			tst_brkm(TFAIL, cleanup, "\t\t\topen failed");
60 
61 		ret = read(fildes, pbuf, 1);
62 		if (ret != -1)
63 			tst_resm(TFAIL, "read should not succeed");
64 		else
65 			tst_resm(TPASS, "Test passed in O_WRONLY.");
66 
67 		close(fildes);
68 
69 		fildes = open(tempfile, O_RDONLY);
70 		if (fildes == -1) {
71 			tst_resm(TFAIL, "\t\t\topen failed");
72 		} else {
73 			ret = write(fildes, pbuf, 1);
74 			if (ret != -1)
75 				tst_resm(TFAIL, "write should not succeed");
76 			else
77 				tst_resm(TPASS, "Test passed in O_RDONLY.");
78 		}
79 		close(fildes);
80 	}
81 
82 	cleanup();
83 	tst_exit();
84 }
85 
setup(void)86 static void setup(void)
87 {
88 	int fildes;
89 
90 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
91 	TEST_PAUSE;
92 	tst_tmpdir();
93 
94 	sprintf(tempfile, "open09.%d", getpid());
95 
96 	fildes = creat(tempfile, 0600);
97 	if (fildes == -1) {
98 		tst_brkm(TBROK, cleanup, "\t\t\tcan't create '%s'",
99 			 tempfile);
100 	} else {
101 		close(fildes);
102 	}
103 }
104 
cleanup(void)105 static void cleanup(void)
106 {
107 	unlink(tempfile);
108 	tst_rmdir();
109 }
110