Pab*_*rin 4 linux glibc shared-memory
我有一个GNU/Linux应用程序,它使用了许多共享内存对象.它可能会在同一系统上运行多次.为了保持整洁,我首先/dev/shm为每个共享内存对象集创建一个目录.
问题是在较新的GNU/Linux发行版上,我似乎不再能够在子目录中创建它们了/dev/shm.
以下是一个最小的C程序,说明了我在说什么:
/*****************************************************************************
* shm_minimal.c
*
* Test shm_open()
*
* Expect to create shared memory file in:
* /dev/shm/
* ??? my_dir
* ??? shm_name
*
* NOTE: Only visible on filesystem during execution. I try to be nice, and
* clean up after myself.
*
* Compile with:
* $ gcc -lrt shm_minimal.c -o shm_minimal
*
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, const char* argv[]) {
int shm_fd = -1;
char* shm_dir = "/dev/shm/my_dir";
char* shm_file = "/my_dir/shm_name"; /* does NOT work */
//char* shm_file = "/my_dir_shm_name"; /* works */
// Create directory in /dev/shm
mkdir(shm_dir, 0777);
// make shared memory segment
shm_fd = shm_open(shm_file, O_RDWR | O_CREAT, 0600);
if (-1 == shm_fd) {
switch (errno) {
case EINVAL:
/* Confirmed on:
* kernel v3.14, GNU libc v2.19 (ArchLinux)
* kernel v3.13, GNU libc v2.19 (Ubuntu 14.04 Beta 2)
*/
perror("FAIL - EINVAL");
return 1;
default:
printf("Some other problem not being tested\n");
return 2;
}
} else {
/* Confirmed on:
* kernel v3.8, GNU libc v2.17 (Mint 15)
* kernel v3.2, GNU libc v2.15 (Xubuntu 12.04 LTS)
* kernel v3.1, GNU libc v2.13 (Debian 6.0)
* kernel v2.6.32, GNU libc v2.12 (RHEL 6.4)
*/
printf("Success !!!\n");
}
// clean up
close(shm_fd);
shm_unlink(shm_file);
rmdir(shm_dir);
return 0;
}
/* vi: set ts=2 sw=2 ai expandtab:
*/
Run Code Online (Sandbox Code Playgroud)
当我在一个相当新的分配,调用运行这个程序shm_open()的回报-1,并errno设置为EINVAL.但是,当我运行较旧的东西时,它会/dev/shm/my_dir按预期创建共享内存对象.
对于更大的应用程序,解决方案很简单.我可以使用公共前缀而不是目录.
如果你可以帮助我了解这种明显的行为改变,那将会非常有帮助.我怀疑其他人可能会尝试做类似的事情.
事实证明,问题源于GNU libc如何验证共享内存名称.具体来说,共享内存对象现在必须位于shmfs挂载点的根目录.
作为bug BZ#16274的结果,这在glibc git commit b20de2c3d9中被更改.
具体来说,改变是这样的:
if (name[0] == '\0' || namelen > NAME_MAX || strchr (name, '/') != NULL)
Run Code Online (Sandbox Code Playgroud)
现在不允许'/'从文件名中的任何地方(不包括领先'/')
| 归档时间: |
|
| 查看次数: |
3672 次 |
| 最近记录: |