Home | History | Annotate | Download | only in sched_setscheduler
      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 
     22 #include <errno.h>
     23 #include <sched.h>
     24 #include <stdio.h>
     25 #include <unistd.h>
     26 #include "posixtest.h"
     27 
     28 struct unique {
     29 	int value;
     30 	char *name;
     31 } sym[] = {
     32 
     33 	{
     34 	SCHED_FIFO, "SCHED_FIFO"}, {
     35 	SCHED_RR, "SCHED_RR"},
     36 #if defined(_POSIX_SPORADIC_SERVER)&&(_POSIX_SPORADIC_SERVER != -1) || defined(_POSIX_THREAD_SPORADIC_SERVER)&&(_POSIX_THREAD_SPORADIC_SERVER != -1)
     37 	{
     38 	SCHED_SPORADIC, "SCHED_SPORADIC"},
     39 #endif
     40 	{
     41 	SCHED_OTHER, "SCHED_OTHER"}, {
     42 	0, 0}
     43 };
     44 
     45 int main(void)
     46 {
     47 	int policy, invalid_priority, result = PTS_PASS;
     48 	int old_priority, old_policy, new_policy;
     49 	struct sched_param param;
     50 
     51 	struct unique *tst;
     52 
     53 	tst = sym;
     54 	while (tst->name) {
     55 		policy = tst->value;
     56 		fflush(stderr);
     57 		printf("Policy: %s\n", tst->name);
     58 		fflush(stdout);
     59 
     60 		if (sched_getparam(getpid(), &param) != 0) {
     61 			perror("An error occurs when calling sched_getparam()");
     62 			return PTS_UNRESOLVED;
     63 		}
     64 		old_priority = param.sched_priority;
     65 
     66 		old_policy = sched_getscheduler(getpid());
     67 		if (old_policy == -1) {
     68 			perror
     69 			    ("An error occurs when calling sched_getscheduler()");
     70 			return PTS_UNRESOLVED;
     71 		}
     72 
     73 		invalid_priority = sched_get_priority_max(policy);
     74 		if (invalid_priority == -1) {
     75 			perror
     76 			    ("An error occurs when calling sched_get_priority_max()");
     77 			return PTS_UNRESOLVED;
     78 		}
     79 
     80 		/* set an invalid priority */
     81 		invalid_priority++;
     82 		param.sched_priority = invalid_priority;
     83 
     84 		sched_setscheduler(0, policy, &param);
     85 
     86 		if (sched_getparam(getpid(), &param) != 0) {
     87 			perror("An error occurs when calling sched_getparam()");
     88 			return PTS_UNRESOLVED;
     89 		}
     90 
     91 		new_policy = sched_getscheduler(getpid());
     92 		if (new_policy == -1) {
     93 			perror
     94 			    ("An error occurs when calling sched_getscheduler()");
     95 			return PTS_UNRESOLVED;
     96 		}
     97 
     98 		if (old_policy == new_policy &&
     99 		    old_priority == param.sched_priority) {
    100 			printf("  OK\n");
    101 		} else {
    102 			if (param.sched_priority != old_priority) {
    103 				printf("  The param has changed\n");
    104 			}
    105 			if (new_policy != old_policy) {
    106 				printf("  The policy has changed\n");
    107 			}
    108 			result = PTS_FAIL;
    109 		}
    110 
    111 		tst++;
    112 	}
    113 
    114 	if (result == PTS_PASS) {
    115 		printf("Test PASSED\n");
    116 	}
    117 	return result;
    118 }
    119