svenXY has asked for the wisdom of the Perl Monks concerning the following question:

Om!

The situation is as follows: Any hints greatly appreciated!

thanks, Sven

Here's the script (the skeleton it taken 1:1 from the Win32::Daemon POD):

#use strict; use Win32; use Win32::Daemon; use Log::Log4perl qw(:easy); use IO::Socket; use constant MYPORT => 9797; use constant TIMEOUT => 5; our $conf_file = 'conf.conf'; my $log_file = 'logfile.log'; $SERVICE_SLEEP_TIME = 20; # 20 milliseconds $PrevState = SERVICE_START_PENDING; Win32::Daemon::StartService(); while( SERVICE_STOPPED != ( $State = Win32::Daemon::State() ) ) { if( SERVICE_START_PENDING == $State ) { # Initialization code Win32::Daemon::State( SERVICE_RUNNING ); $PrevState = SERVICE_RUNNING; } elsif( SERVICE_STOP_PENDING == $State ) { Win32::Daemon::State( SERVICE_STOPPED ); } elsif( SERVICE_PAUSE_PENDING == $State ) { # "Pausing..."; Win32::Daemon::State( SERVICE_PAUSED ); $PrevState = SERVICE_PAUSED; next; } elsif( SERVICE_CONTINUE_PENDING == $State ) { # "Resuming..."; Win32::Daemon::State( SERVICE_RUNNING ); $PrevState = SERVICE_RUNNING; next; } elsif( SERVICE_STOP_PENDING == $State ) { # "Stopping..."; Win32::Daemon::State( SERVICE_STOPPED ); $PrevState = SERVICE_STOPPED; next; } elsif( SERVICE_RUNNING == $State ) { # The service is running as normal... # main code here... ################################################ ## MAIN LOOP ## ################################################ our $sock = new IO::Socket::INET(LocalPort => MYPORT, Reuse => 1, Listen => 5 ) or LOGDIE "can't create local socket: $@\n"; INFO "Accepting connections on Port ", MYPORT, "..."; my $counter = 0; while (our $client = $sock->accept()) { INFO "Connection from ", $client->peerhost(), ":", $client->peerport(); while (<$client>) { unless ($_ =~ /^[\w.-_\s]+$/) { print $client "Invalid Input!. Type [h]elp for help\n" +; next; } chomp; $_ =~ s/\r$//; DEBUG "Got command: $_"; my $output = get_output($_); print $client $output; } INFO "Client " . $client->peerhost() . " has disconnected."; } } else { # Got an unhandled control message. Set the state to # whatever the previous state was. Win32::Daemon::State( $PrevState ); } # Check for any outstanding commands. Pass in a non zero value # and it resets the Last Message to SERVICE_CONTROL_NONE. if( SERVICE_CONTROL_NONE != ( my $Message = Win32::Daemon::QueryLa +stMessage( 1 ) ) ) { if( SERVICE_CONTROL_INTERROGATE == $Message ) { # Got here if the Service Control Manager is requesting # the current state of the service. This can happen for # a variety of reasons. Report the last state we set. Win32::Daemon::State( $PrevState ); } elsif( SERVICE_CONTROL_SHUTDOWN == $Message ) { # Yikes! The system is shutting down. We had better clean up # and stop. # Tell the SCM that we are preparing to shutdown and that we exp +ect # it to take 25 seconds (so don't terminate us for at least 25 s +econds)... Win32::Daemon::State( SERVICE_STOP_PENDING, 25000 ); } } # Snooze for awhile so we don't suck up cpu time... Win32::Sleep( $SERVICE_SLEEP_TIME ); } # We are done so close down... Win32::Daemon::StopService(); exit; ################################ sub get_output { ################################ my ($cmd) = @_; my output = do_something(); return $output; }

Janitored by Arunbear - replaced pre tags with code tags, to allow code extraction; also added READMORE tags

  • Comment on Win32::Daemon + socket->accept: service cannot be stopped [SOLVED]
  • Download Code

Replies are listed 'Best First'.
Re: Win32::Daemon + socket->accept: service cannot be stopped
by Jenda (Abbot) on Jun 23, 2005 at 21:45 UTC

    I don't know whether there is a way to make timeout the accept(), I would probably use two threads instead. One thread would do the work (call the accept() and wait for the commands), the other would respond to the Service Manager request. And if the Service Manager requests that the service stops the second thread would connect to the first one and issue a stop command.

    This way the worker thread would not need to timeout the accept() it would just wait for a connection either from a client or the other thread. Since you do not need to share any data between the threads it's enough to use fork() to create the threads. Do I make sense?

    P.S.: You may want to have a look at Win32::Daemon::Simple, that'd make the service related part of the script much much simpler.

    Jenda
    XML sucks. Badly. SOAP on the other hand is the most powerfull vacuum pump ever invented.

      Hi All,

      Thanks for the help and ideas! It works with a fork() and kill() to kill the child.
      I'll just paste the code that I changed: Thanks again,
      Sven

        I think it would be better to connect to the listening socket and issue a specific command than to kill the working thread. You might kill it at a wrong time.

        If you want to be safe you may for example generate a random number before the fork() and then include the number in the stop command. If you then accept stop requests only from localhost and only if they contain the right number you should be fairly safe.

        Jenda
        XML sucks. Badly. SOAP on the other hand is the most powerfull vacuum pump ever invented.

Re: Win32::Daemon + socket->accept: service cannot be stopped
by ikegami (Patriarch) on Jun 23, 2005 at 16:19 UTC

    Please use <code>...</code> around your blocks of code, not <pre>...</pre>. Also, please use <readmore>...</readmore> around long posts such as this one. Please update your post in this fashion to save the janitors the trouble.

    Now, about your question,

    Ideally, you'd timeout accept periodically in order to check for service messages. accept itself doesn't accept a timeout, so I'd check if alarm can interrupt accept in Windows.

      Hi ikegami,

      I tried alarm() with Windows, but it doesn't seem to work - at least not with $sock->accept().
      Please find my solution below
      thanks anyway
      Sven
      PS: I hope the tagging is better now ;-)