From e90d7e96f45b71464b2a960be24f98e45d91038e Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Thu, 19 Mar 2020 01:43:57 -0500 Subject: [PATCH] moved daemon kill command to its own seperate file in daemonlaunch --- src/onionrcommands/daemonlaunch/__init__.py | 35 +---------- src/onionrcommands/daemonlaunch/killdaemon.py | 59 +++++++++++++++++++ 2 files changed, 62 insertions(+), 32 deletions(-) create mode 100644 src/onionrcommands/daemonlaunch/killdaemon.py diff --git a/src/onionrcommands/daemonlaunch/__init__.py b/src/onionrcommands/daemonlaunch/__init__.py index 50e362d8..d6d0733d 100755 --- a/src/onionrcommands/daemonlaunch/__init__.py +++ b/src/onionrcommands/daemonlaunch/__init__.py @@ -5,10 +5,8 @@ launch the api servers and communicator import os import sys import platform -import sqlite3 from threading import Thread -from gevent import spawn from stem.connection import IncorrectPassword import toomanyobjs import filenuke @@ -35,6 +33,7 @@ from .getapihost import get_api_host_until_available from utils.bettersleep import better_sleep from netcontroller.torcontrol.onionservicecreator import create_onion_service from .quotes import QUOTE +from .killdaemon import kill_daemon # noqa from utils.boxprint import bordered from lan import LANManager from lan.server import LANServer @@ -61,6 +60,8 @@ def _proper_shutdown(): def daemon(): """Start the Onionr communication daemon.""" + # Determine if Onionr is in offline mode. + # When offline, Onionr can only use LAN and disk transport offline_mode = config.get('general.offline_mode', False) if not hastor.has_tor(): @@ -192,36 +193,6 @@ def _ignore_sigint(sig, frame): # pylint: disable=W0612,W0613 return -def kill_daemon(): - """Shutdown the Onionr daemon (communicator).""" - logger.warn('Stopping the running daemon...', timestamp=False, - terminal=True) - - # On platforms where we can, fork out to prevent locking - try: - pid = os.fork() - if pid != 0: - return - except (AttributeError, OSError): - pass - - events.event('daemon_stop') - net = NetController(config.get('client.port', 59496)) - try: - spawn( - localcommand.local_command, - '/shutdownclean' - ).get(timeout=5) - except sqlite3.OperationalError: - pass - - net.killTor() - - -kill_daemon.onionr_help = "Gracefully stops the " # type: ignore -kill_daemon.onionr_help += "Onionr API servers" # type: ignore - - def start(override: bool = False): """If no lock file, make one and start onionr. diff --git a/src/onionrcommands/daemonlaunch/killdaemon.py b/src/onionrcommands/daemonlaunch/killdaemon.py new file mode 100644 index 00000000..dd65fc01 --- /dev/null +++ b/src/onionrcommands/daemonlaunch/killdaemon.py @@ -0,0 +1,59 @@ +"""Onionr - Private P2P Communication. + +Gracefully stop Onionr daemon +""" +import sqlite3 +import os + +from gevent import spawn + +from onionrplugins import events +from onionrutils import localcommand +import logger +from netcontroller import NetController +import config +""" + 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 kill_daemon(): + """Shutdown the Onionr daemon (communicator).""" + config.reload() + logger.warn('Stopping the running daemon...', timestamp=False, + terminal=True) + + # On platforms where we can, fork out to prevent locking + try: + pid = os.fork() + if pid != 0: + return + except (AttributeError, OSError): + pass + + events.event('daemon_stop') + net = NetController(config.get('client.port', 59496)) + try: + spawn( + localcommand.local_command, + '/shutdownclean' + ).get(timeout=5) + except sqlite3.OperationalError: + pass + + net.killTor() + + +kill_daemon.onionr_help = "Gracefully stops the " # type: ignore +kill_daemon.onionr_help += "Onionr API servers" # type: ignore \ No newline at end of file