1/*
2 *  This program is free software; you can redistribute it and/or modify
3 *  it under the terms of the GNU General Public License version 2.
4 *
5 *  This program is distributed in the hope that it will be useful,
6 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
7 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8 *  GNU General Public License for more details.
9 *
10 *
11 * Test that the policy and scheduling parameters remain unchanged when the
12 * sched_priority member is not within the inclusive priority range for the
13 * scheduling policy.
14 *
15 * Test is done for all policy defined in the spec into a loop.
16 * Steps (into loop):
17 *   1. Get the old policy and priority.
18 *   2. Call sched_setscheduler with invalid args.
19 *   3. Check that the policy and priority have not changed.
20 */
21#include <sched.h>
22#include <stdio.h>
23#include <unistd.h>
24#include <errno.h>
25#include "posixtest.h"
26
27struct unique {
28	int value;
29	char *name;
30} sym[] = {
31
32	{
33		SCHED_FIFO, "SCHED_FIFO"
34	},
35	{
36		SCHED_RR, "SCHED_RR"
37	},
38#if defined(_POSIX_SPORADIC_SERVER)&&(_POSIX_SPORADIC_SERVER != -1) || defined(_POSIX_THREAD_SPORADIC_SERVER)&&(_POSIX_THREAD_SPORADIC_SERVER != -1)
39	{
40		SCHED_SPORADIC,"SCHED_SPORADIC"
41	},
42#endif
43	{
44		SCHED_OTHER, "SCHED_OTHER"
45	},
46	{
47		0, 0
48	}
49};
50
51int main(){
52	int policy, invalid_priority, result = PTS_PASS;
53	int old_priority, old_policy, new_policy;
54	struct sched_param param;
55
56
57	struct unique *tst;
58
59	tst = sym;
60	while (tst->name) {
61		policy = tst->value;
62		fflush(stderr);
63		printf("Policy: %s\n", tst->name);
64		fflush(stdout);
65
66		if(sched_getparam(getpid(), &param) != 0) {
67			perror("An error occurs when calling sched_getparam()");
68			return PTS_UNRESOLVED;
69		}
70		old_priority = param.sched_priority;
71
72		old_policy = sched_getscheduler(getpid());
73		if(old_policy == -1) {
74			perror("An error occurs when calling sched_getscheduler()");
75			return PTS_UNRESOLVED;
76		}
77
78
79		invalid_priority = sched_get_priority_max(policy);
80		if(invalid_priority == -1){
81			perror("An error occurs when calling sched_get_priority_max()");
82			return PTS_UNRESOLVED;
83		}
84
85		/* set an invalid priority */
86		invalid_priority++;
87		param.sched_priority = invalid_priority;
88
89		sched_setscheduler(0, policy, &param);
90
91		if(sched_getparam(getpid(), &param) != 0) {
92			perror("An error occurs when calling sched_getparam()");
93			return PTS_UNRESOLVED;
94		}
95
96		new_policy = sched_getscheduler(getpid());
97		if(new_policy == -1) {
98			perror("An error occurs when calling sched_getscheduler()");
99			return PTS_UNRESOLVED;
100		}
101
102
103		if(old_policy == new_policy &&
104		   old_priority == param.sched_priority) {
105			printf("  OK\n");
106		} else {
107			if(param.sched_priority != old_priority) {
108				printf("  The param has changed\n");
109			}
110			if(new_policy != old_policy) {
111				printf("  The policy has changed\n");
112			}
113			result = PTS_FAIL;
114		}
115
116		tst++;
117	}
118
119	if(result == PTS_PASS) {
120		printf("Test PASSED\n");
121	}
122	return result;
123}
124
125