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: nice04 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, nice(2) fails when, a non-root user attempts to increase |
| 25 | * the priority of a process by specifying a negative increment value. |
| 26 | * |
| 27 | * Expected Result: |
| 28 | * nice() returns -1 and sets errno to EPERM. |
| 29 | * |
| 30 | * Algorithm: |
| 31 | * Setup: |
| 32 | * Setup signal handling. |
| 33 | * Pause for SIGUSR1 if option specified. |
| 34 | * |
| 35 | * Test: |
| 36 | * Loop if the proper options are given. |
| 37 | * Execute system call |
| 38 | * Check return code, if system call failed (return=-1) |
| 39 | * if errno set == expected errno |
| 40 | * PASS |
| 41 | * Issue sys call fails with expected return value and errno. |
| 42 | * Otherwise, |
| 43 | * FAIL |
| 44 | * Issue sys call fails with unexpected errno. |
| 45 | * Otherwise, |
| 46 | * Issue sys call returns unexpected value. |
| 47 | * |
| 48 | * Cleanup: |
| 49 | * Print errno log and/or timing stats if options given |
| 50 | * |
| 51 | * Usage: <for command-line> |
| 52 | * nice04 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 53 | * where, -c n : Run n copies concurrently. |
| 54 | * -e : Turn on errno logging. |
| 55 | * -i n : Execute test n times. |
| 56 | * -I x : Execute test for x seconds. |
| 57 | * -P x : Pause for x seconds between iterations. |
| 58 | * -t : Turn on syscall timing. |
| 59 | * |
| 60 | * HISTORY |
| 61 | * 07/2001 Ported by Wayne Boyer |
| 62 | * |
| 63 | * RESTRICTIONS: |
| 64 | * This test should be executed by 'non-super-user' only. |
| 65 | */ |
robbiew | c277a36 | 2001-09-12 18:44:14 +0000 | [diff] [blame] | 66 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | #include <unistd.h> |
| 68 | #include <errno.h> |
| 69 | |
| 70 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 72 | char *TCID = "nice04"; |
| 73 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
robbiew | c277a36 | 2001-09-12 18:44:14 +0000 | [diff] [blame] | 75 | char nobody_uid[] = "nobody"; |
| 76 | struct passwd *ltpuser; |
| 77 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 78 | struct test_case_t { /* test case struct. to hold ref. test cond's */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | int nice_val; |
| 80 | char *desc; |
| 81 | int exp_errno; |
| 82 | } Test_cases[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | { |
| 84 | -5, "Non-root cannot specify higher priority", EPERM} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | void setup(); /* Main setup function of test */ |
| 88 | void cleanup(); /* cleanup function for the test */ |
| 89 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 92 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 93 | const char *msg; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 94 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | int incr_val; /* nice value for the process */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | char *test_desc; /* test specific error message */ |
| 97 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 98 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 99 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | setup(); |
| 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 104 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 105 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | incr_val = Test_cases[i].nice_val; |
| 109 | test_desc = Test_cases[i].desc; |
| 110 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 111 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | * Call nice(2) with different 'incr' parameter |
| 113 | * values and verify that it fails as expected. |
| 114 | */ |
| 115 | TEST(nice(incr_val)); |
| 116 | |
| 117 | /* check return code from nice(2) */ |
| 118 | if (TEST_RETURN == -1) { |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 119 | tst_resm(TPASS, "nice(2) returned %ld for %s", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | TEST_RETURN, test_desc); |
| 121 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | tst_resm(TFAIL | TTERRNO, |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 123 | "nice() returned %ld for %s", |
| 124 | TEST_RETURN, test_desc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 130 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 132 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * setup() - performs all ONE TIME setup for this test. |
| 136 | * Make sure the test process uid is non-root only. |
| 137 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 138 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 140 | tst_require_root(NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 141 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 143 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | ltpuser = getpwnam(nobody_uid); |
| 146 | if (setuid(ltpuser->pw_uid) == -1) { |
| 147 | tst_resm(TINFO, "setuid failed to " |
| 148 | "to set the effective uid to %d", ltpuser->pw_uid); |
| 149 | perror("setuid"); |
| 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 | TEST_PAUSE; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 157 | * completion or premature exit. |
| 158 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 159 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 162 | } |