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 sched_setscheduler() sets errno == EINVAL when the sched_priority
     12  * member is not within the inclusive priority range for the scheduling policy.
     13  *
     14  * Test is done for all policy defined in the spec into a loop.
     15  */
     16 
     17 #include <errno.h>
     18 #include <sched.h>
     19 #include <stdio.h>
     20 #include <unistd.h>
     21 #include "posixtest.h"
     22 
     23 struct unique {
     24 	int value;
     25 	char *name;
     26 } sym[] = {
     27 
     28 	{
     29 	SCHED_FIFO, "SCHED_FIFO"}, {
     30 	SCHED_RR, "SCHED_RR"},
     31 #if defined(_POSIX_SPORADIC_SERVER)&&(_POSIX_SPORADIC_SERVER != -1) || defined(_POSIX_THREAD_SPORADIC_SERVER)&&(_POSIX_THREAD_SPORADIC_SERVER != -1)
     32 	{
     33 	SCHED_SPORADIC, "SCHED_SPORADIC"},
     34 #endif
     35 	{
     36 	SCHED_OTHER, "SCHED_OTHER"}, {
     37 	0, 0}
     38 };
     39 
     40 int main(void)
     41 {
     42 	int policy, invalid_priority, tmp, result = PTS_PASS;
     43 	struct sched_param param;
     44 
     45 	struct unique *tst;
     46 
     47 	tst = sym;
     48 	while (tst->name) {
     49 		policy = tst->value;
     50 		fflush(stderr);
     51 		printf("Policy: %s\n", tst->name);
     52 		fflush(stdout);
     53 
     54 		invalid_priority = sched_get_priority_max(policy);
     55 		if (invalid_priority == -1) {
     56 			perror
     57 			    ("An error occurs when calling sched_get_priority_max()");
     58 			return PTS_UNRESOLVED;
     59 		}
     60 
     61 		/* set an invalid priority */
     62 		invalid_priority++;
     63 		param.sched_priority = invalid_priority;
     64 
     65 		tmp = sched_setscheduler(0, policy, &param);
     66 
     67 		if (tmp == -1 && errno == EINVAL) {
     68 			printf("  OK\n");
     69 		} else if (tmp != -1) {
     70 			printf("  The returned code is not -1.\n");
     71 			result = PTS_FAIL;
     72 		} else if (errno == EPERM) {
     73 			printf
     74 			    ("  This process does not have the permission to set its own scheduling policy.\n  Try to launch this test as root.\n");
     75 			if (result != PTS_FAIL) {
     76 				result = PTS_UNRESOLVED;
     77 			}
     78 		} else {
     79 			perror("  Unknow error");
     80 			result = PTS_FAIL;
     81 		}
     82 
     83 		tst++;
     84 	}
     85 
     86 	if (result == PTS_PASS) {
     87 		printf("Test PASSED\n");
     88 	}
     89 	return result;
     90 }
     91