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 state of the shared memory object, including all data 11 * associated with the shared memory object, persists until all mapping 12 * references are gone even if the shared memory object is unlinked and there 13 * is no open reference anymore. 14 * 15 * Steps: 16 * 1. Create a shared memory object, map it and write a string in it. 17 * 2. Close the file descriptor and unlink the object. 18 * 3. Check that the previously written string is always in the mapped memory. 19 */ 20 21/* ftruncate was formerly an XOPEN extension. We define _XOPEN_SOURCE here to 22 avoid warning if the implementation does not program ftruncate as a base 23 interface */ 24#define _XOPEN_SOURCE 600 25 26#include <stdio.h> 27#include <sys/mman.h> 28#include <sys/stat.h> 29#include <unistd.h> 30#include <string.h> 31#include <fcntl.h> 32#include <errno.h> 33#include "posixtest.h" 34 35#define BUF_SIZE 8 36#define SHM_NAME "posixtest_28-2" 37 38int main(void) 39{ 40 int fd; 41 char str[BUF_SIZE] = "qwerty"; 42 char *buf; 43 44 fd = shm_open(SHM_NAME, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); 45 if (fd == -1) { 46 perror("An error occurs when calling shm_open()"); 47 return PTS_UNRESOLVED; 48 } 49 50 if (ftruncate(fd, BUF_SIZE) != 0) { 51 perror("An error occurs when calling ftruncate()"); 52 shm_unlink(SHM_NAME); 53 return PTS_UNRESOLVED; 54 } 55 56 buf = mmap(NULL, BUF_SIZE, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0); 57 if (buf == MAP_FAILED) { 58 perror("An error occurs when calling mmap()"); 59 shm_unlink(SHM_NAME); 60 return PTS_UNRESOLVED; 61 } 62 63 strcpy(buf, str); 64 65 if (close(fd) != 0) { 66 perror("An error occurs when calling close()"); 67 shm_unlink(SHM_NAME); 68 return PTS_UNRESOLVED; 69 } 70 71 if (shm_unlink(SHM_NAME) != 0) { 72 perror("An error occurs when calling shm_unlink()"); 73 return PTS_UNRESOLVED; 74 } 75 /* Now, SHM_NAME is unlinked and there are no more open references on 76 it but a mapping reference remain */ 77 78 if (strcmp(buf, str) == 0) { 79 printf("Test PASSED\n"); 80 return PTS_PASS; 81 } 82 printf("The content of the shared memory object was removed.\n"); 83 return PTS_FAIL; 84} 85