Re-factored code to get status and supported actions for an init script
authorJamie Cameron <jcameron@webmin.com>
Wed, 4 May 2011 23:38:46 +0000 (16:38 -0700)
committerJamie Cameron <jcameron@webmin.com>
Wed, 4 May 2011 23:38:46 +0000 (16:38 -0700)
init/edit_action.cgi
init/index.cgi
init/init-lib.pl

index dcccf75..c79521d 100755 (executable)
@@ -13,14 +13,8 @@ if ($ty == 0) {
        $ac = $ARGV[1];
        &ui_print_header(undef, $text{'edit_title'}, "");
        $file = &action_filename($ac);
-       open(FILE, $file);
-       while(<FILE>) {
-               $data .= $_;
-               if (/^\s*(['"]?)([a-z]+)\1\)/i) {
-                       $hasarg{$2}++;
-                       }
-               }
-       close(FILE);
+       $data = &read_file_contents($file);
+       $hasarg = &get_action_args($file);
        }
 elsif ($ty == 1) {
        # Editing an action in one of the runlevels
@@ -126,18 +120,14 @@ elsif (!$config{'expert'} || $access{'bootup'} == 2) {
                }
 
        # Show if action is currently running
-       if ($hasarg{'status'} && $config{'status_check'}) {
-               $out = &backquote_command("$file status</dev/null 2>/dev/null");
-               if ($out =~ /not\s+running/i ||
-                   $out =~ /no\s+server\s+running/i) {
+       if ($hasarg->{'status'} && $config{'status_check'}) {
+               $r = &action_running($file);
+               if ($r == 0) {
                        $status = "<font color=#ff0000>$text{'no'}</font>";
                        }
-               elsif ($out =~ /running/i) {
+               elsif ($r == 1) {
                        $status = $text{'yes'};
                        }
-               elsif ($out =~ /stopped/i) {
-                       $status = "<font color=#ff0000>$text{'no'}</font>";
-                       }
                else {
                        $status = "<i>$text{'edit_unknown'}</i>";
                        }
@@ -197,7 +187,7 @@ if ($ty != 2) {
        $args = join("+", @ARGV);
        print &ui_hidden("back", "edit_action.cgi?$args");
        foreach $a (@action_buttons) {
-               if ($a eq 'start' || $a eq 'stop' || $hasarg{$a}) {
+               if ($a eq 'start' || $a eq 'stop' || $hasarg->{$a}) {
                        push(@buts, [ $a, $text{'edit_'.$a.'now'} ]);
                        }
                }
index 5221de9..0d41b9e 100755 (executable)
@@ -158,23 +158,16 @@ elsif ($init_mode eq "init" && $access{'bootup'}) {
                                push(@cols, $order);
                                }
                        if ($config{'status_check'} == 2) {
-                               if ($actsl[$i] =~ /^0/) {
-                                       local $out = $has{'status'} ?
-                                               `$actsf[$i] status` : '';
-                                       if ($out =~ /not\s+running/i ||
-                                           $out =~ /no\s+server\s+running/i) {
+                               if ($actsl[$i] =~ /^0/ && $has{'status'}) {
+                                       local $r = &action_running($actsf[$i]);
+                                       if ($r == 0) {
                                                push(@cols,
                                                        "<font color=#ff0000>".
                                                        "$text{'no'}</font>");
                                                }
-                                       elsif ($out =~ /running/i) {
+                                       elsif ($r == 1) {
                                                push(@cols, $text{'yes'});
                                                }
-                                       elsif ($out =~ /stopped/i) {
-                                               push(@cols,
-                                                       "<font color=#ff0000>".
-                                                       "$text{'no'}</font>");
-                                               }
                                        else {
                                                push(@cols, undef);
                                                }
index 481bd5d..132fbd0 100755 (executable)
@@ -1601,17 +1601,15 @@ foreach my $a (&list_actions()) {
        my $l = glob("/etc/rc$rl.d/S*$a");
        $s->{'boot'} = $l ? 'start' : 'stop';
        $s->{'desc'} = &init_description($f);
-       my $out = &backquote_command("$f status 2>&1 </dev/null");
-       if ($out =~ /not\s+running/i ||
-           $out =~ /no\s+server\s+running/i ||
-           $out =~ /not\s+access\s+PID/i) {
-               $s->{'status'} = 'waiting';
-               }
-       elsif ($out =~ /running/i) {
-               $s->{'status'} = 'running';
-               }
-       elsif ($out =~ /stopped/) {
-               $s->{'status'} = 'waiting';
+       my $hasarg = &get_action_args($f);
+       if ($hasarg->{'status'}) {
+               my $r = &action_running($f);
+               if ($r == 0) {
+                       $s->{'status'} = 'waiting';
+                       }
+               elsif ($r == 1) {
+                       $s->{'status'} = 'running';
+                       }
                }
        push(@rv, $s);
        }
@@ -1719,5 +1717,42 @@ sub shutdown_system
 &system_logged("$config{'shutdown_command'} >$null_file 2>$null_file");
 }
 
+# get_action_args(filename)
+# Returns the args that this action script appears to support, like stop, start
+# and status.
+sub get_action_args
+{
+my ($file) = @_;
+my %hasarg;
+open(FILE, $file);
+while(<FILE>) {
+       if (/^\s*(['"]?)([a-z]+)\1\)/i) {
+               $hasarg{$2}++;
+               }
+       }
+close(FILE);
+return \%hasarg;
+}
+
+# action_running(filename)
+# Assuming some init.d action supports the status parameter, returns a 1 if
+# running, 0 if not, or -1 if unknown
+sub action_running
+{
+my ($file) = @_;
+my $out = &backquote_command("$file status");
+if ($out =~ /not\s+running/i ||
+    $out =~ /no\s+server\s+running/i) {
+       return 0;
+       }
+elsif ($out =~ /running/i) {
+       return 1;
+       }
+elsif ($out =~ /stopped/i) {
+       return 0;
+       }
+return -1;
+}
+
 1;