root / trunk / gdhostd

Revision 66, 3.7 kB (checked in by docwhat, 3 years ago)

r1089@LC2000: docwhat | 2005-09-22T02:50:35.797237Z

  • alcohol bad
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to author date id revision
Line 
1#!/usr/bin/python
2## -*- coding: utf-8 -*-
3##
4## gdhostd -- Gerf Dynamic Host Daemon Wrapper
5##
6## Copyright (C) 2001-2002,2005 Nick Borko & Christian H�e
7## This program is free software; you can redistribute it and/or modify
8## it under the terms of the GNU General Public License as published by
9## the Free Software Foundation; either version 2 of the License, or
10## (at your option) any later version.
11##
12## This program is distributed in the hope that it will be useful,
13## but WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15## GNU General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
18## along with this program; if not, write to the Free Software
19## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20##
21
22import sys, os, syslog, signal
23import gdhost.server as server
24import gdhost.daemon as daemon
25from gdhost.version import version_string
26
27## Version Check
28if sys.version_info >= (2, 2) and \
29   sys.version_info <  (2,3):
30    try:
31        import optik as optparse
32    except ImportError:
33        try:
34            import optparse
35        except ImportError:
36            print >> sys.stderr, "You must either install optik (http://optik.sf.net) or upgrade to python 2.3"
37elif sys.version_info >= (2,3):
38    import optparse
39else:
40    print >> sys.stderr, "This program requires python 2.2 with the optik module (http://optik.sf.net) or python 2.3 or greater"
41    sys.exit(1)
42
43parser = optparse.OptionParser(version=version_string,
44                               usage="%prog [options] command")
45parser.add_option( '-d', '--debug', action="store_true",
46                   dest="debug", default=False,
47                   help="Turn on debugging" )
48parser.add_option('-D', '--daemon', action="store_true",
49                  dest="daemon", default=False,
50                  help="Fork off a daemon" )
51parser.add_option('-c', '--config', action="store",
52                  dest="config", default="/etc/gdhost/config",
53                  help="Configuration File" )
54parser.add_option( '-S', '--syslog', action="store_true",
55                   dest="syslog", default=False,
56                   help="Use Syslog to log events. This is forced to true if you run as a daemon." )
57
58if '__main__' == __name__:
59    options, commands = parser.parse_args()
60    progname = os.path.basename(sys.argv[0])
61
62    if not os.path.exists(options.config):
63        print >> sys.stderr, "The configuration file doesn't exist: %s" % options.config
64        sys.exit(10)
65   
66    if options.daemon:
67        retCode = deamon.createDaemon()
68
69    if options.daemon or options.syslog:
70        def log_info(msg, pri = syslog.LOG_INFO):
71            syslog.openlog('%s[%d]' % ( progname,
72                                          os.getpid()), 0, syslog.LOG_DAEMON)
73            syslog.syslog(pri, msg)
74            syslog.closelog()
75    else:
76        def log_info( msg ):
77            sys.stdout.write( '%s[%d] ' % ( progname,
78                                            os.getpid()) )
79            sys.stdout.write( msg )
80            if not msg.endswith('\n'):
81                sys.stdout.write( '\n' )
82            sys.stdout.flush()
83
84    server = server.Server(options.config,
85                           log_info,
86                           debug=options.debug)
87
88    def exitServer(signum = None, frame = None):
89        "Quit nicely on the correct signal"
90        server.log( "...Shutting down" )
91        sys.exit(0)
92
93    server.log( "Starting..." )
94    signal.signal(signal.SIGHUP, server.reconfig)
95    signal.signal(signal.SIGINT, exitServer)
96    signal.signal(signal.SIGTERM, exitServer)
97
98    server.serve_forever()
Note: See TracBrowser for help on using the browser.