#!/usr/bin/perl -w # # $Id: schedule_cfexecd.pl,v 1.1 2005/10/23 22:31:32 jmates Exp $ # # Copyright (c) 2005, Alex Dioso. # All rights reserved. # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # ##################################################################### # # Options: # install : installs this script as a service # remove : removes this service # This service runs cfexecd at a predefined time use strict; use Win32::Daemon; use Win32::Service; # TODO Add eventlog reporting use Win32::EventLog qw/EVENTLOG_ERROR_TYPE/; # Service runs SCRIPT every RUN_TIME seconds use constant RUN_TIME => 3600; # Service calls the running callback every SLEEP_TIME milliseconds use constant SLEEP_TIME => 60000; use constant SCRIPT => 'c:\cygwin\var\cfengine\local\services\schedule_cfexecd.pl'; my @CFEXECD = ( 'c:\cygwin\bin\bash.exe', '--login', '-c', '/usr/sbin/cfexecd' ); my %service_info = ( name => 'cfexecd', display => 'CFExecd', path => $^X, description => 'Runs cfexecd every ' . RUN_TIME . ' minutes', parameters => SCRIPT, service_type => SERVICE_WIN32_OWN_PROCESS, start_type => SERVICE_AUTO_START ); usage() unless ( scalar @ARGV <= 1 ); # Install or remove the service if ( scalar @ARGV == 1 ) { my $option = shift; if ( $option eq "install" ) { Win32::Daemon::CreateService( \%service_info ); Win32::Service::StopService( "", $service_info{'name'} ); Win32::Service::StartService( "", $service_info{'name'} ); } elsif ( $option eq "remove" ) { Win32::Service::StopService( "", $service_info{'name'} ); # TODO Really should check the status of this but I'm feeling lazy Win32::Daemon::DeleteService( "", $service_info{'name'} ); } else { usage(); } exit; } # The actual service section Win32::Daemon::RegisterCallbacks( { start => \&Callback_Start, running => \&Callback_Running, stop => \&Callback_Stop, pause => \&Callback_Pause, continue => \&Callback_Continue, } ); my %Context = ( last_state => SERVICE_STOPPED, start_time => time(), last_run_time => 0, ); Win32::Daemon::StartService( \%Context, SLEEP_TIME ); ############################################################################### # Subs sub Callback_Start { my ( $Event, $Context ) = @_; $Context->{last_state} = SERVICE_RUNNING; Win32::Daemon::State(SERVICE_RUNNING); } sub Callback_Running { my ( $Event, $Context ) = @_; if ( SERVICE_RUNNING == Win32::Daemon::State() ) { if ( ( time() - $Context->{last_run_time} ) > RUN_TIME ) { system @CFEXECD; $Context->{last_run_time} = time(); } } } sub Callback_Stop { my ( $Event, $Context ) = @_; $Context->{last_state} = SERVICE_STOPPED; Win32::Daemon::State(SERVICE_STOPPED); Win32::Daemon::StopService(); } sub Callback_Pause { my ( $Event, $Context ) = @_; $Context->{last_state} = SERVICE_PAUSED; Win32::Daemon::State(SERVICE_PAUSED); } sub Callback_Continue { my ( $Event, $Context ) = @_; $Context->{last_state} = SERVICE_RUNNING; Win32::Daemon::State(SERVICE_RUNNING); } sub usage { print <