]> granicus.if.org Git - strace/blob - tests/sched_xetattr.c
23eb19cfd1587713af1fbf11a8d813f988d74deb
[strace] / tests / sched_xetattr.c
1 /*
2  * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "tests.h"
29 #include <asm/unistd.h>
30
31 #if defined __NR_sched_getattr && defined __NR_sched_setattr
32
33 # include <inttypes.h>
34 # include <stdio.h>
35 # include <sched.h>
36 # include <unistd.h>
37 # include "sched_attr.h"
38 # include "xlat.h"
39 # include "xlat/schedulers.h"
40
41 static const char *errstr;
42
43 static long
44 sys_sched_getattr(kernel_ulong_t pid, kernel_ulong_t attr,
45                   kernel_ulong_t size, kernel_ulong_t flags)
46 {
47         long rc = syscall(__NR_sched_getattr, pid, attr, size, flags);
48         errstr = sprintrc(rc);
49         return rc;
50 }
51
52 static long
53 sys_sched_setattr(kernel_ulong_t pid, kernel_ulong_t attr, kernel_ulong_t flags)
54 {
55         long rc = syscall(__NR_sched_setattr, pid, attr, flags);
56         errstr = sprintrc(rc);
57         return rc;
58 }
59
60 int
61 main(void)
62 {
63         static const kernel_ulong_t bogus_pid =
64                 (kernel_ulong_t) 0xdefacedfacefeedULL;
65         static const kernel_ulong_t bogus_size =
66                 (kernel_ulong_t) 0xdefacedcafef00dULL;
67         static const kernel_ulong_t bogus_flags =
68                 (kernel_ulong_t) 0xdefaceddeadc0deULL;
69
70         struct sched_attr *const attr = tail_alloc(sizeof(*attr));
71         unsigned int *const psize = tail_alloc(sizeof(*psize));
72         void *const efault = attr + 1;
73
74         sys_sched_getattr(0, 0, 0, 0);
75         printf("sched_getattr(0, NULL, 0, 0) = %s\n", errstr);
76
77         sys_sched_getattr(0, (unsigned long) attr, 0, 0);
78         printf("sched_getattr(0, %p, 0, 0) = %s\n", attr, errstr);
79
80         sys_sched_getattr(bogus_pid, 0, 0, 0);
81         printf("sched_getattr(%d, NULL, 0, 0) = %s\n", (int) bogus_pid, errstr);
82
83         sys_sched_getattr(-1U, (unsigned long) attr, bogus_size, bogus_flags);
84         printf("sched_getattr(-1, %p, %u, %u) = %s\n",
85                attr, (unsigned) bogus_size, (unsigned) bogus_flags, errstr);
86
87         sys_sched_getattr(0, (unsigned long) efault, sizeof(*attr), 0);
88         printf("sched_getattr(0, %p, %u, 0) = %s\n",
89                efault, (unsigned) sizeof(*attr), errstr);
90
91         if (sys_sched_getattr(0, (unsigned long) attr, sizeof(*attr), 0))
92                 perror_msg_and_skip("sched_getattr");
93         printf("sched_getattr(0, {size=%u, sched_policy=", attr->size);
94         printxval(schedulers, attr->sched_policy, NULL);
95         printf(", sched_flags=%s, sched_nice=%d, sched_priority=%u"
96                ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
97                ", sched_period=%" PRIu64 "}, %u, 0) = 0\n",
98                attr->sched_flags ? "SCHED_FLAG_RESET_ON_FORK" : "0",
99                attr->sched_nice,
100                attr->sched_priority,
101                attr->sched_runtime,
102                attr->sched_deadline,
103                attr->sched_period,
104                (unsigned) sizeof(*attr));
105
106         sys_sched_getattr(F8ILL_KULONG_MASK, (unsigned long) attr,
107                           F8ILL_KULONG_MASK | sizeof(*attr), F8ILL_KULONG_MASK);
108         printf("sched_getattr(0, {size=%u, sched_policy=", attr->size);
109         printxval(schedulers, attr->sched_policy, NULL);
110         printf(", sched_flags=%s, sched_nice=%d, sched_priority=%u"
111                ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
112                ", sched_period=%" PRIu64 "}, %u, 0) = 0\n",
113                attr->sched_flags ? "SCHED_FLAG_RESET_ON_FORK" : "0",
114                attr->sched_nice,
115                attr->sched_priority,
116                attr->sched_runtime,
117                attr->sched_deadline,
118                attr->sched_period,
119                (unsigned) sizeof(*attr));
120
121         sys_sched_setattr(bogus_pid, 0, 0);
122         printf("sched_setattr(%d, NULL, 0) = %s\n", (int) bogus_pid, errstr);
123
124         attr->sched_flags |= 1;
125
126         if (sys_sched_setattr(0, (unsigned long) attr, 0))
127                 perror_msg_and_skip("sched_setattr");
128         printf("sched_setattr(0, {size=%u, sched_policy=", attr->size);
129         printxval(schedulers, attr->sched_policy, NULL);
130         printf(", sched_flags=%s, sched_nice=%d, sched_priority=%u"
131                ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
132                ", sched_period=%" PRIu64 "}, 0) = 0\n",
133                "SCHED_FLAG_RESET_ON_FORK",
134                attr->sched_nice,
135                attr->sched_priority,
136                attr->sched_runtime,
137                attr->sched_deadline,
138                attr->sched_period);
139
140         sys_sched_setattr(F8ILL_KULONG_MASK, (unsigned long) attr,
141                           F8ILL_KULONG_MASK);
142         printf("sched_setattr(0, {size=%u, sched_policy=", attr->size);
143         printxval(schedulers, attr->sched_policy, NULL);
144         printf(", sched_flags=%s, sched_nice=%d, sched_priority=%u"
145                ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
146                ", sched_period=%" PRIu64 "}, 0) = 0\n",
147                "SCHED_FLAG_RESET_ON_FORK",
148                attr->sched_nice,
149                attr->sched_priority,
150                attr->sched_runtime,
151                attr->sched_deadline,
152                attr->sched_period);
153
154         *psize = attr->size;
155
156         sys_sched_setattr(0, (unsigned long) psize, 0);
157         printf("sched_setattr(0, %p, 0) = %s\n", psize, errstr);
158
159         attr->size = 0;
160
161         sys_sched_setattr(0, (unsigned long) attr, 0);
162         printf("sched_setattr(0, {size=%u, sched_policy=", attr->size);
163         printxval(schedulers, attr->sched_policy, NULL);
164         printf(", sched_flags=%s, sched_nice=%d, sched_priority=%u"
165                ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
166                ", sched_period=%" PRIu64 "}, 0) = 0\n",
167                "SCHED_FLAG_RESET_ON_FORK",
168                attr->sched_nice,
169                attr->sched_priority,
170                attr->sched_runtime,
171                attr->sched_deadline,
172                attr->sched_period);
173
174         attr->size = 1;
175
176         sys_sched_setattr(0, (unsigned long) attr, 0);
177         printf("sched_setattr(0, {size=%u} => {size=%u}, 0) = %s\n",
178                1, attr->size, errstr);
179
180         attr->size = SCHED_ATTR_MIN_SIZE - 1;
181
182         sys_sched_setattr(0, (unsigned long) attr, 0);
183         printf("sched_setattr(0, {size=%u} => {size=%u}, 0) = %s\n",
184                SCHED_ATTR_MIN_SIZE - 1, attr->size, errstr);
185
186         attr->size = 0x90807060;
187         attr->sched_policy = 0xca7faced;
188         attr->sched_flags = 0xbadc0ded1057da7aULL;
189         attr->sched_nice = 0xafbfcfdf;
190         attr->sched_priority = 0xb8c8d8e8;
191         attr->sched_runtime = 0xbadcaffedeadf157ULL;
192         attr->sched_deadline = 0xc0de70a57badac75ULL;
193         attr->sched_period = 0xded1ca7edda7aca7ULL;
194
195         sys_sched_setattr(bogus_pid, (unsigned long) attr, bogus_flags);
196         printf("sched_setattr(%d, {size=%u, sched_policy=%#x /* SCHED_??? */, "
197                "sched_flags=%#" PRIx64 " /* SCHED_FLAG_??? */, "
198                "sched_nice=%d, sched_priority=%u, sched_runtime=%" PRIu64 ", "
199                "sched_deadline=%" PRIu64 ", sched_period=%" PRIu64 ", ...}, %u)"
200                " = %s\n",
201                (int) bogus_pid,
202                attr->size,
203                attr->sched_policy,
204                attr->sched_flags,
205                attr->sched_nice,
206                attr->sched_priority,
207                attr->sched_runtime,
208                attr->sched_deadline,
209                attr->sched_period,
210                (unsigned) bogus_flags, errstr);
211
212         if (F8ILL_KULONG_SUPPORTED) {
213                 const kernel_ulong_t ill = f8ill_ptr_to_kulong(attr);
214
215                 sys_sched_getattr(0, ill, sizeof(*attr), 0);
216                 printf("sched_getattr(0, %#llx, %u, 0) = %s\n",
217                        (unsigned long long) ill, (unsigned) sizeof(*attr),
218                        errstr);
219
220                 sys_sched_setattr(0, ill, 0);
221                 printf("sched_setattr(0, %#llx, 0) = %s\n",
222                        (unsigned long long) ill, errstr);
223         }
224
225         puts("+++ exited with 0 +++");
226         return 0;
227 }
228
229 #else
230
231 SKIP_MAIN_UNDEFINED("__NR_sched_getattr && __NR_sched_setattr")
232
233 #endif