blob: e7a6fc8bbd832f5fa76c8d54b8ae2bb3beab4fb5 [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: mknod07
22 *
23 * Test Description:
24 * Verify that,
25 * 1) mknod(2) returns -1 and sets errno to EPERM if the process id of
26 * the caller is not super-user.
27 * 2) mknod(2) returns -1 and sets errno to EACCES if parent directory
28 * does not allow write permission to the process.
29 *
30 * Expected Result:
31 * mknod() should fail with return value -1 and set expected errno.
32 *
33 * Algorithm:
34 * Setup:
35 * Setup signal handling.
36 * Create temporary directory.
37 * Pause for SIGUSR1 if option specified.
38 *
39 * Test:
40 * Loop if the proper options are given.
41 * Execute system call
42 * Check return code, if system call failed (return=-1)
subrata_modak56207ce2009-03-23 13:35:39 +000043 * if errno set == expected errno
44 * Issue sys call fails with expected return value and errno.
45 * Otherwise,
plars865695b2001-08-27 22:15:12 +000046 * Issue sys call fails with unexpected errno.
47 * Otherwise,
48 * Issue sys call returns unexpected value.
49 *
50 * Cleanup:
51 * Print errno log and/or timing stats if options given
52 * Delete the temporary directory(s)/file(s) created.
53 *
54 * Usage: <for command-line>
55 * mknod07 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
56 * where, -c n : Run n copies concurrently.
57 * -e : Turn on errno logging.
58 * -i n : Execute test n times.
59 * -I x : Execute test for x seconds.
60 * -P x : Pause for x seconds between iterations.
61 * -t : Turn on syscall timing.
62 *
63 * HISTORY
64 * 07/2001 Ported by Wayne Boyer
65 *
66 * RESTRICTIONS:
67 * This test should be executed by non-super-user only.
68 */
69
70#include <stdio.h>
71#include <stdlib.h>
72#include <unistd.h>
73#include <errno.h>
74#include <string.h>
75#include <signal.h>
76#include <pwd.h>
77#include <sys/types.h>
78#include <sys/stat.h>
79
80#include "test.h"
81#include "usctest.h"
82
83#define MODE_RWX S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO
84#define NEWMODE S_IFIFO | S_IRUSR | S_IRGRP | S_IROTH
85#define SOCKET_MODE S_IFSOCK| S_IRWXU | S_IRWXG | S_IRWXO
86#define DIR_TEMP "testdir_1"
87
Garrett Cooper4d0292e2010-12-19 06:53:26 -080088void setup2(); /* setup function to test mknod for EACCES */
plars865695b2001-08-27 22:15:12 +000089
subrata_modak56207ce2009-03-23 13:35:39 +000090struct test_case_t { /* test case struct. to hold ref. test cond's */
plars865695b2001-08-27 22:15:12 +000091 char *pathname;
plars865695b2001-08-27 22:15:12 +000092 int mode;
93 int exp_errno;
Wanlong Gao354ebb42012-12-07 10:10:04 +080094 void (*setupfunc) (void);
Garrett Cooper4d0292e2010-12-19 06:53:26 -080095} test_cases[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080096 {
97 "tnode_1", SOCKET_MODE, EACCES, NULL}, {
98"tnode_2", NEWMODE, EACCES, setup2},};
plars865695b2001-08-27 22:15:12 +000099
subrata_modak56207ce2009-03-23 13:35:39 +0000100char *TCID = "mknod07"; /* Test program identifier. */
plars865695b2001-08-27 22:15:12 +0000101int TST_TOTAL = 2; /* Total number of test cases. */
subrata_modak56207ce2009-03-23 13:35:39 +0000102int exp_enos[] = { EPERM, EACCES, 0 };
plars865695b2001-08-27 22:15:12 +0000103
robbiew7accaf42001-09-04 20:09:52 +0000104char nobody_uid[] = "nobody";
105struct passwd *ltpuser;
106
plars865695b2001-08-27 22:15:12 +0000107void setup(); /* setup function for the tests */
108void cleanup(); /* cleanup function for the tests */
109
subrata_modak56207ce2009-03-23 13:35:39 +0000110int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000111{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200112 int lc;
113 char *msg;
plars865695b2001-08-27 22:15:12 +0000114 char *node_name; /* ptr. for node name created */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200115 int i;
plars865695b2001-08-27 22:15:12 +0000116 int mode; /* creation mode for the node created */
117
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800118 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000119 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800120
plars865695b2001-08-27 22:15:12 +0000121 setup();
122
plars865695b2001-08-27 22:15:12 +0000123 TEST_EXP_ENOS(exp_enos);
124
plars865695b2001-08-27 22:15:12 +0000125 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800126
Caspar Zhangd59a6592013-03-07 14:59:12 +0800127 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000128
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800129 for (i = 0; i < TST_TOTAL; i++) {
130 node_name = test_cases[i].pathname;
131 mode = test_cases[i].mode;
plars865695b2001-08-27 22:15:12 +0000132
plars865695b2001-08-27 22:15:12 +0000133 TEST(mknod(node_name, mode, 0));
subrata_modakbdbaec52009-02-26 12:14:51 +0000134
plars865695b2001-08-27 22:15:12 +0000135 if (TEST_RETURN != -1) {
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800136 tst_resm(TFAIL, "mknod succeeded unexpectedly");
plars865695b2001-08-27 22:15:12 +0000137 continue;
138 }
139
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800140 if (TEST_ERRNO == test_cases[i].exp_errno)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 tst_resm(TPASS | TTERRNO,
142 "mknod failed as expected");
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800143 else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 tst_resm(TFAIL | TTERRNO,
145 "mknod failed unexpectedly; expected: "
146 "%d - %s", test_cases[i].exp_errno,
147 strerror(test_cases[i].exp_errno));
Garrett Cooper2c282152010-12-16 00:55:50 -0800148 }
plars865695b2001-08-27 22:15:12 +0000149
Garrett Cooper2c282152010-12-16 00:55:50 -0800150 }
plars865695b2001-08-27 22:15:12 +0000151
plars865695b2001-08-27 22:15:12 +0000152 cleanup();
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800153 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800154}
plars865695b2001-08-27 22:15:12 +0000155
subrata_modak56207ce2009-03-23 13:35:39 +0000156void setup()
plars865695b2001-08-27 22:15:12 +0000157{
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800158 int i;
plars865695b2001-08-27 22:15:12 +0000159
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800160 tst_require_root(NULL);
161
plars865695b2001-08-27 22:15:12 +0000162 tst_sig(NOFORK, DEF_HANDLER, cleanup);
163
subrata_modak56207ce2009-03-23 13:35:39 +0000164 ltpuser = getpwnam(nobody_uid);
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800165 if (ltpuser == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800166 tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800167 if (seteuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 tst_brkm(TBROK | TERRNO, NULL, "setuid failed");
plars865695b2001-08-27 22:15:12 +0000169
plars865695b2001-08-27 22:15:12 +0000170 TEST_PAUSE;
171
plars865695b2001-08-27 22:15:12 +0000172 tst_tmpdir();
173
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800174 if (mkdir(DIR_TEMP, MODE_RWX) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 tst_brkm(TBROK | TERRNO, cleanup, "mkdir failed");
plars865695b2001-08-27 22:15:12 +0000176
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800177 if (chmod(DIR_TEMP, MODE_RWX) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800178 tst_brkm(TBROK | TERRNO, cleanup, "chmod failed");
plars865695b2001-08-27 22:15:12 +0000179
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800180 if (chdir(DIR_TEMP) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181 tst_brkm(TBROK | TERRNO, cleanup, "chdir failed");
plars865695b2001-08-27 22:15:12 +0000182
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800183 for (i = 0; i < TST_TOTAL; i++)
184 if (test_cases[i].setupfunc != NULL)
185 test_cases[i].setupfunc();
plars865695b2001-08-27 22:15:12 +0000186}
187
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800188void setup2()
plars865695b2001-08-27 22:15:12 +0000189{
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800190 if (chmod(".", NEWMODE) < 0)
191 tst_brkm(TBROK, cleanup, "chmod failed");
plars865695b2001-08-27 22:15:12 +0000192}
193
subrata_modak56207ce2009-03-23 13:35:39 +0000194void cleanup()
plars865695b2001-08-27 22:15:12 +0000195{
plars865695b2001-08-27 22:15:12 +0000196 TEST_CLEANUP;
subrata_modakbdbaec52009-02-26 12:14:51 +0000197
subrata_modak56207ce2009-03-23 13:35:39 +0000198 if (seteuid(0) == -1)
Garrett Cooper4d0292e2010-12-19 06:53:26 -0800199 tst_resm(TBROK, "seteuid(0) failed");
plars865695b2001-08-27 22:15:12 +0000200
plars865695b2001-08-27 22:15:12 +0000201 tst_rmdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000202
Chris Dearmanec6edca2012-10-17 19:54:01 -0700203}