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 * policy value is not defined in the sched.h header.
13 *
14 * The test attempt to set the policy to a very improbable value.
15 * Steps:
16 *   1. Get the old policy and priority.
17 *   2. Call sched_setscheduler with invalid args.
18 *   3. Check that the policy and priority have not changed.
19 *
20 * Orignal author unknown
21 * Updated:  Peter W. Morreale <pmorreale AT novell DOT com>
22 * Date:     09.07.2011
23 */
24
25#include <sched.h>
26#include <stdio.h>
27#include <limits.h>
28#include <errno.h>
29#include <string.h>
30#include <unistd.h>
31#include "posixtest.h"
32
33#define ERR_MSG(f, rc)  printf("Failed: %s rc: %d errno: %s\n", \
34				f, rc, strerror(errno))
35
36int main(void)
37{
38	int old_priority, old_policy, new_policy;
39	struct sched_param param;
40	int invalid_policy;
41	char *label;
42	pid_t pid = getpid();
43	int rc;
44
45	invalid_policy = INT_MAX;
46
47	label = "sched_getparam()";
48	rc = sched_getparam(pid, &param);
49	if (rc)
50		goto unresolved;
51	old_priority = param.sched_priority;
52
53	label = "sched_getscheduler()";
54	rc = sched_getscheduler(pid);
55	if (rc < 0)
56		goto unresolved;
57	old_policy = rc;
58
59	label = "sched_setscheduler() - invalid policy succeeded?";
60	rc = sched_setscheduler(0, invalid_policy, &param);
61	if (!rc)
62		goto unresolved;
63
64	label = "sched_getparam()";
65	rc = sched_getparam(pid, &param);
66	if (rc)
67		goto unresolved;
68
69	label = "sched_getscheduler()";
70	rc = sched_getscheduler(pid);
71	if (rc < 0)
72		goto unresolved;
73	new_policy = rc;
74
75	if (old_policy != new_policy) {
76		printf("Failed: invalid policy change, old: %u, new %u\n",
77		       old_policy, new_policy);
78		return PTS_FAIL;
79	}
80
81	if (old_priority != param.sched_priority) {
82		printf("Failed: invalid priority change, old: %u, new %u\n",
83		       old_priority, param.sched_priority);
84		return PTS_FAIL;
85	}
86
87	printf("Test PASSED\n");
88	return PTS_PASS;
89
90unresolved:
91	ERR_MSG(label, rc);
92	return PTS_UNRESOLVED;
93}
94