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