• 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 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 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <sys/mman.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <errno.h>
27 
28 #include "posixtest.h"
29 #include "tempfile.h"
30 
main(void)31 int main(void)
32 {
33 	char tmpfname[PATH_MAX];
34 	ssize_t len = 1024;
35 	char data[len];
36 	void *pa;
37 	int fd;
38 
39 	PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_1_1");
40 	unlink(tmpfname);
41 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
42 	if (fd == -1) {
43 		printf("Error at open(): %s\n", strerror(errno));
44 		return PTS_UNRESOLVED;
45 	}
46 
47 	unlink(tmpfname);
48 
49 	memset(data, 'a', len);
50 	if (write(fd, data, len) != len) {
51 		printf("Error at write(): %s\n", strerror(errno));
52 		return PTS_UNRESOLVED;
53 	}
54 
55 	pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
56 	if (pa == MAP_FAILED) {
57 		printf("Error at mmap: %s\n", strerror(errno));
58 		return PTS_FAIL;
59 	}
60 
61 	if (*(char *)pa != 'a') {
62 		printf("Test FAILED: The file was not mapped correctly.\n");
63 		return PTS_FAIL;
64 	}
65 
66 	close(fd);
67 	munmap(pa, len);
68 	printf("Test PASSED\n");
69 	return PTS_PASS;
70 }
71