blob: ebd67bd2704f000e87c5692897642eb351aff54b [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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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"
plars865695b2001-08-27 22:15:12 +000088
89#define MODE_RWX S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO
90
91int setup1(); /* setup function to test mknod for EEXIST */
92int setup3(); /* setup function to test mknod for ENOTDIR */
subrata_modak56207ce2009-03-23 13:35:39 +000093int longpath_setup(); /* setup function to test mknod for ENAMETOOLONG */
plars865695b2001-08-27 22:15:12 +000094int no_setup(); /* simply returns 0 to the caller */
subrata_modak56207ce2009-03-23 13:35:39 +000095char Longpathname[PATH_MAX + 2];
plars865695b2001-08-27 22:15:12 +000096char High_address_node[64];
97
subrata_modak56207ce2009-03-23 13:35:39 +000098struct test_case_t { /* test case struct. to hold ref. test cond's */
plars865695b2001-08-27 22:15:12 +000099 char *pathname;
100 char *desc;
101 int exp_errno;
subrata_modak56207ce2009-03-23 13:35:39 +0000102 int (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000103} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000104 {
105 "tnode_1", "Specified node already exists", EEXIST, setup1},
robbiewd34d5812005-07-11 22:28:09 +0000106#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +0000107 {
108 (char *)-1, "Negative address", EFAULT, no_setup}, {
109 High_address_node, "Address beyond address space", EFAULT,
110 no_setup},
robbiewd34d5812005-07-11 22:28:09 +0000111#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000112 {
113 "testdir_2/tnode_2", "Non-existent file", ENOENT, no_setup}, {
114 "", "Pathname is empty", ENOENT, no_setup}, {
115 Longpathname, "Pathname too long", ENAMETOOLONG, longpath_setup}, {
116 "tnode/tnode_3", "Path contains regular file", ENOTDIR, setup3}, {
117 NULL, NULL, 0, no_setup}
plars865695b2001-08-27 22:15:12 +0000118};
119
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200120char *TCID = "mknod06";
Cyril Hrubisb863a0b2014-09-24 13:15:29 +0200121int TST_TOTAL = ARRAY_SIZE(Test_cases);
robbiewd34d5812005-07-11 22:28:09 +0000122#if !defined(UCLINUX)
plars865695b2001-08-27 22:15:12 +0000123extern char *get_high_address();
robbiewd34d5812005-07-11 22:28:09 +0000124#else
robbiewd34d5812005-07-11 22:28:09 +0000125#endif
plars865695b2001-08-27 22:15:12 +0000126
subrata_modak56207ce2009-03-23 13:35:39 +0000127char *bad_addr = 0;
plars1ad84512002-07-23 13:11:18 +0000128
plars865695b2001-08-27 22:15:12 +0000129void setup(); /* setup function for the tests */
130void cleanup(); /* cleanup function for the tests */
131
subrata_modak56207ce2009-03-23 13:35:39 +0000132int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000133{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200134 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200135 const char *msg;
plars865695b2001-08-27 22:15:12 +0000136 char *node_name; /* ptr. for node name created */
137 char *test_desc; /* test specific error message */
138 int ind; /* counter to test different test conditions */
139
Garrett Cooper45e285d2010-11-22 12:19:25 -0800140 msg = parse_opts(ac, av, NULL, NULL);
141 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000142 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800143
plars865695b2001-08-27 22:15:12 +0000144 }
145
146 /*
147 * Invoke setup function to call individual test setup functions
148 * for the test which run as root/super-user.
149 */
150 setup();
151
plars865695b2001-08-27 22:15:12 +0000152 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800153
Caspar Zhangd59a6592013-03-07 14:59:12 +0800154 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000155
156 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
157 node_name = Test_cases[ind].pathname;
158 test_desc = Test_cases[ind].desc;
159
robbiewd34d5812005-07-11 22:28:09 +0000160#if !defined(UCLINUX)
plars865695b2001-08-27 22:15:12 +0000161 if (node_name == High_address_node) {
162 node_name = get_high_address();
163 }
robbiewd34d5812005-07-11 22:28:09 +0000164#endif
plars865695b2001-08-27 22:15:12 +0000165
166 /*
167 * Call mknod(2) to test different test conditions.
168 * verify that it fails with -1 return value and
169 * sets appropriate errno.
170 */
171 TEST(mknod(node_name, MODE_RWX, 0));
subrata_modakbdbaec52009-02-26 12:14:51 +0000172
plars865695b2001-08-27 22:15:12 +0000173 /* Check return code from mknod(2) */
174 if (TEST_RETURN != -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 tst_resm(TFAIL,
176 "mknod() returned %ld, expected "
plars865695b2001-08-27 22:15:12 +0000177 "-1, errno:%d", TEST_RETURN,
178 Test_cases[ind].exp_errno);
179 continue;
180 }
181
plars865695b2001-08-27 22:15:12 +0000182 if (TEST_ERRNO == Test_cases[ind].exp_errno) {
183 tst_resm(TPASS, "mknod() fails, %s, errno:%d",
184 test_desc, TEST_ERRNO);
185 } else {
186 tst_resm(TFAIL, "mknod() fails, %s, errno:%d, "
187 "expected errno:%d", test_desc,
188 TEST_ERRNO, Test_cases[ind].exp_errno);
189 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800190 }
plars865695b2001-08-27 22:15:12 +0000191
Garrett Cooper2c282152010-12-16 00:55:50 -0800192 }
plars865695b2001-08-27 22:15:12 +0000193
194 /*
195 * Invoke cleanup() to delete the test directories created
196 * in the setup().
197 */
198 cleanup();
199
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800200 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800201}
plars865695b2001-08-27 22:15:12 +0000202
203/*
204 * setup(void) - performs all ONE TIME setup for this test.
205 * Exit the test program on receipt of unexpected signals.
206 * Create a temporary directory used to hold test directories and nodes
207 * created and change the directory to it.
208 * Invoke individual test setup functions according to the order
209 * set in struct. definition.
210 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400211void setup(void)
plars865695b2001-08-27 22:15:12 +0000212{
Garrett Cooper2c282152010-12-16 00:55:50 -0800213 int ind;
plars865695b2001-08-27 22:15:12 +0000214
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200215 tst_require_root(NULL);
216
plars865695b2001-08-27 22:15:12 +0000217 /* Capture unexpected signals */
218 tst_sig(NOFORK, DEF_HANDLER, cleanup);
219
plars865695b2001-08-27 22:15:12 +0000220 TEST_PAUSE;
221
222 /* Make a temp dir and cd to it */
223 tst_tmpdir();
224
vapier62b16cf2007-02-09 20:48:23 +0000225#if !defined(UCLINUX)
robbiewd34d5812005-07-11 22:28:09 +0000226 bad_addr = mmap(0, 1, PROT_NONE,
subrata_modak56207ce2009-03-23 13:35:39 +0000227 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
vapier0722a2b2006-05-12 15:44:11 +0000228 if (bad_addr == MAP_FAILED) {
plars1ad84512002-07-23 13:11:18 +0000229 tst_brkm(TBROK, cleanup, "mmap failed");
230 }
mreed1081534c32006-08-03 05:21:24 +0000231 Test_cases[2].pathname = bad_addr;
mreed1081534c32006-08-03 05:21:24 +0000232#endif
plars865695b2001-08-27 22:15:12 +0000233 /* call individual setup functions */
234 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
235 Test_cases[ind].setupfunc();
236 }
237}
238
239/*
240 * no_setup() - Some test conditions for mknod(2) do not any setup.
241 * Hence, this function just returns 0.
242 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400243int no_setup(void)
plars865695b2001-08-27 22:15:12 +0000244{
245 return 0;
246}
247
248/*
249 * longpath_setup() - setup to create a node with a name length exceeding
250 * the MAX. length of PATH_MAX.
251 * This function retruns 0.
252 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400253int longpath_setup(void)
plars865695b2001-08-27 22:15:12 +0000254{
255 int ind; /* counter variable */
256
257 for (ind = 0; ind <= (PATH_MAX + 1); ind++) {
258 Longpathname[ind] = 'a';
259 }
260 return 0;
261}
subrata_modakbdbaec52009-02-26 12:14:51 +0000262
plars865695b2001-08-27 22:15:12 +0000263/*
264 * setup1() - setup function for a test condition for which mknod(2)
265 * returns -1 and sets errno to EEXIST.
266 * This function creates a node using mknod(2) and tries to create
267 * same node in the test and fails with above errno.
268 * This function returns 0.
269 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400270int setup1(void)
plars865695b2001-08-27 22:15:12 +0000271{
272 /* Create a node using mknod */
273 if (mknod("tnode_1", MODE_RWX, 0) < 0) {
274 tst_brkm(TBROK, cleanup, "Fails to create node in setup1()");
275 }
276
277 return 0;
278}
279
280/*
281 * setup3() - setup function for a test condition for which mknod(2)
282 * returns -1 and sets errno to ENOTDIR.
283 * This function creates a node under temporary directory and the
284 * test attempts to create another node under under this node and fails
285 * with ENOTDIR as the node created here is a regular file.
286 * This function returns 0.
287 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400288int setup3(void)
plars865695b2001-08-27 22:15:12 +0000289{
290 /* Create a node using mknod */
291 if (mknod("tnode", MODE_RWX, 0) < 0) {
292 tst_brkm(TBROK, cleanup, "Fails to create node in setup3()");
293 }
294
295 return 0;
296}
297
298/*
299 * cleanup() - Performs all ONE TIME cleanup for this test at
300 * completion or premature exit.
301 * Print test timing stats and errno log if test executed with options.
302 * Remove temporary directory and sub-directories/files under it
303 * created during setup().
304 * Exit the test program with normal exit code.
305 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400306void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000307{
plars865695b2001-08-27 22:15:12 +0000308
plars865695b2001-08-27 22:15:12 +0000309 tst_rmdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000310
Chris Dearmanec6edca2012-10-17 19:54:01 -0700311}