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 shared memory object is created if the shared memory object
11 * does not exists and the O_CREAT flags is set.
12 *
13 * Steps:
14 *  1. Ensure that the shared memory object does not exists using shm_unlink.
15 *  2. Call shm_open() with O_CREAT flags set.
16 *  3. Check that the shared memory object exists now using fstat.
17 */
18
19#include <sys/mman.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22#include <stdio.h>
23#include <unistd.h>
24#include <errno.h>
25#include "posixtest.h"
26
27#define SHM_NAME "posixtest_15-1"
28
29
30int main(){
31	int fd, result;
32	struct stat stat_buf;
33
34	result = shm_unlink(SHM_NAME);
35	if(result != 0 && errno != ENOENT) {
36		/* The shared memory object exist and shm_unlink can not
37		   remove it. */
38		perror("An error occurs when calling shm_unlink()");
39		return PTS_UNRESOLVED;
40	}
41
42	fd = shm_open(SHM_NAME, O_RDONLY|O_CREAT, S_IRUSR|S_IWUSR);
43	if(fd == -1) {
44		perror("An error occurs when calling shm_open()");
45		return PTS_UNRESOLVED;
46	}
47
48	result = fstat(fd, &stat_buf);
49
50	if(result == 0) {
51		printf("Test PASSED\n");
52		shm_unlink(SHM_NAME);
53		return PTS_PASS;
54	} else if(result == -1 && errno == EBADF) {
55		printf("The shared memory object was no created.\n");
56		shm_unlink(SHM_NAME);
57		return PTS_FAIL;
58	}
59
60	perror("fstat");
61	shm_unlink(SHM_NAME);
62	return PTS_FAIL;
63}
64
65