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 mmap() function shall establish a mapping between a process's
10 * address space and a file,
11 *
12 * Test Steps:
13 * 1. Create a tmp file;
14 * 2. mmap it to memory using mmap();
15 *
16 */
17
18 #define _XOPEN_SOURCE 600
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <sys/mman.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <string.h>
27 #include <errno.h>
28 #include "posixtest.h"
29
main(void)30 int main(void)
31 {
32 char tmpfname[256];
33 ssize_t len = 1024;
34 char data[len];
35 void *pa;
36 int fd;
37
38 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_1_1_%d", getpid());
39 unlink(tmpfname);
40 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
41 if (fd == -1) {
42 printf("Error at open(): %s\n", strerror(errno));
43 return PTS_UNRESOLVED;
44 }
45
46 unlink(tmpfname);
47
48 memset(data, 'a', len);
49 if (write(fd, data, len) != len) {
50 printf("Error at write(): %s\n", strerror(errno));
51 return PTS_UNRESOLVED;
52 }
53
54 pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
55 if (pa == MAP_FAILED) {
56 printf("Error at mmap: %s\n", strerror(errno));
57 return PTS_FAIL;
58 }
59
60 if (*(char *)pa != 'a') {
61 printf("Test FAILED: The file was not mapped correctly.\n");
62 return PTS_FAIL;
63 }
64
65 close(fd);
66 munmap(pa, len);
67 printf("Test PASSED\n");
68 return PTS_PASS;
69 }
70