• 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 add an extra reference to the file
10  * associated with the file descriptor fildes which is not removed
11  * by a subsequent close() on that file descriptor.
12  * This reference shall be removed when there are no more
13  * mappings to the file.
14  *
15  * Test Steps:
16  * 1. Create a file, while it is open call unlink().
17  * 2. mmap the file to memory, then call close(). If mmap() added
18  *    extra reference to the file, the file content should be accesible
19  * 3. Acces and msync the mapped file
20  */
21 
22 #define _XOPEN_SOURCE 600
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <errno.h>
34 #include "posixtest.h"
35 
main(void)36 int main(void)
37 {
38 	char tmpfname[256];
39 	void *pa;
40 	ssize_t size = 1024;
41 	int fd, i;
42 
43 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_12_1_%d", getpid());
44 	unlink(tmpfname);
45 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
46 	if (fd == -1) {
47 		printf("Error at open(): %s\n", strerror(errno));
48 		return PTS_UNRESOLVED;
49 	}
50 
51 	unlink(tmpfname);
52 
53 	if (ftruncate(fd, size) == -1) {
54 		printf("Error at ftruncate(): %s\n", strerror(errno));
55 		return PTS_UNRESOLVED;
56 	}
57 
58 	pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
59 	if (pa == MAP_FAILED) {
60 		printf("Error at mmap: %s\n", strerror(errno));
61 		return PTS_FAIL;
62 	}
63 
64 	/* Close the file, now it's referenced only by the mapping */
65 	close(fd);
66 
67 	/* Fill the buffer */
68 	for (i = 0; i < size; i++)
69 		((char *)pa)[i] = (13 * i) % 21;
70 
71 	/* Force the data to be written to disk */
72 	msync(pa, size, MS_SYNC);
73 
74 	/* Check if the buffer still contains data */
75 	for (i = 0; i < size; i++) {
76 		if (((char *)pa)[i] != (13 * i) % 21) {
77 			printf("FAILED: Mapped buffer was not preserved\n");
78 			return PTS_FAIL;
79 		}
80 	}
81 
82 	/*
83 	 * Unmap the buffer, now data should be freed
84 	 * Unfortunaltely we have no definitive way to check
85 	 */
86 	munmap(pa, size);
87 
88 	printf("Test PASSED\n");
89 	return PTS_PASS;
90 }
91