简体   繁体   English

ftok()碰撞

[英]ftok() collisions

I am using ftok() to generate identifiers for shared memory segments used by a C application. 我使用ftok()为C应用程序使用的共享内存段生成标识符。 I am having problems, where on one box I am getting collisions with the identifiers used by root. 我遇到了问题,在一个盒子上我遇到了root使用的标识符。 I can fix it in this instance by hacking the code, but I would like a more robust solution. 我可以通过破解代码在这个例子中修复它,但我想要一个更强大的解决方案。

The application is installed into its own logical volume, and the path supplied to ftok is the binaries directory for the application (within that lv). 应用程序安装在自己的逻辑卷中,提供给ftok的路径是应用程序的二进制目录(在该lv中)。 The IDs supplied start at 1 and there are usually half a dozen or so. 提供的ID从1开始,通常有6个左右。

I've tracked down that ftok will do something like this: 我已经追踪到ftok会做这样的事情:

(id & 0xff) << 24 | (st.st_dev & 0xff) << 16 | (st.st_ino & 0xffff)

The combination of st.st_dev / st.st_ino should be very unique. st.st_dev / st.st_ino的组合应该是非常独特的。 But I've seen across a number of boxes, the least significant bit of st_dev is often 0 (ie st_dev numbers are generally multiples of 256). 但是我已经看过很多方框,st_dev的最低位通常是0(即st_dev数字通常是256的倍数)。 And because the binary directory is in a logical volume, there is no guarantee that the inode number will be different from something root uses. 并且由于二进制目录位于逻辑卷中,因此无法保证inode编号与root用户不同。

Is there a good way around this - a better alternative to ftok, or a way of setting up machines such that the st_dev numbers will be of more use to ftok? 有没有一个很好的方法 - 一个更好的替代ftok,或一种设置机器的方式,使得st_dev数字将更有用于ftok?

您可能需要考虑使用POSIX共享内存(通过shm_open ),它不会受到此类密钥冲突的影响

Your application should always be able to deal with key collisions. 您的应用程序应始终能够处理关键冲突。 A key could be in use by another unrelated process. 一个密钥可能被另一个不相关的进程使用。 But you could try to create your own version of ftok(), using more relevant bits. 但您可以尝试使用更相关的位创建自己的ftok()版本。

In theory any application needs only one "master" key, pointing to a "scoreboard" where the other keys can be found. 理论上,任何应用程序只需要一个“主”键,指向可以找到其他键的“记分板”。 Publicizing the masterkey on the filesystem might be a good idea. 在文件系统上公布masterkey可能是个好主意。 Restart after a crash will always be a problem. 崩溃后重新启动始终是个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM