1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
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  * Test Name: mknod06
22  *
23  * Test Description:
24  * Verify that,
25  *   1) mknod(2) returns -1 and sets errno to EEXIST if specified path
26  *	already exists.
27  *   2) mknod(2) returns -1 and sets errno to EFAULT if pathname points
28  *	outside user's accessible address space.
29  *   3) mknod(2) returns -1 and sets errno to ENOENT if the directory
30  *	component in pathname does not exist.
31  *   4) mknod(2) returns -1 and sets errno to ENAMETOOLONG if the pathname
32  *	component was too long.
33  *   5) mknod(2) returns -1 and sets errno to ENOTDIR if the directory
34  *	component in pathname is not a directory.
35  *
36  * Expected Result:
37  *  mknod() should fail with return value -1 and set expected errno.
38  *
39  * Algorithm:
40  *  Setup:
41  *   Setup signal handling.
42  *   Create temporary directory.
43  *   Pause for SIGUSR1 if option specified.
44  *
45  *  Test:
46  *   Loop if the proper options are given.
47  *   Execute system call
48  *   Check return code, if system call failed (return=-1)
49  *   	if errno set == expected errno
50  *   		Issue sys call fails with expected return value and errno.
51  *   	Otherwise,
52  *		Issue sys call fails with unexpected errno.
53  *   Otherwise,
54  *	Issue sys call returns unexpected value.
55  *
56  *  Cleanup:
57  *   Print errno log and/or timing stats if options given
58  *   Delete the temporary directory(s)/file(s) created.
59  *
60  * Usage:  <for command-line>
61  *  mknod06 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
62  *     where,  -c n : Run n copies concurrently.
63  *             -e   : Turn on errno logging.
64  *	       -i n : Execute test n times.
65  *	       -I x : Execute test for x seconds.
66  *	       -P x : Pause for x seconds between iterations.
67  *	       -t   : Turn on syscall timing.
68  *
69  * HISTORY
70  *	07/2001 Ported by Wayne Boyer
71  *
72  * RESTRICTIONS:
73  *  This test should be executed by super-user (root) only.
74  */
75 
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <unistd.h>
79 #include <errno.h>
80 #include <string.h>
81 #include <signal.h>
82 #include <sys/types.h>
83 #include <sys/stat.h>
84 #include <sys/param.h>
85 #include <sys/mman.h>
86 
87 #include "test.h"
88 
89 #define MODE_RWX		S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO
90 
91 int setup1();			/* setup function to test mknod for EEXIST */
92 int setup3();			/* setup function to test mknod for ENOTDIR */
93 int longpath_setup();		/* setup function to test mknod for ENAMETOOLONG */
94 int no_setup();			/* simply returns 0 to the caller */
95 char Longpathname[PATH_MAX + 2];
96 
97 struct test_case_t {		/* test case struct. to hold ref. test cond's */
98 	char *pathname;
99 	char *desc;
100 	int exp_errno;
101 	int (*setupfunc) ();
102 } Test_cases[] = {
103 	{"tnode_1", "Specified node already exists", EEXIST, setup1}, {
104 	NULL, "Invalid address", EFAULT, no_setup}, {
105 	"testdir_2/tnode_2", "Non-existent file", ENOENT, no_setup}, {
106 	"", "Pathname is empty", ENOENT, no_setup}, {
107 	Longpathname, "Pathname too long", ENAMETOOLONG, longpath_setup}, {
108 	"tnode/tnode_3", "Path contains regular file", ENOTDIR, setup3}, {
109 	NULL, NULL, 0, no_setup}
110 };
111 
112 char *TCID = "mknod06";
113 int TST_TOTAL = ARRAY_SIZE(Test_cases);
114 
115 void setup();			/* setup function for the tests */
116 void cleanup();			/* cleanup function for the tests */
117 
main(int ac,char ** av)118 int main(int ac, char **av)
119 {
120 	int lc;
121 	char *node_name;	/* ptr. for node name created */
122 	char *test_desc;	/* test specific error message */
123 	int ind;		/* counter to test different test conditions */
124 
125 	tst_parse_opts(ac, av, NULL, NULL);
126 
127 	/*
128 	 * Invoke setup function to call individual test setup functions
129 	 * for the test which run as root/super-user.
130 	 */
131 	setup();
132 
133 	for (lc = 0; TEST_LOOPING(lc); lc++) {
134 
135 		tst_count = 0;
136 
137 		for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
138 			node_name = Test_cases[ind].pathname;
139 			test_desc = Test_cases[ind].desc;
140 
141 			/*
142 			 * Call mknod(2) to test different test conditions.
143 			 * verify that it fails with -1 return value and
144 			 * sets appropriate errno.
145 			 */
146 			TEST(mknod(node_name, MODE_RWX, 0));
147 
148 			/* Check return code from mknod(2) */
149 			if (TEST_RETURN != -1) {
150 				tst_resm(TFAIL,
151 					 "mknod() returned %ld, expected "
152 					 "-1, errno:%d", TEST_RETURN,
153 					 Test_cases[ind].exp_errno);
154 				continue;
155 			}
156 
157 			if (TEST_ERRNO == Test_cases[ind].exp_errno) {
158 				tst_resm(TPASS, "mknod() fails, %s, errno:%d",
159 					 test_desc, TEST_ERRNO);
160 			} else {
161 				tst_resm(TFAIL, "mknod() fails, %s, errno:%d, "
162 					 "expected errno:%d", test_desc,
163 					 TEST_ERRNO, Test_cases[ind].exp_errno);
164 			}
165 		}
166 
167 	}
168 
169 	/*
170 	 * Invoke cleanup() to delete the test directories created
171 	 * in the setup().
172 	 */
173 	cleanup();
174 
175 	tst_exit();
176 }
177 
178 /*
179  * setup(void) - performs all ONE TIME setup for this test.
180  * 	Exit the test program on receipt of unexpected signals.
181  *	Create a temporary directory used to hold test directories and nodes
182  *	created and change the directory to it.
183  *	Invoke individual test setup functions according to the order
184  *	set in struct. definition.
185  */
setup(void)186 void setup(void)
187 {
188 	int ind;
189 
190 	tst_require_root();
191 
192 	/* Capture unexpected signals */
193 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
194 
195 	TEST_PAUSE;
196 
197 	/* Make a temp dir and cd to it */
198 	tst_tmpdir();
199 
200 	/* call individual setup functions */
201 	for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
202 		if (!Test_cases[ind].pathname)
203 			Test_cases[ind].pathname = tst_get_bad_addr(cleanup);
204 		Test_cases[ind].setupfunc();
205 	}
206 }
207 
208 /*
209  * no_setup() - Some test conditions for mknod(2) do not any setup.
210  *		Hence, this function just returns 0.
211  */
no_setup(void)212 int no_setup(void)
213 {
214 	return 0;
215 }
216 
217 /*
218  * longpath_setup() - setup to create a node with a name length exceeding
219  *		      the MAX. length of PATH_MAX.
220  *   This function retruns 0.
221  */
longpath_setup(void)222 int longpath_setup(void)
223 {
224 	int ind;		/* counter variable */
225 
226 	for (ind = 0; ind <= (PATH_MAX + 1); ind++) {
227 		Longpathname[ind] = 'a';
228 	}
229 	return 0;
230 }
231 
232 /*
233  * setup1() - setup function for a test condition for which mknod(2)
234  *	      returns -1 and sets errno to EEXIST.
235  *  This function creates a node using mknod(2) and tries to create
236  *  same node in the test and fails with above errno.
237  *  This function returns 0.
238  */
setup1(void)239 int setup1(void)
240 {
241 	/* Create a node using mknod */
242 	if (mknod("tnode_1", MODE_RWX, 0) < 0) {
243 		tst_brkm(TBROK, cleanup, "Fails to create node in setup1()");
244 	}
245 
246 	return 0;
247 }
248 
249 /*
250  * setup3() - setup function for a test condition for which mknod(2)
251  *	      returns -1 and sets errno to ENOTDIR.
252  *  This function creates a node under temporary directory and the
253  *  test attempts to create another node under under this node and fails
254  *  with ENOTDIR as the node created here is a regular file.
255  *  This function returns 0.
256  */
setup3(void)257 int setup3(void)
258 {
259 	/* Create a node using mknod */
260 	if (mknod("tnode", MODE_RWX, 0) < 0) {
261 		tst_brkm(TBROK, cleanup, "Fails to create node in setup3()");
262 	}
263 
264 	return 0;
265 }
266 
267 /*
268  * cleanup() - Performs all ONE TIME cleanup for this test at
269  *             completion or premature exit.
270  *	Print test timing stats and errno log if test executed with options.
271  *	Remove temporary directory and sub-directories/files under it
272  *	created during setup().
273  *	Exit the test program with normal exit code.
274  */
cleanup(void)275 void cleanup(void)
276 {
277 
278 	tst_rmdir();
279 
280 }
281