769e7ed9 |
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 | |
20 | import os |
21 | import sys |
22 | import getopt |
23 | import socket |
24 | import signal |
25 | import imp |
26 | import logging |
27 | |
28 | from ldd import server |
29 | |
30 | cfname = "/etc/lddd/conf" |
31 | port = 53 |
32 | daemonize = True |
33 | opts, args = getopt.getopt(sys.argv[1:], "ndc:p:") |
34 | for 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 | |
45 | logger = logging.getLogger("ldd.daemon") |
46 | |
47 | def diehandler(signum, frame): |
48 | global alive |
49 | alive = False |
50 | |
51 | for sig in [getattr(signal, "SIG" + s) for s in ["INT", "TERM"]]: |
52 | signal.signal(sig, diehandler) |
53 | |
54 | srv = server.dnsserver() |
55 | |
56 | cf = open(cfname, "r") |
57 | cmod = imp.load_module("servconf", cf, cfname, ("", "r", imp.PY_SOURCE)) |
58 | cf.close() |
59 | |
60 | cmod.setup(srv) |
61 | if(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) |
65 | logger.info("config OK, starting server") |
66 | |
67 | alive = True |
68 | srv.start() |
69 | |
70 | if daemonize: |
71 | if(os.fork() != 0): |
72 | sys.exit(0) |
73 | os.chdir("/") |
74 | |
75 | while alive: |
76 | signal.pause() |
77 | logger.info("terminating") |
78 | srv.stop() |