lock_linux.go 2.5 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. "fmt"
  18. "io"
  19. "os"
  20. "syscall"
  21. )
  22. // This used to call syscall.Flock() but that call fails with EBADF on NFS.
  23. // An alternative is lockf() which works on NFS but that call lets a process lock
  24. // the same file twice. Instead, use Linux's non-standard open file descriptor
  25. // locks which will block if the process already holds the file lock.
  26. //
  27. // constants from /usr/include/bits/fcntl-linux.h
  28. const (
  29. F_OFD_GETLK = 37
  30. F_OFD_SETLK = 37
  31. F_OFD_SETLKW = 38
  32. )
  33. var (
  34. wrlck = syscall.Flock_t{
  35. Type: syscall.F_WRLCK,
  36. Whence: int16(io.SeekStart),
  37. Start: 0,
  38. Len: 0,
  39. }
  40. linuxTryLockFile = flockTryLockFile
  41. linuxLockFile = flockLockFile
  42. )
  43. func init() {
  44. // use open file descriptor locks if the system supports it
  45. getlk := syscall.Flock_t{Type: syscall.F_RDLCK}
  46. if err := syscall.FcntlFlock(0, F_OFD_GETLK, &getlk); err == nil {
  47. linuxTryLockFile = ofdTryLockFile
  48. linuxLockFile = ofdLockFile
  49. }
  50. }
  51. func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
  52. return linuxTryLockFile(path, flag, perm)
  53. }
  54. func ofdTryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
  55. f, err := os.OpenFile(path, flag, perm)
  56. if err != nil {
  57. return nil, fmt.Errorf("ofdTryLockFile failed to open %q (%v)", path, err)
  58. }
  59. flock := wrlck
  60. if err = syscall.FcntlFlock(f.Fd(), F_OFD_SETLK, &flock); err != nil {
  61. f.Close()
  62. if err == syscall.EWOULDBLOCK {
  63. err = ErrLocked
  64. }
  65. return nil, err
  66. }
  67. return &LockedFile{f}, nil
  68. }
  69. func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
  70. return linuxLockFile(path, flag, perm)
  71. }
  72. func ofdLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
  73. f, err := os.OpenFile(path, flag, perm)
  74. if err != nil {
  75. return nil, fmt.Errorf("ofdLockFile failed to open %q (%v)", path, err)
  76. }
  77. flock := wrlck
  78. err = syscall.FcntlFlock(f.Fd(), F_OFD_SETLKW, &flock)
  79. if err != nil {
  80. f.Close()
  81. return nil, err
  82. }
  83. return &LockedFile{f}, nil
  84. }