mmap() fails under QNX RTP, why?

Hi,

I caught the following thread on comp.os.qnx:
http://groups.google.com/groups?hl=en&lr=&safe=off&ic=1&th=ab3322aa77227c12

According to Dmitri Antipov <dmitry.antipov@auriga.ru> stated that the
following
call to mmap will fail under QNX:


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

int
main (int argc, char *argv[])
{
int fd;
char mem;
struct stat buf;
if ((fd = open (
++argv, O_RDONLY)) < 0) {
printf (“open(”%s",…) oops (%s)\n", *argv, strerror (errno));
exit (errno);
}
if (lstat (*argv, &buf) < 0) {
printf (“lstat(”%s",…) oops (%s)\n", *argv, strerror (errno));
exit (errno);
}
if ((mem = mmap (NULL, buf.st_size, PROT_READ, MAP_SHARED, fd, 0))==
MAP_FAILED){
printf (“mmap(”%s",…) oops (%s)\n", *argv, strerror (errno));
exit (errno);
}
printf (“mmap(”%s",…) ok, %d bytes at %p\n", *argv, buf.st_size,
mem);
munmap (mem, buf.st_size);
close (fd);
return 0;
}


Why is this the case, and what is the workaround?
I believe that this problem is one of the problems
causing the ACE library to crash under QNX
(refer to the thread in this newsgroup: QNX, ACE, and gcc,
started by my colleague, Prakash Manghwani).

Craig Rodrigues Distributed Systems and Logistics
crodrigu@bbn.com BBN Technologies
Cambridge, MA

Craig Rodrigues <crodrigu@bbn.com> wrote:

Hi,

Nobody else seems to be answering this, so…

When you run this program, what do you pass as the arguments – in
particular, what do you pass as the pathname that you are trying to
mmap()?

Who owns that file?

What version of QNX are you running?

What errno are you getting?

I would bet that you are trying to mmap() a file (rather than a
memory object). Many QNX filesystems don’t support this, and even
where it is supported, you have to be very careful as combining
mmap() and read()/write() access/modification of a file can give
inconsistent results.

-David

I caught the following thread on comp.os.qnx:
http://groups.google.com/groups?hl=en&lr=&safe=off&ic=1&th=ab3322aa77227c12

According to Dmitri Antipov <> dmitry.antipov@auriga.ru> > stated that the
following
call to mmap will fail under QNX:



#include <stdio.h
#include <fcntl.h
#include <errno.h
#include <sys/stat.h
#include <sys/mman.h

int
main (int argc, char *argv[])
{
int fd;
char mem;
struct stat buf;
if ((fd = open (
++argv, O_RDONLY)) < 0) {
printf (“open(”%s",…) oops (%s)\n", *argv, strerror (errno));
exit (errno);
}
if (lstat (*argv, &buf) < 0) {
printf (“lstat(”%s",…) oops (%s)\n", *argv, strerror (errno));
exit (errno);
}
if ((mem = mmap (NULL, buf.st_size, PROT_READ, MAP_SHARED, fd, 0))==
MAP_FAILED){
printf (“mmap(”%s",…) oops (%s)\n", *argv, strerror (errno));
exit (errno);
}
printf (“mmap(”%s",…) ok, %d bytes at %p\n", *argv, buf.st_size,
mem);
munmap (mem, buf.st_size);
close (fd);
return 0;
}



Why is this the case, and what is the workaround?
I believe that this problem is one of the problems
causing the ACE library to crash under QNX
(refer to the thread in this newsgroup: QNX, ACE, and gcc,
started by my colleague, Prakash Manghwani).

Craig Rodrigues Distributed Systems and Logistics
crodrigu@bbn.com > BBN Technologies
Cambridge, MA


QNX Training Services
dagibbs@qnx.com

Put this goddamned problem into FAQ, will you David?

You must pass MAP_NOSYNCFILE with flags if you’re mapping a file from
any filesystem other than /dev/shmem or /proc/boot. Which means, QNX
does not support syncronized memory & I/O access to file (except when
the file is in memory), which means whatever you do with your mapped
memory will not be reflected in the file.

There is msync() call to syncronize them explicitly, but I never tested
if it works.

Many applications do not need syncronized access but default behavior of
mmap() on Unix is to support it. Some applications might rely on it,
which is why this flag must be passed by application to avoid false
assumptions. On the other hand, this flag is not so common on Unix
systems (I think FreeBSD has MAP_NOSYNC) so if you want to port
something with mmap() you have to figure out if that app really needs
syncronized access or not and if not you might try putting
MAP_NOSYNCFILE there. You usually also have to enable using mmap()
manually after ./configure because ./configure will disable mmap() since
it’s tests will fail due to the same reason.

  • Igor

David Gibbs wrote:

Craig Rodrigues <> crodrigu@bbn.com> > wrote:
Hi,

Nobody else seems to be answering this, so…

When you run this program, what do you pass as the arguments – in
particular, what do you pass as the pathname that you are trying to
mmap()?

Who owns that file?

What version of QNX are you running?

What errno are you getting?

I would bet that you are trying to mmap() a file (rather than a
memory object). Many QNX filesystems don’t support this, and even
where it is supported, you have to be very careful as combining
mmap() and read()/write() access/modification of a file can give
inconsistent results.

-David

I caught the following thread on comp.os.qnx:
http://groups.google.com/groups?hl=en&lr=&safe=off&ic=1&th=ab3322aa77227c12

According to Dmitri Antipov <> dmitry.antipov@auriga.ru> > stated that the
following
call to mmap will fail under QNX:

#include <stdio.h
#include <fcntl.h
#include <errno.h
#include <sys/stat.h
#include <sys/mman.h

int
main (int argc, char *argv[])
{
int fd;
char mem;
struct stat buf;
if ((fd = open (
++argv, O_RDONLY)) < 0) {
printf (“open(”%s",…) oops (%s)\n", *argv, strerror (errno));
exit (errno);
}
if (lstat (*argv, &buf) < 0) {
printf (“lstat(”%s",…) oops (%s)\n", *argv, strerror (errno));
exit (errno);
}
if ((mem = mmap (NULL, buf.st_size, PROT_READ, MAP_SHARED, fd, 0))==
MAP_FAILED){
printf (“mmap(”%s",…) oops (%s)\n", *argv, strerror (errno));
exit (errno);
}
printf (“mmap(”%s",…) ok, %d bytes at %p\n", *argv, buf.st_size,
mem);
munmap (mem, buf.st_size);
close (fd);
return 0;
}

Why is this the case, and what is the workaround?
I believe that this problem is one of the problems
causing the ACE library to crash under QNX
(refer to the thread in this newsgroup: QNX, ACE, and gcc,
started by my colleague, Prakash Manghwani).

Craig Rodrigues Distributed Systems and Logistics
crodrigu@bbn.com > BBN Technologies
Cambridge, MA


QNX Training Services
dagibbs@qnx.com