added OpenSecurity Windows Service
authorOliver Maurhart <oliver.maurhart@ait.ac.at>
Fri, 28 Feb 2014 12:46:38 +0100
changeset 82d1c6a193b888
parent 81 84663db906d7
child 83 7a9d73ac796a
added OpenSecurity Windows Service
OpenSecurity/bin/opensecurity_service.pyw
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/OpenSecurity/bin/opensecurity_service.pyw	Fri Feb 28 12:46:38 2014 +0100
     1.3 @@ -0,0 +1,112 @@
     1.4 +#!/bin/env python
     1.5 +# -*- coding: utf-8 -*-
     1.6 +
     1.7 +# ------------------------------------------------------------
     1.8 +# opensecurity_service
     1.9 +# 
    1.10 +# turn the opensecurityd into a Windows Service
    1.11 +#
    1.12 +# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    1.13 +#
    1.14 +# Copyright (C) 2014 AIT Austrian Institute of Technology
    1.15 +# AIT Austrian Institute of Technology GmbH
    1.16 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
    1.17 +# http://www.ait.ac.at
    1.18 +#
    1.19 +# This program is free software; you can redistribute it and/or
    1.20 +# modify it under the terms of the GNU General Public License
    1.21 +# as published by the Free Software Foundation version 2.
    1.22 +# 
    1.23 +# This program is distributed in the hope that it will be useful,
    1.24 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.25 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.26 +# GNU General Public License for more details.
    1.27 +# 
    1.28 +# You should have received a copy of the GNU General Public License
    1.29 +# along with this program; if not, write to the Free Software
    1.30 +# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    1.31 +# Boston, MA  02110-1301, USA.
    1.32 +# ------------------------------------------------------------
    1.33 +
    1.34 +
    1.35 +# ------------------------------------------------------------
    1.36 +# imports
    1.37 +
    1.38 +import os
    1.39 +import os.path
    1.40 +import time
    1.41 +import servicemanager
    1.42 +import win32event
    1.43 +import win32service
    1.44 +import win32serviceutil
    1.45 +
    1.46 +from opensecurity_util import logger
    1.47 +import opensecurityd
    1.48 +
    1.49 +
    1.50 +# ------------------------------------------------------------
    1.51 +# code
    1.52 +
    1.53 +
    1.54 +class OpenSecurityService(win32serviceutil.ServiceFramework):
    1.55 +    
    1.56 +    """This is the OpenSecurity Windows Service,"""
    1.57 +
    1.58 +    _svc_name_ = 'OpenSecurityService'
    1.59 +    _svc_display_name_ = 'OpenSecurity Service'
    1.60 +    _svc_description_ = 'OpenSecurity Daemon Service'
    1.61 +
    1.62 +    _run = True
    1.63 +    
    1.64 +    def __init__(self, args):
    1.65 +
    1.66 +        """Init the Service"""
    1.67 +        
    1.68 +        # super call 
    1.69 +        win32serviceutil.ServiceFramework.__init__(self, args)
    1.70 +        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
    1.71 +
    1.72 +
    1.73 +    def SvcStop(self):
    1.74 +        
    1.75 +        """Stopping the Service"""
    1.76 +
    1.77 +        self.log('OpenSecurity Service stopping...\n') 
    1.78 +        self._run = False
    1.79 +        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    1.80 +        win32event.SetEvent(self.hWaitStop)
    1.81 +        self.log('OpenSecurity Service stopped...\n') 
    1.82 +
    1.83 +
    1.84 +    def SvcDoRun(self):
    1.85 +    
    1.86 +        """Run the Service"""
    1.87 +
    1.88 +        self.log('OpenSecurity Service stared now')
    1.89 +        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
    1.90 +                              servicemanager.PYS_SERVICE_STARTED,
    1.91 +                              (self._svc_name_, ''))
    1.92 +        self._run = True
    1.93 +        self.main()
    1.94 +
    1.95 +
    1.96 +    def log(self, s):
    1.97 +
    1.98 +        """log some string to the log file"""
    1.99 +
   1.100 +        try:
   1.101 +            logger.debug(s)
   1.102 +        except:
   1.103 +            pass
   1.104 +
   1.105 +
   1.106 +    def main(self):
   1.107 +
   1.108 +        """Main service method"""
   1.109 +        opensecurityd.main()
   1.110 +
   1.111 +
   1.112 +if __name__ == '__main__':
   1.113 +    """startup"""
   1.114 +    win32serviceutil.HandleCommandLine(OpenSecurityService)
   1.115 +