author | wenzelm |
Sat, 20 Feb 2021 22:09:16 +0100 | |
changeset 73263 | ad60214bef09 |
parent 73230 | d1bc5a376cf9 |
child 73265 | 76c9fcf80f96 |
permissions | -rw-r--r-- |
62584 | 1 |
/* Title: Pure/System/bash.scala |
60991 | 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 |
||
71706 | 13 |
import scala.annotation.tailrec |
14 |
||
60991 | 15 |
|
16 |
object Bash |
|
17 |
{ |
|
64304 | 18 |
/* concrete syntax */ |
19 |
||
20 |
private def bash_chr(c: Byte): String = |
|
21 |
{ |
|
22 |
val ch = c.toChar |
|
23 |
ch match { |
|
24 |
case '\t' => "$'\\t'" |
|
25 |
case '\n' => "$'\\n'" |
|
26 |
case '\f' => "$'\\f'" |
|
27 |
case '\r' => "$'\\r'" |
|
28 |
case _ => |
|
67200 | 29 |
if (Symbol.is_ascii_letter(ch) || Symbol.is_ascii_digit(ch) || "+,-./:_".contains(ch)) |
64304 | 30 |
Symbol.ascii(ch) |
31 |
else if (c < 0) "$'\\x" + Integer.toHexString(256 + c) + "'" |
|
32 |
else if (c < 16) "$'\\x0" + Integer.toHexString(c) + "'" |
|
33 |
else if (c < 32 || c >= 127) "$'\\x" + Integer.toHexString(c) + "'" |
|
34 |
else "\\" + ch |
|
35 |
} |
|
36 |
} |
|
37 |
||
38 |
def string(s: String): String = |
|
39 |
if (s == "") "\"\"" |
|
71601 | 40 |
else UTF8.bytes(s).iterator.map(bash_chr).mkString |
64304 | 41 |
|
42 |
def strings(ss: List[String]): String = |
|
71601 | 43 |
ss.iterator.map(Bash.string).mkString(" ") |
64304 | 44 |
|
45 |
||
46 |
/* process and result */ |
|
47 |
||
72598 | 48 |
type Watchdog = (Time, Process => Boolean) |
49 |
||
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
50 |
def process(script: String, |
62573 | 51 |
cwd: JFile = null, |
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
52 |
env: Map[String, String] = Isabelle_System.settings(), |
62602 | 53 |
redirect: Boolean = false, |
54 |
cleanup: () => Unit = () => ()): Process = |
|
55 |
new Process(script, cwd, env, redirect, cleanup) |
|
60991 | 56 |
|
63996 | 57 |
class Process private[Bash]( |
62602 | 58 |
script: String, |
59 |
cwd: JFile, |
|
60 |
env: Map[String, String], |
|
61 |
redirect: Boolean, |
|
62 |
cleanup: () => Unit) |
|
60991 | 63 |
{ |
62604 | 64 |
private val timing_file = Isabelle_System.tmp_file("bash_timing") |
62569 | 65 |
private val timing = Synchronized[Option[Timing]](None) |
65317 | 66 |
def get_timing: Timing = timing.value getOrElse Timing.zero |
62569 | 67 |
|
68 |
private val script_file = Isabelle_System.tmp_file("bash_script") |
|
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
69 |
File.write(script_file, script) |
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
70 |
|
62296 | 71 |
private val proc = |
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
72 |
Isabelle_System.process( |
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
73 |
List(File.platform_path(Path.variable("ISABELLE_BASH_PROCESS")), "-", |
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
74 |
File.standard_path(timing_file), "bash", File.standard_path(script_file)), |
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
75 |
cwd = cwd, env = env, redirect = redirect) |
60991 | 76 |
|
77 |
||
78 |
// channels |
|
79 |
||
80 |
val stdin: BufferedWriter = |
|
81 |
new BufferedWriter(new OutputStreamWriter(proc.getOutputStream, UTF8.charset)) |
|
82 |
||
83 |
val stdout: BufferedReader = |
|
84 |
new BufferedReader(new InputStreamReader(proc.getInputStream, UTF8.charset)) |
|
85 |
||
86 |
val stderr: BufferedReader = |
|
87 |
new BufferedReader(new InputStreamReader(proc.getErrorStream, UTF8.charset)) |
|
88 |
||
89 |
||
90 |
// signals |
|
91 |
||
92 |
private val pid = stdout.readLine |
|
93 |
||
71707 | 94 |
@tailrec private def kill(signal: String, count: Int = 1): Boolean = |
60991 | 95 |
{ |
71706 | 96 |
count <= 0 || |
97 |
{ |
|
71707 | 98 |
Isabelle_System.kill(signal, pid) |
99 |
val running = Isabelle_System.kill("0", pid)._2 == 0 |
|
100 |
if (running) { |
|
71705 | 101 |
Time.seconds(0.1).sleep |
71707 | 102 |
kill(signal, count - 1) |
60991 | 103 |
} |
71706 | 104 |
else false |
60991 | 105 |
} |
106 |
} |
|
107 |
||
71712
c6b7f4da67b3
more robust kill: not always running on Isabelle_Thread (e.g. POSIX_Interrupt handler);
wenzelm
parents:
71708
diff
changeset
|
108 |
def terminate(): Unit = Isabelle_Thread.try_uninterruptible |
62574 | 109 |
{ |
71708 | 110 |
kill("INT", count = 7) && kill("TERM", count = 3) && kill("KILL") |
62574 | 111 |
proc.destroy |
62602 | 112 |
do_cleanup() |
62574 | 113 |
} |
60991 | 114 |
|
71712
c6b7f4da67b3
more robust kill: not always running on Isabelle_Thread (e.g. POSIX_Interrupt handler);
wenzelm
parents:
71708
diff
changeset
|
115 |
def interrupt(): Unit = Isabelle_Thread.try_uninterruptible |
71705 | 116 |
{ |
117 |
Isabelle_System.kill("INT", pid) |
|
118 |
} |
|
119 |
||
60991 | 120 |
|
121 |
// JVM shutdown hook |
|
122 |
||
71712
c6b7f4da67b3
more robust kill: not always running on Isabelle_Thread (e.g. POSIX_Interrupt handler);
wenzelm
parents:
71708
diff
changeset
|
123 |
private val shutdown_hook = Isabelle_Thread.create(() => terminate()) |
60991 | 124 |
|
125 |
try { Runtime.getRuntime.addShutdownHook(shutdown_hook) } |
|
126 |
catch { case _: IllegalStateException => } |
|
127 |
||
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
128 |
|
62574 | 129 |
// cleanup |
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
130 |
|
62602 | 131 |
private def do_cleanup() |
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
132 |
{ |
60991 | 133 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
134 |
catch { case _: IllegalStateException => } |
|
135 |
||
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
136 |
script_file.delete |
60991 | 137 |
|
62569 | 138 |
timing.change { |
139 |
case None => |
|
62574 | 140 |
if (timing_file.isFile) { |
141 |
val t = |
|
142 |
Word.explode(File.read(timing_file)) match { |
|
63805 | 143 |
case List(Value.Long(elapsed), Value.Long(cpu)) => |
62574 | 144 |
Timing(Time.ms(elapsed), Time.ms(cpu), Time.zero) |
145 |
case _ => Timing.zero |
|
146 |
} |
|
147 |
timing_file.delete |
|
148 |
Some(t) |
|
149 |
} |
|
150 |
else Some(Timing.zero) |
|
62569 | 151 |
case some => some |
152 |
} |
|
62602 | 153 |
|
154 |
cleanup() |
|
62574 | 155 |
} |
62569 | 156 |
|
62574 | 157 |
|
158 |
// join |
|
159 |
||
160 |
def join: Int = |
|
161 |
{ |
|
162 |
val rc = proc.waitFor |
|
62602 | 163 |
do_cleanup() |
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
164 |
rc |
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
165 |
} |
62543 | 166 |
|
167 |
||
62574 | 168 |
// result |
62543 | 169 |
|
170 |
def result( |
|
171 |
progress_stdout: String => Unit = (_: String) => (), |
|
172 |
progress_stderr: String => Unit = (_: String) => (), |
|
72598 | 173 |
watchdog: Option[Watchdog] = None, |
62543 | 174 |
strict: Boolean = true): Process_Result = |
175 |
{ |
|
176 |
stdin.close |
|
177 |
||
178 |
val out_lines = |
|
72105 | 179 |
Future.thread("bash_stdout") { File.read_lines(stdout, progress_stdout) } |
62543 | 180 |
val err_lines = |
72105 | 181 |
Future.thread("bash_stderr") { File.read_lines(stderr, progress_stderr) } |
62543 | 182 |
|
72598 | 183 |
val watchdog_thread = |
184 |
for ((time, check) <- watchdog) |
|
185 |
yield { |
|
186 |
Future.thread("bash_watchdog") { |
|
187 |
while (proc.isAlive) { |
|
188 |
time.sleep |
|
189 |
if (check(this)) interrupt() |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
||
62543 | 194 |
val rc = |
195 |
try { join } |
|
62574 | 196 |
catch { case Exn.Interrupt() => terminate(); Exn.Interrupt.return_code } |
72598 | 197 |
|
198 |
watchdog_thread.foreach(_.cancel) |
|
199 |
||
62543 | 200 |
if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt() |
201 |
||
73134
8a8ffe78eee7
clarified return code: re-use SIGALRM for soft timeout;
wenzelm
parents:
72598
diff
changeset
|
202 |
Process_Result(rc, out_lines.join, err_lines.join, get_timing) |
62543 | 203 |
} |
60991 | 204 |
} |
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
205 |
|
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
206 |
|
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
207 |
/* Scala function */ |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
208 |
|
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
209 |
object Process extends Scala.Fun("bash_process") |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
210 |
{ |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
211 |
val here = Scala_Project.here |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
212 |
def apply(script: String): String = |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
213 |
{ |
73263
ad60214bef09
more uniform Bash.process: always ask Isabelle/Scala;
wenzelm
parents:
73230
diff
changeset
|
214 |
val result = Exn.capture { Isabelle_System.bash(script) } |
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
215 |
|
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
216 |
val is_interrupt = |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
217 |
result match { |
73263
ad60214bef09
more uniform Bash.process: always ask Isabelle/Scala;
wenzelm
parents:
73230
diff
changeset
|
218 |
case Exn.Res(res) => res.rc == Exn.Interrupt.return_code |
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
219 |
case Exn.Exn(exn) => Exn.is_interrupt(exn) |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
220 |
} |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
221 |
|
73263
ad60214bef09
more uniform Bash.process: always ask Isabelle/Scala;
wenzelm
parents:
73230
diff
changeset
|
222 |
val encode: XML.Encode.T[Exn.Result[Process_Result]] = |
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
223 |
{ |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
224 |
import XML.Encode._ |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
225 |
val string = XML.Encode.string |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
226 |
variant(List( |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
227 |
{ case _ if is_interrupt => (Nil, Nil) }, |
73230 | 228 |
{ case Exn.Exn(exn) => (Nil, string(Exn.message(exn))) }, |
73263
ad60214bef09
more uniform Bash.process: always ask Isabelle/Scala;
wenzelm
parents:
73230
diff
changeset
|
229 |
{ case Exn.Res(res) => |
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
230 |
val out = Library.terminate_lines(res.out_lines) |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
231 |
val err = Library.terminate_lines(res.err_lines) |
73263
ad60214bef09
more uniform Bash.process: always ask Isabelle/Scala;
wenzelm
parents:
73230
diff
changeset
|
232 |
(List(int_atom(res.rc)), pair(string, string)(out, err)) })) |
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
233 |
} |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
234 |
YXML.string_of_body(encode(result)) |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
235 |
} |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
236 |
} |
60991 | 237 |
} |