xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sched_getattr/sched_getattr02.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 
14  /* Description:
15  *   Verify that:
16  *              1) sched_getattr fails with unused pid
17  *              2) sched_getattr fails with invalid address
18  *              3) sched_getattr fails with invalid value
19  *              4) sched_getattr fails with invalid flag
20  */
21 
22 #define _GNU_SOURCE
23 
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <linux/unistd.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <sys/syscall.h>
33 #include <pthread.h>
34 #include <errno.h>
35 
36 #include "test.h"
37 #include "lapi/sched.h"
38 
39 char *TCID = "sched_getattr02";
40 
41 static pid_t pid;
42 static pid_t unused_pid;
43 struct sched_attr attr_copy;
44 
45 static struct test_case {
46 	pid_t *pid;
47 	struct sched_attr *a;
48 	unsigned int size;
49 	unsigned int flags;
50 	int exp_errno;
51 } test_cases[] = {
52 	{&unused_pid, &attr_copy, sizeof(struct sched_attr), 0, ESRCH},
53 	{&pid, NULL, sizeof(struct sched_attr), 0, EINVAL},
54 	{&pid, &attr_copy, sizeof(struct sched_attr) - 1, 0, EINVAL},
55 	{&pid, &attr_copy, sizeof(struct sched_attr), 1000, EINVAL}
56 };
57 
58 static void setup(void);
59 static void sched_getattr_verify(const struct test_case *test);
60 
61 int TST_TOTAL = ARRAY_SIZE(test_cases);
62 
sched_getattr_verify(const struct test_case * test)63 static void sched_getattr_verify(const struct test_case *test)
64 {
65 	TEST(sched_getattr(*(test->pid), test->a, test->size,
66 			test->flags));
67 
68 	if (TEST_RETURN != -1) {
69 		tst_resm(TFAIL, "sched_getattr() succeeded unexpectedly.");
70 		return;
71 	}
72 
73 	if (TEST_ERRNO == test->exp_errno) {
74 		tst_resm(TPASS | TTERRNO,
75 			"sched_getattr() failed expectedly");
76 		return;
77 	}
78 
79 	tst_resm(TFAIL | TTERRNO, "sched_getattr() failed unexpectedly "
80 		": expected: %d - %s",
81 		test->exp_errno, tst_strerrno(test->exp_errno));
82 }
83 
main(int argc,char ** argv)84 int main(int argc, char **argv)
85 {
86 	int lc, i;
87 
88 	tst_parse_opts(argc, argv, NULL, NULL);
89 
90 	setup();
91 
92 	for (lc = 0; TEST_LOOPING(lc); lc++) {
93 		for (i = 0; i < TST_TOTAL; i++)
94 			sched_getattr_verify(&test_cases[i]);
95 	}
96 
97 	tst_exit();
98 }
99 
setup(void)100 void setup(void)
101 {
102 	unused_pid = tst_get_unused_pid(setup);
103 
104 	if ((tst_kvercmp(3, 14, 0)) < 0)
105 		tst_brkm(TCONF, NULL, "EDF needs kernel 3.14 or higher");
106 
107 	TEST_PAUSE;
108 }
109