1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/* Tests for sched_setattr and sched_getattr.
Copyright (C) 2024 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <sched.h>
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <support/check.h>
#include <sys/resource.h>
#include <unistd.h>
/* Padding struct to detect unexpected writes. */
union
{
struct sched_attr attr;
/* Hopefully the kernel will never need as much. */
unsigned char padding[4096];
} u;
static void
check_unused (void)
{
TEST_VERIFY (u.attr.size < sizeof (u));
for (unsigned int i = u.attr.size; i < sizeof (u); ++i)
TEST_COMPARE (u.padding[i], 0xcc);
}
static int
do_test (void)
{
_Static_assert (SCHED_OTHER == SCHED_NORMAL,
"SCHED_OTHER, SCHED_NORMAL values");
TEST_VERIFY (sizeof (struct sched_attr) < sizeof (u));
/* Check that reading and re-applying the current policy works. */
memset (&u, 0xcc, sizeof (u));
/* Compiler barrier to bypass write access attribute. */
volatile unsigned int size = sizeof (u);
TEST_COMPARE (sched_getattr (0, (struct sched_attr *) &u, size, 0), 0);
check_unused ();
TEST_COMPARE (sched_setattr (0, &u.attr, 0), 0); /* Apply unchanged. */
/* Try to switch to the SCHED_OTHER policy. */
memset (&u, 0, sizeof (u));
u.attr.size = sizeof (u); /* With padding, kernel should accept zeroes. */
u.attr.sched_policy = SCHED_OTHER; /* Should be the default. */
{
errno = 0;
int prio = getpriority (PRIO_PROCESS, 0);
if (errno != 0)
prio = 0;
u.attr.sched_nice = prio;
}
TEST_COMPARE (sched_setattr (0, &u.attr, 0), 0);
/* Non-zero values not known to the kernel result in an E2BIG error. */
memset (&u, 0, sizeof (u));
TEST_COMPARE (sched_getattr (0, (struct sched_attr *) &u, size, 0), 0);
u.padding[u.attr.size] = 0xcc;
u.attr.size = sizeof (u);
errno = 0;
TEST_COMPARE (sched_setattr (0, &u.attr, 0), -1);
TEST_COMPARE (errno, E2BIG);
memset (&u, 0xcc, sizeof (u));
TEST_COMPARE (sched_getattr (0, (struct sched_attr *) &u, size, 0), 0);
TEST_COMPARE (u.attr.sched_policy, SCHED_OTHER);
check_unused ();
/* Raise the niceless level to 19 and observe its effect. */
TEST_COMPARE (nice (19), 19);
TEST_COMPARE (sched_getattr (0, &u.attr, sizeof (u.attr), 0), 0);
TEST_COMPARE (u.attr.sched_policy, SCHED_OTHER);
TEST_COMPARE (u.attr.sched_nice, 19);
check_unused ();
/* Invalid buffer arguments result in EINVAL (not EFAULT). */
{
errno = 0;
void *volatile null_pointer = NULL; /* compiler barrier. */
TEST_COMPARE (sched_setattr (0, null_pointer, 0), -1);
TEST_COMPARE (errno, EINVAL);
errno = 0;
TEST_COMPARE (sched_getattr (0, null_pointer, size, 0), -1);
TEST_COMPARE (errno, EINVAL);
}
return 0;
}
#include <support/test-driver.c>
|