From patchwork Mon May 24 17:38:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 4369 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) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4Fpkvx1pHRz3wc7 for ; Mon, 24 May 2021 17:38:37 +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 4Fpkvv0hVXz6YF; Mon, 24 May 2021 17:38:35 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4Fpkvt3T8vz2y4C; Mon, 24 May 2021 17:38:34 +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) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4Fpkvs6Fb7z2xXY for ; Mon, 24 May 2021 17:38:33 +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 4Fpkvr6lkjzmP; Mon, 24 May 2021 17:38:32 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1621877913; 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; bh=oYADJeebHKqRpiAGIjN59aE4QtShiB/nRBhcMk0dzmY=; b=edKdc1R5x72x4y31PocdcP8kHTm5fLs01ceLqpE2/x3PAyk4kEDXgWOUYVUWzEoaE6CTV/ 7lNWc2OjiqX1XnAw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1621877913; 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; bh=oYADJeebHKqRpiAGIjN59aE4QtShiB/nRBhcMk0dzmY=; b=Pt0bYGdY8+Ox9CE/AarPFmXaIXl53h5ebXsRu6DFKK3o7xYRWrLHp4h4AaccLSsexNUp3a ZJg4SH4xYdmOAi4Ekn1iYN5dYjAF/Av+02oTC5snUN0eq9e6Gqp5XxBOxFYHpukGdtBgPI augVYANj9xH94J8h/iLPCKhTSu1Au8HwV0if3JExqZXg7Nym6jnC/6jR2Yv2BxjAC57j1N gSm/onzZxazv2m9lTEbgXWoPgkjicpNuZfOB+IpoGJ5OXNMt8vJzKIPongDBsS42C20w/a Kh4jmRlEPdM448rQGnpptcJyBNLeaYgS0uyynZrCyI/kxMw8oagHguVo5f1GuQ== From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCH 1/2] pakfire: Prevent from get launched multiple times. Date: Mon, 24 May 2021 19:38:20 +0200 Message-Id: <20210524173821.4596-1-stefan.schantl@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: , Errors-To: development-bounces@lists.ipfire.org Sender: "Development" When pakfire gets launched a check if a so called lockfile exists and the process will be aborted, otherwise the file will be created which prevents any other pakfire instance to perform any operations until the first process gets finished and the lock will be released again. Because the release of the lock is located in an END block, the lock also will be released in case the pakfire process gets interuped or gains an error. This prevents from an lock loop and an unuseable pakfire. Reference: #12621. Signed-off-by: Stefan Schantl --- src/pakfire/lib/functions.pl | 3 +++ src/pakfire/pakfire | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/pakfire/lib/functions.pl b/src/pakfire/lib/functions.pl index 4d5c6219a..f9a19b60d 100644 --- a/src/pakfire/lib/functions.pl +++ b/src/pakfire/lib/functions.pl @@ -73,6 +73,9 @@ my %pakfiresettings = (); # Make version $Conf::version = &make_version(); +# Pakfire lock file. +our $lockfile = "/tmp/pakfire_lock"; + sub message { my $message = shift; diff --git a/src/pakfire/pakfire b/src/pakfire/pakfire index c69a8d3ad..4139d106b 100644 --- a/src/pakfire/pakfire +++ b/src/pakfire/pakfire @@ -2,7 +2,7 @@ ############################################################################### # # # IPFire.org - A linux based firewall # -# Copyright (C) 2007-2015 IPFire Team # +# Copyright (C) 2007-2021 IPFire Team # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # @@ -31,6 +31,7 @@ my $interactive = 1; my $force = "noforce"; + my $locked; &Pakfire::logger("PAKFIRE INFO: IPFire Pakfire $Conf::version started!"); @@ -47,6 +48,21 @@ &Pakfire::message("PAKFIRE ERROR: You need to be online to run pakfire!"); exit 2; } + + # Check if a lockfile already exists. + if (-e "$Pakfire::lockfile") { + &Pakfire::message("PAKFIRE ERROR: Another instance of pakfire is already running!"); + exit 1; + } + + # Write lockfile. + open(LOCK, ">$Pakfire::lockfile"); + + # Pakfire has locked in this session set locket to "1". + $locked = "1"; + + # Close filehandle. + close(LOCK); ### Check if we are started by another name # @@ -330,4 +346,12 @@ &Pakfire::logger("PAKFIRE INFO: Pakfire has finished. Closing."); + END { + # Check if pakfire has been locked in this session. + if ($locked) { + # Remove lockfile. + unlink($Pakfire::lockfile); + } + } + exit 0;