ftok() returns different result for same (path, id) pair if the inode of the file is changed

Bug #1767097 reported by Mattias Säteri
6
This bug affects 1 person
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) {
        printf("Usage: ft <file>\n");
        return 1;
    }

    if (access(argv[1], F_OK) < 0) {
        printf("Failed to access file\n");
        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.

Revision history for this message
Florian Weimer (fw) wrote :

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.

Revision history for this message
Mattias Säteri (msateri) wrote :

Thanks for your response.

So, if this is the case then the behavior of the function is essentially this: given a set of inputs it returns a number. Given the same set of inputs again, it may return the same number or it may return a completely different number. It is basically undeterministic, since it is impossible to guarantee that nobody outside of my control will change the files on the file system. Why would you want it to behave this way instead of returning the same value every time for the same (path, id) pair?

Revision history for this message
Florian Weimer (fw) wrote :

I believe the expectations actually is that the function result depends on file system contents.

Revision history for this message
Mattias Säteri (msateri) wrote :

So I see. I just don't see how this is meant to be useful, since you can't know how the function will behave. Any file you choose can always be changed by someone outside of the program's control, so you can never know if you will get the same result every time or a different one.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.