[4/4] general-functions.pl: Do not die if no red interface could be determined.

Message ID 20220309141232.2401848-4-stefan.schantl@ipfire.org
State Accepted
Commit 47b2640d3766786a12864fb295d41a20eaaa850e
Headers
Series [1/4] libloc: Update to 0.9.11 |

Commit Message

Stefan Schantl March 9, 2022, 2:12 p.m. UTC
  Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 config/cfgroot/general-functions.pl | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
  

Comments

Michael Tremer March 9, 2022, 2:26 p.m. UTC | #1
Hello Stefan,

> On 9 Mar 2022, at 14:12, Stefan Schantl <stefan.schantl@ipfire.org> wrote:
> 
> Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
> ---
> config/cfgroot/general-functions.pl | 15 +++++++++------
> 1 file changed, 9 insertions(+), 6 deletions(-)
> 
> diff --git a/config/cfgroot/general-functions.pl b/config/cfgroot/general-functions.pl
> index 5118a9d69..6f49585dc 100644
> --- a/config/cfgroot/general-functions.pl
> +++ b/config/cfgroot/general-functions.pl
> @@ -1228,12 +1228,15 @@ sub firewall_reload() {
> # Function which will return the used interface for the red network zone (red0, ppp0, etc).
> # if you change this also check speed.cgi that include a local copy for systemload reasons
> sub get_red_interface() {
> -
> -	open(IFACE, "${General::swroot}/red/iface") or die "Could not open /var/ipfire/red/iface";

You could have just replaced the “die …” part with a return statement and save the extra check if the file exists.

Best,
-Michael

> -
> -	my $interface = <IFACE>;
> -	close(IFACE);
> -	chomp $interface;
> +	my $interface;
> +	my $red_iface_file = "${General::swroot}/red/iface";
> +
> +	if (-e $red_iface_file) {
> +		open(IFACE, "$red_iface_file") or die "Could not open $red_iface_file";
> +		$interface = <IFACE>;
> +		close(IFACE);
> +		chomp $interface;
> +	}
> 
> 	return $interface;
> }
> -- 
> 2.30.2
>
  

Patch

diff --git a/config/cfgroot/general-functions.pl b/config/cfgroot/general-functions.pl
index 5118a9d69..6f49585dc 100644
--- a/config/cfgroot/general-functions.pl
+++ b/config/cfgroot/general-functions.pl
@@ -1228,12 +1228,15 @@  sub firewall_reload() {
 # Function which will return the used interface for the red network zone (red0, ppp0, etc).
 # if you change this also check speed.cgi that include a local copy for systemload reasons
 sub get_red_interface() {
-
-	open(IFACE, "${General::swroot}/red/iface") or die "Could not open /var/ipfire/red/iface";
-
-	my $interface = <IFACE>;
-	close(IFACE);
-	chomp $interface;
+	my $interface;
+	my $red_iface_file = "${General::swroot}/red/iface";
+
+	if (-e $red_iface_file) {
+		open(IFACE, "$red_iface_file") or die "Could not open $red_iface_file";
+		$interface = <IFACE>;
+		close(IFACE);
+		chomp $interface;
+	}
 
 	return $interface;
 }