133965Sjdp/*
233965Sjdp *  This program is free software; you can redistribute it and/or modify
333965Sjdp *  it under the terms of the GNU General Public License version 2.
433965Sjdp *
533965Sjdp *  This program is distributed in the hope that it will be useful,
633965Sjdp *  but WITHOUT ANY WARRANTY; without even the implied warranty of
733965Sjdp *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
833965Sjdp *  GNU General Public License for more details.
933965Sjdp *
1033965Sjdp * Test that when name begins with the slash character, then processes calling
1133965Sjdp * shm_open() with the same value of name refer to the same shared memory
1233965Sjdp * object, as long as that name has not been removed.
1333965Sjdp *
1433965Sjdp * Steps:
1533965Sjdp *  1. Create a child process.
1633965Sjdp * (The child process)
1733965Sjdp *  2. Open a shared memory file O_RDWR with a name beginning with slash.
18 *  3. Write a string in the file.
19 * (The father)
20 *  2. Open a shared memory file O_RDONLY with the same name.
21 *  3. Read into the file.
22 *  4. Check that it obtain the right string.
23 */
24
25/* ftruncate was formerly an XOPEN extension. We define _XOPEN_SOURCE here to
26   avoid warning if the implementation does not program ftruncate as a base
27   interface */
28#define _XOPEN_SOURCE 600
29
30#include <stdio.h>
31#include <sys/mman.h>
32#include <sys/stat.h>
33#include <sys/wait.h>
34#include <sys/types.h>
35#include <unistd.h>
36#include <stdlib.h>
37#include <string.h>
38#include <fcntl.h>
39#include <signal.h>
40#include "posixtest.h"
41
42#define BUF_SIZE 8
43#define SHM_NAME "/posixtest_5-1"
44
45char str[BUF_SIZE] = "qwerty";
46
47int child_process() {
48	int fd;
49	char *buf;
50
51	fd = shm_open(SHM_NAME, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
52	if(fd == -1) {
53		perror("An error occurs when calling shm_open()");
54		kill(getppid(), SIGUSR1);
55		return PTS_UNRESOLVED;
56	}
57
58	if(ftruncate(fd, BUF_SIZE) != 0) {
59		perror("An error occurs when calling ftruncate()");
60		kill(getppid(), SIGUSR1);
61		return PTS_UNRESOLVED;
62	}
63
64	buf = mmap(NULL, BUF_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
65	if( buf == MAP_FAILED) {
66		perror("An error occurs when calling mmap()");
67		kill(getppid(), SIGUSR1);
68		return PTS_UNRESOLVED;
69	}
70
71	strcpy(buf, str);
72
73	return PTS_PASS;
74}
75
76
77int main() {
78	int fd, child_pid;
79	char *buf;
80
81	child_pid = fork();
82	if(child_pid == -1) {
83		perror("An error occurs when calling fork()");
84		return PTS_UNRESOLVED;
85	} else if(child_pid == 0) {
86		return child_process();
87	}
88
89	wait(NULL);
90
91	fd = shm_open(SHM_NAME, O_RDONLY, S_IRUSR|S_IWUSR);
92	if(fd == -1) {
93		perror("An error occurs when calling shm_open()");
94		return PTS_UNRESOLVED;
95	}
96
97	buf = mmap(NULL, BUF_SIZE, PROT_READ, MAP_SHARED, fd, 0);
98	if( buf == MAP_FAILED) {
99		perror("An error occurs when calling mmap()");
100		return PTS_UNRESOLVED;
101	}
102
103	shm_unlink(SHM_NAME);
104
105	if(strcmp(buf, str) == 0) {
106		printf("Test PASSED\n");
107		return PTS_PASS;
108	}
109
110	printf("Test FAILED\n");
111	return PTS_FAIL;
112}
113