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_getparam() sets errno == EPERM if the requesting process 12 * does not have permission. 13 */ 14 15 /* adam.li (at) intel.com - 2004-05-21 16 * 17 * On Linux, e.g, the kernel makes no check on user permission to call this 18 * API. So basically we don't know on what condition a system should return 19 * EPERM. It is implementation defined. 20 */ 21 22 #include <sys/types.h> 23 #include <errno.h> 24 #include <pwd.h> 25 #include <sched.h> 26 #include <stdio.h> 27 #include <string.h> 28 #include <unistd.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 (pw->pw_uid != 0) 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 (setgid(pw->pw_gid) != 0) { 47 if (errno == EPERM) 48 printf("You don't have permission to change " 49 "your GID.\n"); 50 else 51 perror("setgid failed"); 52 return 1; 53 } 54 55 if (setuid(pw->pw_uid) != 0) { 56 if (errno == EPERM) 57 printf("You don't have permission to change " 58 "your UID.\n"); 59 else 60 perror("setuid failed"); 61 62 return 1; 63 } 64 65 printf("Testing with user '%s' (euid,uid) = (%d,%d)\n", 66 pw->pw_name, geteuid(), getuid()); 67 return 0; 68 } 69 70 int main(void) 71 { 72 73 struct sched_param param; 74 int result = -1; 75 76 /* 77 * We assume process Number 1 is created by root 78 * and can only be accessed by root 79 * This test should be run under standard user permissions 80 */ 81 if (geteuid() == 0) { 82 if (set_nonroot() != 0) { 83 printf("Cannot run this test as non-root user\n"); 84 return PTS_UNTESTED; 85 } 86 } 87 88 result = sched_getparam(1, ¶m); 89 90 if (result == -1 && errno == EPERM) { 91 printf("Test PASS\n"); 92 return PTS_PASS; 93 } 94 if (result == 0) { 95 printf("The function sched_getparam has successed.\n"); 96 return PTS_UNTESTED; 97 } 98 if (errno != EPERM) { 99 perror("errno is not EPERM: The system allows a non-root" 100 "user to use sched_getparam()"); 101 return PTS_UNRESOLVED; 102 } else { 103 perror("Unresolved test error"); 104 return PTS_UNRESOLVED; 105 } 106 } 107