5-1.c revision ec6edca7aa42b6affd989ef91b5897f96795e40f
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
240dc076565f772bb1953209fb69ea150b494aaa40robbiewint main() {
250dc076565f772bb1953209fb69ea150b494aaa40robbiew	int fd;
262c28215423293e443469a07ae7011135d058b671Garrett Cooper
270dc076565f772bb1953209fb69ea150b494aaa40robbiew	fd = shm_open(SHM_NAME, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
28df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper	if (fd == -1) {
290dc076565f772bb1953209fb69ea150b494aaa40robbiew		perror("An error occurs when calling shm_open()");
300dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNRESOLVED;
310dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
320dc076565f772bb1953209fb69ea150b494aaa40robbiew
338fb1cdb0538640f295691929650408688537fb7fGarrett Cooper	if (shm_unlink(SHM_NAME) != 0) {
340dc076565f772bb1953209fb69ea150b494aaa40robbiew		perror("An error occurs when calling shm_unlink()");
350dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNRESOLVED;
360dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
370dc076565f772bb1953209fb69ea150b494aaa40robbiew
380dc076565f772bb1953209fb69ea150b494aaa40robbiew	fd = shm_open(SHM_NAME, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
390dc076565f772bb1953209fb69ea150b494aaa40robbiew	if (fd == -1 && errno == EEXIST) {
400dc076565f772bb1953209fb69ea150b494aaa40robbiew		printf("shm_open() can not create a new object.\n");
410dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_FAIL;
42df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper	} else if (fd == -1) {
430dc076565f772bb1953209fb69ea150b494aaa40robbiew		perror("shm_open");
440dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNRESOLVED;
450dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
460dc076565f772bb1953209fb69ea150b494aaa40robbiew
470dc076565f772bb1953209fb69ea150b494aaa40robbiew	printf("Test PASSED\n");
480dc076565f772bb1953209fb69ea150b494aaa40robbiew	shm_unlink(SHM_NAME);
490dc076565f772bb1953209fb69ea150b494aaa40robbiew	return PTS_PASS;
502c28215423293e443469a07ae7011135d058b671Garrett Cooper
51ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
52