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 establish a connection between a shared 11 * memory object and a file descriptor. 12 * 13 * Step: 14 * 1. Create a file a shared memory object using shm_open(). 15 * 2. Write in the file referenced by the file descriptor return by 16 * shm_open(). 17 * 3. Reopen the file with shm_open(). 18 * 4. Read in the file. 19 * The test pass if it read what it was previously written. 20 */ 21 22/* ftruncate was formerly an XOPEN extension. We define _XOPEN_SOURCE here to 23 avoid warning if the implementation does not program ftruncate as a base 24 interface */ 25#define _XOPEN_SOURCE 600 26 27#include <stdio.h> 28#include <sys/mman.h> 29#include <sys/stat.h> 30#include <unistd.h> 31#include <string.h> 32#include <fcntl.h> 33#include "posixtest.h" 34 35#define BUF_SIZE 8 36#define SHM_NAME "posixtest_1-1" 37 38int main() { 39 int fd; 40 char str[BUF_SIZE] = "qwerty"; 41 char *buf; 42 43 fd = shm_open(SHM_NAME, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); 44 if(fd == -1) { 45 perror("An error occurs when calling shm_open()"); 46 return PTS_UNRESOLVED; 47 } 48 49 if(ftruncate(fd, BUF_SIZE) != 0) { 50 perror("An error occurs when calling ftruncate()"); 51 shm_unlink(SHM_NAME); 52 return PTS_UNRESOLVED; 53 } 54 55 buf = mmap(NULL, BUF_SIZE, PROT_WRITE, MAP_SHARED, fd, 0); 56 if( buf == MAP_FAILED) { 57 perror("An error occurs when calling mmap()"); 58 shm_unlink(SHM_NAME); 59 return PTS_UNRESOLVED; 60 } 61 62 strcpy(buf, str); 63 64 fd = shm_open(SHM_NAME, O_RDONLY, S_IRUSR|S_IWUSR); 65 if(fd == -1) { 66 perror("An error occurs when calling shm_open()"); 67 shm_unlink(SHM_NAME); 68 return PTS_UNRESOLVED; 69 } 70 71 buf = mmap(NULL, BUF_SIZE, PROT_READ, MAP_SHARED, fd, 0); 72 if( buf == MAP_FAILED) { 73 perror("An error occurs when calling mmap()"); 74 shm_unlink(SHM_NAME); 75 return PTS_UNRESOLVED; 76 } 77 78 shm_unlink(SHM_NAME); 79 if(strcmp(buf, str) == 0) { 80 printf("Test PASSED\n"); 81 return PTS_PASS; 82 } 83 printf("Test FAILED\n"); 84 return PTS_FAIL; 85} 86