From patchwork Wed Mar 30 09:56:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tremer X-Patchwork-Id: 5415 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4KT20350Zkz3wfF for ; Wed, 30 Mar 2022 09:56:51 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4KT1zy6khsz4lQ; Wed, 30 Mar 2022 09:56:46 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4KT1zy2ZrWz2ysF; Wed, 30 Mar 2022 09:56:46 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4KT1zx0H2Rz2y0B for ; Wed, 30 Mar 2022 09:56:45 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4KT1zw3nwMznF; Wed, 30 Mar 2022 09:56:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1648634204; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3uEdpjMXlQ+xrU/s8pPFmgQUSXQaGOLimIpD7xWQgkA=; b=qwIsrej/QJlKt92ll1F0/ujv2JShiv+l0nMV9oauf8rbO4unhUzGiQ6JXqVk1P55qmr4ZX WzkofZlarfsSXj/zbKyBKSpWCpv7dpfVc8ACMYzw/ukLoTk+ePqrPN3vN7zTUE63oYgneC bswgZJjC4sLOkRxD1xa3xkEox9DgJ2X0TD2/Y+hCcygN383TmNrLMZR4+wzWdaXgALwBI/ IDUZfflb2sBixIErXv6AYsSsewbVGK5UMZ5CNBhYc8CDuI2ED141vAihmHUiIGanHcml51 MWFEXJp4OuAaUarx+/oHu/N2SgXQsG6i4R8PEM36N05Qdyxm0loAIhiZK0PT2w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1648634204; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3uEdpjMXlQ+xrU/s8pPFmgQUSXQaGOLimIpD7xWQgkA=; b=/vGJWXoRaH48cp3mXYAygz8UIuqsKo/TliN/xLLUgBZEAGtVJB6JKmrc66cuHfxEHovezY rE3ycb2wfaqt+6Ag== From: Michael Tremer To: development@lists.ipfire.org Subject: [PATCH v2 2/5] unbound-dhcp-leases-bridge: Fix inotify handling Date: Wed, 30 Mar 2022 09:56:32 +0000 Message-Id: <20220330095635.621181-2-michael.tremer@ipfire.org> In-Reply-To: <20220330095635.621181-1-michael.tremer@ipfire.org> References: <20220330095635.621181-1-michael.tremer@ipfire.org> MIME-Version: 1.0 X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Tremer Errors-To: development-bounces@lists.ipfire.org Sender: "Development" This patch changes that the script will listen to changes to the directory instead of the file which got complicated when files got renamed. It also processes all changes at the same time and tries finding out what actions have to be performed in order to avoid unnecessary iterations. The script is also limited to process any changes only once every five seconds to keep resource usage in check on busy systems. Suggested-by: Anthony Heading Signed-off-by: Michael Tremer --- config/unbound/unbound-dhcp-leases-bridge | 70 +++++++++++++++-------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/config/unbound/unbound-dhcp-leases-bridge b/config/unbound/unbound-dhcp-leases-bridge index ce0cb8614..c0e5977bf 100644 --- a/config/unbound/unbound-dhcp-leases-bridge +++ b/config/unbound/unbound-dhcp-leases-bridge @@ -32,6 +32,7 @@ import stat import subprocess import sys import tempfile +import time import inotify.adapters @@ -82,6 +83,12 @@ class UnboundDHCPLeasesBridge(object): self.fix_leases_file = fix_leases_file self.hosts_file = hosts_file + self.watches = { + self.leases_file : inotify.constants.IN_MODIFY, + self.fix_leases_file : 0, + self.hosts_file : 0, + } + self.unbound = UnboundConfigWriter(unbound_leases_file) self.running = False @@ -89,37 +96,54 @@ class UnboundDHCPLeasesBridge(object): log.info("Unbound DHCP Leases Bridge started on %s" % self.leases_file) self.running = True - # Initial setup - self.hosts = self.read_static_hosts() - self.update_dhcp_leases() + i = inotify.adapters.Inotify() - i = inotify.adapters.Inotify([ - self.leases_file, - self.fix_leases_file, - self.hosts_file, - ]) + # Add watches for the directories of every relevant file + for f, mask in self.watches.items(): + i.add_watch( + os.path.dirname(f), + mask | inotify.constants.IN_CLOSE_WRITE | inotify.constants.IN_MOVED_TO, + ) - for event in i.event_gen(): - # End if we are requested to terminate - if not self.running: - break + # Enabled so that we update hosts and leases on startup + update_hosts = update_leases = True - if event is None: - continue + while self.running: + log.debug("Wakeup of main loop") + + # Process the entire inotify queue and identify what we need to do + for event in i.event_gen(): + # Nothing to do + if event is None: + break + + # Decode the event + header, type_names, path, filename = event - header, type_names, watch_path, filename = event + file = os.path.join(path, filename) - # Update leases after leases file has been modified - if "IN_MODIFY" in type_names: - # Reload hosts - if watch_path == self.hosts_file: - self.hosts = self.read_static_hosts() + log.debug("inotify event received for %s: %s", file, " ".join(type_names)) + # Did the hosts file change? + if self.hosts_file == file: + update_hosts = True + + # We will need to update the leases on any change + update_leases = True + + # Update hosts (if needed) + if update_hosts: + self.hosts = self.read_static_hosts() + + # Update leases (if needed) + if update_leases: self.update_dhcp_leases() - # If the file is deleted, we re-add the watcher - if "IN_IGNORED" in type_names: - i.add_watch(watch_path) + # Reset + update_hosts = update_leases = False + + # Wait a moment before we start the next iteration + time.sleep(5) log.info("Unbound DHCP Leases Bridge terminated")