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 open references 12 * are gone even if the shared memory object is unlinked and there 13 * is no mapping reference anymore. 14 * 15 * Steps: 16 * 1. Create a shared memory object, map it and write a string in it. 17 * 2. Unmap the memory and unlink the shared memory object. 18 * 3. Remap the shared memory object. 19 * 4. Check that the previously written string is always in the memory. 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 <errno.h> 34#include "posixtest.h" 35 36#define BUF_SIZE 8 37#define SHM_NAME "posixtest_28-3" 38 39int main(void) 40{ 41 int fd; 42 char str[BUF_SIZE] = "qwerty"; 43 char *buf; 44 45 fd = shm_open(SHM_NAME, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); 46 if (fd == -1) { 47 perror("An error occurs when calling shm_open()"); 48 return PTS_UNRESOLVED; 49 } 50 51 if (ftruncate(fd, BUF_SIZE) != 0) { 52 perror("An error occurs when calling ftruncate()"); 53 shm_unlink(SHM_NAME); 54 return PTS_UNRESOLVED; 55 } 56 57 buf = mmap(NULL, BUF_SIZE, PROT_WRITE, MAP_SHARED, fd, 0); 58 if (buf == MAP_FAILED) { 59 perror("An error occurs when calling mmap()"); 60 shm_unlink(SHM_NAME); 61 return PTS_UNRESOLVED; 62 } 63 64 strcpy(buf, str); 65 66 if (munmap(buf, BUF_SIZE) != 0) { 67 perror("An error occurs when calling munmap()"); 68 shm_unlink(SHM_NAME); 69 return PTS_UNRESOLVED; 70 } 71 72 if (shm_unlink(SHM_NAME) != 0) { 73 perror("An error occurs when calling shm_unlink()"); 74 return PTS_UNRESOLVED; 75 } 76 /* Now, SHM_NAME is unlinked and there are no more mapping references 77 on it but an open reference remain */ 78 79 buf = mmap(NULL, BUF_SIZE, PROT_READ, MAP_SHARED, fd, 0); 80 if (buf == MAP_FAILED && errno == EBADF) { 81 printf("The shared memory object was removed.\n"); 82 return PTS_FAIL; 83 } else if (buf == MAP_FAILED) { 84 perror("An error occurs when calling mmap()"); 85 return PTS_UNRESOLVED; 86 } 87 88 if (strcmp(buf, str) == 0) { 89 printf("Test PASSED\n"); 90 return PTS_PASS; 91 } 92 printf("The content of the shared memory object was removed.\n"); 93 return PTS_FAIL; 94} 95