Revision eb0f0ce0 daemons/ganeti-watcher

b/daemons/ganeti-watcher
90 90
  return res
91 91

  
92 92

  
93
def LockFile(fd):
94
  """Locks a file using POSIX locks.
95

  
96
  """
97
  try:
98
    fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
99
  except IOError, err:
100
    if err.errno == errno.EAGAIN:
101
      raise errors.LockError("File already locked")
102
    raise
103

  
104

  
105 93
class WatcherState(object):
106 94
  """Interface to a state file recording restart attempts.
107 95

  
......
118 106
    fd = os.open(constants.WATCHER_STATEFILE, os.O_RDWR | os.O_CREAT)
119 107
    self.statefile = os.fdopen(fd, 'w+')
120 108

  
121
    LockFile(self.statefile.fileno())
109
    utils.LockFile(self.statefile.fileno())
122 110

  
123 111
    try:
124 112
      self._data = serializer.Load(self.statefile.read())
......
151 139
    # starting ganeti-watcher again at the same time will create a conflict.
152 140
    fd = utils.WriteFile(constants.WATCHER_STATEFILE,
153 141
                         data=serialized_form,
154
                         prewrite=LockFile, close=False)
142
                         prewrite=utils.LockFile, close=False)
155 143
    self.statefile = os.fdopen(fd, 'w+')
156 144

  
157 145
  def Close(self):

Also available in: Unified diff