10dc076565f772bb1953209fb69ea150b494aaa40robbiew/*
20dc076565f772bb1953209fb69ea150b494aaa40robbiew *  This program is free software; you can redistribute it and/or modify
30dc076565f772bb1953209fb69ea150b494aaa40robbiew *  it under the terms of the GNU General Public License version 2.
40dc076565f772bb1953209fb69ea150b494aaa40robbiew *
50dc076565f772bb1953209fb69ea150b494aaa40robbiew *  This program is distributed in the hope that it will be useful,
60dc076565f772bb1953209fb69ea150b494aaa40robbiew *  but WITHOUT ANY WARRANTY; without even the implied warranty of
70dc076565f772bb1953209fb69ea150b494aaa40robbiew *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80dc076565f772bb1953209fb69ea150b494aaa40robbiew *  GNU General Public License for more details.
90dc076565f772bb1953209fb69ea150b494aaa40robbiew *
100dc076565f772bb1953209fb69ea150b494aaa40robbiew * Test that the reuse of the name subsequently causes shm_open() to
110dc076565f772bb1953209fb69ea150b494aaa40robbiew * create a new shared memory object if O_CREAT is set even if the object
122c28215423293e443469a07ae7011135d058b671Garrett Cooper * continues to exist after the last shm_unlink(),
130dc076565f772bb1953209fb69ea150b494aaa40robbiew */
140dc076565f772bb1953209fb69ea150b494aaa40robbiew
150dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdio.h>
160dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <sys/mman.h>
170dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <sys/stat.h>
180dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <fcntl.h>
190dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <errno.h>
200dc076565f772bb1953209fb69ea150b494aaa40robbiew#include "posixtest.h"
210dc076565f772bb1953209fb69ea150b494aaa40robbiew
220dc076565f772bb1953209fb69ea150b494aaa40robbiew#define SHM_NAME "posixtest_5-1"
230dc076565f772bb1953209fb69ea150b494aaa40robbiew
244ca2bbdcd3003f3c8df4e6129e9c7b2bd1514f87Cyril Hrubisint main(void)
25354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao{
260dc076565f772bb1953209fb69ea150b494aaa40robbiew	int fd;
272c28215423293e443469a07ae7011135d058b671Garrett Cooper
28354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	fd = shm_open(SHM_NAME, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
29df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper	if (fd == -1) {
300dc076565f772bb1953209fb69ea150b494aaa40robbiew		perror("An error occurs when calling shm_open()");
310dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNRESOLVED;
320dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
330dc076565f772bb1953209fb69ea150b494aaa40robbiew
348fb1cdb0538640f295691929650408688537fb7fGarrett Cooper	if (shm_unlink(SHM_NAME) != 0) {
350dc076565f772bb1953209fb69ea150b494aaa40robbiew		perror("An error occurs when calling shm_unlink()");
360dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNRESOLVED;
370dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
380dc076565f772bb1953209fb69ea150b494aaa40robbiew
39354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	fd = shm_open(SHM_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
400dc076565f772bb1953209fb69ea150b494aaa40robbiew	if (fd == -1 && errno == EEXIST) {
410dc076565f772bb1953209fb69ea150b494aaa40robbiew		printf("shm_open() can not create a new object.\n");
420dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_FAIL;
43df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper	} else if (fd == -1) {
440dc076565f772bb1953209fb69ea150b494aaa40robbiew		perror("shm_open");
450dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNRESOLVED;
460dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
470dc076565f772bb1953209fb69ea150b494aaa40robbiew
480dc076565f772bb1953209fb69ea150b494aaa40robbiew	printf("Test PASSED\n");
490dc076565f772bb1953209fb69ea150b494aaa40robbiew	shm_unlink(SHM_NAME);
500dc076565f772bb1953209fb69ea150b494aaa40robbiew	return PTS_PASS;
512c28215423293e443469a07ae7011135d058b671Garrett Cooper
52ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
53