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 #define _XOPEN_SOURCE 600
     19 
     20 #include <stdio.h>
     21 #include <stdlib.h>
     22 #include <unistd.h>
     23 #include <sys/mman.h>
     24 #include <sys/types.h>
     25 #include <sys/stat.h>
     26 #include <sys/wait.h>
     27 #include <fcntl.h>
     28 #include <string.h>
     29 #include <errno.h>
     30 #include "posixtest.h"
     31 
     32 #define LOOP_NUM 100000
     33 
     34 int main(void)
     35 {
     36 	int rc;
     37 	unsigned long cnt;
     38 
     39 	char tmpfname[256];
     40 	long total_size;
     41 
     42 	void *pa;
     43 	size_t size;
     44 	int fd;
     45 
     46 	total_size = 1024;
     47 	size = total_size;
     48 
     49 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_10_1_%d", getpid());
     50 	unlink(tmpfname);
     51 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
     52 	if (fd == -1) {
     53 		printf("Error at open(): %s\n", strerror(errno));
     54 		return PTS_UNRESOLVED;
     55 	}
     56 	unlink(tmpfname);
     57 	if (ftruncate(fd, total_size) == -1) {
     58 		printf("Error at ftruncate(): %s\n", strerror(errno));
     59 		return PTS_UNRESOLVED;
     60 	}
     61 
     62 	for (cnt = 0; cnt < LOOP_NUM; cnt++) {
     63 		pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
     64 			  0);
     65 		if (pa == MAP_FAILED) {
     66 			printf("Test FAILED: Error at mmap: %s\n",
     67 			       strerror(errno));
     68 			return PTS_FAIL;
     69 		}
     70 
     71 		if (pa == NULL) {
     72 			printf("Test FAILED:"
     73 			       " mmap() map the file to 0 address "
     74 			       "without setting MAP_FIXED\n");
     75 			return PTS_FAIL;
     76 		}
     77 		rc = munmap(pa, size);
     78 		if (rc != 0) {
     79 			printf("Error at mnumap(): %s\n", strerror(errno));
     80 			return PTS_UNRESOLVED;
     81 		}
     82 	}
     83 
     84 	close(fd);
     85 	printf("Test PASSED\n");
     86 	return PTS_PASS;
     87 }
     88