configd.py 3.74 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2 3 4
#!/usr/local/bin/python2.7
"""
    Copyright (c) 2014 Ad Schellevis

Ad Schellevis's avatar
Ad Schellevis committed
5
    part of OPNsense (https://www.opnsense.org/)
Ad Schellevis's avatar
Ad Schellevis committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

    --------------------------------------------------------------------------------------
31
    package : configd
Ad Schellevis's avatar
Ad Schellevis committed
32 33 34 35 36 37 38 39 40 41 42 43 44
    function: delivers a process coordinator to handle frontend functions



"""
__author__ = 'Ad Schellevis'

#
import os
import sys
import ConfigParser
import modules.processhandler
from modules.daemonize import Daemonize
45
import cProfile, pstats
Ad Schellevis's avatar
Ad Schellevis committed
46 47 48 49 50 51 52 53 54 55 56 57 58

# find program path
if len(__file__.split('/')[:-1]) >0 :
    program_path = '/'.join(__file__.split('/')[:-1])
else:
    program_path = os.getcwd()

# set working directory to program_path
sys.path.append(program_path)
os.chdir(program_path)

# open configuration
cnf = ConfigParser.ConfigParser()
59
cnf.read('conf/configd.conf')
Ad Schellevis's avatar
Ad Schellevis committed
60 61 62 63

# validate configuration, exit on missing item
for config_item in  ['socket_filename','pid_filename']:
    if cnf.has_section('main') == False or cnf.has_option('main',config_item) == False:
64
        print('configuration item main/%s not found in %s/conf/configd.conf'%(config_item,program_path))
Ad Schellevis's avatar
Ad Schellevis committed
65 66 67 68 69 70 71 72 73 74 75
        sys.exit(0)

# run process coordinator ( on console or as daemon )
# if command-line arguments contain "emulate",  start in emulation mode
if len(sys.argv) > 1 and 'simulate' in sys.argv[1:]:
    proc_handler = modules.processhandler.Handler(socket_filename=cnf.get('main','socket_filename'),config_path='%s/conf'%(program_path),simulation_mode=True)
else:
    proc_handler = modules.processhandler.Handler(socket_filename=cnf.get('main','socket_filename'),config_path='%s/conf'%(program_path))

if len(sys.argv) > 1 and 'console' in sys.argv[1:]:
    print('run %s in console mode'%sys.argv[0])
76 77 78 79 80 81 82
    if 'profile' in sys.argv[1:]:
        # profile configd
        # for graphical output use gprof2dot:
        #   gprof2dot -f pstats /tmp/configd.profile  -o /tmp/callingGraph.dot
        # (https://code.google.com/p/jrfonseca/wiki/Gprof2Dot)
        print ("...<ctrl><c> to stop profiling")
        profile = cProfile.Profile()
83
        profile.enable(subcalls=True)
84
        try:
85
            proc_handler.single_threaded = True
86 87 88 89 90 91 92 93 94
            proc_handler.run()
        except KeyboardInterrupt:
            pass
        except:
            raise
        profile.disable()
        profile.dump_stats('/tmp/configd.profile')
    else:
        proc_handler.run()
Ad Schellevis's avatar
Ad Schellevis committed
95 96 97 98 99 100
else:
    # daemonize process
    daemon = Daemonize(app=__file__.split('/')[-1].split('.py')[0], pid=cnf.get('main','pid_filename'), action=proc_handler.run)
    daemon.start()

sys.exit(0)