大概是吧umask
。
引用手册页shm_open
:
O_CREAT Create the shared memory object if it does not exist. The user and
group ownership of the object are taken from the corresponding effec‐
tive IDs of the calling process, and the object's permission bits are
set according to the low-order 9 bits of mode, except that those bits
set in the process file mode creation mask (see umask(2)) are cleared
for the new object. A set of macro constants which can be used to
define mode is listed in open(2). (Symbolic definitions of these
constants can be obtained by including <sys/stat.h>.)
因此,为了允许创建全局可写文件,您需要设置一个允许它的 umask,例如:
umask(0);
像这样设置,umask
不会再影响对创建文件的任何权限。但是,您应该注意,如果您随后创建另一个文件而不明确指定权限,那么它也将是全局可写的。
因此,您可能只想暂时清除 umask,然后将其恢复:
#include <sys/types.h>
#include <sys/stat.h>
...
void yourfunc()
{
// store old
mode_t old_umask = umask(0);
int fd = shm_open(SHARE_MEM_NAME,O_RDWR | O_CREAT,0606);
// restore old
umask(old_umask);
}