3131import ippool , dnsserver
3232from icmp import ping
3333
34+
3435def loadconfig (path ):
3536 if not isfile (path ):
3637 print "[FATAL] can't find config file %s !" % path
@@ -116,6 +117,22 @@ def prepare_run(run_env):
116117 run_env ['tcp' ].append ([f ,ip ])
117118 run_env ['udp' ].append ([p ,ip ])
118119
120+ def sdns_write_pid (pid_file ):
121+ pid = str (os .getpid ())
122+ with open (pid_file , 'w' ) as f :
123+ f .write (pid )
124+
125+
126+ def sdns_kill_pid (pid_file ):
127+ if not os .path .exists (pid_file ): return
128+ try :
129+ with open (pid_file , 'r' ) as f :
130+ pid = int (f .read ())
131+ a = os .kill (pid , signal .SIGKILL )
132+ print 'PID %s has been killed, return code:%s' % (pid , a )
133+ except OSError as e :
134+ logger .info (e )
135+ print 'PID was not discovered.'
119136
120137# run it through twistd!
121138if __name__ == '__main__' :
@@ -124,27 +141,12 @@ def prepare_run(run_env):
124141 '..' ,
125142 'sdns.pid' ,
126143 )
127- def write_pid ():
128- pid = str (os .getpid ())
129- with open (pid_file , 'w' ) as f :
130- f .write (pid )
131- def kill_pid ():
132- if not os .path .exists (pid_file ): return
133- try :
134- with open (pid_file , 'r' ) as f :
135- pid = int (f .read ())
136- a = os .kill (pid , signal .SIGKILL )
137- print 'PID %s has been killed, return code:%s' % (pid , a )
138- except OSError as e :
139- logger .info (e )
140- print 'PID was not discovered.'
141-
142- kill_pid ()
144+ sdns_kill_pid (pid_file )
143145 run_env = {'udp' :[], 'tcp' :[], 'closed' :0 , 'updated' : False , 'finder' :None }
144146 prepare_run (run_env )
145147 for e in run_env ['tcp' ]:
146148 reactor .listenTCP (53 , e [0 ], interface = e [1 ])
147149 for e in run_env ['udp' ]:
148150 reactor .listenUDP (53 , e [0 ], interface = e [1 ])
149- write_pid ( )
151+ sdns_write_pid ( pid_file )
150152 reactor .run ()
0 commit comments