1From 86744d408b91acdcb086a03d7779fcda152f2ac3 Mon Sep 17 00:00:00 2001
2From: Hongxu Jia <hongxu.jia@windriver.com>
3Date: Mon, 8 May 2017 14:39:56 +0800
4Subject: [PATCH 02/13] run_program support timeout
5
6Upstream-Status: Pending
7
8Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
9---
10 blivet/util.py | 68 +++++++++++++++++++++++++++++++++-------------------------
11 1 file changed, 39 insertions(+), 29 deletions(-)
12
13diff --git a/blivet/util.py b/blivet/util.py
14index 0cf5188..d4bd9bb 100644
15--- a/blivet/util.py
16+++ b/blivet/util.py
17@@ -158,6 +158,30 @@ class Path(str):
18     def __hash__(self):
19         return self._path.__hash__()
20
21+def timeout_command(argv, timeout, *args, **kwargs):
22+    """call shell-command and either return its output or kill it
23+    if it doesn't normally exit within timeout seconds and return None"""
24+    import subprocess, datetime, os, time, signal
25+    start = datetime.datetime.now()
26+
27+    try:
28+        proc = subprocess.Popen(argv, *args, **kwargs)
29+        while proc.poll() is None:
30+            time.sleep(0.1)
31+            now = datetime.datetime.now()
32+            if (now - start).seconds> timeout:
33+                os.kill(proc.pid, signal.SIGKILL)
34+                os.waitpid(-1, os.WNOHANG)
35+                program_log.debug("%d seconds timeout" % timeout)
36+                return (-1, None)
37+
38+
39+    except OSError as e:
40+        program_log.error("Error running %s: %s", argv[0], e.strerror)
41+        raise
42+
43+    program_log.debug("Return code: %d", proc.returncode)
44+    return (proc.returncode, proc.stdout.read())
45
46 def _run_program(argv, root='/', stdin=None, env_prune=None, stderr_to_stdout=False, binary_output=False):
47     if env_prune is None:
48@@ -180,36 +204,22 @@ def _run_program(argv, root='/', stdin=None, env_prune=None, stderr_to_stdout=Fa
49             stderr_dir = subprocess.STDOUT
50         else:
51             stderr_dir = subprocess.PIPE
52-        try:
53-            proc = subprocess.Popen(argv,
54-                                    stdin=stdin,
55-                                    stdout=subprocess.PIPE,
56-                                    stderr=stderr_dir,
57-                                    close_fds=True,
58-                                    preexec_fn=chroot, cwd=root, env=env)
59-
60-            out, err = proc.communicate()
61-            if not binary_output and six.PY3:
62-                out = out.decode("utf-8")
63-            if out:
64-                if not stderr_to_stdout:
65-                    program_log.info("stdout:")
66-                for line in out.splitlines():
67-                    program_log.info("%s", line)
68-
69-            if not stderr_to_stdout and err:
70-                program_log.info("stderr:")
71-                for line in err.splitlines():
72-                    program_log.info("%s", line)
73-
74-        except OSError as e:
75-            program_log.error("Error running %s: %s", argv[0], e.strerror)
76-            raise
77-
78-        program_log.debug("Return code: %d", proc.returncode)
79-
80-    return (proc.returncode, out)
81
82+        res, out = timeout_command(argv, 10,
83+                                   stdin=stdin,
84+                                   stdout=subprocess.PIPE,
85+                                   stderr=stderr_dir,
86+                                   close_fds=True,
87+                                   preexec_fn=chroot, cwd=root, env=env)
88+        if not binary_output and six.PY3:
89+            out = out.decode("utf-8")
90+        if out:
91+            if not stderr_to_stdout:
92+                program_log.info("stdout:")
93+            for line in out.splitlines():
94+                program_log.info("%s", line)
95+
96+    return (res, out)
97
98 def run_program(*args, **kwargs):
99     return _run_program(*args, **kwargs)[0]
100--
1012.7.4
102
103