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 | * 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
| 49 | char *TCID = "setrlimit02"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | |
robbiew | 1b8acfb | 2001-09-13 19:45:25 +0000 | [diff] [blame] | 51 | char nobody_uid[] = "nobody"; |
| 52 | struct passwd *ltpuser; |
| 53 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | struct rlimit rlim; |
| 55 | |
| 56 | void setup(); |
| 57 | void cleanup(); |
| 58 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | struct test_case_t { |
| 60 | int resource; |
| 61 | struct rlimit *rlim; |
| 62 | int error; |
| 63 | } TC[] = { |
mreed10 | 81534c3 | 2006-08-03 05:21:24 +0000 | [diff] [blame] | 64 | #if !defined(UCLINUX) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | /* rlim points outside the process address space - EFAULT */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | { |
| 67 | RLIMIT_NOFILE, (void *)-1, EFAULT}, |
mreed10 | 81534c3 | 2006-08-03 05:21:24 +0000 | [diff] [blame] | 68 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | /* the resource is invalid - EINVAL */ |
| 70 | { |
| 71 | -1, &rlim, EINVAL}, |
| 72 | /* a non-root user attemps to increase the rlim_max value - EPERM */ |
| 73 | { |
| 74 | RLIMIT_NOFILE, &rlim, EPERM} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Cyril Hrubis | b863a0b | 2014-09-24 13:15:29 +0200 | [diff] [blame] | 77 | int TST_TOTAL = ARRAY_SIZE(TC); |
vapier | 70df7d7 | 2006-03-04 00:52:53 +0000 | [diff] [blame] | 78 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 79 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 81 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 82 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | int i; |
| 84 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 85 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 86 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 87 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
| 89 | setup(); |
| 90 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 92 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 93 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
| 95 | /* loop through the test cases */ |
| 96 | for (i = 0; i < TST_TOTAL; i++) { |
| 97 | |
| 98 | TEST(setrlimit(TC[i].resource, TC[i].rlim)); |
| 99 | |
| 100 | if (TEST_RETURN != -1) { |
| 101 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 102 | continue; |
| 103 | } |
| 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | if (TEST_ERRNO == TC[i].error) { |
| 106 | tst_resm(TPASS, "expected failure - " |
| 107 | "errno = %d : %s", TEST_ERRNO, |
| 108 | strerror(TEST_ERRNO)); |
| 109 | } else { |
| 110 | tst_resm(TFAIL, "unexpected error - %d : %s - " |
| 111 | "expected %d", TEST_ERRNO, |
| 112 | strerror(TEST_ERRNO), TC[i].error); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | cleanup(); |
| 117 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 118 | tst_exit(); |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 119 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * setup() - performs all ONE TIME setup for this test. |
| 124 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 125 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 127 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 130 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | TEST_PAUSE; |
| 132 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | ltpuser = getpwnam(nobody_uid); |
| 135 | if (setuid(ltpuser->pw_uid) == -1) { |
| 136 | tst_resm(TINFO, "setuid failed to " |
| 137 | "to set the effective uid to %d", ltpuser->pw_uid); |
| 138 | perror("setuid"); |
| 139 | } |
robbiew | 1b8acfb | 2001-09-13 19:45:25 +0000 | [diff] [blame] | 140 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | /* set an illegal value for a non-root user - test #3 - EPERM */ |
robbiew | 1806c8f | 2005-05-26 20:34:22 +0000 | [diff] [blame] | 142 | getrlimit(RLIMIT_NOFILE, &rlim); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | rlim.rlim_max++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 148 | * completion or premature exit. |
| 149 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 150 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 153 | } |