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 with read only permition.
17 * 2. Mmap the file to a memory region setting prot as PROT_WRITE.
18 * 3. Setting flag as MAP_PRIVATE.
19 * 4. The mmap() should be sucessful.
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 #include "posixtest.h"
33
main(void)34 int main(void)
35 {
36 char tmpfname[256];
37 void *pa;
38 size_t size = 1024;
39 int fd;
40
41 /* Create the file */
42 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_6_5_%d", getpid());
43 unlink(tmpfname);
44 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
45 if (fd == -1) {
46 printf("Error at open(): %s\n", strerror(errno));
47 return PTS_UNRESOLVED;
48 }
49 if (ftruncate(fd, size) == -1) {
50 printf("Error at ftruncate(): %s\n", strerror(errno));
51 return PTS_UNRESOLVED;
52 }
53 close(fd);
54
55 /* Open it readonly */
56 fd = open(tmpfname, O_RDONLY, S_IRUSR | S_IWUSR);
57 if (fd == -1) {
58 printf("Error at 2nd open(): %s\n", strerror(errno));
59 return PTS_UNRESOLVED;
60 }
61 unlink(tmpfname);
62
63 pa = mmap(NULL, size, PROT_WRITE, MAP_PRIVATE, fd, 0);
64 if (pa == MAP_FAILED) {
65 printf("Error at mmap(): %s\n", strerror(errno));
66 return PTS_FAIL;
67 }
68
69 munmap(pa, size);
70 close(fd);
71
72 printf("Succesfully mapped readonly file with "
73 "PROT_WRITE, MAP_PRIVATE\n" "Test PASSED\n");
74 return PTS_PASS;
75 }
76