blob: aad692242541656a7b4d1415589409f253eab11a [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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"
plars865695b2001-08-27 22:15:12 +000080
81#define INVAL_PID -1
82#define INVAL_FLAG -1
83
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020084char *TCID = "getpriority02";
85int TST_TOTAL = 4;
plars865695b2001-08-27 22:15:12 +000086
subrata_modak56207ce2009-03-23 13:35:39 +000087struct test_case_t { /* test case struct. to hold ref. test cond's */
plars865695b2001-08-27 22:15:12 +000088 int pro_which;
89 uid_t pro_uid;
90 char *desc;
91 int exp_errno;
92} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000093 {
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}
plars865695b2001-08-27 22:15:12 +000099};
100
101void setup(); /* Main setup function of test */
102void cleanup(); /* cleanup function for the test */
103
subrata_modak56207ce2009-03-23 13:35:39 +0000104int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000105{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200106 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200107 const char *msg;
plars865695b2001-08-27 22:15:12 +0000108 int ind; /* counter variable for test case looping */
subrata_modak56207ce2009-03-23 13:35:39 +0000109 char *test_desc; /* test specific error message */
plars865695b2001-08-27 22:15:12 +0000110 int which; /* process priority category */
111 uid_t who; /* process uid of the test process */
subrata_modak56207ce2009-03-23 13:35:39 +0000112
Garrett Cooper45e285d2010-11-22 12:19:25 -0800113 msg = parse_opts(ac, av, NULL, NULL);
114 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000115 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800116
plars865695b2001-08-27 22:15:12 +0000117 }
118
plars865695b2001-08-27 22:15:12 +0000119 setup();
120
plars865695b2001-08-27 22:15:12 +0000121 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800122
Caspar Zhangd59a6592013-03-07 14:59:12 +0800123 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000124
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_modak4bb656a2009-02-26 12:02:09 +0000135 /*
plars865695b2001-08-27 22:15:12 +0000136 * Invoke getpriority with the specified
137 * 'which' and 'who' arguments and verify
138 * that it fails with expected errno.
139 */
plars865695b2001-08-27 22:15:12 +0000140 TEST(getpriority(which, who));
141
142 /* check return code from getpriority(2) */
143 if (TEST_RETURN < 0) {
plars865695b2001-08-27 22:15:12 +0000144 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_modak358c3ee2009-10-26 14:55:46 +0000155 tst_resm(TFAIL, "getpriority() returned %ld, "
plars865695b2001-08-27 22:15:12 +0000156 "expected -1, errno:%d", TEST_RETURN,
157 Test_cases[ind].exp_errno);
158 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800159 }
plars865695b2001-08-27 22:15:12 +0000160
Garrett Cooper2c282152010-12-16 00:55:50 -0800161 }
plars865695b2001-08-27 22:15:12 +0000162
plars865695b2001-08-27 22:15:12 +0000163 cleanup();
164
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800165 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800166}
plars865695b2001-08-27 22:15:12 +0000167
168/*
169 * setup() - performs all ONE TIME setup for this test.
170 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400171void setup(void)
plars865695b2001-08-27 22:15:12 +0000172{
Garrett Cooper2c282152010-12-16 00:55:50 -0800173
plars865695b2001-08-27 22:15:12 +0000174 tst_sig(NOFORK, DEF_HANDLER, cleanup);
175
plars865695b2001-08-27 22:15:12 +0000176 TEST_PAUSE;
177}
178
179/*
180 * cleanup() - performs all ONE TIME cleanup for this test at
181 * completion or premature exit.
182 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400183void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000184{
plars865695b2001-08-27 22:15:12 +0000185
Chris Dearmanec6edca2012-10-17 19:54:01 -0700186}