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 the shared memory
12 * object is unlinked even if all other references are gone.
13 *
14 * Steps:
15 *  1. Create a shared memory object, map it and write a string in it.
16 *  2. Unmap the memory and close the file descriptor.
17 *  3. Reopen the shared memory object and remap it.
18 *  4. Check that the previously written string is always in the memory.
19 */
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-1"
38
39int main() {
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, 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(munmap(buf, BUF_SIZE) != 0) {
66		perror("An error occurs when calling munmap()");
67		shm_unlink(SHM_NAME);
68		return PTS_UNRESOLVED;
69	}
70
71	if(close(fd) != 0) {
72		perror("An error occurs when calling close()");
73		shm_unlink(SHM_NAME);
74		return PTS_UNRESOLVED;
75	}
76
77        /* Now, there are no more reference on SHM_NAME but it is not unlink */
78
79	fd = shm_open(SHM_NAME, O_RDONLY, S_IRUSR|S_IWUSR);
80	if(fd == -1 && errno == ENOENT) {
81		printf("The name of the shared memory object was removed.\n");
82		return PTS_FAIL;
83	} else if(fd == -1) {
84		perror("An error occurs when calling shm_open()");
85		shm_unlink(SHM_NAME);
86		return PTS_UNRESOLVED;
87	}
88
89	buf = mmap(NULL, BUF_SIZE, PROT_READ, MAP_SHARED, fd, 0);
90	if( buf == MAP_FAILED) {
91		perror("An error occurs when calling mmap()");
92		shm_unlink(SHM_NAME);
93		return PTS_UNRESOLVED;
94	}
95
96	if(strcmp(buf, str) == 0) {
97		printf("Test PASSED\n");
98		shm_unlink(SHM_NAME);
99		return PTS_PASS;
100	}
101	printf("The content of the shared memory object was removed.\n");
102	shm_unlink(SHM_NAME);
103	return PTS_FAIL;
104}
105