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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * chroot01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check the whether chroot sets errno to EPERM. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * As a non-root user attempt to perform chroot() to a directory. The |
| 29 | * chroot() call should fail with EPERM |
| 30 | * |
| 31 | * USAGE: <for command-line> |
| 32 | * chroot01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 33 | * where, -c n : Run n copies concurrently. |
| 34 | * -e : Turn on errno logging. |
| 35 | * -i n : Execute test n times. |
| 36 | * -I x : Execute test for x seconds. |
| 37 | * -P x : Pause for x seconds between iterations. |
| 38 | * -t : Turn on syscall timing. |
| 39 | * |
| 40 | * HISTORY |
| 41 | * 07/2001 Ported by Wayne Boyer |
| 42 | * |
| 43 | * RESTRICTIONS |
| 44 | * Must be run as non-root user. |
| 45 | */ |
| 46 | |
| 47 | #include <stdio.h> |
| 48 | #include <errno.h> |
| 49 | #include "test.h" |
| 50 | #include "usctest.h" |
robbiew | 096b081 | 2001-08-31 15:32:25 +0000 | [diff] [blame^] | 51 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
| 53 | char *TCID = "chroot01"; |
| 54 | int TST_TOTAL = 1; |
| 55 | extern int Tst_count; |
| 56 | int fail; |
| 57 | |
| 58 | char path[] = "/tmp"; |
| 59 | int exp_enos[] = {EPERM, 0}; |
robbiew | 096b081 | 2001-08-31 15:32:25 +0000 | [diff] [blame^] | 60 | char nobody_uid[] = "nobody"; |
| 61 | struct passwd *ltpuser; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
| 63 | void setup(void); |
| 64 | void cleanup(void); |
| 65 | |
| 66 | main(int ac, char **av) |
| 67 | { |
| 68 | int lc; |
| 69 | char *msg; |
| 70 | |
| 71 | /* parse standard options */ |
| 72 | if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ |
| 73 | tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); |
| 74 | } |
| 75 | |
| 76 | setup(); |
| 77 | |
| 78 | /* set up expected errnos */ |
| 79 | TEST_EXP_ENOS(exp_enos); |
| 80 | |
| 81 | /* Check for looping state if -i option is given */ |
| 82 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 83 | |
| 84 | /* reset Tst_count in case we are looping */ |
| 85 | Tst_count = 0; |
| 86 | |
| 87 | TEST(chroot(path)); |
| 88 | |
| 89 | if (TEST_RETURN != -1) { |
| 90 | tst_resm(TFAIL, "call succeeded on expected fail"); |
| 91 | } else if (errno != EPERM) { |
| 92 | tst_resm(TFAIL, "Received unexpected error - %d : %s", |
| 93 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 94 | } else { |
| 95 | TEST_ERROR_LOG(TEST_ERRNO); |
| 96 | tst_resm(TPASS, "chroot set errno to EPERM."); |
| 97 | } |
| 98 | } |
| 99 | cleanup(); |
| 100 | |
| 101 | /*NOTREACHED*/ |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * setup() - performs all ONE TIME setup for this test. |
| 106 | */ |
| 107 | void |
| 108 | setup() |
| 109 | { |
robbiew | 096b081 | 2001-08-31 15:32:25 +0000 | [diff] [blame^] | 110 | |
| 111 | /* Switch to nobody user for correct error code collection */ |
| 112 | if (geteuid() != 0) { |
| 113 | tst_brkm(TBROK, tst_exit, "Test must be run as root"); |
| 114 | } |
| 115 | ltpuser = getpwnam(nobody_uid); |
| 116 | if (seteuid(ltpuser->pw_uid) == -1) { |
| 117 | tst_resm(TINFO, "seteuid failed to " |
| 118 | "to set the effective uid to %d", |
| 119 | ltpuser->pw_uid); |
| 120 | perror("seteuid"); |
| 121 | } |
| 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | |
| 124 | /* capture signals */ |
| 125 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 126 | |
| 127 | /* Pause if that option was specified */ |
| 128 | TEST_PAUSE; |
| 129 | |
| 130 | /* make a temporary directory and cd to it */ |
| 131 | tst_tmpdir(); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* |
| 136 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 137 | * completion or premature exit. |
| 138 | */ |
| 139 | void |
| 140 | cleanup() |
| 141 | { |
| 142 | /* |
| 143 | * print timing stats if that option was specified. |
| 144 | * print errno log if that option was specified. |
| 145 | */ |
| 146 | TEST_CLEANUP; |
| 147 | |
| 148 | /* delete the test directory created in setup() */ |
| 149 | tst_rmdir(); |
| 150 | |
| 151 | /* exit with return code appropriate for results */ |
| 152 | tst_exit(); |
| 153 | } |
| 154 | |