def kill(pidfile, killsignal=SIG, succmsg="", errmsg="", restart_file=SERVER_RESTART, restart=False): """ Send a kill signal to a process based on PID. A customized success/error message will be returned. If clean=True, the system will attempt to manually remove the pid file. Args: pidfile (str): The path of the pidfile to get the PID from. killsignal (int, optional): Signal identifier for signal to send. succmsg (str, optional): Message to log on success. errmsg (str, optional): Message to log on failure. restart_file (str, optional): Restart file location. restart (bool, optional): Are we in restart mode or not. """ pid = get_pid(pidfile) if pid: if os.name == 'nt': os.remove(pidfile) # set restart/norestart flag if restart: django.core.management.call_command('collectstatic', interactive=False, verbosity=0) with open(restart_file, 'w') as f: f.write("reload") else: with open(restart_file, 'w') as f: f.write("shutdown") try: if os.name == 'nt': from win32api import GenerateConsoleCtrlEvent, SetConsoleCtrlHandler try: # Windows can only send a SIGINT-like signal to # *every* process spawned off the same console, so we must # avoid killing ourselves here. SetConsoleCtrlHandler(None, True) GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) except KeyboardInterrupt: # We must catch and ignore the interrupt sent. pass else: # Linux can send the SIGINT signal directly # to the specified PID. os.kill(int(pid), killsignal) except OSError: print("Process %(pid)s cannot be stopped. "\ "The PID file 'server/%(pidfile)s' seems stale. "\ "Try removing it." % {'pid': pid, 'pidfile': pidfile}) return print("Evennia:", succmsg) return print("Evennia:", errmsg)
def __init__(self, listen_interface=None, interface=None, port=None, standalone=False, read_only_config_keys=None): """ Args: listen_interface (str, optional): The IP address to listen to bittorrent connections on. interface (str, optional): The IP address the daemon will listen for UI connections on. port (int, optional): The port the daemon will listen for UI connections on. standalone (bool, optional): If True the client is in Standalone mode otherwise, if False, start the daemon as separate process. read_only_config_keys (list of str, optional): A list of config keys that will not be altered by core.set_config() RPC method. """ self.standalone = standalone self.pid_file = get_config_dir('deluged.pid') log.info('Deluge daemon %s', get_version()) if is_daemon_running(self.pid_file): raise DaemonRunningError( 'Deluge daemon already running with this config directory!') # Twisted catches signals to terminate, so just have it call the shutdown method. reactor.addSystemEventTrigger('before', 'shutdown', self._shutdown) # Catch some Windows specific signals if windows_check(): def win_handler(ctrl_type): """Handle the Windows shutdown or close events.""" log.debug('windows handler ctrl_type: %s', ctrl_type) if ctrl_type == CTRL_CLOSE_EVENT or ctrl_type == CTRL_SHUTDOWN_EVENT: self._shutdown() return 1 SetConsoleCtrlHandler(win_handler) # Start the core as a thread and join it until it's done self.core = Core(listen_interface=listen_interface, read_only_config_keys=read_only_config_keys) if port is None: port = self.core.config['daemon_port'] self.port = port if interface and not is_ip(interface): log.error('Invalid UI interface (must be IP Address): %s', interface) interface = None self.rpcserver = RPCServer( port=port, allow_remote=self.core.config['allow_remote'], listen=not standalone, interface=interface) log.debug('Listening to UI on: %s:%s and bittorrent on: %s', interface, port, listen_interface)
def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) SetConsoleCtrlHandler(lambda x: True, True) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) self.count = 0 self.estinmins = 0 self.isAlive = True self.envv = '' self.prlist = []
def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) # create an event that SvcDoRun can wait on and SvcStop # can set. # self.stop_event = win32event.CreateEvent(None, 0, 0, None) self._server = None self._protocol = eventlet.wsgi.HttpProtocol bind_addr = ('', settings.getint('WEBSERVER', 'PORT')) self._socket = eventlet.listen(bind_addr) SetConsoleCtrlHandler(lambda x: True, True) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def __init__(self): self.registry = {} self.event_handles = {} self.admin_event_handle = win32event.CreateEvent(None, 0, 0, None) self.shutdown_requested = False # Register a "console control handler" for Ctrl+C/Break notification. SetConsoleCtrlHandler(consoleCtrlHandler) # Start the thread that is watching for events. thread = threading.Thread(target=self.signalCheckerThread) # If something goes terribly wrong, don't wait for this thread! thread.setDaemon(True) thread.start() self.signal_thread = thread
def install_signal_handlers(self): # Since twisted assigns itself all the signals may as well make # use of it. reactor.addSystemEventTrigger("after", "shutdown", self.shutdown) # Twisted doesn't handle windows specific signals so we still # need to attach to those to handle the close correctly. if common.windows_check(): from win32api import SetConsoleCtrlHandler from win32con import CTRL_CLOSE_EVENT, CTRL_SHUTDOWN_EVENT def win_handler(ctrl_type): log.debug("ctrl type: %s", ctrl_type) if ctrl_type == CTRL_CLOSE_EVENT or \ ctrl_type == CTRL_SHUTDOWN_EVENT: self.shutdown() return 1 SetConsoleCtrlHandler(win_handler)
class GtkUI(object): def __init__(self, args): self.daemon_bps = (0, 0, 0) # Setup signals try: import gnome.ui import gnome #Suppress: Warning: Attempt to add property GnomeProgram::*** after class was initialised original_filters = warnings.filters[:] warnings.simplefilter("ignore") try: self.gnome_prog = gnome.init("Deluge", deluge.common.get_version()) finally: warnings.filters = original_filters self.gnome_client = gnome.ui.master_client() def on_die(*args): reactor.stop() self.gnome_client.connect("die", on_die) log.debug("GNOME session 'die' handler registered!") except Exception, e: log.warning( "Unable to register a 'die' handler with the GNOME session manager: %s", e) if deluge.common.windows_check(): from win32api import SetConsoleCtrlHandler from win32con import CTRL_CLOSE_EVENT from win32con import CTRL_SHUTDOWN_EVENT def win_handler(ctrl_type): log.debug("ctrl_type: %s", ctrl_type) if ctrl_type in (CTRL_CLOSE_EVENT, CTRL_SHUTDOWN_EVENT): reactor.stop() return 1 SetConsoleCtrlHandler(win_handler) if deluge.common.osx_check() and Gdk.WINDOWING == "quartz": import gtkosx_application self.osxapp = gtkosx_application.gtkosx_application_get() def on_die(*args): reactor.stop() self.osxapp.connect("NSApplicationWillTerminate", on_die) # Set process name again to fix gtk issue setproctitle(getproctitle()) # Attempt to register a magnet URI handler with gconf, but do not overwrite # if already set by another program. common.associate_magnet_links(False) # Make sure gtkui.conf has at least the defaults set self.config = deluge.configmanager.ConfigManager( "gtkui.conf", DEFAULT_PREFS) # We need to check on exit if it was started in classic mode to ensure we # shutdown the daemon. self.started_in_classic = self.config["classic_mode"] # Start the IPC Interface before anything else.. Just in case we are # already running. self.queuedtorrents = QueuedTorrents() self.ipcinterface = IPCInterface(args) # Initialize gdk threading Gdk.threads_init() GObject.threads_init() # We make sure that the UI components start once we get a core URI client.set_disconnect_callback(self.__on_disconnect) self.trackericons = TrackerIcons() self.sessionproxy = SessionProxy() # Initialize various components of the gtkui self.mainwindow = MainWindow() self.menubar = MenuBar() self.toolbar = ToolBar() self.torrentview = TorrentView() self.torrentdetails = TorrentDetails() self.sidebar = SideBar() self.filtertreeview = FilterTreeView() self.preferences = Preferences() self.systemtray = SystemTray() self.statusbar = StatusBar() self.addtorrentdialog = AddTorrentDialog() if deluge.common.osx_check() and Gdk.WINDOWING == "quartz": def nsapp_open_file(osxapp, filename): # Will be raised at app launch (python opening main script) if filename.endswith('Deluge-bin'): return True from deluge.ui.gtkui.ipcinterface import process_args process_args([filename]) self.osxapp.connect("NSApplicationOpenFile", nsapp_open_file) from menubar_osx import menubar_osx menubar_osx(self, self.osxapp) self.osxapp.ready() # Initalize the plugins self.plugins = PluginManager() # Show the connection manager self.connectionmanager = ConnectionManager() from twisted.internet.task import LoopingCall rpc_stats = LoopingCall(self.print_rpc_stats) rpc_stats.start(10) reactor.callWhenRunning(self._on_reactor_start) # Start the gtk main loop Gdk.threads_enter() reactor.run() self.shutdown() Gdk.threads_leave()
def __init__(self, args): w32su.ServiceFramework.__init__(self, args) SetConsoleCtrlHandler(lambda x: True, True) # create an event to listen for stop requests on self.hWaitStop = w32e.CreateEvent(None, 0, 0, None)
class Daemon(object): def __init__(self, options=None, args=None, classic=False): # Check for another running instance of the daemon if os.path.isfile(deluge.configmanager.get_config_dir("deluged.pid")): # Get the PID and the port of the supposedly running daemon try: (pid, port) = open( deluge.configmanager.get_config_dir( "deluged.pid")).read().strip().split(";") pid = int(pid) port = int(port) except ValueError: pid = None port = None def process_running(pid): if deluge.common.windows_check(): import win32process return pid in win32process.EnumProcesses() else: # We can just use os.kill on UNIX to test if the process is running try: os.kill(pid, 0) except OSError: return False else: return True if pid is not None and process_running(pid): # Ok, so a process is running with this PID, let's make doubly-sure # it's a deluged process by trying to open a socket to it's port. import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(("127.0.0.1", port)) except socket.error: # Can't connect, so it must not be a deluged process.. pass else: # This is a deluged! s.close() raise deluge.error.DaemonRunningError( "There is a deluge daemon running with this config directory!" ) # Initialize gettext try: locale.setlocale(locale.LC_ALL, '') if hasattr(locale, "bindtextdomain"): locale.bindtextdomain( "deluge", pkg_resources.resource_filename("deluge", "i18n")) if hasattr(locale, "textdomain"): locale.textdomain("deluge") gettext.bindtextdomain( "deluge", pkg_resources.resource_filename("deluge", "i18n")) gettext.textdomain("deluge") gettext.install("deluge", pkg_resources.resource_filename("deluge", "i18n")) except Exception, e: log.error("Unable to initialize gettext/locale: %s", e) import __builtin__ __builtin__.__dict__["_"] = lambda x: x # Twisted catches signals to terminate, so just have it call the shutdown # method. reactor.addSystemEventTrigger("before", "shutdown", self._shutdown) # Catch some Windows specific signals if deluge.common.windows_check(): from win32api import SetConsoleCtrlHandler from win32con import CTRL_CLOSE_EVENT from win32con import CTRL_SHUTDOWN_EVENT def win_handler(ctrl_type): log.debug("ctrl_type: %s", ctrl_type) if ctrl_type == CTRL_CLOSE_EVENT or ctrl_type == CTRL_SHUTDOWN_EVENT: self._shutdown() return 1 SetConsoleCtrlHandler(win_handler) version = deluge.common.get_version() log.info("Deluge daemon %s", version) log.debug("options: %s", options) log.debug("args: %s", args) # Set the config directory if options and options.config: deluge.configmanager.set_config_dir(options.config) if options and options.listen_interface: listen_interface = options.listen_interface else: listen_interface = "" from deluge.core.core import Core # Start the core as a thread and join it until it's done self.core = Core(listen_interface=listen_interface) port = self.core.config["daemon_port"] if options and options.port: port = options.port if options and options.ui_interface: interface = options.ui_interface else: interface = "" self.rpcserver = RPCServer( port=port, allow_remote=self.core.config["allow_remote"], listen=not classic, interface=interface) # Register the daemon and the core RPCs self.rpcserver.register_object(self.core) self.rpcserver.register_object(self) # Make sure we start the PreferencesManager first component.start("PreferencesManager") if not classic: # Write out a pid file all the time, we use this to see if a deluged is running # We also include the running port number to do an additional test open(deluge.configmanager.get_config_dir("deluged.pid"), "wb").write("%s;%s\n" % (os.getpid(), port)) component.start() try: reactor.run() finally: self._shutdown()
def __init__(self, args): # Setup gtkbuilder/glade translation setup_translations(setup_gettext=False, setup_pygtk=True) # Setup signals def on_die(*args): log.debug('OS signal "die" caught with args: %s', args) reactor.stop() if windows_check(): from win32api import SetConsoleCtrlHandler SetConsoleCtrlHandler(on_die, True) log.debug('Win32 "die" handler registered') elif osx_check() and WINDOWING == 'quartz': import gtkosx_application self.osxapp = gtkosx_application.gtkosx_application_get() self.osxapp.connect('NSApplicationWillTerminate', on_die) log.debug('OSX quartz "die" handler registered') # Set process name again to fix gtk issue setproctitle(getproctitle()) # Attempt to register a magnet URI handler with gconf, but do not overwrite # if already set by another program. associate_magnet_links(False) # Make sure gtkui.conf has at least the defaults set self.config = ConfigManager('gtkui.conf', DEFAULT_PREFS) # Make sure the gtkui state folder has been created if not os.path.exists(os.path.join(get_config_dir(), 'gtkui_state')): os.makedirs(os.path.join(get_config_dir(), 'gtkui_state')) # Set language if self.config['language'] is not None: set_language(self.config['language']) # Start the IPC Interface before anything else.. Just in case we are # already running. self.queuedtorrents = QueuedTorrents() self.ipcinterface = IPCInterface(args.torrents) # Initialize gdk threading threads_init() # We make sure that the UI components start once we get a core URI client.set_disconnect_callback(self.__on_disconnect) self.trackericons = TrackerIcons() self.sessionproxy = SessionProxy() # Initialize various components of the gtkui self.mainwindow = MainWindow() self.menubar = MenuBar() self.toolbar = ToolBar() self.torrentview = TorrentView() self.torrentdetails = TorrentDetails() self.sidebar = SideBar() self.filtertreeview = FilterTreeView() self.preferences = Preferences() self.systemtray = SystemTray() self.statusbar = StatusBar() self.addtorrentdialog = AddTorrentDialog() if osx_check() and WINDOWING == 'quartz': def nsapp_open_file(osxapp, filename): # Ignore command name which is raised at app launch (python opening main script). if filename == sys.argv[0]: return True process_args([filename]) self.osxapp.connect('NSApplicationOpenFile', nsapp_open_file) from deluge.ui.gtkui.menubar_osx import menubar_osx menubar_osx(self, self.osxapp) self.osxapp.ready() # Initalize the plugins self.plugins = PluginManager() # Show the connection manager self.connectionmanager = ConnectionManager() # Setup RPC stats logging # daemon_bps: time, bytes_sent, bytes_recv self.daemon_bps = (0, 0, 0) self.rpc_stats = LoopingCall(self.log_rpc_stats) self.closing = False # Twisted catches signals to terminate, so have it call a pre_shutdown method. reactor.addSystemEventTrigger('before', 'gtkui_close', self.close) def gtkui_sigint_handler(num, frame): log.debug('SIGINT signal caught, firing event: gtkui_close') reactor.callLater(0, reactor.fireSystemEvent, 'gtkui_close') signal.signal(signal.SIGINT, gtkui_sigint_handler)
class GtkUI(object): def __init__(self, args): self.daemon_bps = (0, 0, 0) # Setup signals try: import gnome.ui import gnome self.gnome_prog = gnome.init("Deluge", deluge.common.get_version()) self.gnome_client = gnome.ui.master_client() def on_die(*args): reactor.stop() self.gnome_client.connect("die", on_die) log.debug("GNOME session 'die' handler registered!") except Exception, e: log.warning( "Unable to register a 'die' handler with the GNOME session manager: %s", e) if deluge.common.windows_check(): from win32api import SetConsoleCtrlHandler from win32con import CTRL_CLOSE_EVENT from win32con import CTRL_SHUTDOWN_EVENT def win_handler(ctrl_type): log.debug("ctrl_type: %s", ctrl_type) if ctrl_type in (CTRL_CLOSE_EVENT, CTRL_SHUTDOWN_EVENT): reactor.stop() return 1 SetConsoleCtrlHandler(win_handler) # Attempt to register a magnet URI handler with gconf, but do not overwrite # if already set by another program. common.associate_magnet_links(False) # Make sure gtkui.conf has at least the defaults set self.config = deluge.configmanager.ConfigManager( "gtkui.conf", DEFAULT_PREFS) # We need to check on exit if it was started in classic mode to ensure we # shutdown the daemon. self.started_in_classic = self.config["classic_mode"] # Start the IPC Interface before anything else.. Just in case we are # already running. self.queuedtorrents = QueuedTorrents() self.ipcinterface = IPCInterface(args) # Initialize gdk threading gtk.gdk.threads_init() # We make sure that the UI components start once we get a core URI client.set_disconnect_callback(self.__on_disconnect) self.trackericons = TrackerIcons() self.sessionproxy = SessionProxy() # Initialize various components of the gtkui self.mainwindow = MainWindow() self.menubar = MenuBar() self.toolbar = ToolBar() self.torrentview = TorrentView() self.torrentdetails = TorrentDetails() self.sidebar = SideBar() self.filtertreeview = FilterTreeView() self.preferences = Preferences() self.systemtray = SystemTray() self.statusbar = StatusBar() self.addtorrentdialog = AddTorrentDialog() # Initalize the plugins self.plugins = PluginManager() # Late import because of setting up translations from connectionmanager import ConnectionManager # Show the connection manager self.connectionmanager = ConnectionManager() from twisted.internet.task import LoopingCall rpc_stats = LoopingCall(self.print_rpc_stats) rpc_stats.start(10) reactor.callWhenRunning(self._on_reactor_start) # Initialize gdk threading gtk.gdk.threads_enter() reactor.run() self.shutdown() gtk.gdk.threads_leave()
def __init__(self, options=None, args=None, classic=False): # Check for another running instance of the daemon if os.path.isfile(deluge.configmanager.get_config_dir("deluged.pid")): # Get the PID and the port of the supposedly running daemon try: (pid, port) = open( deluge.configmanager.get_config_dir( "deluged.pid")).read().strip().split(";") pid = int(pid) port = int(port) except ValueError: pid = None port = None def process_running(pid): if deluge.common.windows_check(): # Do some fancy WMI junk to see if the PID exists in Windows from win32com.client import GetObject def get_proclist(): WMI = GetObject('winmgmts:') processes = WMI.InstancesOf('Win32_Process') return [ process.Properties_('ProcessID').Value for process in processes ] return pid in get_proclist() else: # We can just use os.kill on UNIX to test if the process is running try: os.kill(pid, 0) except OSError: return False else: return True if pid is not None and process_running(pid): # Ok, so a process is running with this PID, let's make doubly-sure # it's a deluged process by trying to open a socket to it's port. import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(("127.0.0.1", port)) except socket.error: # Can't connect, so it must not be a deluged process.. pass else: # This is a deluged! s.close() raise deluge.error.DaemonRunningError( "There is a deluge daemon running with this config " "directory!") # Initialize gettext deluge.common.setup_translations() # Twisted catches signals to terminate, so just have it call the shutdown # method. reactor.addSystemEventTrigger("after", "shutdown", self.shutdown) # Catch some Windows specific signals if deluge.common.windows_check(): from win32api import SetConsoleCtrlHandler from win32con import CTRL_CLOSE_EVENT from win32con import CTRL_SHUTDOWN_EVENT def win_handler(ctrl_type): log.debug("ctrl_type: %s", ctrl_type) if ctrl_type == CTRL_CLOSE_EVENT or ctrl_type == CTRL_SHUTDOWN_EVENT: self.__shutdown() return 1 SetConsoleCtrlHandler(win_handler) version = deluge.common.get_version() log.info("Deluge daemon %s", version) log.debug("options: %s", options) log.debug("args: %s", args) # Set the config directory if options and options.config: deluge.configmanager.set_config_dir(options.config) from deluge.core.core import Core # Start the core as a thread and join it until it's done self.core = Core() port = self.core.config["daemon_port"] if options and options.port: port = options.port if options and options.ui_interface: interface = options.ui_interface else: interface = "" self.rpcserver = RPCServer( port=port, allow_remote=self.core.config["allow_remote"], listen=not classic, interface=interface) # Register the daemon and the core RPCs self.rpcserver.register_object(self.core) self.rpcserver.register_object(self) # Make sure we start the PreferencesManager first component.start("PreferencesManager") if not classic: # Write out a pid file all the time, we use this to see if a deluged is running # We also include the running port number to do an additional test open(deluge.configmanager.get_config_dir("deluged.pid"), "wb").write("%s;%s\n" % (os.getpid(), port)) component.start() try: reactor.run() finally: self._shutdown()
def disable_exit_handler(): global __win32_exit_handler if __win32_exit_handler is not None: SetConsoleCtrlHandler(__win32_exit_handler, False) __win32_exit_handler = None
def enable_exit_handler(handler=lambda *args: exit()): SetConsoleCtrlHandler(handler, True) __exit_handler = handler # TODO: comment ?
def enable_exit_handler(handler=lambda *args: exit()): global __win32_exit_handler SetConsoleCtrlHandler(handler, True) __win32_exit_handler = handler