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