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 shm_open() fails if the shared memory object exist and O_EXCL and 11 * O_CREAT are set. 12 */ 13 14 #include <sys/mman.h> 15 #include <sys/stat.h> 16 #include <fcntl.h> 17 #include <stdio.h> 18 #include <errno.h> 19 #include <unistd.h> 20 #include "posixtest.h" 21 22 #define SHM_NAME "posixtest_22-1" 23 24 int main(void) 25 { 26 int fd; 27 28 /* Create the shared memory object */ 29 fd = shm_open(SHM_NAME, O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR); 30 if (fd == -1) { 31 perror("An error occurs when calling shm_open()"); 32 return PTS_UNRESOLVED; 33 } 34 35 fd = shm_open(SHM_NAME, O_RDONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); 36 37 if (fd == -1 && errno == EEXIST) { 38 printf("Test PASSED\n"); 39 shm_unlink(SHM_NAME); 40 return PTS_PASS; 41 } else if (fd != -1) { 42 printf("Test FAILED\n"); 43 shm_unlink(SHM_NAME); 44 return PTS_FAIL; 45 } 46 47 perror("shm_open"); 48 shm_unlink(SHM_NAME); 49 return PTS_FAIL; 50 } 51