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 file is not open for write access when the applications
11 * specify the value of O_RDONLY.
12 *
13 * The test uses ftruncate to check that the file is not open for write access.
14 */
15
16/* ftruncate was formerly an XOPEN extension. We define _XOPEN_SOURCE here to
17   avoid warning if the implementation does not program ftruncate as a base
18   interface */
19#define _XOPEN_SOURCE 600
20
21#include <sys/mman.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <stdio.h>
25#include <unistd.h>
26#include <errno.h>
27#include "posixtest.h"
28
29#define SHM_NAME "posixtest_13-1"
30#define BUF_SIZE 8
31
32int main(){
33	int fd, result;
34
35	fd = shm_open(SHM_NAME, O_RDONLY|O_CREAT, S_IRUSR|S_IWUSR);
36	if(fd == -1) {
37		perror("An error occurs when calling shm_open()");
38		return PTS_UNRESOLVED;
39	}
40
41	result = ftruncate(fd, BUF_SIZE);
42
43	if(result == -1 && errno == EINVAL) {
44		printf("Test PASSED\n");
45		shm_unlink(SHM_NAME);
46		return PTS_PASS;
47	} else if(result == 0){
48		printf("The file is open for write acces.\n");
49		shm_unlink(SHM_NAME);
50		return PTS_FAIL;
51	}
52
53	perror("ftruncate");
54	shm_unlink(SHM_NAME);
55	return PTS_FAIL;
56}
57
58