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