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()