diff --git a/control.cmake b/control.cmake new file mode 100644 index 0000000..4a08152 --- /dev/null +++ b/control.cmake @@ -0,0 +1,127 @@ +#!/usr/bin/perl +# +# SMArT service control helper for MARS_NWE systemd unit. +# Installed next to the other SMArT perl helpers. +# + +use strict; +use warnings; + +our ($server_id, $mars_nwe_service, $smart_systemctl_path, $cc, %p); + +sub html_escape($) +{ + my ($s) = @_; + $s = '' unless defined $s; + $s =~ s/&/&/g; + $s =~ s//>/g; + $s =~ s/"/"/g; + return $s; +} + +sub handle_request() +{ + my $service = $mars_nwe_service || '@MARS_NWE_SYSTEMD_SERVICE@'; + my $systemctl = $smart_systemctl_path || '@SYSTEMCTL_EXECUTABLE@'; + my $query = defined($cc) ? $cc : ''; + my $action = ''; + + $query =~ s/^\?//; + + if( $query =~ /^(start|stop|restart|status)$/ ) + { + $action = $1; + } + elsif( $query =~ /(?:^|&)action=(start|stop|restart|status)(?:&|$)/ ) + { + $action = $1; + } + elsif( defined($p{action}) && $p{action} =~ /^(start|stop|restart|status)$/ ) + { + $action = $1; + } + + print < + + + + +MARS_NWE service control + + + + +
+

MARS_NWE service control

+EOF + + if( $action eq '' ) + { + print "

Invalid action.

\n"; + print "

Allowed actions: start, stop, restart, status

\n"; + print "

Back

\n"; + print "
\n"; + return; + } + + print '
Action: ' . html_escape($action) . '
' . "\n"; + print 'Service: ' . html_escape($service) . '
' . "\n"; + print 'systemctl: ' . html_escape($systemctl) . '
' . "\n"; + print "
\n";
+
+    my @cmd = ( $systemctl, $action, $service );
+    my $fh;
+    if( ! open( $fh, '-|', @cmd ) )
+    {
+        print 'Could not execute systemctl: ' . html_escape($!) . "\n";
+        print "

Failed.

\n"; + print "

Back

\n"; + print "\n"; + return; + } + + while( my $line = <$fh> ) + { + print html_escape($line); + } + + close($fh); + my $rc = $? >> 8; + + if( $rc == 0 ) + { + print "

Command completed successfully.

\n"; + } + else + { + print "

Command failed with exit code $rc.

\n"; + } + + print < +Status +Start +Stop +Restart + +

Back

+ + + +EOF +} + +1;