From: Richard Sharman (sharman@Software.Mitel.COM)
Date: 07/27/92


From: sharman@Software.Mitel.COM (Richard Sharman)
Subject: I can't get mmap to work
Date: 28 Jul 1992 04:28:59 GMT

I was trying to use "mmap", and am having problems.

I have a simple example, below, which attempts to use mmap on a file,
and simply prints the value returned. I looked in the source
(linux/mm/mmap.c) and it seemed that the address has to be a valid
address in the data space, at a multiple of 0x1000, and the length
also has to be a multiple of that.

The demo program fails on mmap, with errno=1 (EPERM). This I don't
understand, since I couldn't see any failures of the kind
return(-EPERM). I tried it on the SUN under gdb, and the mmap
succeeded and buff1 looked ok. (It bombed on the printf though!)

System: 0.96c-pl2 (and GCC 2.2.2 of course!)

Here is the code:
=====================
/* attempt to use mmap */

#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>

main(int argc, char **argv)
{

  caddr_t file1, file2 ;
  int fd1, fd2 ;
  int save_errno ;
  
#define LEN1 2048
  caddr_t buff1 ;

  buff1 = (caddr_t) memalign(0x1000, LEN1) ;
  if ( buff1 == NULL ) {
    perror("malloc failed") ;
    exit(2) ;
  }

  if ( argc !=2 ) {
    fprintf(stderr, "no file specified\n") ;
    exit(2) ;
  }

  fd1 = open(argv[1], O_RDONLY) ;
  if ( fd1 < 0 ) {
    perror("open failed") ;
    exit(2) ;
  }
  file1 = mmap(buff1, LEN1, PROT_READ, MAP_SHARED|MAP_FIXED, fd1, 0) ;
  save_errno = errno ;
  perror("") ;
  printf("mmap returned %x errno was %d\n", file1, save_errno) ;

  exit(0) ;
}
  
======================
and this is the output:
======================
~/test% demo1 demo1.c
Operation not permitted
mmap returned ffffffff errno was 1
~/test%

Any help appreciated,
Richard