From 40bd0c5dc8212d5584daf741b27372afb72be533 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Sun, 2 Jun 2019 10:43:37 +0200 Subject: [PATCH] dhcpd leases watcher, file rotation issue. closes https://github.com/opnsense/core/issues/3478 --- src/opnsense/site-python/watchers/dhcpd.py | 42 +++++++++++----------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/opnsense/site-python/watchers/dhcpd.py b/src/opnsense/site-python/watchers/dhcpd.py index f78b28287..8ebfc10a5 100644 --- a/src/opnsense/site-python/watchers/dhcpd.py +++ b/src/opnsense/site-python/watchers/dhcpd.py @@ -39,20 +39,17 @@ class DHCPDLease(object): 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): @@ -86,25 +83,28 @@ class DHCPDLease(object): """ 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()