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