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 * Test that the mlockall() function set errno = EPERM if the calling process 11 * does not have the appropriate privilege to perform the requested operation 12 * 13 * It is a may assertion. 14 */ 15 16 #include <sys/mman.h> 17 #include <stdio.h> 18 #include <unistd.h> 19 #include <errno.h> 20 #include <sys/types.h> 21 #include <pwd.h> 22 #include <string.h> 23 #include <sys/resource.h> 24 #include "posixtest.h" 25 26 /** Set the euid of this process to a non-root uid */ set_nonroot()27static int set_nonroot() 28 { 29 struct passwd *pw; 30 struct rlimit rlim; 31 int ret = 0; 32 33 setpwent(); 34 /* search for the first user which is non root */ 35 while ((pw = getpwent()) != NULL) 36 if (strcmp(pw->pw_name, "root")) 37 break; 38 endpwent(); 39 if (pw == NULL) { 40 printf("There is no other user than current and root.\n"); 41 return 1; 42 } 43 44 rlim.rlim_cur = 0; 45 rlim.rlim_max = 0; 46 if ((ret = setrlimit(RLIMIT_MEMLOCK, &rlim)) != 0) 47 printf("Failed at setrlimit() return %d \n", ret); 48 49 if (seteuid(pw->pw_uid) != 0) { 50 if (errno == EPERM) { 51 printf 52 ("You don't have permission to change your UID.\n"); 53 return 1; 54 } 55 perror("An error occurs when calling seteuid()"); 56 return 1; 57 } 58 59 printf("Testing with user '%s' (uid: %d)\n", 60 pw->pw_name, (int)geteuid()); 61 return 0; 62 } 63 main(void)64int main(void) 65 { 66 int result; 67 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 result = mlockall(MCL_CURRENT); 77 78 if (result == -1 && errno == EPERM) { 79 printf("Test PASSED\n"); 80 return PTS_PASS; 81 } else if (result == 0) { 82 printf("You have the right to call mlockall\n"); 83 return PTS_UNRESOLVED; 84 } else { 85 perror("Unexpected error"); 86 return PTS_UNRESOLVED; 87 } 88 } 89