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  * requesting process  does not have permission to set the scheduling parameters
     13  * for the specified process, or does not have the appropriate privilege to
     14  * invoke sched_setscheduler().
     15  *
     16  * Atempt to change the policy of the process whose ID is 1 which is generally
     17  * belongs to root. This test can not be run by root.
     18  * Steps:
     19  *   1. Get the old policy and priority.
     20  *   2. Call sched_setscheduler with pid arg == 1.
     21  *   3. Check that the policy and priority have not changed.
     22  */
     23 #include <sched.h>
     24 #include <stdio.h>
     25 #include <errno.h>
     26 #include <unistd.h>
     27 #include <pwd.h>
     28 #include <string.h>
     29 #include "posixtest.h"
     30 
     31 /** Set the euid of this process to a non-root uid */
     32 int set_nonroot()
     33 {
     34 	struct passwd *pw;
     35 	setpwent();
     36 	/* search for the first user which is non root */
     37 	while ((pw = getpwent()) != NULL)
     38 		if (strcmp(pw->pw_name, "root"))
     39 			break;
     40 	endpwent();
     41 	if (pw == NULL) {
     42 		printf("There is no other user than current and root.\n");
     43 		return 1;
     44 	}
     45 
     46 	if (seteuid(pw->pw_uid) != 0) {
     47 		if (errno == EPERM) {
     48 			printf
     49 			    ("You don't have permission to change your UID.\n");
     50 			return 1;
     51 		}
     52 		perror("An error occurs when calling seteuid()");
     53 		return 1;
     54 	}
     55 
     56 	printf("Testing with user '%s' (uid: %d)\n",
     57 	       pw->pw_name, (int)geteuid());
     58 	return 0;
     59 }
     60 
     61 int main(void)
     62 {
     63 	int max_priority, old_priority, old_policy, new_policy, policy;
     64 	struct sched_param param;
     65 
     66 	/* We assume process Number 1 is created by root */
     67 	/* and can only be accessed by root */
     68 	/* This test should be run under standard user permissions */
     69 	if (getuid() == 0) {
     70 		if (set_nonroot() != 0) {
     71 			printf("Cannot run this test as non-root user\n");
     72 			return PTS_UNTESTED;
     73 		}
     74 	}
     75 
     76 	if (sched_getparam(getpid(), &param) == -1) {
     77 		perror("An error occurs when calling sched_getparam()");
     78 		return PTS_UNRESOLVED;
     79 	}
     80 	old_priority = param.sched_priority;
     81 
     82 	old_policy = sched_getscheduler(getpid());
     83 	if (old_policy == -1) {
     84 		perror("An error occurs when calling sched_getscheduler()");
     85 		return PTS_UNRESOLVED;
     86 	}
     87 
     88 	/* Make sure that policy != old_policy */
     89 	policy = old_policy == SCHED_FIFO ? SCHED_RR : SCHED_FIFO;
     90 
     91 	/* Make sure that param.sched_priority != old_priority */
     92 	max_priority = sched_get_priority_max(policy);
     93 	param.sched_priority = (old_priority == max_priority) ?
     94 	    sched_get_priority_min(policy) : max_priority;
     95 
     96 	sched_setscheduler(1, policy, &param);
     97 
     98 	if (sched_getparam(getpid(), &param) != 0) {
     99 		perror("An error occurs when calling sched_getparam()");
    100 		return PTS_UNRESOLVED;
    101 	}
    102 
    103 	new_policy = sched_getscheduler(getpid());
    104 	if (new_policy == -1) {
    105 		perror("An error occurs when calling sched_getscheduler()");
    106 		return PTS_UNRESOLVED;
    107 	}
    108 
    109 	if (old_policy == new_policy && old_priority == param.sched_priority) {
    110 		printf("Test PASSED\n");
    111 		return PTS_PASS;
    112 	}
    113 
    114 	if (param.sched_priority != old_priority) {
    115 		printf("The param has changed\n");
    116 	}
    117 	if (new_policy != old_policy) {
    118 		printf("The policy has changed\n");
    119 	}
    120 	return PTS_FAIL;
    121 }
    122