From 15872f8f7cb6cbb727bebcd9140a44c4bac039ce Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 9 Dec 2019 05:02:37 -0600 Subject: [PATCH] added watchdog, use it to kill tor --- requirements.in | 1 + requirements.txt | 12 ++++++++++ src/netcontroller/netcontrol.py | 4 ++++ src/netcontroller/watchdog.py | 37 +++++++++++++++++++++++++++++++ src/onionrproofs/subprocesspow.py | 4 ++-- 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/netcontroller/watchdog.py diff --git a/requirements.in b/requirements.in index 58fb7134..2458801a 100644 --- a/requirements.in +++ b/requirements.in @@ -11,3 +11,4 @@ streamedrequests==1.0.0 jinja2==2.10.3 toomanyobjs==1.1.0 niceware==0.2.1 +psutil==5.6.7 diff --git a/requirements.txt b/requirements.txt index 82c89be6..43c516b1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -140,6 +140,18 @@ markupsafe==1.1.1 \ niceware==0.2.1 \ --hash=sha256:0f8b192f2a1e800e068474f6e208be9c7e2857664b33a96f4045340de4e5c69c \ --hash=sha256:cf2dc0e1567d36d067c61b32fed0f1b9c4534ed511f9eeead4ba548d03b5c9eb +psutil==5.6.7 \ + --hash=sha256:094f899ac3ef72422b7e00411b4ed174e3c5a2e04c267db6643937ddba67a05b \ + --hash=sha256:10b7f75cc8bd676cfc6fa40cd7d5c25b3f45a0e06d43becd7c2d2871cbb5e806 \ + --hash=sha256:1b1575240ca9a90b437e5a40db662acd87bbf181f6aa02f0204978737b913c6b \ + --hash=sha256:21231ef1c1a89728e29b98a885b8e0a8e00d09018f6da5cdc1f43f988471a995 \ + --hash=sha256:28f771129bfee9fc6b63d83a15d857663bbdcae3828e1cb926e91320a9b5b5cd \ + --hash=sha256:70387772f84fa5c3bb6a106915a2445e20ac8f9821c5914d7cbde148f4d7ff73 \ + --hash=sha256:b560f5cd86cf8df7bcd258a851ca1ad98f0d5b8b98748e877a0aec4e9032b465 \ + --hash=sha256:b74b43fecce384a57094a83d2778cdfc2e2d9a6afaadd1ebecb2e75e0d34e10d \ + --hash=sha256:e85f727ffb21539849e6012f47b12f6dd4c44965e56591d8dec6e8bc9ab96f4a \ + --hash=sha256:fd2e09bb593ad9bdd7429e779699d2d47c1268cbde4dda95fcd1bd17544a0217 \ + --hash=sha256:ffad8eb2ac614518bbe3c0b8eb9dffdb3a8d2e3a7d5da51c5b974fb723a5c5aa pycparser==2.19 \ --hash=sha256:a988718abfad80b6b157acce7bf130a30876d27603738ac39f140993246b25b3 \ # via cffi diff --git a/src/netcontroller/netcontrol.py b/src/netcontroller/netcontrol.py index dcd0d18a..32834b24 100644 --- a/src/netcontroller/netcontrol.py +++ b/src/netcontroller/netcontrol.py @@ -18,10 +18,12 @@ along with this program. If not, see . ''' 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): diff --git a/src/netcontroller/watchdog.py b/src/netcontroller/watchdog.py new file mode 100644 index 00000000..9ccbc771 --- /dev/null +++ b/src/netcontroller/watchdog.py @@ -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 . +""" + + +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() diff --git a/src/onionrproofs/subprocesspow.py b/src/onionrproofs/subprocesspow.py index 472e85ad..b1c7eeb6 100755 --- a/src/onionrproofs/subprocesspow.py +++ b/src/onionrproofs/subprocesspow.py @@ -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