Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 3 Jun 1996 06:52:32 +1000
From:      Bruce Evans <bde@zeta.org.au>
To:        bde@freebsd.org, pjf@cts.com
Cc:        dyson@freebsd.org, hackers@freebsd.org
Subject:   Re: bugs
Message-ID:  <199606022052.GAA06199@godzilla.zeta.org.au>

next in thread | raw e-mail | index | archive | help
>| Subject: Re: bugs
>| MMAP doesn't extend files, AFAIK it doesn't on many (if not most
>| other OSes.)  To set the length of the file you need to do an ftruncate.
>| After that, then blocks will be allocated as needed.

>Sorry, I screwed up my example.  Here's the real one:

>-------------------------
>#include <stdio.h>
>#include <sys/mman.h>
>#include <sys/file.h>

>main()
>{
>  int fd = open("newfile", O_RDWR|O_CREAT|O_EXCL, 0660);
>  char *buf;
>  ftruncate(fd, 100);
>  buf = mmap(NULL, 100, PROT_WRITE, MAP_SHARED, fd, 0);
>  printf("%lx\n", buf);
>  strcpy(buf, "hi!");
>}
>-------------------------

>This doesn't work either.  The ftruncate() appears not to work; the file
>is still zero length after the program crashes.  So perhaps the problem
>is with ftruncate() and not mmap().

>This works on all of our 10 existing UNIX platforms except BSDI and
>Linux.  BSDI has the same problem with ftruncate, I think; Linux's
>ftruncate works, but its mmap() appears to be totally broken, at least
>in 1.2.13.

The ftruncate() works if a prototype for ftruncate() is in scope or if
the `length' arg to ftruncate has the correct type (off_t = long long).
Otherwise the top 32 bits of the length are random.  ftruncate() is
prototyped in <unistd.h>.  You should also include <sys/types.h> before
including <sys/mman.h>.  The above happens to work because <stdio.h>
bogusly includes <sys/types.h>.  You should also include <fcntl.h>
instead of <sys/file.h> except on old systems.

The strcpy() doesn't work unless PROT_WRITE is changed to
`PROT_READ | PROT_WRITE', and this isn't because strcpy() reads its
target - `buf[0] = 1' fails in the same way.

Bruce



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199606022052.GAA06199>