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 scheduling policy and scheduling parameters are set for
12 * the calling process when pid == 0.
13 */
14#include <sched.h>
15#include <stdio.h>
16#include <errno.h>
17#include <unistd.h>
18#include "posixtest.h"
19
20int main(){
21        int result, new_priority, old_priority, max_prio;
22	int old_policy, new_policy, test_policy;
23	struct sched_param param;
24
25	if(sched_getparam(getpid(), &param) != 0) {
26		perror("An error occurs when calling sched_getparam()");
27		return PTS_UNRESOLVED;
28	}
29
30	old_policy = sched_getscheduler(getpid());
31	if(old_policy == -1) {
32		perror("An error occurs when calling sched_getscheduler()");
33		return PTS_UNRESOLVED;
34	}
35
36	/* Make sure new_policy != old_policy */
37	new_policy = (old_policy == SCHED_FIFO) ? SCHED_RR : SCHED_FIFO;
38
39	/* Make sure new_priority != old_priority */
40	max_prio = sched_get_priority_max(new_policy);
41	old_priority = param.sched_priority;
42	new_priority = (old_priority == max_prio) ?
43		(param.sched_priority = sched_get_priority_min(new_policy)) :
44		(param.sched_priority = max_prio);
45
46	result = sched_setscheduler(0, new_policy, &param);
47
48	if(sched_getparam(getpid(), &param) != 0){
49		perror("An error occurs when calling sched_getparam()");
50		return PTS_UNRESOLVED;
51	}
52
53	test_policy = sched_getscheduler(getpid());
54	if(test_policy == -1) {
55		perror("An error occurs when calling sched_getscheduler()");
56		return PTS_UNRESOLVED;
57	}
58
59	if(result != -1 && param.sched_priority == new_priority &&
60	   test_policy == new_policy) {
61		printf("Test PASSED\n");
62		return PTS_PASS;
63	} else if(result != -1 &&
64		  (param.sched_priority == old_priority ||
65		   test_policy == old_policy)) {
66		if(param.sched_priority == old_priority) {
67			printf("The param does not change\n");
68		}
69		if(test_policy == old_policy) {
70			printf("The policy does not change\n");
71		}
72		return PTS_FAIL;
73	} else if(result == -1 && errno == EPERM) {
74		printf("The process have not permission to change its own policy.\nTry to launch this test as root.\n");
75		return PTS_UNRESOLVED;
76	}
77
78	perror("Unknow error");
79	return PTS_FAIL;
80}
81