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 stefan.schantl@ipfire.org --- 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 info@ipfire.org # +# Copyright (C) 2007-2021 IPFire Team info@ipfire.org # # # # 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;
Fixes #12621.
Signed-off-by: Stefan Schantl stefan.schantl@ipfire.org --- html/cgi-bin/pakfire.cgi | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/html/cgi-bin/pakfire.cgi b/html/cgi-bin/pakfire.cgi index a9e12d23c..faaeb4222 100644 --- a/html/cgi-bin/pakfire.cgi +++ b/html/cgi-bin/pakfire.cgi @@ -54,7 +54,7 @@ sub refreshpage{&Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' cont &Header::openpage($Lang::tr{'pakfire configuration'}, 1); &Header::openbigbox('100%', 'left', '', $errormessage);
-if ($cgiparams{'ACTION'} eq 'install'){ +if (($cgiparams{'ACTION'} eq 'install') && (! -e $Pakfire::lockfile)) { $cgiparams{'INSPAKS'} =~ s/|/\ /g; if ("$cgiparams{'FORCE'}" eq "on") { my $command = "/usr/local/bin/pakfire install --non-interactive --no-colors $cgiparams{'INSPAKS'} &>/dev/null &"; @@ -93,7 +93,7 @@ END &Header::closepage(); exit; } -} elsif ($cgiparams{'ACTION'} eq 'remove') { +} elsif (($cgiparams{'ACTION'} eq 'remove') && (! -e $Pakfire::lockfile)) {
$cgiparams{'DELPAKS'} =~ s/|/\ /g; if ("$cgiparams{'FORCE'}" eq "on") { @@ -134,11 +134,11 @@ END exit; }
-} elsif ($cgiparams{'ACTION'} eq 'update') { +} elsif (($cgiparams{'ACTION'} eq 'update') && (! -e $Pakfire::lockfile)) {
system("/usr/local/bin/pakfire update --force --no-colors &>/dev/null &"); system("/bin/sleep 1"); -} elsif ($cgiparams{'ACTION'} eq 'upgrade') { +} elsif (($cgiparams{'ACTION'} eq 'upgrade') && (!-e $Pakfire::lockfile)) { my $command = "/usr/local/bin/pakfire upgrade -y --no-colors &>/dev/null &"; system("$command"); system("/bin/sleep 1"); @@ -176,9 +176,7 @@ if ($errormessage) { &Header::closebox(); }
-my $return = `pidof pakfire`; -chomp($return); -if ($return) { +if (-e $Pakfire::lockfile) { &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" ); print <<END; <table>
Thank you very much.
I merged this and hopefully this won’t break anything *fingers crossed*.
On 24 May 2021, at 18:38, Stefan Schantl stefan.schantl@ipfire.org wrote:
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 stefan.schantl@ipfire.org
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 info@ipfire.org # +# Copyright (C) 2007-2021 IPFire Team info@ipfire.org # # # # 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;
-- 2.20.1