]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - config/cfgroot/general-functions.pl
Merge remote-tracking branch 'origin/next'
[people/pmueller/ipfire-2.x.git] / config / cfgroot / general-functions.pl
index a6656ccf5668d605aca52f22a3028416eeaff4a7..550afcf827b4b5a50c49e99a15593a8c2de6f619 100644 (file)
@@ -28,6 +28,77 @@ $General::adminmanualurl = 'http://wiki.ipfire.org';
 
 require "${General::swroot}/network-functions.pl";
 
+# This function executes a shell command without forking a shell or do any other
+# Perl-voodoo before it. It deprecates the "system" command and is the only way
+# to call shell commands.
+sub safe_system($) {
+       my @command = @_;
+
+       system { ${command[0]} } @command;
+
+       # Return exit code
+       return $? >> 8;
+}
+
+# Calls a process in the background and returns nothing
+sub system_background($) {
+       my $pid = fork();
+
+       unless ($pid) {
+               my $rc = &system(@_);
+               exit($rc);
+       }
+
+       return 0;
+}
+
+# Returns the output of a shell command
+sub system_output($) {
+       my @command = @_;
+       my $pid;
+       my @output = ();
+
+       unless ($pid = open(OUTPUT, "-|")) {
+               open(STDERR, ">&STDOUT");
+               exec { ${command[0]} } @command;
+               die "Could not execute @command: $!";
+       }
+
+       waitpid($pid, 0);
+
+       while (<OUTPUT>) {
+               push(@output, $_);
+       }
+       close(OUTPUT);
+
+       return @output;
+}
+
+# Calls a shell command and throws away the output
+sub system($) {
+       my @command = @_;
+
+       open(SAVEOUT, ">&STDOUT");
+       open(SAVEERR, ">&STDERR");
+
+       open(STDOUT, ">/dev/null");
+       open(STDERR, ">&STDOUT");
+
+       select(STDERR); $|=1;
+       select(STDOUT); $|=1;
+
+       my $rc = &safe_system(@command);
+
+       close(STDOUT);
+       close(STDERR);
+
+       # Restore
+       open(STDOUT, ">&SAVEOUT");
+       open(STDERR, ">&SAVEERR");
+
+       return $rc;
+}
+
 # Function to remove duplicates from an array
 sub uniq { my %seen; grep !$seen{$_}++, @_ }
 
@@ -42,7 +113,7 @@ sub log
        my $logmessage = $_[0];
        $logmessage =~ /([\w\W]*)/;
        $logmessage = $1;
-       system('logger', '-t', $tag, $logmessage);
+       &system('logger', '-t', $tag, $logmessage);
 }
 sub setup_default_networks
 {
@@ -1152,7 +1223,7 @@ sub firewall_needs_reload() {
 }
 
 sub firewall_reload() {
-       system("/usr/local/bin/firewallctrl");
+       &system("/usr/local/bin/firewallctrl");
 }
 
 # Function which will return the used interface for the red network zone (red0, ppp0, etc).