1 /* Title: Pure/Concurrent/bash.scala |
|
2 Author: Makarius |
|
3 |
|
4 GNU bash processes, with propagation of interrupts. |
|
5 */ |
|
6 |
|
7 package isabelle |
|
8 |
|
9 |
|
10 import java.io.{File => JFile, BufferedReader, InputStreamReader, |
|
11 BufferedWriter, OutputStreamWriter} |
|
12 |
|
13 |
|
14 object Bash |
|
15 { |
|
16 private class Limited_Progress(proc: Process, progress_limit: Option[Long]) |
|
17 { |
|
18 private var count = 0L |
|
19 def apply(progress: String => Unit)(line: String): Unit = synchronized { |
|
20 progress(line) |
|
21 count = count + line.length + 1 |
|
22 progress_limit match { |
|
23 case Some(limit) if count > limit => proc.terminate |
|
24 case _ => |
|
25 } |
|
26 } |
|
27 } |
|
28 |
|
29 def process(script: String, |
|
30 cwd: JFile = null, |
|
31 env: Map[String, String] = Map.empty, |
|
32 redirect: Boolean = false): Process = |
|
33 new Process(script, cwd, env, redirect) |
|
34 |
|
35 class Process private [Bash]( |
|
36 script: String, cwd: JFile, env: Map[String, String], redirect: Boolean) |
|
37 extends Prover.System_Process |
|
38 { |
|
39 private val timing_file = Isabelle_System.tmp_file("bash_script") |
|
40 private val timing = Synchronized[Option[Timing]](None) |
|
41 |
|
42 private val script_file = Isabelle_System.tmp_file("bash_script") |
|
43 File.write(script_file, script) |
|
44 |
|
45 private val proc = |
|
46 Isabelle_System.process(cwd, Isabelle_System.settings(env), redirect, |
|
47 File.platform_path(Path.variable("ISABELLE_BASH_PROCESS")), "-", |
|
48 File.standard_path(timing_file), "bash", File.standard_path(script_file)) |
|
49 |
|
50 |
|
51 // channels |
|
52 |
|
53 val stdin: BufferedWriter = |
|
54 new BufferedWriter(new OutputStreamWriter(proc.getOutputStream, UTF8.charset)) |
|
55 |
|
56 val stdout: BufferedReader = |
|
57 new BufferedReader(new InputStreamReader(proc.getInputStream, UTF8.charset)) |
|
58 |
|
59 val stderr: BufferedReader = |
|
60 new BufferedReader(new InputStreamReader(proc.getErrorStream, UTF8.charset)) |
|
61 |
|
62 |
|
63 // signals |
|
64 |
|
65 private val pid = stdout.readLine |
|
66 |
|
67 def interrupt() |
|
68 { Exn.Interrupt.postpone { Isabelle_System.kill("INT", pid) } } |
|
69 |
|
70 private def kill(signal: String): Boolean = |
|
71 Exn.Interrupt.postpone { |
|
72 Isabelle_System.kill(signal, pid) |
|
73 Isabelle_System.kill("0", pid)._2 == 0 } getOrElse true |
|
74 |
|
75 private def multi_kill(signal: String): Boolean = |
|
76 { |
|
77 var running = true |
|
78 var count = 10 |
|
79 while (running && count > 0) { |
|
80 if (kill(signal)) { |
|
81 Exn.Interrupt.postpone { |
|
82 Thread.sleep(100) |
|
83 count -= 1 |
|
84 } |
|
85 } |
|
86 else running = false |
|
87 } |
|
88 running |
|
89 } |
|
90 |
|
91 def terminate() |
|
92 { |
|
93 multi_kill("INT") && multi_kill("TERM") && kill("KILL") |
|
94 proc.destroy |
|
95 cleanup() |
|
96 } |
|
97 |
|
98 |
|
99 // JVM shutdown hook |
|
100 |
|
101 private val shutdown_hook = new Thread { override def run = terminate() } |
|
102 |
|
103 try { Runtime.getRuntime.addShutdownHook(shutdown_hook) } |
|
104 catch { case _: IllegalStateException => } |
|
105 |
|
106 |
|
107 // cleanup |
|
108 |
|
109 private def cleanup() |
|
110 { |
|
111 try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
|
112 catch { case _: IllegalStateException => } |
|
113 |
|
114 script_file.delete |
|
115 |
|
116 timing.change { |
|
117 case None => |
|
118 if (timing_file.isFile) { |
|
119 val t = |
|
120 Word.explode(File.read(timing_file)) match { |
|
121 case List(Properties.Value.Long(elapsed), Properties.Value.Long(cpu)) => |
|
122 Timing(Time.ms(elapsed), Time.ms(cpu), Time.zero) |
|
123 case _ => Timing.zero |
|
124 } |
|
125 timing_file.delete |
|
126 Some(t) |
|
127 } |
|
128 else Some(Timing.zero) |
|
129 case some => some |
|
130 } |
|
131 } |
|
132 |
|
133 |
|
134 // join |
|
135 |
|
136 def join: Int = |
|
137 { |
|
138 val rc = proc.waitFor |
|
139 cleanup() |
|
140 rc |
|
141 } |
|
142 |
|
143 |
|
144 // result |
|
145 |
|
146 def result( |
|
147 progress_stdout: String => Unit = (_: String) => (), |
|
148 progress_stderr: String => Unit = (_: String) => (), |
|
149 progress_limit: Option[Long] = None, |
|
150 strict: Boolean = true): Process_Result = |
|
151 { |
|
152 stdin.close |
|
153 |
|
154 val limited = new Limited_Progress(this, progress_limit) |
|
155 val out_lines = |
|
156 Future.thread("bash_stdout") { File.read_lines(stdout, limited(progress_stdout)) } |
|
157 val err_lines = |
|
158 Future.thread("bash_stderr") { File.read_lines(stderr, limited(progress_stderr)) } |
|
159 |
|
160 val rc = |
|
161 try { join } |
|
162 catch { case Exn.Interrupt() => terminate(); Exn.Interrupt.return_code } |
|
163 if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt() |
|
164 |
|
165 Process_Result(rc, out_lines.join, err_lines.join, false, timing.value getOrElse Timing.zero) |
|
166 } |
|
167 } |
|
168 } |
|