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 shm_open() function sets errno = EACCES if the shared memory 11 * object exists and the permissions specified by oflag are denied 12 * 13 * Create a shared memory object with no read or write permission and try to 14 * open it. 15 */ 16 17#define _XOPEN_SOURCE 600 18#include <stdio.h> 19#include <sys/mman.h> 20#include <sys/stat.h> 21#include <fcntl.h> 22#include <errno.h> 23#include <unistd.h> 24#include <sys/types.h> 25#include <pwd.h> 26#include <string.h> 27#include "posixtest.h" 28 29#define SHM_NAME "posixtest_32-1" 30 31/** Set the euid of this process to a non-root uid */ 32int 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(strcmp(pw->pw_name, "root")) 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(seteuid(pw->pw_uid) != 0) { 47 if(errno == EPERM) { 48 printf("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' (uid: %d)\n", 56 pw->pw_name, (int)geteuid()); 57 return 0; 58} 59 60int main() { 61 int fd; 62 63 /* This test should be run under standard user permissions */ 64 if (getuid() == 0) { 65 if (set_nonroot() != 0) { 66 printf("Cannot run this test as non-root user\n"); 67 return PTS_UNTESTED; 68 } 69 } 70 71 fd = shm_open(SHM_NAME, O_RDWR|O_CREAT, 0); 72 if(fd == -1) { 73 perror("An error occurs when calling shm_open()"); 74 return PTS_UNRESOLVED; 75 } 76 77 fd = shm_open(SHM_NAME, O_RDWR, 0); 78 79 if(fd == -1 && errno == EACCES) { 80 printf("Test PASSED\n"); 81 shm_unlink(SHM_NAME); 82 return PTS_PASS; 83 } else if(fd != -1) { 84 printf("shm_open success.\n"); 85 shm_unlink(SHM_NAME); 86 return PTS_FAIL; 87 } 88 89 perror("shm_open"); 90 return PTS_FAIL; 91} 92