Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 28 May 2000 02:04:52 +0800
From:      Trent Nelson <tpnelson@echidna.stu.cowan.edu.au>
To:        hackers@FreeBSD.org
Cc:        dillon@backplane.com
Subject:   Erroneous mmap() behaviour?
Message-ID:  <39300E44.558B632F@student.cowan.edu.au>

next in thread | raw e-mail | index | archive | help
	
	Can someone explain to me why mmap() returns an address map you're
prohibited from accessing if the fd argument represents a file that has
just been created?

	I have a function that calls the following, where name represents a
file that, if it exists, is intended to get written over, and if it
doesn't exist, is intended to get created: 
		open(name,
		     (O_RDWR | O_CREAT | O_TRUNC),
		     (S_IRUSR | S_IWUSR));

	What I'm trying to do on return of this fd is, pass it through mmap()
and allocate a certain amount of memory to encapsulate some data I need
to put there - then calling msync() to have the memory buffer flushed
back to disk.

	The mmap() call is:

248:    mksd->msize_db = align(mksd->fsize_db);
249:    mksd->mmap_db = mmap(NULL,
                             mksd->msize_db,
                             (PROT_READ | PROT_WRITE),
                             MAP_PRIVATE,
                             mksd->fd_db,
                             NULL);

	If the file exists (size irrespective), then the following happens,
which is what we want:

bash-2.03$ ls -la database.dat
-rw-------  1 tnelson  tnelson  0 May 27 23:59 database.dat

bash-2.03$ gcc -g -o mksigdata mksd-2.c file_sigs.o
bash-2.03$ gdb mksigdata
[...]
(gdb) break 249
Breakpoint 1 at 0x8048df7: file mksd-2.c, line 249.
(gdb) run files.dat database.dat

Breakpoint 1, write_database (mksd=0xbfbffb50) at mksd-2.c:249
249	    mksd->mmap_db = mmap(NULL,
(gdb) n
255	    if ((int)mksd->mmap_db == -1)
(gdb) display *mksd
1: *mksd = {file_list = {files = 0x280f3000, history = 0x804d030, size =
[...]
  mmap_db = 0x280f4000}
            ^^^^^^^^^^ This is what we're after.

(Check the validity of the returned memory)
(gdb) x/4 0x280f4000
0x280f4000:	0x00000000	0x00000000	0x00000000	0x00000000

	So, it all works fine. As soon as the file is removed and it has to be
created, though:

bash-2.03$ rm database.dat
bash-2.03$ gdb mksigdata
[...]
  mmap_db = 0x280f4000}
(gdb) x/4 0x2980f4000
0x280f4000:	Error accessing memory address 0x280f4000: Bad address.

	Which is where my problem lies.

	Is it me, or mmap()? If it's me, is there any better way of doing what
I want to do? (which is essentially allocating memory to be modified,
then sync'd to a most probably newly-created file).

	From what I can see, the code works as it should in Linux.

	Thanks in advance.

	Regards,

		Trent.


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?39300E44.558B632F>