Added wk.exe.stop_process()

* Replaced wk.hw.ddrescue.stop_ddrescue()
This commit is contained in:
2Shirt 2020-02-25 20:52:08 -07:00
parent 45a6b31910
commit a4df2f41d3
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
2 changed files with 26 additions and 21 deletions

View file

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

View file

@ -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 = []