1/*
2 * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like.  Any license provided herein, whether implied or
15 * otherwise, applies only to this software file.  Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA  94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
32 */
33/* $Id: mkdir01.c,v 1.7 2009/03/23 13:35:54 subrata_modak Exp $ */
34/**********************************************************
35 *
36 *    OS Test - Silicon Graphics, Inc.
37 *
38 *    TEST IDENTIFIER	: mkdir01
39 *
40 *    EXECUTED BY	: anyone
41 *
42 *    TEST TITLE	: Basic errno test for mkdir(2)
43 *
44 *    PARENT DOCUMENT	: mkstds02
45 *
46 *    TEST CASE TOTAL	: 2
47 *
48 *    WALL CLOCK TIME	: 1
49 *
50 *    CPU TYPES		: ALL
51 *
52 *    AUTHOR		: Bill Branum
53 *
54 *    CO-PILOT		: Kathy Olmsted
55 *
56 *    DATE STARTED	: 4/15/92
57 *
58 *    INITIAL RELEASE	: UNICOS 7.0
59 *
60 *    TEST CASES
61 *
62 * 	mkdir(2) test for errno(s) EFAULT.
63 *
64 *    INPUT SPECIFICATIONS
65 * 	The standard options for system call tests are accepted.
66 *	(See the parse_opts(3) man page).
67 *
68 *    DURATION
69 * 	Terminates - with frequency and infinite modes.
70 *
71 *    SIGNALS
72 * 	Uses SIGUSR1 to pause before test if option set.
73 * 	(See the parse_opts(3) man page).
74 *
75 *    ENVIRONMENTAL NEEDS
76 *      No run-time environmental needs.
77 *
78 *    DETAILED DESCRIPTION
79 *	This test will verify that mkdir(2) returns a value of
80 *	-1 and sets errno to EFAULT when the path argument points
81 *	outside (above/below) the allocated address space of the
82 *	process.
83 *
84 * 	Setup:
85 * 	  Setup signal handling.
86 *	  Create and make current a temporary directory.
87 *	  Pause for SIGUSR1 if option specified.
88 *
89 * 	Test:
90 *	 Loop if the proper options are given.
91 * 	  Execute system call
92 *	  Check return code, if system call failed (return=-1)
93 *		Log the errno.
94 *        If doing functional test
95 *            check the errno returned and print result message
96 *
97 * 	Cleanup:
98 * 	  Print errno log and/or timing stats if options given
99 *	  Remove the temporary directory.
100 *	  Exit.
101 *
102 *
103 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
104
105#include <errno.h>
106#include <string.h>
107#include <signal.h>
108#include <sys/stat.h>
109#include <sys/types.h>
110#include <sys/mman.h>
111#include <fcntl.h>
112#include <unistd.h>
113#include "test.h"
114
115void setup();
116void cleanup();
117
118char *TCID = "mkdir01";
119int TST_TOTAL = 2;
120
121char *bad_addr = 0;
122
123int main(int ac, char **av)
124{
125	int lc;
126
127	tst_parse_opts(ac, av, NULL, NULL);
128
129	setup();
130
131	for (lc = 0; TEST_LOOPING(lc); lc++) {
132
133		tst_count = 0;
134
135		/*
136		 * TEST CASE: 1
137		 * mkdir() call with pointer below allocated address space.
138		 */
139
140		/* Call mkdir(2) */
141		TEST(mkdir(bad_addr, 0777));
142
143		/* check return code */
144		if (TEST_RETURN == -1) {
145		}
146
147		if (TEST_RETURN == -1) {
148			if (TEST_ERRNO == EFAULT) {
149				tst_resm(TPASS,
150					 "mkdir - path argument pointing below allocated address space failed as expected with errno %d : %s",
151					 TEST_ERRNO,
152					 strerror(TEST_ERRNO));
153			} else {
154				tst_resm(TFAIL,
155					 "mkdir - path argument pointing below allocated address space failed with errno %d : %s but expected %d (EFAULT)",
156					 TEST_ERRNO,
157					 strerror(TEST_ERRNO), EFAULT);
158			}
159		} else {
160			tst_resm(TFAIL,
161				 "mkdir - path argument pointing below allocated address space succeeded unexpectedly.");
162
163		}
164#if !defined(UCLINUX)
165		/*
166		 * TEST CASE: 2
167		 * mkdir() call with pointer above allocated address space.
168		 */
169
170		/* Call mkdir(2) */
171		TEST(mkdir(get_high_address(), 0777));
172
173		/* check return code */
174		if (TEST_RETURN == -1) {
175		}
176
177		if (TEST_RETURN == -1) {
178			if (TEST_ERRNO == EFAULT) {
179				tst_resm(TPASS,
180					 "mkdir - path argument pointing above allocated address space failed as expected with errno %d : %s",
181					 TEST_ERRNO,
182					 strerror(TEST_ERRNO));
183			} else {
184				tst_resm(TFAIL,
185					 "mkdir - path argument pointing above allocated address space failed with errno %d : %s but expected %d (EFAULT)",
186					 TEST_ERRNO,
187					 strerror(TEST_ERRNO), EFAULT);
188			}
189		} else {
190			tst_resm(TFAIL,
191				 "mkdir - path argument pointing above allocated address space succeeded unexpectedly.");
192
193		}
194#endif /* if !defined(UCLINUX) */
195
196	}
197
198	cleanup();
199	tst_exit();
200}
201
202/***************************************************************
203 * setup() - performs all ONE TIME setup for this test.
204 ***************************************************************/
205void setup(void)
206{
207
208	tst_sig(NOFORK, DEF_HANDLER, cleanup);
209
210	TEST_PAUSE;
211
212	/* Create a temporary directory and make it current. */
213	tst_tmpdir();
214
215	bad_addr = mmap(0, 1, PROT_NONE,
216			MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
217	if (bad_addr == MAP_FAILED) {
218		tst_brkm(TBROK, cleanup, "mmap failed");
219	}
220}
221
222/***************************************************************
223 * cleanup() - performs all ONE TIME cleanup for this test at
224 *		completion or premature exit.
225 ***************************************************************/
226void cleanup(void)
227{
228
229	/*
230	 * Remove the temporary directory.
231	 */
232	tst_rmdir();
233
234	/*
235	 * Exit with return code appropriate for results.
236	 */
237
238}
239