Skip to content

Commit

Permalink
chore(power/qemu): simplify shutdown
Browse files Browse the repository at this point in the history
Attempt to write to the FIFO for QEMU's monitor may block. Simplify
shutting down of qemu/kvm by removing the use of the "quit" command
and immediately send SIGTERM.

Signed-off-by: Cedric Hombourger <[email protected]>
  • Loading branch information
chombourger committed Jan 17, 2024
1 parent 087ca3e commit deb23af
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions mtda/power/qemu.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,12 @@ def start(self):
"({0})".format(result))
return False

def kill(self, name, pid, wait_before_kill=True, timeout=3):
def kill(self, name, pid, timeout=3):
tries = timeout
while wait_before_kill and tries > 0 and psutil.pid_exists(pid):
self.mtda.debug(2, "waiting {0} more seconds for {1} "
"[{2}] to terminate".format(timeout, name, pid))
time.sleep(1)
tries = tries - 1
if psutil.pid_exists(pid):
self.mtda.debug(2, "terminating {0} "
"[{1}] using SIGTERM".format(name, pid))
os.kill(pid, signal.SIGTERM)
tries = timeout
while tries > 0 and psutil.pid_exists(pid):
time.sleep(1)
tries = tries - 1
Expand All @@ -297,20 +291,17 @@ def stop(self):
result = True

if self.pidOfQemu is not None:

with open("/tmp/qemu-mtda.in", "w") as f:
f.write("quit\n")
result = self.kill("qemu", self.pidOfQemu)
if result:
self.pidOfQemu = None

if self.pidOfSwTpm is not None:
result = self.kill("swtpm", self.pidOfSwTpm, False)
result = self.kill("swtpm", self.pidOfSwTpm)
if result:
self.pidOfSwTpm = None

if self.pidOfWebsockify is not None:
result = self.kill("websockify", self.pidOfWebsockify, False)
result = self.kill("websockify", self.pidOfWebsockify)
if result:
self.pidOfWebsockify = None

Expand Down

0 comments on commit deb23af

Please sign in to comment.