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: nice02 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that any user can successfully increase the nice value of |
| 25 | * the process by passing a higher increment value (> max. applicable limits) |
| 26 | * to nice() system call. |
| 27 | * |
| 28 | * Expected Result: |
| 29 | * nice() should return value 0 on success and root user should succeed to |
| 30 | * increase the nice value of the test process. |
| 31 | * |
| 32 | * Algorithm: |
| 33 | * Setup: |
| 34 | * Setup signal handling. |
| 35 | * Pause for SIGUSR1 if option specified. |
| 36 | * |
| 37 | * Test: |
| 38 | * Loop if the proper options are given. |
| 39 | * Execute system call |
| 40 | * Check return code, if system call failed (return=-1) |
| 41 | * Log the errno and Issue a FAIL message. |
| 42 | * Otherwise, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 43 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * if successful, |
| 45 | * Issue Functionality-Pass message. |
| 46 | * Otherwise, |
| 47 | * Issue Functionality-Fail message. |
| 48 | * Cleanup: |
| 49 | * Print errno log and/or timing stats if options given |
| 50 | * |
| 51 | * Usage: <for command-line> |
| 52 | * nice02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 53 | * where, -c n : Run n copies concurrently. |
| 54 | * -f : Turn off functionality Testing. |
| 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 | * none |
| 65 | */ |
| 66 | #include <unistd.h> |
| 67 | #include <errno.h> |
| 68 | #include <fcntl.h> |
| 69 | #include <sys/time.h> |
| 70 | #include <sys/resource.h> |
| 71 | |
| 72 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 74 | char *TCID = "nice02"; |
| 75 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | |
| 77 | #define NICEINC 50 |
| 78 | |
| 79 | void setup(); /* Main setup function of test */ |
| 80 | void cleanup(); /* cleanup function for the test */ |
| 81 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 84 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 85 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | int New_nice; /* priority of process after nice() */ |
| 87 | int max_val; /* Maximum nice value per OS. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 89 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 90 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | setup(); |
| 93 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 95 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 96 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 98 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | * Call nice(2) with an 'incr' parameter set |
| 100 | * to a higher +ve value. |
| 101 | */ |
| 102 | TEST(nice(NICEINC)); |
| 103 | |
| 104 | /* check return code */ |
| 105 | if (TEST_RETURN == -1) { |
| 106 | tst_resm(TFAIL, "nice(%d) Failed, errno=%d : %s", |
| 107 | NICEINC, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 112 | * Get the current priority of the test process. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 114 | errno = 0; |
| 115 | New_nice = getpriority(PRIO_PROCESS, 0); |
| 116 | if (New_nice == -1 && errno != 0) { |
| 117 | tst_brkm(TFAIL, cleanup, "Fail to get priority " |
| 118 | "of process after nice()"); |
| 119 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 121 | /* |
| 122 | * Validate functionality of the nice(). |
| 123 | * |
| 124 | * Default priority is 0, Max is 20. |
| 125 | */ |
| 126 | max_val = 20; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 128 | if (New_nice != (max_val - 1)) { |
| 129 | tst_resm(TFAIL, "Priority of process : %d " |
| 130 | "doesn't match the expected:%d", |
| 131 | New_nice, (max_val - 1)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 133 | tst_resm(TPASS, "Functionality of nice(%d)" |
| 134 | " successful", NICEINC); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 136 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 139 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 140 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * setup() - performs all ONE TIME setup for this test. |
| 144 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 145 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 147 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 149 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | TEST_PAUSE; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 155 | * completion or premature exit. |
| 156 | * Remove the test directory and testfile created in the setup. |
| 157 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 158 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 161 | } |