设置boost创建的共享内存的权限

发布于 2024-12-25 04:09:40 字数 402 浏览 0 评论 0原文

我们打开一个由另一个进程创建的增强共享内存,如下所示

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

内存的进程是 root 用户,那么读取它的进程(如果是普通用户)将失败,原因如下:

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

但是,如果创建共享 我该怎么做才能避免这种情况?也就是给所有人共享内存的权限?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北笙凉宸 2025-01-01 04:09:40

如果您查看 shared_memory 构造函数,它需要一个权限对象。 boost::interprocess::permissions::set_unrestricted 可能是您正在寻找的

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

根据 这个,是1.45版本添加的

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

北渚 2025-01-01 04:09:40

下面是一个示例代码片段,用于在创建期间授予对共享内存的不受限制的权限

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);

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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文