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 mlock() function sets errno = EPERM if the calling process 11 * does not have the appropriate privilege to perform the requested operation. 12 */ 13 14#define _XOPEN_SOURCE 600 15 16#include <sys/mman.h> 17#include <stdio.h> 18#include <unistd.h> 19#include <stdlib.h> 20#include <errno.h> 21#include <sys/types.h> 22#include <pwd.h> 23#include <string.h> 24#include "posixtest.h" 25 26#define BUFSIZE 8 27 28/** Set the euid of this process to a non-root uid */ 29int set_nonroot() 30{ 31 struct passwd *pw; 32 setpwent(); 33 /* search for the first user which is non root */ 34 while((pw = getpwent()) != NULL) 35 if(strcmp(pw->pw_name, "root")) 36 break; 37 endpwent(); 38 if(pw == NULL) { 39 printf("There is no other user than current and root.\n"); 40 return 1; 41 } 42 43 if(seteuid(pw->pw_uid) != 0) { 44 if(errno == EPERM) { 45 printf("You don't have permission to change your UID.\n"); 46 return 1; 47 } 48 perror("An error occurs when calling seteuid()"); 49 return 1; 50 } 51 52 printf("Testing with user '%s' (uid: %d)\n", 53 pw->pw_name, (int)geteuid()); 54 return 0; 55} 56 57int main() { 58 int result; 59 void *ptr; 60 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 ptr = malloc(BUFSIZE); 70 if(ptr == NULL) { 71 printf("Can not allocate memory.\n"); 72 return PTS_UNRESOLVED; 73 } 74 75 result = mlock(ptr, BUFSIZE); 76 77 if(result == -1 && errno == EPERM) { 78 printf("Test PASSED\n"); 79 return PTS_PASS; 80 } else if(result == 0) { 81 printf("You have the right to call mlock\n"); 82 return PTS_FAIL; 83 } else { 84 perror("Unexpected error"); 85 return PTS_UNRESOLVED; 86 } 87 88} 89