blob: 48ee9b7443f1a3e1c39aca82017d5cdff67e2ee3 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000029 * 3) mknod(2) returns -1 and sets errno to ENOENT if the directory
plars865695b2001-08-27 22:15:12 +000030 * 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>
plars1ad84512002-07-23 13:11:18 +000085#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +000086
87#include "test.h"
88#include "usctest.h"
89
90#define MODE_RWX S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO
91
92int setup1(); /* setup function to test mknod for EEXIST */
93int setup3(); /* setup function to test mknod for ENOTDIR */
subrata_modak56207ce2009-03-23 13:35:39 +000094int longpath_setup(); /* setup function to test mknod for ENAMETOOLONG */
plars865695b2001-08-27 22:15:12 +000095int no_setup(); /* simply returns 0 to the caller */
subrata_modak56207ce2009-03-23 13:35:39 +000096char Longpathname[PATH_MAX + 2];
plars865695b2001-08-27 22:15:12 +000097char High_address_node[64];
98
subrata_modak56207ce2009-03-23 13:35:39 +000099struct test_case_t { /* test case struct. to hold ref. test cond's */
plars865695b2001-08-27 22:15:12 +0000100 char *pathname;
101 char *desc;
102 int exp_errno;
subrata_modak56207ce2009-03-23 13:35:39 +0000103 int (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000104} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000105 {
106 "tnode_1", "Specified node already exists", EEXIST, setup1},
robbiewd34d5812005-07-11 22:28:09 +0000107#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +0000108 {
109 (char *)-1, "Negative address", EFAULT, no_setup}, {
110 High_address_node, "Address beyond address space", EFAULT,
111 no_setup},
robbiewd34d5812005-07-11 22:28:09 +0000112#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000113 {
114 "testdir_2/tnode_2", "Non-existent file", ENOENT, no_setup}, {
115 "", "Pathname is empty", ENOENT, no_setup}, {
116 Longpathname, "Pathname too long", ENAMETOOLONG, longpath_setup}, {
117 "tnode/tnode_3", "Path contains regular file", ENOTDIR, setup3}, {
118 NULL, NULL, 0, no_setup}
plars865695b2001-08-27 22:15:12 +0000119};
120
subrata_modak56207ce2009-03-23 13:35:39 +0000121char *TCID = "mknod06"; /* Test program identifier. */
122int TST_TOTAL = (sizeof(Test_cases) / sizeof(*Test_cases));
123extern int Tst_count; /* Test Case counter for tst_* routines */
robbiewd34d5812005-07-11 22:28:09 +0000124#if !defined(UCLINUX)
plars865695b2001-08-27 22:15:12 +0000125extern char *get_high_address();
subrata_modak56207ce2009-03-23 13:35:39 +0000126int exp_enos[] = { EEXIST, EFAULT, ENOENT, ENAMETOOLONG, ENOTDIR, 0 };
robbiewd34d5812005-07-11 22:28:09 +0000127#else
subrata_modak56207ce2009-03-23 13:35:39 +0000128int exp_enos[] = { EEXIST, ENOENT, ENAMETOOLONG, ENOTDIR, 0 };
robbiewd34d5812005-07-11 22:28:09 +0000129#endif
plars865695b2001-08-27 22:15:12 +0000130
subrata_modak56207ce2009-03-23 13:35:39 +0000131char *bad_addr = 0;
plars1ad84512002-07-23 13:11:18 +0000132
plars865695b2001-08-27 22:15:12 +0000133void setup(); /* setup function for the tests */
134void cleanup(); /* cleanup function for the tests */
135
subrata_modak56207ce2009-03-23 13:35:39 +0000136int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000137{
138 int lc; /* loop counter */
139 char *msg; /* message returned from parse_opts */
140 char *node_name; /* ptr. for node name created */
141 char *test_desc; /* test specific error message */
142 int ind; /* counter to test different test conditions */
143
144 /* Parse standard options given to run the test. */
Garrett Cooper45e285d2010-11-22 12:19:25 -0800145 msg = parse_opts(ac, av, NULL, NULL);
146 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000147 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800148
plars865695b2001-08-27 22:15:12 +0000149 }
150
151 /*
152 * Invoke setup function to call individual test setup functions
153 * for the test which run as root/super-user.
154 */
155 setup();
156
157 /* set the expected errnos... */
158 TEST_EXP_ENOS(exp_enos);
159
plars865695b2001-08-27 22:15:12 +0000160 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800161
subrata_modak56207ce2009-03-23 13:35:39 +0000162 Tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000163
164 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
165 node_name = Test_cases[ind].pathname;
166 test_desc = Test_cases[ind].desc;
167
robbiewd34d5812005-07-11 22:28:09 +0000168#if !defined(UCLINUX)
plars865695b2001-08-27 22:15:12 +0000169 if (node_name == High_address_node) {
170 node_name = get_high_address();
171 }
robbiewd34d5812005-07-11 22:28:09 +0000172#endif
plars865695b2001-08-27 22:15:12 +0000173
174 /*
175 * Call mknod(2) to test different test conditions.
176 * verify that it fails with -1 return value and
177 * sets appropriate errno.
178 */
179 TEST(mknod(node_name, MODE_RWX, 0));
subrata_modakbdbaec52009-02-26 12:14:51 +0000180
plars865695b2001-08-27 22:15:12 +0000181 /* Check return code from mknod(2) */
182 if (TEST_RETURN != -1) {
subrata_modak923b23f2009-11-02 13:57:16 +0000183 tst_resm(TFAIL, "mknod() returned %ld, expected "
plars865695b2001-08-27 22:15:12 +0000184 "-1, errno:%d", TEST_RETURN,
185 Test_cases[ind].exp_errno);
186 continue;
187 }
188
189 TEST_ERROR_LOG(TEST_ERRNO);
190
191 if (TEST_ERRNO == Test_cases[ind].exp_errno) {
192 tst_resm(TPASS, "mknod() fails, %s, errno:%d",
193 test_desc, TEST_ERRNO);
194 } else {
195 tst_resm(TFAIL, "mknod() fails, %s, errno:%d, "
196 "expected errno:%d", test_desc,
197 TEST_ERRNO, Test_cases[ind].exp_errno);
198 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800199 }
plars865695b2001-08-27 22:15:12 +0000200
Garrett Cooper2c282152010-12-16 00:55:50 -0800201 }
plars865695b2001-08-27 22:15:12 +0000202
203 /*
204 * Invoke cleanup() to delete the test directories created
205 * in the setup().
206 */
207 cleanup();
208
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800209 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800210}
plars865695b2001-08-27 22:15:12 +0000211
212/*
213 * setup(void) - performs all ONE TIME setup for this test.
214 * Exit the test program on receipt of unexpected signals.
215 * Create a temporary directory used to hold test directories and nodes
216 * created and change the directory to it.
217 * Invoke individual test setup functions according to the order
218 * set in struct. definition.
219 */
subrata_modak56207ce2009-03-23 13:35:39 +0000220void setup()
plars865695b2001-08-27 22:15:12 +0000221{
Garrett Cooper2c282152010-12-16 00:55:50 -0800222 int ind;
plars865695b2001-08-27 22:15:12 +0000223
224 /* Capture unexpected signals */
225 tst_sig(NOFORK, DEF_HANDLER, cleanup);
226
227 /* Check that the test process id is super/root */
228 if (geteuid() != 0) {
229 tst_brkm(TBROK, NULL, "Must be super/root for this test!");
230 tst_exit();
231 }
232
plars865695b2001-08-27 22:15:12 +0000233 TEST_PAUSE;
234
235 /* Make a temp dir and cd to it */
236 tst_tmpdir();
237
vapier62b16cf2007-02-09 20:48:23 +0000238#if !defined(UCLINUX)
robbiewd34d5812005-07-11 22:28:09 +0000239 bad_addr = mmap(0, 1, PROT_NONE,
subrata_modak56207ce2009-03-23 13:35:39 +0000240 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
vapier0722a2b2006-05-12 15:44:11 +0000241 if (bad_addr == MAP_FAILED) {
plars1ad84512002-07-23 13:11:18 +0000242 tst_brkm(TBROK, cleanup, "mmap failed");
243 }
mreed1081534c32006-08-03 05:21:24 +0000244 Test_cases[2].pathname = bad_addr;
mreed1081534c32006-08-03 05:21:24 +0000245#endif
plars865695b2001-08-27 22:15:12 +0000246 /* call individual setup functions */
247 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
248 Test_cases[ind].setupfunc();
249 }
250}
251
252/*
253 * no_setup() - Some test conditions for mknod(2) do not any setup.
254 * Hence, this function just returns 0.
255 */
subrata_modak56207ce2009-03-23 13:35:39 +0000256int no_setup()
plars865695b2001-08-27 22:15:12 +0000257{
258 return 0;
259}
260
261/*
262 * longpath_setup() - setup to create a node with a name length exceeding
263 * the MAX. length of PATH_MAX.
264 * This function retruns 0.
265 */
subrata_modak56207ce2009-03-23 13:35:39 +0000266int longpath_setup()
plars865695b2001-08-27 22:15:12 +0000267{
268 int ind; /* counter variable */
269
270 for (ind = 0; ind <= (PATH_MAX + 1); ind++) {
271 Longpathname[ind] = 'a';
272 }
273 return 0;
274}
subrata_modakbdbaec52009-02-26 12:14:51 +0000275
plars865695b2001-08-27 22:15:12 +0000276/*
277 * setup1() - setup function for a test condition for which mknod(2)
278 * returns -1 and sets errno to EEXIST.
279 * This function creates a node using mknod(2) and tries to create
280 * same node in the test and fails with above errno.
281 * This function returns 0.
282 */
subrata_modak56207ce2009-03-23 13:35:39 +0000283int setup1()
plars865695b2001-08-27 22:15:12 +0000284{
285 /* Create a node using mknod */
286 if (mknod("tnode_1", MODE_RWX, 0) < 0) {
287 tst_brkm(TBROK, cleanup, "Fails to create node in setup1()");
288 }
289
290 return 0;
291}
292
293/*
294 * setup3() - setup function for a test condition for which mknod(2)
295 * returns -1 and sets errno to ENOTDIR.
296 * This function creates a node under temporary directory and the
297 * test attempts to create another node under under this node and fails
298 * with ENOTDIR as the node created here is a regular file.
299 * This function returns 0.
300 */
subrata_modak56207ce2009-03-23 13:35:39 +0000301int setup3()
plars865695b2001-08-27 22:15:12 +0000302{
303 /* Create a node using mknod */
304 if (mknod("tnode", MODE_RWX, 0) < 0) {
305 tst_brkm(TBROK, cleanup, "Fails to create node in setup3()");
306 }
307
308 return 0;
309}
310
311/*
312 * cleanup() - Performs all ONE TIME cleanup for this test at
313 * completion or premature exit.
314 * Print test timing stats and errno log if test executed with options.
315 * Remove temporary directory and sub-directories/files under it
316 * created during setup().
317 * Exit the test program with normal exit code.
318 */
subrata_modak56207ce2009-03-23 13:35:39 +0000319void cleanup()
plars865695b2001-08-27 22:15:12 +0000320{
321 /*
322 * print timing stats if that option was specified.
323 * print errno log if that option was specified.
324 */
325 TEST_CLEANUP;
326
plars865695b2001-08-27 22:15:12 +0000327 tst_rmdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000328
Garrett Cooper2c282152010-12-16 00:55:50 -0800329}