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 shm_open() return a file descriptor for the shared memory object
11 * that is the lowest numbered file descriptor not currently open for that
12 * process
13 *
14 * Steps:
15 *  1. Open a temporary file.
16 *  2. Open a shared memory object.
17 *  3. Check that the file descriptor number of the shared memory object
18 *     follows the one of the temporary file.
19 * It assume that no file descriptor are closed before getting the two file
20 * descriptors.
21 */
22
23/* mkstemp is an XOPEN extension. */
24#define _XOPEN_SOURCE 600
25
26#include <stdio.h>
27#include <sys/mman.h>
28#include <sys/stat.h>
29#include <unistd.h>
30#include <stdlib.h>
31#include <fcntl.h>
32#include "posixtest.h"
33
34#define SHM_NAME "posixtest_8-1"
35
36int main() {
37	int fd1, fd2;
38	char path[25] = "/tmp/posixtestXXXXXX";
39
40	fd1 = mkstemp(path);
41	if(fd1 == -1) {
42		perror("An error occurs when calling mkstemp()");
43		return PTS_UNRESOLVED;
44	}
45
46	fd2 = shm_open(SHM_NAME, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR);
47	if(fd2 == -1) {
48		perror("An error occurs when calling shm_open()");
49		unlink(path);
50		return PTS_UNRESOLVED;
51	}
52
53	unlink(path);
54	shm_unlink(SHM_NAME);
55
56	if(fd2 == (fd1+1)) {
57		printf("Test PASSED\n");
58		return PTS_PASS;
59	}
60	printf("Test FAILED\n");
61	return PTS_FAIL;
62}
63