lock_linux.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // +build linux
  15. package fileutil
  16. import (
  17. "io"
  18. "os"
  19. "syscall"
  20. )
  21. // This used to call syscall.Flock() but that call fails with EBADF on NFS.
  22. // An alternative is lockf() which works on NFS but that call lets a process lock
  23. // the same file twice. Instead, use Linux's non-standard open file descriptor
  24. // locks which will block if the process already holds the file lock.
  25. //
  26. // constants from /usr/include/bits/fcntl-linux.h
  27. const (
  28. F_OFD_GETLK = 37
  29. F_OFD_SETLK = 37
  30. F_OFD_SETLKW = 38
  31. )
  32. var (
  33. wrlck = syscall.Flock_t{
  34. Type: syscall.F_WRLCK,
  35. Whence: int16(io.SeekStart),
  36. Start: 0,
  37. Len: 0,
  38. }
  39. linuxTryLockFile = flockTryLockFile
  40. linuxLockFile = flockLockFile
  41. )
  42. func init() {
  43. // use open file descriptor locks if the system supports it
  44. getlk := syscall.Flock_t{Type: syscall.F_RDLCK}
  45. if err := syscall.FcntlFlock(0, F_OFD_GETLK, &getlk); err == nil {
  46. linuxTryLockFile = ofdTryLockFile
  47. linuxLockFile = ofdLockFile
  48. }
  49. }
  50. func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
  51. return linuxTryLockFile(path, flag, perm)
  52. }
  53. func ofdTryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
  54. f, err := os.OpenFile(path, flag, perm)
  55. if err != nil {
  56. return nil, err
  57. }
  58. flock := wrlck
  59. if err = syscall.FcntlFlock(f.Fd(), F_OFD_SETLK, &flock); err != nil {
  60. f.Close()
  61. if err == syscall.EWOULDBLOCK {
  62. err = ErrLocked
  63. }
  64. return nil, err
  65. }
  66. return &LockedFile{f}, nil
  67. }
  68. func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
  69. return linuxLockFile(path, flag, perm)
  70. }
  71. func ofdLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
  72. f, err := os.OpenFile(path, flag, perm)
  73. if err != nil {
  74. return nil, err
  75. }
  76. flock := wrlck
  77. err = syscall.FcntlFlock(f.Fd(), F_OFD_SETLKW, &flock)
  78. if err != nil {
  79. f.Close()
  80. return nil, err
  81. }
  82. return &LockedFile{f}, err
  83. }