xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sched_setparam/sched_setparam04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2021, BELLSOFT. All rights reserved.
4  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
5  * Author: Saji Kumar.V.R <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Verify that:
12  *
13  * 1. sched_setparam(2) returns -1 and sets errno to ESRCH if the
14  *    process with specified pid could not be found.
15  * 2. sched_setparam(2) returns -1 and sets errno to EINVAL if
16  *    the parameter pid is an invalid value (-1).
17  * 3. sched_setparam(2) returns -1 and sets errno to EINVAL if the
18  *    parameter p is an invalid address.
19  * 4. sched_setparam(2) returns -1 sets errno to EINVAL if the
20  *    value for p.sched_priority is other than 0 for scheduling
21  *    policy, SCHED_OTHER.
22  */
23 
24 #include "tst_test.h"
25 #include "tst_sched.h"
26 
27 static struct sched_param p = { .sched_priority = 0 };
28 static struct sched_param p1 = { .sched_priority = 1 };
29 
30 static pid_t unused_pid;
31 static pid_t inval_pid = -1;
32 static pid_t zero_pid;
33 
34 static struct test_cases_t {
35 	char *desc;
36 	pid_t *pid;
37 	struct sched_param *p;
38 	int exp_errno;
39 } tcases[] = {
40 	{
41 	"test with non-existing pid", &unused_pid, &p, ESRCH}, {
42 	"test invalid pid value", &inval_pid, &p, EINVAL,}, {
43 	"test with invalid address for p", &zero_pid, NULL, EINVAL}, {
44 	"test with invalid p.sched_priority", &zero_pid, &p1, EINVAL}
45 };
46 
setup(void)47 static void setup(void)
48 {
49 	tst_res(TINFO, "Testing %s variant", sched_variants[tst_variant].desc);
50 
51 	unused_pid = tst_get_unused_pid();
52 }
53 
run(unsigned int n)54 static void run(unsigned int n)
55 {
56 	struct sched_variant *tv = &sched_variants[tst_variant];
57 	struct test_cases_t *tc = &tcases[n];
58 
59 	TST_EXP_FAIL(tv->sched_setparam(*tc->pid, tc->p), tc->exp_errno, "%s", tc->desc);
60 }
61 
62 static struct tst_test test = {
63 	.setup = setup,
64 	.test = run,
65 	.test_variants = ARRAY_SIZE(sched_variants),
66 	.tcnt = ARRAY_SIZE(tcases),
67 };
68