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 | * setrlimit02.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to test the different errnos set by setrlimit(2) system call. |
| 26 | * |
| 27 | * USAGE: <for command-line> |
| 28 | * setrlimit02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 29 | * where, -c n : Run n copies concurrently. |
| 30 | * -e : Turn on errno logging. |
| 31 | * -i n : Execute test n times. |
| 32 | * -I x : Execute test for x seconds. |
| 33 | * -P x : Pause for x seconds between iterations. |
| 34 | * -t : Turn on syscall timing. |
| 35 | * |
| 36 | * HISTORY |
| 37 | * 07/2001 Ported by Wayne Boyer |
| 38 | * |
| 39 | * RESTRICTIONS |
| 40 | * NONE |
| 41 | */ |
| 42 | #include <sys/time.h> |
| 43 | #include <sys/resource.h> |
| 44 | #include <unistd.h> |
| 45 | #include <errno.h> |
robbiew | 1b8acfb | 2001-09-13 19:45:25 +0000 | [diff] [blame] | 46 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | #include "test.h" |
| 48 | #include "usctest.h" |
| 49 | |
| 50 | char *TCID = "setrlimit02"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | extern int Tst_count; |
| 52 | |
robbiew | 1b8acfb | 2001-09-13 19:45:25 +0000 | [diff] [blame] | 53 | char nobody_uid[] = "nobody"; |
| 54 | struct passwd *ltpuser; |
| 55 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | struct rlimit rlim; |
| 57 | |
| 58 | void setup(); |
| 59 | void cleanup(); |
| 60 | |
| 61 | int exp_enos[]={EFAULT, EINVAL, EPERM, 0}; |
| 62 | |
| 63 | struct test_case_t { |
| 64 | int resource; |
| 65 | struct rlimit *rlim; |
| 66 | int error; |
| 67 | } TC[] = { |
| 68 | /* rlim points outside the process address space - EFAULT */ |
| 69 | {RLIMIT_NOFILE, (void *)-1, EFAULT}, |
| 70 | |
| 71 | /* the resource is invalid - EINVAL */ |
| 72 | {-1, &rlim, EINVAL}, |
| 73 | |
| 74 | /* a non-root user attemps to increase the rlim_max value - EPERM */ |
| 75 | {RLIMIT_NOFILE, &rlim, EPERM} |
| 76 | }; |
| 77 | |
vapier | 70df7d7 | 2006-03-04 00:52:53 +0000 | [diff] [blame^] | 78 | int TST_TOTAL = sizeof(TC)/sizeof(*TC); |
| 79 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 80 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | { |
| 82 | int lc; /* loop counter */ |
| 83 | char *msg; /* message returned from parse_opts */ |
| 84 | int i; |
| 85 | |
| 86 | /* parse standard options */ |
| 87 | if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ |
| 88 | tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
| 89 | /*NOTREACHED*/ |
| 90 | } |
| 91 | |
| 92 | setup(); |
| 93 | |
| 94 | /* set up the expected errnos */ |
| 95 | TEST_EXP_ENOS(exp_enos); |
| 96 | |
| 97 | /* check looping state if -i option given */ |
| 98 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 99 | |
| 100 | /* reset Tst_count in case we are looping. */ |
| 101 | Tst_count = 0; |
| 102 | |
| 103 | /* loop through the test cases */ |
| 104 | for (i = 0; i < TST_TOTAL; i++) { |
| 105 | |
| 106 | TEST(setrlimit(TC[i].resource, TC[i].rlim)); |
| 107 | |
| 108 | if (TEST_RETURN != -1) { |
| 109 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | TEST_ERROR_LOG(TEST_ERRNO); |
| 114 | |
| 115 | if (TEST_ERRNO == TC[i].error) { |
| 116 | tst_resm(TPASS, "expected failure - " |
| 117 | "errno = %d : %s", TEST_ERRNO, |
| 118 | strerror(TEST_ERRNO)); |
| 119 | } else { |
| 120 | tst_resm(TFAIL, "unexpected error - %d : %s - " |
| 121 | "expected %d", TEST_ERRNO, |
| 122 | strerror(TEST_ERRNO), TC[i].error); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | cleanup(); |
| 127 | |
| 128 | /*NOTREACHED*/ |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 129 | |
| 130 | return(0); |
| 131 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* |
| 135 | * setup() - performs all ONE TIME setup for this test. |
| 136 | */ |
| 137 | void |
| 138 | setup() |
| 139 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | |
| 141 | /* capture signals */ |
| 142 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 143 | |
| 144 | /* Pause if that option was specified */ |
| 145 | TEST_PAUSE; |
| 146 | |
robbiew | 1b8acfb | 2001-09-13 19:45:25 +0000 | [diff] [blame] | 147 | /* Switch to nobody user for correct error code collection */ |
| 148 | if (geteuid() != 0) { |
| 149 | tst_brkm(TBROK, tst_exit, "Test must be run as root"); |
| 150 | } |
| 151 | ltpuser = getpwnam(nobody_uid); |
| 152 | if (setuid(ltpuser->pw_uid) == -1) { |
| 153 | tst_resm(TINFO, "setuid failed to " |
| 154 | "to set the effective uid to %d", |
| 155 | ltpuser->pw_uid); |
| 156 | perror("setuid"); |
| 157 | } |
| 158 | |
| 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | /* set an illegal value for a non-root user - test #3 - EPERM */ |
robbiew | 1806c8f | 2005-05-26 20:34:22 +0000 | [diff] [blame] | 161 | getrlimit(RLIMIT_NOFILE, &rlim); |
| 162 | rlim.rlim_max ++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* |
| 166 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 167 | * completion or premature exit. |
| 168 | */ |
| 169 | void |
| 170 | cleanup() |
| 171 | { |
| 172 | /* |
| 173 | * print timing stats if that option was specified. |
| 174 | * print errno log if that option was specified. |
| 175 | */ |
| 176 | TEST_CLEANUP; |
| 177 | |
| 178 | /* exit with return code appropriate for results */ |
| 179 | tst_exit(); |
| 180 | } |