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 sched_setscheduler() sets the scheduling policy and scheduling
12 * parameters of the process specified by pid to policy and the parameters
13 * specified in the sched_param structure pointed to by param, respectively.
14 *
15 * For all policy describe in the spec, the test will check the policy and the
16 * param of the process after the call of sched_setscheduler.
17 */
18#include <sched.h>
19#include <errno.h>
20#include <unistd.h>
21#include <stdio.h>
22#include "posixtest.h"
23
24struct unique {
25	int value;
26	char *name;
27} sym[] = {
28
29	{
30		SCHED_FIFO, "SCHED_FIFO"
31	},
32	{
33		SCHED_RR, "SCHED_RR"
34	},
35#if defined(_POSIX_SPORADIC_SERVER)&&(_POSIX_SPORADIC_SERVER != -1) || defined(_POSIX_THREAD_SPORADIC_SERVER)&&(_POSIX_THREAD_SPORADIC_SERVER != -1)
36	{
37		SCHED_SPORADIC,"SCHED_SPORADIC"
38	},
39#endif
40	{
41		SCHED_OTHER, "SCHED_OTHER"
42	},
43	{
44		0, 0
45	}
46};
47
48int main(int argc, char **argv)
49{
50	int tmp, policy, priority, result = PTS_PASS;
51	struct sched_param param;
52	struct unique *tst;
53
54	tst = sym;
55	while (tst->name) {
56	        fflush(stderr);
57		printf("Policy: %s\n", tst->name);
58		fflush(stdout);
59
60		policy = tst->value;
61		priority = ( sched_get_priority_min(policy) +
62			     sched_get_priority_max(policy) ) / 2;
63		param.sched_priority = priority;
64
65		tmp = sched_setscheduler(getpid(), policy, &param);
66
67		if(tmp == -1 || errno != 0) {
68			if(errno == EPERM){
69				printf("  The process do not have permission to change its own scheduler\n  Try to run this test as root.\n");
70			} else {
71				printf("  Error calling sched_setscheduler() for %s policy\n", tst->name);
72			}
73			if(result != PTS_FAIL) result = PTS_UNRESOLVED;
74			tst++;
75			continue;
76		}
77
78		if(sched_getparam(getpid(), &param) != 0) {
79			perror("Error calling sched_getparam()");
80			return PTS_UNRESOLVED;
81		}
82
83		if(policy != sched_getscheduler(getpid())){
84			printf("  sched_setscheduler() does not set the policy to %s.\n", tst->name);
85			result = PTS_FAIL;
86		}
87	        if(priority != param.sched_priority) {
88			printf("  sched_setscheduler() does not set the right param for %s policy.\n", tst->name);
89			result = PTS_FAIL;
90		}
91
92		tst++;
93	}
94
95	if(result == PTS_PASS)
96		printf("Test PASSED\n");
97        return result;
98}
99