added watchdog, use it to kill tor

This commit is contained in:
Kevin Froman 2019-12-09 05:02:37 -06:00
parent 63c0c51f38
commit 15872f8f7c
5 changed files with 56 additions and 2 deletions

View file

@ -18,10 +18,12 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import os, sys, base64, subprocess, signal, time
import multiprocessing
import platform # For windows sigkill workaround
import config, logger
from . import getopenport
from utils import identifyhome
from . import watchdog
config.reload()
TOR_KILL_WAIT = 3
@ -175,6 +177,8 @@ HiddenServicePort 80 ''' + self.apiServerIP + ''':''' + str(self.hsPort)
torPidFile.write(str(tor.pid))
torPidFile.close()
multiprocessing.Process(target=watchdog.watchdog, args=[os.getpid(), tor.pid]).start()
return True
def killTor(self):

View file

@ -0,0 +1,37 @@
"""
Onionr - Private P2P Communication
Watch 1 process, then terminate second safely
"""
import time
import psutil
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
def watchdog(parent_proc, child_proc):
"""watch for proc1 to die, then kill proc2"""
parent_proc = psutil.Process(parent_proc)
child_proc = psutil.Process(child_proc)
while parent_proc.is_running():
time.sleep(10)
if child_proc.is_running():
child_proc.terminate()

View file

@ -77,7 +77,7 @@ class SubprocessPOW:
"""spawn the multiproc handler threads"""
# Create a new thread for each subprocess
for _ in range(self.subproc_count): # noqa
threading.Thread(target=self._spawn_proc).start()
threading.Thread(target=self._spawn_proc, daemon=True).start()
# Monitor the processes for a payload, shut them down when its found
while True:
if self.payload is None:
@ -90,7 +90,7 @@ class SubprocessPOW:
"""Create a child proof of work process
wait for data and send shutdown signal when its found"""
parent_conn, child_conn = Pipe()
p = Process(target=self.do_pow, args=(child_conn,))
p = Process(target=self.do_pow, args=(child_conn,), daemon=True)
p.start()
p.join()
payload = None