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: getpriority02 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, |
| 25 | * 1) getpriority() sets errno to ESRCH if no process was located |
| 26 | * was located for 'which' and 'who' arguments. |
| 27 | * 2) getpriority() sets errno to EINVAL if 'which' argument was |
| 28 | * not one of PRIO_PROCESS, PRIO_PGRP, or PRIO_USER. |
| 29 | * |
| 30 | * Expected Result: |
| 31 | * getpriority() should fail with return value -1 and set expected errno. |
| 32 | * |
| 33 | * Algorithm: |
| 34 | * Setup: |
| 35 | * Setup signal handling. |
| 36 | * Pause for SIGUSR1 if option specified. |
| 37 | * |
| 38 | * Test: |
| 39 | * Loop if the proper options are given. |
| 40 | * Execute system call |
| 41 | * Check return code, if system call failed (return=-1) |
| 42 | * if errno set == expected errno |
| 43 | * Issue sys call fails with expected return value and errno. |
| 44 | * Otherwise, |
| 45 | * Issue sys call fails with unexpected errno. |
| 46 | * Otherwise, |
| 47 | * Issue sys call returns unexpected value. |
| 48 | * |
| 49 | * Cleanup: |
| 50 | * Print errno log and/or timing stats if options given |
| 51 | * |
| 52 | * Usage: <for command-line> |
| 53 | * getpriority02 [-c n] [-e] [-i n] [-I x] [-p x] [-t] |
| 54 | * where, -c n : Run n copies concurrently. |
| 55 | * -e : Turn on errno logging. |
| 56 | * -i n : Execute test n times. |
| 57 | * -I x : Execute test for x seconds. |
| 58 | * -P x : Pause for x seconds between iterations. |
| 59 | * -t : Turn on syscall timing. |
| 60 | * |
| 61 | * HISTORY |
| 62 | * 07/2001 Ported by Wayne Boyer |
| 63 | * |
| 64 | * RESTRICTIONS: |
| 65 | * None. |
| 66 | */ |
| 67 | |
| 68 | #include <stdio.h> |
| 69 | #include <unistd.h> |
| 70 | #include <sys/types.h> |
| 71 | #include <errno.h> |
| 72 | #include <fcntl.h> |
| 73 | #include <string.h> |
| 74 | #include <signal.h> |
| 75 | #include <sys/param.h> |
| 76 | #include <sys/time.h> |
| 77 | #include <sys/resource.h> |
| 78 | |
| 79 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | #define INVAL_PID -1 |
| 82 | #define INVAL_FLAG -1 |
| 83 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 84 | char *TCID = "getpriority02"; |
| 85 | int TST_TOTAL = 4; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | struct test_case_t { /* test case struct. to hold ref. test cond's */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | int pro_which; |
| 89 | uid_t pro_uid; |
| 90 | char *desc; |
| 91 | int exp_errno; |
| 92 | } Test_cases[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | { |
| 94 | INVAL_FLAG, 0, "Invalid 'which' value specified", EINVAL}, { |
| 95 | PRIO_PROCESS, INVAL_PID, "Invalid 'who' value specified", ESRCH}, { |
| 96 | PRIO_PGRP, INVAL_PID, "Invalid 'who' value specified", ESRCH}, { |
| 97 | PRIO_USER, INVAL_PID, "Invalid 'who' value specified", ESRCH}, { |
| 98 | 0, 0, NULL, 0} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | void setup(); /* Main setup function of test */ |
| 102 | void cleanup(); /* cleanup function for the test */ |
| 103 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 106 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 107 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | int ind; /* counter variable for test case looping */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | char *test_desc; /* test specific error message */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | int which; /* process priority category */ |
| 111 | uid_t who; /* process uid of the test process */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 113 | msg = parse_opts(ac, av, NULL, NULL); |
| 114 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 116 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | } |
| 118 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | setup(); |
| 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 122 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 123 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | |
| 125 | for (ind = 0; ind < TST_TOTAL; ind++) { |
| 126 | which = Test_cases[ind].pro_which; |
| 127 | who = Test_cases[ind].pro_uid; |
| 128 | test_desc = Test_cases[ind].desc; |
| 129 | |
| 130 | if (who == 0) { |
| 131 | /* Get the actual uid of the process */ |
| 132 | who = getuid(); |
| 133 | } |
| 134 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 135 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | * Invoke getpriority with the specified |
| 137 | * 'which' and 'who' arguments and verify |
| 138 | * that it fails with expected errno. |
| 139 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | TEST(getpriority(which, who)); |
| 141 | |
| 142 | /* check return code from getpriority(2) */ |
| 143 | if (TEST_RETURN < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | if (TEST_ERRNO == Test_cases[ind].exp_errno) { |
| 145 | tst_resm(TPASS, "getpriority(2) fails, " |
| 146 | "%s, errno:%d", |
| 147 | test_desc, TEST_ERRNO); |
| 148 | } else { |
| 149 | tst_resm(TFAIL, "getpriority() fails, " |
| 150 | "%s, errno:%d, expected errno:" |
| 151 | "%d", test_desc, TEST_ERRNO, |
| 152 | Test_cases[ind].exp_errno); |
| 153 | } |
| 154 | } else { |
subrata_modak | 358c3ee | 2009-10-26 14:55:46 +0000 | [diff] [blame] | 155 | tst_resm(TFAIL, "getpriority() returned %ld, " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | "expected -1, errno:%d", TEST_RETURN, |
| 157 | Test_cases[ind].exp_errno); |
| 158 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 159 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 161 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | cleanup(); |
| 164 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 165 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 166 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * setup() - performs all ONE TIME setup for this test. |
| 170 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 171 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 173 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 175 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | TEST_PAUSE; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 181 | * completion or premature exit. |
| 182 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 183 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 186 | } |