Home | History | Annotate | Download | only in sched_getscheduler
      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_getscheduler() sets errno == EPERM when the requesting
     12  * process does not have permission
     13  */
     14 #define _XOPEN_SOURCE 600
     15 #include <stdio.h>
     16 #include <sched.h>
     17 #include <errno.h>
     18 #include <unistd.h>
     19 #include <sys/types.h>
     20 #include <pwd.h>
     21 #include <string.h>
     22 #include "posixtest.h"
     23 
     24 /* Set the euid of this process to a non-root uid */
     25 int set_nonroot(void)
     26 {
     27 	struct passwd *pw;
     28 	setpwent();
     29 	/* search for the first user which is non root */
     30 	while ((pw = getpwent()) != NULL)
     31 		if (strcmp(pw->pw_name, "root"))
     32 			break;
     33 	endpwent();
     34 	if (pw == NULL) {
     35 		printf("There is no other user than current and root.\n");
     36 		return 1;
     37 	}
     38 
     39 	if (seteuid(pw->pw_uid) != 0) {
     40 		if (errno == EPERM) {
     41 			printf("You don't have permission to change "
     42 			       "your UID.\n");
     43 			return 1;
     44 		}
     45 		perror("An error occurs when calling seteuid()");
     46 		return 1;
     47 	}
     48 
     49 	printf("Testing with user '%s' (uid: %d)\n",
     50 	       pw->pw_name, (int)geteuid());
     51 	return 0;
     52 }
     53 
     54 int main(void)
     55 {
     56 
     57 	int result = -1;
     58 
     59 	/* We assume process Number 1 is created by root */
     60 	/* and can only be accessed by root */
     61 	/* This test should be run under standard user permissions */
     62 	if (getuid() == 0) {
     63 		if (set_nonroot() != 0) {
     64 			printf("Cannot run this test as non-root user\n");
     65 			return PTS_UNTESTED;
     66 		}
     67 	}
     68 
     69 	result = sched_getscheduler(1);
     70 
     71 	if (result == -1 && errno == EPERM) {
     72 		printf("Test PASSED\n");
     73 		return PTS_PASS;
     74 	}
     75 	if (result == 0) {
     76 		printf("The function sched_getscheduler has successed.\n");
     77 		return PTS_UNTESTED;
     78 	}
     79 	if (errno != EPERM) {
     80 		perror("errno is not EPERM");
     81 		return PTS_FAIL;
     82 	} else {
     83 		perror("Unresolved test error");
     84 		return PTS_UNRESOLVED;
     85 	}
     86 }
     87