Search code examples
cfileposixfile-permissions

What is the expected behaviour of a write after fchmod?


If I open and create a file for r/w access then subsequently use fchmod to remove write access what is the expected behaviour of a write on the same file descriptor directly after the fchmod call and before closing/reopening that file?

Is this specified by POSIX?

Are permissions only checked on an open?


Solution

  • For a fully-conforming implementation, it appears that changing the permissions of the file after it has been opened should have no effect on already-open file descriptors.

    From the version 7 chmod()/fchmodat() documentation:

    APPLICATION USAGE

    ...

    Any file descriptors currently open by any process on the file could possibly become invalid if the mode of the file is changed to a value which would deny access to that process. One situation where this could occur is on a stateless file system. This behavior will not occur in a conforming environment.

    This differs from the version 6 of POSIX, where the chmod() documentation states:

    The effect on file descriptors for files open at the time of a call to chmod() is implementation-defined.