plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 43 | * if errno set == expected errno |
| 44 | * Issue sys call fails with expected return value and errno. |
| 45 | * Otherwise, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | * 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 Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 88 | void setup2(); /* setup function to test mknod for EACCES */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | struct test_case_t { /* test case struct. to hold ref. test cond's */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | char *pathname; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | int mode; |
| 93 | int exp_errno; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 94 | void (*setupfunc) (void); |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 95 | } test_cases[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 96 | { |
| 97 | "tnode_1", SOCKET_MODE, EACCES, NULL}, { |
| 98 | "tnode_2", NEWMODE, EACCES, setup2},}; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | char *TCID = "mknod07"; /* Test program identifier. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | int TST_TOTAL = 2; /* Total number of test cases. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | int exp_enos[] = { EPERM, EACCES, 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
robbiew | 7accaf4 | 2001-09-04 20:09:52 +0000 | [diff] [blame] | 104 | char nobody_uid[] = "nobody"; |
| 105 | struct passwd *ltpuser; |
| 106 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | void setup(); /* setup function for the tests */ |
| 108 | void cleanup(); /* cleanup function for the tests */ |
| 109 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 112 | int lc; |
| 113 | char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | char *node_name; /* ptr. for node name created */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 115 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | int mode; /* creation mode for the node created */ |
| 117 | |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 118 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | setup(); |
| 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | TEST_EXP_ENOS(exp_enos); |
| 124 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 126 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame^] | 127 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 129 | for (i = 0; i < TST_TOTAL; i++) { |
| 130 | node_name = test_cases[i].pathname; |
| 131 | mode = test_cases[i].mode; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | TEST(mknod(node_name, mode, 0)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 134 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | if (TEST_RETURN != -1) { |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 136 | tst_resm(TFAIL, "mknod succeeded unexpectedly"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | continue; |
| 138 | } |
| 139 | |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 140 | if (TEST_ERRNO == test_cases[i].exp_errno) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | tst_resm(TPASS | TTERRNO, |
| 142 | "mknod failed as expected"); |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 143 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 144 | 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 148 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 150 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | cleanup(); |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 153 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 154 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | { |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 158 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 160 | tst_require_root(NULL); |
| 161 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 163 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | ltpuser = getpwnam(nobody_uid); |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 165 | if (ltpuser == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 166 | tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed"); |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 167 | if (seteuid(ltpuser->pw_uid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 168 | tst_brkm(TBROK | TERRNO, NULL, "setuid failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | TEST_PAUSE; |
| 171 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | tst_tmpdir(); |
| 173 | |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 174 | if (mkdir(DIR_TEMP, MODE_RWX) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 175 | tst_brkm(TBROK | TERRNO, cleanup, "mkdir failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 177 | if (chmod(DIR_TEMP, MODE_RWX) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 178 | tst_brkm(TBROK | TERRNO, cleanup, "chmod failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 180 | if (chdir(DIR_TEMP) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 181 | tst_brkm(TBROK | TERRNO, cleanup, "chdir failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 183 | for (i = 0; i < TST_TOTAL; i++) |
| 184 | if (test_cases[i].setupfunc != NULL) |
| 185 | test_cases[i].setupfunc(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 188 | void setup2() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | { |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 190 | if (chmod(".", NEWMODE) < 0) |
| 191 | tst_brkm(TBROK, cleanup, "chmod failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | } |
| 193 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 194 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | TEST_CLEANUP; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 197 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 198 | if (seteuid(0) == -1) |
Garrett Cooper | 4d0292e | 2010-12-19 06:53:26 -0800 | [diff] [blame] | 199 | tst_resm(TBROK, "seteuid(0) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | tst_rmdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 202 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 203 | } |