ftok() returns different result for same (path, id) pair if the inode of the file is changed
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
glibc (Ubuntu) |
New
|
Undecided
|
Unassigned |
Bug Description
Release: Ubuntu 16.04.2 LTS
Package: libc6:
Installed: 2.23-0ubuntu7
The ftok man page states that: "The resulting value is the same for all pathnames that name the same file, when the same value of proj_id is used".
However, if you call ftok() twice with the same path and id, and the file has been deleted and created again in between (and the inode of the file has been changed by this operation, which it will if other files were created in between), then the result of ftok() will not be the same.
The following program can be used to reproduce the issue:
#include <sys/types.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int id = 121;
if (argc != 2) {
return 1;
}
if (access(argv[1], F_OK) < 0) {
return 1;
}
printf("ftok %s %d: 0x%x\n", argv[1], id, ftok(argv[1], (char)id));
return 0;
}
Compile it into a binary named 'ft', then do the following:
$ touch foo
$ ./ft foo
ftok foo 121: 0x790225bd
$ rm foo
$ touch bar
$ touch foo
$ ftok foo
ftok foo 121: 0x790226ea
The result is not the same although the path and id are the same, which is not what should happen according to the man page.
You misunderstood what the manpage intends to say. Over time, two pathnames can refer to different files. Essentially, the return value depends on the file, not on the pathname. If you delete a file and recreate the file, it is no longer the same file.