From: Waldemar Kozaczuk <[email protected]>
Committer: Waldemar Kozaczuk <[email protected]>
Branch: master

firecracker: enhance firecracker.py to save/restore terminal

This patch borrows similar logic from run.py to save and
restore terminal line settings to prevent firecracker.py mess up
the terminal.

Signed-off-by: Waldemar Kozaczuk <[email protected]>

---
diff --git a/scripts/firecracker.py b/scripts/firecracker.py
--- a/scripts/firecracker.py
+++ b/scripts/firecracker.py
@@ -6,6 +6,7 @@
 import stat
 import json
 import subprocess
+import signal
 import time
 import argparse
 import re
@@ -16,6 +17,19 @@

 verbose = False

+stty_params = None
+
+devnull = open('/dev/null', 'w')
+
+def stty_save():
+    global stty_params
+ p = subprocess.Popen(["stty", "-g"], stdout=subprocess.PIPE, stderr=devnull)
+    stty_params, err = p.communicate()
+    stty_params = stty_params.strip()
+
+def stty_restore():
+    if stty_params:
+        subprocess.call(["stty", stty_params], stderr=devnull)

 class ApiException(Exception):
     pass
@@ -212,13 +226,15 @@ def start_firecracker(firecracker_path, socket_path):
         os.unlink(socket_path)

# Start firecracker process to communicate over specified UNIX socket file
+    stty_save()
     return subprocess.Popen([firecracker_path, '--api-sock', socket_path],
                            stdout=sys.stdout, stderr=subprocess.STDOUT)

def start_firecracker_with_no_api(firecracker_path, firecracker_config_json):
     #  Start firecracker process and pass configuration JSON as a file
     api_file = tempfile.NamedTemporaryFile(delete=False)
     api_file.write(firecracker_config_json)
+    stty_save()
return subprocess.Popen([firecracker_path, "--no-api", "--config-file", api_file.name], stdout=sys.stdout, stderr=subprocess.STDOUT), api_file.name

@@ -313,15 +329,23 @@ def main(options):
     except ApiException as e:
         print("Failed to make firecracker API call: %s." % e)
         firecracker.kill()
+        stty_restore()
         exit(-1)

     except Exception as e:
print("Failed to run OSv on firecracker due to: ({0}): {1} !!!".format(e.errno, e.strerror))
         firecracker.kill()
+        stty_restore()
         exit(-1)

     print_time("Waiting for firecracker process to terminate")
-    firecracker.wait()
+    try:
+        firecracker.wait()
+    except KeyboardInterrupt:
+        os.kill(firecracker.pid, signal.SIGINT)
+
+    stty_restore()
+
     if options.api_less:
         os.unlink(config_file_path)
     print_time("End")

--
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/000000000000d135dd05965dbb8b%40google.com.

Reply via email to