1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  * Ported to LTP: Wayne Boyer
5  *  11/2016 Modified by Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
6  */
7 
8 /*
9  * Verify that,
10  *  1) getpriority(2) fails with -1 and sets errno to EINVAL if 'which'
11  *     argument was not one of PRIO_PROCESS, PRIO_PGRP, or PRIO_USER.
12  *  2) getpriority(2) fails with -1 and sets errno to ESRCH if no
13  *     process was located for 'which' and 'who' arguments.
14  */
15 
16 #include <errno.h>
17 #include <sys/resource.h>
18 #include <sys/time.h>
19 #include "tst_test.h"
20 
21 #define INVAL_FLAG	-1
22 #define INVAL_ID	-1
23 
24 static struct tcase {
25 	int which;
26 	int who;
27 	int exp_errno;
28 } tcases[] = {
29 	/* test 1 */
30 	{INVAL_FLAG, 0, EINVAL},
31 
32 	/* test 2 */
33 	{PRIO_PROCESS, INVAL_ID, ESRCH},
34 	{PRIO_PGRP, INVAL_ID, ESRCH},
35 	{PRIO_USER, INVAL_ID, ESRCH}
36 };
37 
verify_getpriority(unsigned int n)38 static void verify_getpriority(unsigned int n)
39 {
40 	struct tcase *tc = &tcases[n];
41 
42 	TEST(getpriority(tc->which, tc->who));
43 
44 	if (TST_RET != -1) {
45 		tst_res(TFAIL, "getpriority(%d, %d) succeeds unexpectedly, "
46 			       "returned %li", tc->which, tc->who, TST_RET);
47 		return;
48 	}
49 
50 	if (tc->exp_errno != TST_ERR) {
51 		tst_res(TFAIL | TTERRNO,
52 			"getpriority(%d, %d) should fail with %s",
53 			tc->which, tc->who, tst_strerrno(tc->exp_errno));
54 		return;
55 	}
56 
57 	tst_res(TPASS | TTERRNO, "getpriority(%d, %d) fails as expected",
58 		tc->which, tc->who);
59 }
60 
61 static struct tst_test test = {
62 	.tcnt = ARRAY_SIZE(tcases),
63 	.test = verify_getpriority,
64 };
65