Skip to content

Commit

Permalink
dhcpd leases watcher, file rotation issue. closes opnsense#3478
Browse files Browse the repository at this point in the history
  • Loading branch information
AdSchellevis authored and EugenMayer committed Jul 22, 2019
1 parent 73f6bde commit 878e87d
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/opnsense/site-python/watchers/dhcpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,17 @@ def __init__(self, watch_file='/var/dhcpd/var/db/dhcpd.leases'):
self._section_data = []
self._fhandle = None
self._last_pos = None
self._open()

def _open(self):
""" (re)open watched file
:return: watcher object
:return: None
"""
try:
self._fhandle = open(self.watch_file, 'r')
self._last_pos = None
self._section_data = []
return True
except IOError:
self._fhandle = None
return False

@staticmethod
def parse_lease(lines):
Expand Down Expand Up @@ -86,25 +83,28 @@ def watch(self):
""" watch file, return lease dictionaries
:return: iterator for leases
"""
if self._fhandle is None or os.fstat(self._fhandle.fileno()).st_nlink == 0:
if self._fhandle is None:
# nothing to watch, try to (re)open return when failed
if not self._open():
return
self._open()
elif os.fstat(self._fhandle.fileno()).st_ino != os.stat(self.watch_file).st_ino:
# file rotation, inode changed
self._open()
elif self._last_pos is not None:
self._fhandle.seek(self._last_pos)

while True:
line = self._fhandle.readline()
if line:
if len(line) > 5 and line[0:5] == 'lease':
self._section_data.append(line)
elif len(line) > 1 and line[0] == '}' and len(self._section_data) > 0:
self._section_data.append(line)
yield self.parse_lease(self._section_data)
self._section_data = []
elif len(self._section_data) > 0:
self._section_data.append(line)
else:
break
if self._fhandle is not None:
while True:
line = self._fhandle.readline()
if line:
if len(line) > 5 and line[0:5] == 'lease':
self._section_data.append(line)
elif len(line) > 1 and line[0] == '}' and len(self._section_data) > 0:
self._section_data.append(line)
yield self.parse_lease(self._section_data)
self._section_data = []
elif len(self._section_data) > 0:
self._section_data.append(line)
else:
break

self._last_pos = self._fhandle.tell()
self._last_pos = self._fhandle.tell()

0 comments on commit 878e87d

Please sign in to comment.