用于生成 ftok() 生成的密钥的公式是什么?ftok 是一个用于为 SYSTEM V IPC 创建密钥的 Linux 函数。
问问题
564 次
2 回答
3
key_t
ftok (const char *pathname, int proj_id)
{
struct stat64 st;
key_t key;
if (__xstat64 (_STAT_VER, pathname, &st) < 0)
return (key_t) -1;
key = ((st.st_ino & 0xffff) | ((st.st_dev & 0xff) << 16)
| ((proj_id & 0xff) << 24));
return key;
}
key_t
即它通过从 的低 8 位中proj_id
获取高 8 位、从所提供设备号的低 8 位中获取第二个高 8 位pathname
以及从低 16 位中获取低 16 位来创建一个 32 位提供的 inode 编号pathname
。
musl libc使用相同的算法:
key_t ftok(const char *path, int id)
{
struct stat st;
if (stat(path, &st) < 0) return -1;
return ((st.st_ino & 0xffff) | ((st.st_dev & 0xff) << 16) | ((id & 0xffu) << 24));
}
于 2019-02-02T12:07:04.900 回答
0
glibc库中的 ftok() 源代码是:
#include <sys/ipc.h>
#include <sys/stat.h>
key_t
ftok (const char *pathname, int proj_id)
{
struct stat64 st;
key_t key;
if (__xstat64 (_STAT_VER, pathname, &st) < 0)
return (key_t) -1;
key = ((st.st_ino & 0xffff) | ((st.st_dev & 0xff) << 16)
| ((proj_id & 0xff) << 24));
return key;
}
其他功能也可以在这里使用。
于 2019-02-02T13:35:08.053 回答