From a4df2f41d32c0b7b782262d7a1eb412634e62dde Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Tue, 25 Feb 2020 20:52:08 -0700 Subject: [PATCH] Added wk.exe.stop_process() * Replaced wk.hw.ddrescue.stop_ddrescue() --- scripts/wk/exe.py | 24 ++++++++++++++++++++++++ scripts/wk/hw/ddrescue.py | 23 ++--------------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/scripts/wk/exe.py b/scripts/wk/exe.py index e49b51d6..2097469b 100644 --- a/scripts/wk/exe.py +++ b/scripts/wk/exe.py @@ -6,6 +6,7 @@ import logging import os import re import subprocess +import time from threading import Thread from queue import Queue, Empty @@ -214,6 +215,29 @@ def start_thread(function, args=None, daemon=True): return thread +def stop_process(proc, graceful=True): + """Stop process. + + NOTES: proc should be a subprocess.Popen obj. + If graceful is True then a SIGTERM is sent before SIGKILL. + """ + running_as_root = os.geteuid() == 0 + + # Graceful exit + if graceful: + if running_as_root: + proc.terminate() + else: + run_program(['sudo', 'kill', str(proc.pid)], check=False) + time.sleep(2) + + # Force exit + if running_as_root: + proc.kill() + else: + run_program(['sudo', 'kill', '-9', str(proc.pid)], check=False) + + def wait_for_procs(name, exact=True, timeout=None): """Wait for all process matching name.""" LOG.debug('name: %s, exact: %s, timeout: %s', name, exact, timeout) diff --git a/scripts/wk/hw/ddrescue.py b/scripts/wk/hw/ddrescue.py index 66ac3d5a..5d3d7e2e 100644 --- a/scripts/wk/hw/ddrescue.py +++ b/scripts/wk/hw/ddrescue.py @@ -1883,7 +1883,7 @@ def run_ddrescue(state, block_pair, pass_name, settings, dry_run=True): warning_message = check_destination_health(state.destination) if warning_message: # Error detected on destination, stop recovery - stop_ddrescue(proc) + exe.stop_process(proc) break if _i % 60 == 0: # Clear ddrescue pane @@ -1903,7 +1903,7 @@ def run_ddrescue(state, block_pair, pass_name, settings, dry_run=True): LOG.warning('ddrescue stopped by user') warning_message = 'Aborted' std.sleep(2) - stop_ddrescue(proc, graceful=False) + exe.stop_process(proc, graceful=False) break except subprocess.TimeoutExpired: # Continue to next loop to update panes @@ -2180,25 +2180,6 @@ def set_mode(docopt_args): return mode -def stop_ddrescue(proc, graceful=True): - """Stop ddrescue.""" - running_as_root = os.geteuid() == 0 - - # Graceful exit - if graceful: - if running_as_root: - proc.terminate() - else: - exe.run_program(['sudo', 'kill', str(proc.pid)], check=False) - std.sleep(2) - - # Force exit - if running_as_root: - proc.kill() - else: - exe.run_program(['sudo', 'kill', '-9', str(proc.pid)], check=False) - - def unmount_loopback_device(path): """Unmount loopback device using OS specific methods.""" cmd = []