Bug 7382 - Can't mmap a O_WRONLY fd (must be O_RDWR)
Summary: Can't mmap a O_WRONLY fd (must be O_RDWR)
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: man-pages
Version: 6.1
Hardware: i386
OS: Linux
Target Milestone: ---
Assignee: Trond Eivind Glomsrxd
QA Contact:
Depends On:
TreeView+ depends on / blocked
Reported: 1999-11-28 02:45 UTC by James Manning
Modified: 2008-05-01 15:37 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Last Closed: 2000-05-29 22:00:07 UTC

Attachments (Terms of Use)

Description James Manning 1999-11-28 02:45:33 UTC
open("/tmp/newfile", O_WRONLY)          = 3
mmap(0, 4096, PROT_WRITE, MAP_SHARED, 3, 0) = -1 EACCES (Permission denied)

open("/tmp/newfile", O_RDWR)            = 3
mmap(0, 4096, PROT_WRITE, MAP_SHARED, 3, 0) = 0x40013000

According to the mmap man page, EACCES when MAP_SHARED
was asked and PROT_WRITE is set means that the fd
was not open for writing (O_WRONLY obviously is)

Since expecting O_RDWR seems reasonable, maybe the man page could
simply better reflect this requirement.

--- /usr/man/man2/mmap.2.orig	Sat Nov 27 22:51:15 1999
+++ /usr/man/man2/mmap.2	Sat Nov 27 22:51:41 1999
@@ -128 +128 @@
-is not open for writing.
+is not open in read/write (O_RDWR) mode.

Sample code:

#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
   char *newfile="/tmp/newfile",*newptr;
   int newfd,size=getpagesize(); // guaranteed to be page-sized :)

   if (atoi(argv[1])) newfd=open(newfile,O_RDWR,0777);
   else newfd=open(newfile,O_WRONLY,0777);
   newptr=(char *)mmap(NULL,size,PROT_WRITE,MAP_SHARED,newfd,(int)NULL);
   if (newptr == MAP_FAILED) {
      perror("We had an mmap error");
   printf("mapped file %s size %d at %p.\n", newfile, size, newptr);
   strcpy(newptr,"Mary had a little lamb\n");


Comment 1 Cristian Gafton 2000-05-22 15:02:59 UTC
assigned to teg

Comment 2 Trond Eivind Glomsrxd 2000-05-29 22:00:59 UTC
Should be fixed in the next rawhide. Please note that glibc doesn't come with
man pages, so you should use the info pages are a better reference. The GNU
project considers man-pages obsolete (bah - much more convenient IMHO. I don't
like info.)

Note You need to log in before you can comment on or make changes to this bug.