]> granicus.if.org Git - strace/blob - tests/sched_xetattr.c
Move definition of struct sched_attr to a separate header file
[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         void *const efault = attr + 1;
72
73         sys_sched_getattr(bogus_pid, 0, 0, 0);
74         printf("sched_getattr(%d, NULL, 0, 0) = %s\n", (int) bogus_pid, errstr);
75
76         sys_sched_getattr(-1U, (unsigned long) attr, bogus_size, bogus_flags);
77         printf("sched_getattr(-1, %p, %u, %u) = %s\n",
78                attr, (unsigned) bogus_size, (unsigned) bogus_flags, errstr);
79
80         sys_sched_getattr(0, (unsigned long) efault, sizeof(*attr), 0);
81         printf("sched_getattr(0, %p, %u, 0) = %s\n",
82                efault, (unsigned) sizeof(*attr), errstr);
83
84         if (sys_sched_getattr(0, (unsigned long) attr, sizeof(*attr), 0))
85                 perror_msg_and_skip("sched_getattr");
86         printf("sched_getattr(0, {size=%u, sched_policy=", attr->size);
87         printxval(schedulers, attr->sched_policy, NULL);
88         printf(", sched_flags=%s, sched_nice=%d, sched_priority=%u"
89                ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
90                ", sched_period=%" PRIu64 "}, %u, 0) = 0\n",
91                attr->sched_flags ? "SCHED_FLAG_RESET_ON_FORK" : "0",
92                attr->sched_nice,
93                attr->sched_priority,
94                attr->sched_runtime,
95                attr->sched_deadline,
96                attr->sched_period,
97                (unsigned) sizeof(*attr));
98
99         attr->sched_flags |= 1;
100
101         if (sys_sched_setattr(0, (unsigned long) attr, 0))
102                 perror_msg_and_skip("sched_setattr");
103         printf("sched_setattr(0, {size=%u, sched_policy=", attr->size);
104         printxval(schedulers, attr->sched_policy, NULL);
105         printf(", sched_flags=%s, sched_nice=%d, sched_priority=%u"
106                ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
107                ", sched_period=%" PRIu64 "}, 0) = 0\n",
108                "SCHED_FLAG_RESET_ON_FORK",
109                attr->sched_nice,
110                attr->sched_priority,
111                attr->sched_runtime,
112                attr->sched_deadline,
113                attr->sched_period);
114
115         attr->size = 0x90807060;
116         attr->sched_policy = 0xca7faced;
117         attr->sched_flags = 0xbadc0ded1057da7aULL;
118         attr->sched_nice = 0xafbfcfdf;
119         attr->sched_priority = 0xb8c8d8e8;
120         attr->sched_runtime = 0xbadcaffedeadf157ULL;
121         attr->sched_deadline = 0xc0de70a57badac75ULL;
122         attr->sched_period = 0xded1ca7edda7aca7ULL;
123
124         sys_sched_setattr(bogus_pid, (unsigned long) attr, bogus_flags);
125         printf("sched_setattr(%d, {size=%u, sched_policy=%#x /* SCHED_??? */, "
126                "sched_flags=%#" PRIx64 " /* SCHED_FLAG_??? */, "
127                "sched_nice=%d, sched_priority=%u, sched_runtime=%" PRIu64 ", "
128                "sched_deadline=%" PRIu64 ", sched_period=%" PRIu64 "}, %u)"
129                " = %s\n",
130                (int) bogus_pid,
131                attr->size,
132                attr->sched_policy,
133                attr->sched_flags,
134                attr->sched_nice,
135                attr->sched_priority,
136                attr->sched_runtime,
137                attr->sched_deadline,
138                attr->sched_period,
139                (unsigned) bogus_flags, errstr);
140
141         puts("+++ exited with 0 +++");
142         return 0;
143 }
144
145 #else
146
147 SKIP_MAIN_UNDEFINED("__NR_sched_getattr && __NR_sched_setattr")
148
149 #endif