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  * When the implementation selects a
     10  * value for pa, it never places a mapping at address 0.
     11  *
     12  * Test steps:
     13  * This is not a good test. Cannot make sure (pa == 0) never happens.
     14  * Repeat LOOP_NUM times mmap() and mnumap(),
     15  * make sure pa will not equal 0.
     16  */
     17 
     18 
     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 <sys/wait.h>
     26 #include <fcntl.h>
     27 #include <string.h>
     28 #include <errno.h>
     29 #include "posixtest.h"
     30 
     31 #define LOOP_NUM 100000
     32 
     33 int main(void)
     34 {
     35 	int rc;
     36 	unsigned long cnt;
     37 
     38 	char tmpfname[256];
     39 	long total_size;
     40 
     41 	void *pa;
     42 	size_t size;
     43 	int fd;
     44 
     45 	total_size = 1024;
     46 	size = total_size;
     47 
     48 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_10_1_%d", getpid());
     49 	unlink(tmpfname);
     50 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
     51 	if (fd == -1) {
     52 		printf("Error at open(): %s\n", strerror(errno));
     53 		return PTS_UNRESOLVED;
     54 	}
     55 	unlink(tmpfname);
     56 	if (ftruncate(fd, total_size) == -1) {
     57 		printf("Error at ftruncate(): %s\n", strerror(errno));
     58 		return PTS_UNRESOLVED;
     59 	}
     60 
     61 	for (cnt = 0; cnt < LOOP_NUM; cnt++) {
     62 		pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
     63 			  0);
     64 		if (pa == MAP_FAILED) {
     65 			printf("Test FAILED: Error at mmap: %s\n",
     66 			       strerror(errno));
     67 			return PTS_FAIL;
     68 		}
     69 
     70 		if (pa == NULL) {
     71 			printf("Test FAILED:"
     72 			       " mmap() map the file to 0 address "
     73 			       "without setting MAP_FIXED\n");
     74 			return PTS_FAIL;
     75 		}
     76 		rc = munmap(pa, size);
     77 		if (rc != 0) {
     78 			printf("Error at mnumap(): %s\n", strerror(errno));
     79 			return PTS_UNRESOLVED;
     80 		}
     81 	}
     82 
     83 	close(fd);
     84 	printf("Test PASSED\n");
     85 	return PTS_PASS;
     86 }
     87