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 * Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Verify that any user can successfully increase the nice value of
12f08c3bdfSopenharmony_ci * the process by passing an increment value (< max. applicable limits) to
13f08c3bdfSopenharmony_ci * nice() system call.
14f08c3bdfSopenharmony_ci */
15f08c3bdfSopenharmony_ci#include <unistd.h>
16f08c3bdfSopenharmony_ci#include <stdlib.h>
17f08c3bdfSopenharmony_ci#include <errno.h>
18f08c3bdfSopenharmony_ci#include <sys/resource.h>
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#define	NICEINC	2
22f08c3bdfSopenharmony_ci#define MAX_PRIO 19
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic void nice_test(void)
25f08c3bdfSopenharmony_ci{
26f08c3bdfSopenharmony_ci	int new_nice;
27f08c3bdfSopenharmony_ci	int orig_nice;
28f08c3bdfSopenharmony_ci	int exp_nice;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	orig_nice = SAFE_GETPRIORITY(PRIO_PROCESS, 0);
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	TEST(nice(NICEINC));
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
35f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "nice(%d) returned -1", NICEINC);
36f08c3bdfSopenharmony_ci		return;
37f08c3bdfSopenharmony_ci	}
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	if (TST_ERR) {
40f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "nice(%d) failed", NICEINC);
41f08c3bdfSopenharmony_ci		return;
42f08c3bdfSopenharmony_ci	}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	new_nice = SAFE_GETPRIORITY(PRIO_PROCESS, 0);
45f08c3bdfSopenharmony_ci	exp_nice = MIN(MAX_PRIO, (orig_nice + NICEINC));
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	if (new_nice != exp_nice) {
48f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Process priority %i, expected %i",
49f08c3bdfSopenharmony_ci				new_nice, exp_nice);
50f08c3bdfSopenharmony_ci		return;
51f08c3bdfSopenharmony_ci	}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	tst_res(TPASS, "nice(%d) passed", NICEINC);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	exit(0);
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic void verify_nice(void)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	if (!SAFE_FORK())
61f08c3bdfSopenharmony_ci		nice_test();
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	tst_reap_children();
64f08c3bdfSopenharmony_ci}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic struct tst_test test = {
67f08c3bdfSopenharmony_ci	.forks_child = 1,
68f08c3bdfSopenharmony_ci	.test_all = verify_nice,
69f08c3bdfSopenharmony_ci};
70