lock_linux.go 2.4 KB

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