• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
3  * Copyright (c) 2012, Cyril Hrubis <chrubis@suse.cz>
4  *
5  * This file is licensed under the GPL license.  For the full content
6  * of this license, see the COPYING file at the top level of this
7  * source tree.
8  *
9  * The file descriptor fildes shall have been opened with read permission,
10  * regardless of the protection options specified. If PROT_WRITE is
11  * specified, the application shall ensure that it has opened the file
12  * descriptor fildes with write permission unless MAP_PRIVATE
13  * is specified in the flags parameter as described below.
14  *
15  * Test Steps:
16  * 1  Open a file RDONLY permition.
17  * 2. Mmap the file to a memory region setting prot as PROT_WRITE.
18  * 3. Setting flag as MAP_SHARED.
19  * 4. Get EACCES error when mmap().
20  *
21  */
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/wait.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <errno.h>
32 
33 #include "posixtest.h"
34 #include "tempfile.h"
35 
main(void)36 int main(void)
37 {
38 	char tmpfname[PATH_MAX];
39 	void *pa;
40 	size_t size = 1024;
41 	int fd;
42 
43 	PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_6_4");
44 
45 	/* Create a tmp file */
46 	unlink(tmpfname);
47 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
48 	if (fd == -1) {
49 		printf("Error at open(): %s\n", strerror(errno));
50 		return PTS_UNRESOLVED;
51 	}
52 	if (ftruncate(fd, size) == -1) {
53 		printf("Error at ftruncate(): %s\n", strerror(errno));
54 		return PTS_UNRESOLVED;
55 	}
56 	close(fd);
57 
58 	/* Open as read only */
59 	fd = open(tmpfname, O_RDONLY, S_IRUSR | S_IWUSR);
60 	if (fd == -1) {
61 		printf("Error at 2nd open(): %s\n", strerror(errno));
62 		return PTS_UNRESOLVED;
63 	}
64 	unlink(tmpfname);
65 
66 	pa = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
67 	if (pa == MAP_FAILED && errno == EACCES) {
68 		printf("EACCES on attempt to map readonly file as "
69 		       "PROT_WRITE, MAP_SHARED\n" "Test PASSED\n");
70 		return PTS_PASS;
71 	}
72 
73 	printf("Maping readonly file with PROT_WRITE, MAP_SHARED have not "
74 	       "returned EACCES\n" "Test FAILED\n");
75 	return PTS_FAIL;
76 }
77