12

We open a boost shared memory that was created by another process like this

  boost::interprocess::managed_shared_memory segment(boost::interprocess::open_only, "SharedMem");

But if the process that created the shared memory was a root user, then the process reading it, if it was a normal user, will fail with the reason as:

terminate called after throwing an instance of 'boost::interprocess::interprocess_exception'
what():  Permission denied

What should i do to avoid this? that is to give permission to the shared memory to all?

Prasanth Madhavan
  • 12,657
  • 15
  • 62
  • 94

2 Answers2

8

If you look at the shared_memory constructor, it takes a permissions object. boost::interprocess::permissions::set_unrestricted is probably what you are looking for

void set_unrestricted();
//Sets permissions to unrestricted access:
//        A null DACL for windows or 0666 for UNIX.

According to this, it was added in 1.45 version

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
0

Below is a sample code snippet, to grant unrestricted permission to shared memory during creation

boost::interprocess::permissions  unrestricted_permissions;
unrestricted_permissions.set_unrestricted();

shared_mem = new managed_shared_memory(open_or_create, name.c_str(), size, 0, unrestricted_permissions);

KRG
  • 655
  • 7
  • 18