Home | History | Annotate | Download | only in mmap
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Copyright (c) 2012, Cyril Hrubis <chrubis (at) 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  * Implementation performs mapping operations over whole pages.
     10  * Thus, while the argument len
     11  * need not meet a size or alignment constraint,
     12  * the implementation shall include, in any mapping
     13  * operation, any partial page specified by the range [pa,pa+len).
     14  * The system shall always zero-fill any partial page at the end of an object.
     15  * Further, the system shall never write out any modified portions of
     16  * the last page of an object which are beyond its end.
     17  *
     18  * Test Steps:
     19  * 1. Create a process, in this process:
     20  *    a. map a file with size of 1/2 * page_size,
     21  *       set len = 1/2 * page_size
     22  *    b. Read the partial page beyond the object size.
     23  *       Make sure the partial page is zero-filled;
     24  *    c. Modify a byte in the partial page, then un-map the and close the
     25  *       file descriptor.
     26  * 2. Wait for the child proces to exit, then read the file using read()
     27  *    and check that change wasn't written.
     28  */
     29 
     30 
     31 #include <stdio.h>
     32 #include <stdlib.h>
     33 #include <unistd.h>
     34 #include <sys/mman.h>
     35 #include <sys/types.h>
     36 #include <sys/stat.h>
     37 #include <sys/wait.h>
     38 #include <fcntl.h>
     39 #include <string.h>
     40 #include <errno.h>
     41 #include "posixtest.h"
     42 
     43 int main(void)
     44 {
     45 	char tmpfname[256];
     46 	long page_size;
     47 
     48 	char *pa, ch;
     49 	ssize_t len;
     50 	int fd;
     51 
     52 	pid_t child;
     53 	int i, exit_val, ret, size;
     54 
     55 	page_size = sysconf(_SC_PAGE_SIZE);
     56 
     57 	/* mmap will create a partial page */
     58 	len = page_size / 2;
     59 
     60 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_11_5_%d", getpid());
     61 	child = fork();
     62 	switch (child) {
     63 	case 0:
     64 		/* Create shared object */
     65 		unlink(tmpfname);
     66 		fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL,
     67 			  S_IRUSR | S_IWUSR);
     68 		if (fd == -1) {
     69 			printf("Error at open(): %s\n", strerror(errno));
     70 			return PTS_UNRESOLVED;
     71 		}
     72 		if (ftruncate(fd, len) == -1) {
     73 			printf("Error at ftruncate(): %s\n", strerror(errno));
     74 			return PTS_UNRESOLVED;
     75 		}
     76 
     77 		pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
     78 		if (pa == MAP_FAILED) {
     79 			printf("Error at mmap(): %s\n", strerror(errno));
     80 			return PTS_FAIL;
     81 		}
     82 
     83 		/* Check the patial page is ZERO filled */
     84 		for (i = len; i < page_size; i++)
     85 			if (pa[i] != 0) {
     86 				printf("Test FAILED: The partial page at the "
     87 				       "end of an object is not zero-filled\n");
     88 				return PTS_FAIL;
     89 			}
     90 
     91 		/* Write the partial page */
     92 		pa[len + 1] = 'b';
     93 		munmap(pa, len);
     94 		close(fd);
     95 		return PTS_PASS;
     96 	case -1:
     97 		printf("Error at fork(): %s\n", strerror(errno));
     98 		return PTS_UNRESOLVED;
     99 	default:
    100 		break;
    101 	}
    102 
    103 	wait(&exit_val);
    104 	if (!(WIFEXITED(exit_val) && (WEXITSTATUS(exit_val) == PTS_PASS))) {
    105 		unlink(tmpfname);
    106 
    107 		if (WIFEXITED(exit_val))
    108 			return WEXITSTATUS(exit_val);
    109 
    110 		printf("Child exited abnormally\n");
    111 		return PTS_UNRESOLVED;
    112 	}
    113 
    114 	fd = open(tmpfname, O_RDWR, 0);
    115 	unlink(tmpfname);
    116 
    117 	size = 0;
    118 
    119 	while ((ret = read(fd, &ch, 1)) == 1) {
    120 
    121 		if (ch != 0) {
    122 			printf("File is not zeroed\n");
    123 			return PTS_FAIL;
    124 		}
    125 
    126 		size += ret;
    127 	}
    128 
    129 	if (ret == -1) {
    130 		printf("Error at read(): %s\n", strerror(errno));
    131 		return PTS_UNRESOLVED;
    132 	}
    133 
    134 	if (size != len) {
    135 		printf("File has wrong size\n");
    136 		return PTS_UNRESOLVED;
    137 	}
    138 
    139 	close(fd);
    140 
    141 	printf("Test PASSED\n");
    142 	return PTS_PASS;
    143 }
    144