Initial import
[ldd.git] / lddd
... / ...
CommitLineData
1#!/usr/bin/python
2# ldd - DNS implementation in Python
3# Copyright (C) 2006 Fredrik Tolf <fredrik@dolda2000.com>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20import os
21import sys
22import getopt
23import socket
24import signal
25import imp
26import logging
27
28from ldd import server
29
30cfname = "/etc/lddd/conf"
31port = 53
32daemonize = True
33opts, args = getopt.getopt(sys.argv[1:], "ndc:p:")
34for o, a in opts:
35 if o == "-d":
36 logging.basicConfig(level = logging.DEBUG)
37 daemonize = False
38 if o == "-c":
39 cfname = a
40 if o == "-n":
41 daemonize = False
42 if o == "-p":
43 port = int(a)
44
45logger = logging.getLogger("ldd.daemon")
46
47def diehandler(signum, frame):
48 global alive
49 alive = False
50
51for sig in [getattr(signal, "SIG" + s) for s in ["INT", "TERM"]]:
52 signal.signal(sig, diehandler)
53
54srv = server.dnsserver()
55
56cf = open(cfname, "r")
57cmod = imp.load_module("servconf", cf, cfname, ("", "r", imp.PY_SOURCE))
58cf.close()
59
60cmod.setup(srv)
61if(len(srv.sockets) < 1):
62 sk = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
63 sk.bind(("", port))
64 srv.addsock(socket.AF_INET6, sk)
65logger.info("config OK, starting server")
66
67alive = True
68srv.start()
69
70if daemonize:
71 if(os.fork() != 0):
72 sys.exit(0)
73 os.chdir("/")
74
75while alive:
76 signal.pause()
77logger.info("terminating")
78srv.stop()