1 /*
2  * Copyright (c) 2015-2016 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 <unistd.h>
36 
37 int
main(void)38 main(void)
39 {
40 	struct {
41 		uint32_t size;
42 		uint32_t sched_policy;
43 		uint64_t sched_flags;
44 		int32_t  sched_nice;
45 		uint32_t sched_priority;
46 		uint64_t sched_runtime;
47 		uint64_t sched_deadline;
48 		uint64_t sched_period;
49 	} *sched_attr = tail_alloc(sizeof(*sched_attr));
50 
51 	long rc = syscall(__NR_sched_getattr, 0xdeadface, NULL, 0, 0);
52 	printf("sched_getattr\\(%d, NULL, 0, 0\\) += %s\n",
53 		0xdeadface, sprintrc_grep(rc));
54 
55 	rc = syscall(__NR_sched_getattr, -1,
56 		     sched_attr, 0xbadfaced, 0xc0defeed);
57 	printf("sched_getattr\\(-1, %p, %u, %u\\) += %s\n",
58 		sched_attr, 0xbadfaced, 0xc0defeed, sprintrc_grep(rc));
59 
60 	rc = syscall(__NR_sched_getattr, 0,
61 		     sched_attr + 1, sizeof(*sched_attr), 0);
62 	printf("sched_getattr\\(0, %p, %u, 0\\) += %s\n",
63 		sched_attr + 1, (unsigned)sizeof(*sched_attr),
64 		sprintrc_grep(rc));
65 
66 	if (syscall(__NR_sched_getattr, 0, sched_attr, sizeof(*sched_attr), 0))
67 		perror_msg_and_skip("sched_getattr");
68 
69 	printf("sched_getattr\\(0, \\{size=%u, sched_policy=SCHED_[A-Z]+"
70 	       ", sched_flags=%s, sched_nice=%d, sched_priority=%u"
71 	       ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
72 	       ", sched_period=%" PRIu64 "\\}, %u, 0\\) += 0\n",
73 	       sched_attr->size,
74 	       sched_attr->sched_flags ? "SCHED_FLAG_RESET_ON_FORK" : "0",
75 	       sched_attr->sched_nice,
76 	       sched_attr->sched_priority,
77 	       sched_attr->sched_runtime,
78 	       sched_attr->sched_deadline,
79 	       sched_attr->sched_period,
80 	       (unsigned) sizeof(*sched_attr));
81 
82 	sched_attr->sched_flags |= 1;
83 	if (syscall(__NR_sched_setattr, 0, sched_attr, 0))
84 		perror_msg_and_skip("sched_setattr");
85 
86 	printf("sched_setattr\\(0, \\{size=%u, sched_policy=SCHED_[A-Z]+"
87 	       ", sched_flags=%s, sched_nice=%d, sched_priority=%u"
88 	       ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
89 	       ", sched_period=%" PRIu64 "\\}, 0\\) += 0\n",
90 	       sched_attr->size,
91 	       "SCHED_FLAG_RESET_ON_FORK",
92 	       sched_attr->sched_nice,
93 	       sched_attr->sched_priority,
94 	       sched_attr->sched_runtime,
95 	       sched_attr->sched_deadline,
96 	       sched_attr->sched_period);
97 
98 	sched_attr->size = 0x90807060;
99 	sched_attr->sched_policy = 0xca7faced;
100 	sched_attr->sched_flags = 0xbadc0ded1057da7aULL;
101 	sched_attr->sched_nice = 0xafbfcfdf;
102 	sched_attr->sched_priority = 0xb8c8d8e8;
103 	sched_attr->sched_runtime = 0xbadcaffedeadf157ULL;
104 	sched_attr->sched_deadline = 0xc0de70a57badac75ULL;
105 	sched_attr->sched_period = 0xded1ca7edda7aca7ULL;
106 
107 	rc = syscall(__NR_sched_setattr, 0xfacec0de, sched_attr, 0xbeeff00d);
108 
109 	printf("sched_setattr\\(%d, \\{size=%u, "
110 		"sched_policy=%#x /\\* SCHED_\\?\\?\\? \\*/, "
111 		"sched_flags=%#" PRIx64 " /\\* SCHED_FLAG_\\?\\?\\? \\*/, "
112 		"sched_nice=%d, sched_priority=%u, sched_runtime=%" PRIu64 ", "
113 		"sched_deadline=%" PRIu64 ", sched_period=%" PRIu64 "\\}, "
114 		"%u\\) += %s\n",
115 		0xfacec0de, sched_attr->size,
116 		sched_attr->sched_policy,
117 		sched_attr->sched_flags,
118 		sched_attr->sched_nice,
119 		sched_attr->sched_priority,
120 		sched_attr->sched_runtime,
121 		sched_attr->sched_deadline,
122 		sched_attr->sched_period, 0xbeeff00d, sprintrc_grep(rc));
123 
124 	return 0;
125 }
126 
127 #else
128 
129 SKIP_MAIN_UNDEFINED("__NR_sched_getattr && __NR_sched_setattr")
130 
131 #endif
132