Home | History | Annotate | Download | only in mmap
      1 /*
      2  * Copyright (c) International Business Machines  Corp., 2001
      3  *
      4  * This program is free software;  you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation; either version 2 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     12  * the GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program;  if not, write to the Free Software
     16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     17  */
     18 
     19 /*
     20  * Test Description:
     21  *  Verify that mmap() fails to map a file creating a mapped region
     22  *  when the file specified by file descriptor is not valid.
     23  *
     24  * Expected Result:
     25  *  mmap() should fail returning -1 and errno should get set to EBADF.
     26  *
     27  * HISTORY
     28  *	07/2001 Ported by Wayne Boyer
     29  */
     30 #include <stdio.h>
     31 #include <stdlib.h>
     32 #include <sys/types.h>
     33 #include <errno.h>
     34 #include <unistd.h>
     35 #include <fcntl.h>
     36 #include <string.h>
     37 #include <signal.h>
     38 #include <sys/stat.h>
     39 #include <sys/mman.h>
     40 
     41 #include "test.h"
     42 
     43 #define TEMPFILE	"mmapfile"
     44 
     45 char *TCID = "mmap08";
     46 int TST_TOTAL = 1;
     47 
     48 static size_t page_sz;
     49 static char *addr;
     50 static int fildes;
     51 
     52 static void setup(void);
     53 static void cleanup(void);
     54 
     55 int main(int ac, char **av)
     56 {
     57 	int lc;
     58 
     59 	tst_parse_opts(ac, av, NULL, NULL);
     60 
     61 	setup();
     62 
     63 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     64 
     65 		tst_count = 0;
     66 
     67 		/*
     68 		 * Call mmap to map the temporary file 'TEMPFILE'
     69 		 * which is already closed. so, fildes is not valid.
     70 		 */
     71 		errno = 0;
     72 		addr = mmap(0, page_sz, PROT_WRITE,
     73 			    MAP_FILE | MAP_SHARED, fildes, 0);
     74 		TEST_ERRNO = errno;
     75 
     76 		/* Check for the return value of mmap() */
     77 		if (addr != MAP_FAILED) {
     78 			tst_resm(TFAIL, "mmap() didn't fail (%p != %p)",
     79 				 addr, MAP_FAILED);
     80 			/* Unmap the mapped memory */
     81 			if (munmap(addr, page_sz) != 0) {
     82 				tst_brkm(TBROK, cleanup, "munmap() failed");
     83 			}
     84 			continue;
     85 		}
     86 		if (TEST_ERRNO == EBADF) {
     87 			tst_resm(TPASS, "mmap failed with EBADF");
     88 		} else {
     89 			tst_resm(TFAIL | TERRNO,
     90 				 "mmap failed with an invalid errno");
     91 		}
     92 	}
     93 
     94 	cleanup();
     95 	tst_exit();
     96 }
     97 
     98 static void setup(void)
     99 {
    100 	char *tst_buff;
    101 
    102 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    103 
    104 	TEST_PAUSE;
    105 
    106 	page_sz = getpagesize();
    107 
    108 	if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) {
    109 		tst_brkm(TFAIL, NULL,
    110 			 "calloc() failed to allocate space for tst_buff");
    111 	}
    112 
    113 	/* Fill the test buffer with the known data */
    114 	memset(tst_buff, 'A', page_sz);
    115 
    116 	tst_tmpdir();
    117 
    118 	/* Creat a temporary file used for mapping */
    119 	if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) {
    120 		free(tst_buff);
    121 		tst_brkm(TFAIL, cleanup, "opening %s failed", TEMPFILE);
    122 	}
    123 
    124 	/* Write test buffer contents into temporary file */
    125 	if (write(fildes, tst_buff, page_sz) != page_sz) {
    126 		free(tst_buff);
    127 		tst_brkm(TFAIL, cleanup, "writing to %s failed", TEMPFILE);
    128 	}
    129 
    130 	/* Free the memory allocated for test buffer */
    131 	free(tst_buff);
    132 
    133 	/* Close the temporary file opened for writing */
    134 	if (close(fildes) < 0) {
    135 		tst_brkm(TFAIL, cleanup, "closing %s failed", TEMPFILE);
    136 	}
    137 }
    138 
    139 static void cleanup(void)
    140 {
    141 	tst_rmdir();
    142 }
    143