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, policy;
22	struct sched_param param;
23
24	if(sched_getparam(getpid(), &param) != 0){
25		perror("An error occurs when calling sched_getparam()");
26		return PTS_UNRESOLVED;
27	}
28
29	policy = sched_getscheduler(getpid());
30	if(policy == -1) {
31		perror("An error occurs when calling sched_getscheduler()");
32		return PTS_UNRESOLVED;
33	}
34
35	/* Make sure new_priority != old_priority */
36	max_prio = sched_get_priority_max(policy);
37	old_priority = param.sched_priority;
38	new_priority = (old_priority == max_prio) ?
39		sched_get_priority_min(policy) :
40		max_prio;
41	param.sched_priority = new_priority;
42
43	result = sched_setparam(0, &param);
44
45	if(sched_getparam(getpid(), &param) != 0){
46		perror("An error occurs when calling sched_getparam()");
47		return PTS_UNRESOLVED;
48	}
49
50	if(result == 0 && param.sched_priority == new_priority) {
51		printf("Test PASSED\n");
52		return PTS_PASS;
53	} else if(result == 0 && param.sched_priority == old_priority) {
54		printf("The param does not change\n");
55		return PTS_FAIL;
56	} else if(result == -1 && errno == EPERM) {
57		printf("The process have not permission to change its own param.\n");
58		return PTS_UNRESOLVED;
59	}
60
61	perror("Unknow error");
62	return PTS_FAIL;
63}
64