mandatory.txt 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. Mandatory File Locking For The Linux Operating System
  2. Andy Walker <andy@lysaker.kvaerner.no>
  3. 15 April 1996
  4. 1. What is mandatory locking?
  5. ------------------------------
  6. Mandatory locking is kernel enforced file locking, as opposed to the more usual
  7. cooperative file locking used to guarantee sequential access to files among
  8. processes. File locks are applied using the flock() and fcntl() system calls
  9. (and the lockf() library routine which is a wrapper around fcntl().) It is
  10. normally a process' responsibility to check for locks on a file it wishes to
  11. update, before applying its own lock, updating the file and unlocking it again.
  12. The most commonly used example of this (and in the case of sendmail, the most
  13. troublesome) is access to a user's mailbox. The mail user agent and the mail
  14. transfer agent must guard against updating the mailbox at the same time, and
  15. prevent reading the mailbox while it is being updated.
  16. In a perfect world all processes would use and honour a cooperative, or
  17. "advisory" locking scheme. However, the world isn't perfect, and there's
  18. a lot of poorly written code out there.
  19. In trying to address this problem, the designers of System V UNIX came up
  20. with a "mandatory" locking scheme, whereby the operating system kernel would
  21. block attempts by a process to write to a file that another process holds a
  22. "read" -or- "shared" lock on, and block attempts to both read and write to a
  23. file that a process holds a "write " -or- "exclusive" lock on.
  24. The System V mandatory locking scheme was intended to have as little impact as
  25. possible on existing user code. The scheme is based on marking individual files
  26. as candidates for mandatory locking, and using the existing fcntl()/lockf()
  27. interface for applying locks just as if they were normal, advisory locks.
  28. Note 1: In saying "file" in the paragraphs above I am actually not telling
  29. the whole truth. System V locking is based on fcntl(). The granularity of
  30. fcntl() is such that it allows the locking of byte ranges in files, in addition
  31. to entire files, so the mandatory locking rules also have byte level
  32. granularity.
  33. Note 2: POSIX.1 does not specify any scheme for mandatory locking, despite
  34. borrowing the fcntl() locking scheme from System V. The mandatory locking
  35. scheme is defined by the System V Interface Definition (SVID) Version 3.
  36. 2. Marking a file for mandatory locking
  37. ---------------------------------------
  38. A file is marked as a candidate for mandatory locking by setting the group-id
  39. bit in its file mode but removing the group-execute bit. This is an otherwise
  40. meaningless combination, and was chosen by the System V implementors so as not
  41. to break existing user programs.
  42. Note that the group-id bit is usually automatically cleared by the kernel when
  43. a setgid file is written to. This is a security measure. The kernel has been
  44. modified to recognize the special case of a mandatory lock candidate and to
  45. refrain from clearing this bit. Similarly the kernel has been modified not
  46. to run mandatory lock candidates with setgid privileges.
  47. 3. Available implementations
  48. ----------------------------
  49. I have considered the implementations of mandatory locking available with
  50. SunOS 4.1.x, Solaris 2.x and HP-UX 9.x.
  51. Generally I have tried to make the most sense out of the behaviour exhibited
  52. by these three reference systems. There are many anomalies.
  53. All the reference systems reject all calls to open() for a file on which
  54. another process has outstanding mandatory locks. This is in direct
  55. contravention of SVID 3, which states that only calls to open() with the
  56. O_TRUNC flag set should be rejected. The Linux implementation follows the SVID
  57. definition, which is the "Right Thing", since only calls with O_TRUNC can
  58. modify the contents of the file.
  59. HP-UX even disallows open() with O_TRUNC for a file with advisory locks, not
  60. just mandatory locks. That would appear to contravene POSIX.1.
  61. mmap() is another interesting case. All the operating systems mentioned
  62. prevent mandatory locks from being applied to an mmap()'ed file, but HP-UX
  63. also disallows advisory locks for such a file. SVID actually specifies the
  64. paranoid HP-UX behaviour.
  65. In my opinion only MAP_SHARED mappings should be immune from locking, and then
  66. only from mandatory locks - that is what is currently implemented.
  67. SunOS is so hopeless that it doesn't even honour the O_NONBLOCK flag for
  68. mandatory locks, so reads and writes to locked files always block when they
  69. should return EAGAIN.
  70. I'm afraid that this is such an esoteric area that the semantics described
  71. below are just as valid as any others, so long as the main points seem to
  72. agree.
  73. 4. Semantics
  74. ------------
  75. 1. Mandatory locks can only be applied via the fcntl()/lockf() locking
  76. interface - in other words the System V/POSIX interface. BSD style
  77. locks using flock() never result in a mandatory lock.
  78. 2. If a process has locked a region of a file with a mandatory read lock, then
  79. other processes are permitted to read from that region. If any of these
  80. processes attempts to write to the region it will block until the lock is
  81. released, unless the process has opened the file with the O_NONBLOCK
  82. flag in which case the system call will return immediately with the error
  83. status EAGAIN.
  84. 3. If a process has locked a region of a file with a mandatory write lock, all
  85. attempts to read or write to that region block until the lock is released,
  86. unless a process has opened the file with the O_NONBLOCK flag in which case
  87. the system call will return immediately with the error status EAGAIN.
  88. 4. Calls to open() with O_TRUNC, or to creat(), on a existing file that has
  89. any mandatory locks owned by other processes will be rejected with the
  90. error status EAGAIN.
  91. 5. Attempts to apply a mandatory lock to a file that is memory mapped and
  92. shared (via mmap() with MAP_SHARED) will be rejected with the error status
  93. EAGAIN.
  94. 6. Attempts to create a shared memory map of a file (via mmap() with MAP_SHARED)
  95. that has any mandatory locks in effect will be rejected with the error status
  96. EAGAIN.
  97. 5. Which system calls are affected?
  98. -----------------------------------
  99. Those which modify a file's contents, not just the inode. That gives read(),
  100. write(), readv(), writev(), open(), creat(), mmap(), truncate() and
  101. ftruncate(). truncate() and ftruncate() are considered to be "write" actions
  102. for the purposes of mandatory locking.
  103. The affected region is usually defined as stretching from the current position
  104. for the total number of bytes read or written. For the truncate calls it is
  105. defined as the bytes of a file removed or added (we must also consider bytes
  106. added, as a lock can specify just "the whole file", rather than a specific
  107. range of bytes.)
  108. Note 3: I may have overlooked some system calls that need mandatory lock
  109. checking in my eagerness to get this code out the door. Please let me know, or
  110. better still fix the system calls yourself and submit a patch to me or Linus.
  111. 6. Warning!
  112. -----------
  113. Not even root can override a mandatory lock, so runaway processes can wreak
  114. havoc if they lock crucial files. The way around it is to change the file
  115. permissions (remove the setgid bit) before trying to read or write to it.
  116. Of course, that might be a bit tricky if the system is hung :-(