xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sched_setattr/sched_setattr01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd., 2015
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  *  (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11  * the GNU General Public License for more details.
12  */
13  /* Description:
14  *   Verify that:
15  *              1) sched_setattr succeed with correct parameters
16  *              2) sched_setattr fails with unused pid
17  *              3) sched_setattr fails with invalid address
18  *              4) sched_setattr fails with invalid flag
19  */
20 
21 #define _GNU_SOURCE
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <linux/unistd.h>
28 #include <linux/kernel.h>
29 #include <linux/types.h>
30 #include <sys/syscall.h>
31 #include <pthread.h>
32 #include <errno.h>
33 
34 #include "test.h"
35 #include "lapi/sched.h"
36 
37 char *TCID = "sched_setattr01";
38 
39 #define RUNTIME_VAL 10000000
40 #define PERIOD_VAL 30000000
41 #define DEADLINE_VAL 30000000
42 
43 static pid_t pid;
44 static pid_t unused_pid;
45 
46 static struct sched_attr attr = {
47 	.size = sizeof(struct sched_attr),
48 	.sched_flags = 0,
49 	.sched_nice = 0,
50 	.sched_priority = 0,
51 
52 	.sched_policy = SCHED_DEADLINE,
53 	.sched_runtime = RUNTIME_VAL,
54 	.sched_period = PERIOD_VAL,
55 	.sched_deadline = DEADLINE_VAL,
56 };
57 
58 static struct test_case {
59 	pid_t *pid;
60 	struct sched_attr *a;
61 	unsigned int flags;
62 	int exp_return;
63 	int exp_errno;
64 } test_cases[] = {
65 	{&pid, &attr, 0, 0, 0},
66 	{&unused_pid, &attr, 0, -1, ESRCH},
67 	{&pid, NULL, 0, -1, EINVAL},
68 	{&pid, &attr, 1000, -1, EINVAL}
69 };
70 
71 static void setup(void);
72 static void sched_setattr_verify(const struct test_case *test);
73 
74 int TST_TOTAL = ARRAY_SIZE(test_cases);
75 
do_test(void * data LTP_ATTRIBUTE_UNUSED)76 void *do_test(void *data LTP_ATTRIBUTE_UNUSED)
77 {
78 	int i;
79 
80 	for (i = 0; i < TST_TOTAL; i++)
81 		sched_setattr_verify(&test_cases[i]);
82 
83 	return NULL;
84 }
85 
sched_setattr_verify(const struct test_case * test)86 static void sched_setattr_verify(const struct test_case *test)
87 {
88 	TEST(sched_setattr(*(test->pid), test->a, test->flags));
89 
90 	if (TEST_RETURN != test->exp_return) {
91 		tst_resm(TFAIL | TTERRNO, "sched_setattr(%i,attr,%u) "
92 		         "returned: %ld expected: %d",
93 		         *(test->pid), test->flags,
94 		         TEST_RETURN, test->exp_return);
95 		return;
96 	}
97 
98 	if (TEST_ERRNO == test->exp_errno) {
99 		tst_resm(TPASS | TTERRNO,
100 			"sched_setattr() works as expected");
101 		return;
102 	}
103 
104 	tst_resm(TFAIL | TTERRNO, "sched_setattr(%i,attr,%u): "
105 		"expected: %d - %s",
106 		*(test->pid), test->flags,
107 		test->exp_errno, tst_strerrno(test->exp_errno));
108 }
109 
main(int argc,char ** argv)110 int main(int argc, char **argv)
111 {
112 	pthread_t thread;
113 	int lc;
114 
115 	tst_parse_opts(argc, argv, NULL, NULL);
116 
117 	setup();
118 
119 	for (lc = 0; TEST_LOOPING(lc); lc++) {
120 		pthread_create(&thread, NULL, do_test, NULL);
121 		pthread_join(thread, NULL);
122 	}
123 
124 	tst_exit();
125 }
126 
setup(void)127 void setup(void)
128 {
129 	unused_pid = tst_get_unused_pid(setup);
130 
131 	tst_require_root();
132 
133 	if ((tst_kvercmp(3, 14, 0)) < 0)
134 		tst_brkm(TCONF, NULL, "EDF needs kernel 3.14 or higher");
135 
136 	TEST_PAUSE;
137 }
138