From owner-freebsd-hackers Sat May 27 11: 9:10 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from echidna.stu.cowan.edu.au (echidna.stu.cowan.edu.au [139.230.33.5]) by hub.freebsd.org (Postfix) with ESMTP id AE5A237B518 for ; Sat, 27 May 2000 11:09:02 -0700 (PDT) (envelope-from tpnelson@echidna.stu.cowan.edu.au) Received: from student.cowan.edu.au (s212n183.rev.ecu.edu.au [139.230.212.183]) by echidna.stu.cowan.edu.au (8.9.1/8.9.1) with ESMTP id CAA3406022; Sun, 28 May 2000 02:11:33 +0800 (WST) Message-ID: <39300E44.558B632F@student.cowan.edu.au> Date: Sun, 28 May 2000 02:04:52 +0800 From: Trent Nelson X-Mailer: Mozilla 4.7 [en] (WinNT; I) X-Accept-Language: en MIME-Version: 1.0 To: hackers@FreeBSD.org Cc: dillon@backplane.com Subject: Erroneous mmap() behaviour? Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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