author | wenzelm |
Sat, 07 Aug 2021 21:25:47 +0200 | |
changeset 74141 | bba35ad317ab |
parent 74067 | 0b1462ce5fda |
child 74142 | 0f051404f487 |
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 |
||
73897 | 10 |
import java.util.{LinkedList, List => JList, Map => JMap} |
73895 | 11 |
import java.io.{BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter, File => JFile} |
71706 | 12 |
import scala.annotation.tailrec |
73602
37243ad3ecb6
fast approximation of test for process group (NB: initial process might already be terminated, while background processes are still running);
wenzelm
parents:
73601
diff
changeset
|
13 |
import scala.jdk.OptionConverters._ |
71706 | 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, |
73897 | 52 |
env: JMap[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, |
|
73897 | 60 |
env: JMap[String, String], |
62602 | 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 |
|
73603
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
68 |
private val winpid_file: Option[JFile] = |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
69 |
if (Platform.is_windows) Some(Isabelle_System.tmp_file("bash_winpid")) else None |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
70 |
private val winpid_script = |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
71 |
winpid_file match { |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
72 |
case None => script |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
73 |
case Some(file) => |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
74 |
"read < /proc/self/winpid > /dev/null 2> /dev/null\n" + |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
75 |
"""echo -n "$REPLY" > """ + File.bash_path(file) + "\n\n" + script |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
76 |
} |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
77 |
|
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
78 |
private val script_file: JFile = Isabelle_System.tmp_file("bash_script") |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
79 |
File.write(script_file, winpid_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
|
80 |
|
62296 | 81 |
private val proc = |
73906 | 82 |
isabelle.setup.Environment.process_builder( |
73895 | 83 |
JList.of(File.platform_path(Path.variable("ISABELLE_BASH_PROCESS")), |
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
84 |
File.standard_path(timing_file), "bash", File.standard_path(script_file)), |
73906 | 85 |
cwd, env, redirect).start() |
60991 | 86 |
|
87 |
||
88 |
// channels |
|
89 |
||
90 |
val stdin: BufferedWriter = |
|
91 |
new BufferedWriter(new OutputStreamWriter(proc.getOutputStream, UTF8.charset)) |
|
92 |
||
93 |
val stdout: BufferedReader = |
|
94 |
new BufferedReader(new InputStreamReader(proc.getInputStream, UTF8.charset)) |
|
95 |
||
96 |
val stderr: BufferedReader = |
|
97 |
new BufferedReader(new InputStreamReader(proc.getErrorStream, UTF8.charset)) |
|
98 |
||
99 |
||
100 |
// signals |
|
101 |
||
73601 | 102 |
private val group_pid = stdout.readLine |
60991 | 103 |
|
73603
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
104 |
private def process_alive(pid: String): Boolean = |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
105 |
(for { |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
106 |
p <- Value.Long.unapply(pid) |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
107 |
handle <- ProcessHandle.of(p).toScala |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
108 |
} yield handle.isAlive) getOrElse false |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
109 |
|
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
110 |
private def root_process_alive(): Boolean = |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
111 |
winpid_file match { |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
112 |
case None => process_alive(group_pid) |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
113 |
case Some(file) => |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
114 |
file.exists() && process_alive(Library.trim_line(File.read(file))) |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
115 |
} |
73602
37243ad3ecb6
fast approximation of test for process group (NB: initial process might already be terminated, while background processes are still running);
wenzelm
parents:
73601
diff
changeset
|
116 |
|
73601 | 117 |
@tailrec private def signal(s: String, count: Int = 1): Boolean = |
60991 | 118 |
{ |
71706 | 119 |
count <= 0 || |
120 |
{ |
|
73911 | 121 |
isabelle.setup.Environment.kill_process(group_pid, s) |
122 |
val running = |
|
123 |
root_process_alive() || |
|
124 |
isabelle.setup.Environment.kill_process(group_pid, "0") |
|
71707 | 125 |
if (running) { |
73702
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents:
73604
diff
changeset
|
126 |
Time.seconds(0.1).sleep() |
73601 | 127 |
signal(s, count - 1) |
60991 | 128 |
} |
71706 | 129 |
else false |
60991 | 130 |
} |
131 |
} |
|
132 |
||
71712
c6b7f4da67b3
more robust kill: not always running on Isabelle_Thread (e.g. POSIX_Interrupt handler);
wenzelm
parents:
71708
diff
changeset
|
133 |
def terminate(): Unit = Isabelle_Thread.try_uninterruptible |
62574 | 134 |
{ |
73601 | 135 |
signal("INT", count = 7) && signal("TERM", count = 3) && signal("KILL") |
73367 | 136 |
proc.destroy() |
62602 | 137 |
do_cleanup() |
62574 | 138 |
} |
60991 | 139 |
|
71712
c6b7f4da67b3
more robust kill: not always running on Isabelle_Thread (e.g. POSIX_Interrupt handler);
wenzelm
parents:
71708
diff
changeset
|
140 |
def interrupt(): Unit = Isabelle_Thread.try_uninterruptible |
71705 | 141 |
{ |
73911 | 142 |
isabelle.setup.Environment.kill_process(group_pid, "INT") |
71705 | 143 |
} |
144 |
||
60991 | 145 |
|
146 |
// JVM shutdown hook |
|
147 |
||
71712
c6b7f4da67b3
more robust kill: not always running on Isabelle_Thread (e.g. POSIX_Interrupt handler);
wenzelm
parents:
71708
diff
changeset
|
148 |
private val shutdown_hook = Isabelle_Thread.create(() => terminate()) |
60991 | 149 |
|
150 |
try { Runtime.getRuntime.addShutdownHook(shutdown_hook) } |
|
151 |
catch { case _: IllegalStateException => } |
|
152 |
||
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
|
153 |
|
62574 | 154 |
// 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
|
155 |
|
73340 | 156 |
private def do_cleanup(): Unit = |
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
|
157 |
{ |
60991 | 158 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
159 |
catch { case _: IllegalStateException => } |
|
160 |
||
73603
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
161 |
script_file.delete() |
342362c9496c
clarified check of root process on Windows (NB: the winpid is less stable than the Cygwin/Posix pid, so it needs to be "patched" into the the bash script, instead of bash_process.c);
wenzelm
parents:
73602
diff
changeset
|
162 |
winpid_file.foreach(_.delete()) |
60991 | 163 |
|
62569 | 164 |
timing.change { |
165 |
case None => |
|
62574 | 166 |
if (timing_file.isFile) { |
167 |
val t = |
|
168 |
Word.explode(File.read(timing_file)) match { |
|
63805 | 169 |
case List(Value.Long(elapsed), Value.Long(cpu)) => |
62574 | 170 |
Timing(Time.ms(elapsed), Time.ms(cpu), Time.zero) |
171 |
case _ => Timing.zero |
|
172 |
} |
|
173 |
timing_file.delete |
|
174 |
Some(t) |
|
175 |
} |
|
176 |
else Some(Timing.zero) |
|
62569 | 177 |
case some => some |
178 |
} |
|
62602 | 179 |
|
180 |
cleanup() |
|
62574 | 181 |
} |
62569 | 182 |
|
62574 | 183 |
|
184 |
// join |
|
185 |
||
74141 | 186 |
def join(): Int = |
62574 | 187 |
{ |
74141 | 188 |
val rc = proc.waitFor() |
62602 | 189 |
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
|
190 |
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
|
191 |
} |
62543 | 192 |
|
193 |
||
62574 | 194 |
// result |
62543 | 195 |
|
196 |
def result( |
|
197 |
progress_stdout: String => Unit = (_: String) => (), |
|
198 |
progress_stderr: String => Unit = (_: String) => (), |
|
72598 | 199 |
watchdog: Option[Watchdog] = None, |
62543 | 200 |
strict: Boolean = true): Process_Result = |
201 |
{ |
|
73367 | 202 |
stdin.close() |
62543 | 203 |
|
204 |
val out_lines = |
|
72105 | 205 |
Future.thread("bash_stdout") { File.read_lines(stdout, progress_stdout) } |
62543 | 206 |
val err_lines = |
72105 | 207 |
Future.thread("bash_stderr") { File.read_lines(stderr, progress_stderr) } |
62543 | 208 |
|
72598 | 209 |
val watchdog_thread = |
210 |
for ((time, check) <- watchdog) |
|
211 |
yield { |
|
212 |
Future.thread("bash_watchdog") { |
|
213 |
while (proc.isAlive) { |
|
73702
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents:
73604
diff
changeset
|
214 |
time.sleep() |
72598 | 215 |
if (check(this)) interrupt() |
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
||
62543 | 220 |
val rc = |
74141 | 221 |
try { join() } |
74067 | 222 |
catch { case Exn.Interrupt() => terminate(); Process_Result.interrupt_rc } |
72598 | 223 |
|
73367 | 224 |
watchdog_thread.foreach(_.cancel()) |
72598 | 225 |
|
74067 | 226 |
if (strict && rc == Process_Result.interrupt_rc) throw Exn.Interrupt() |
62543 | 227 |
|
73134
8a8ffe78eee7
clarified return code: re-use SIGALRM for soft timeout;
wenzelm
parents:
72598
diff
changeset
|
228 |
Process_Result(rc, out_lines.join, err_lines.join, get_timing) |
62543 | 229 |
} |
60991 | 230 |
} |
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
231 |
|
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
232 |
|
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
233 |
/* 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
|
234 |
|
73565 | 235 |
object Process extends Scala.Fun_Strings("bash_process", thread = true) |
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
236 |
{ |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
237 |
val here = Scala_Project.here |
73565 | 238 |
def apply(args: List[String]): List[String] = |
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
239 |
{ |
73604 | 240 |
val result = |
241 |
Exn.capture { |
|
242 |
val redirect = args.head == "true" |
|
243 |
val script = cat_lines(args.tail) |
|
244 |
Isabelle_System.bash(script, redirect = redirect) |
|
245 |
} |
|
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
246 |
|
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
247 |
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
|
248 |
result match { |
74067 | 249 |
case Exn.Res(res) => res.rc == Process_Result.interrupt_rc |
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
250 |
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
|
251 |
} |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
252 |
|
73268
c8abfe393c16
more accurate process_result in ML, corresponding to Process_Result in Scala;
wenzelm
parents:
73265
diff
changeset
|
253 |
result match { |
73565 | 254 |
case _ if is_interrupt => Nil |
255 |
case Exn.Exn(exn) => List(Exn.message(exn)) |
|
73268
c8abfe393c16
more accurate process_result in ML, corresponding to Process_Result in Scala;
wenzelm
parents:
73265
diff
changeset
|
256 |
case Exn.Res(res) => |
73565 | 257 |
res.rc.toString :: |
258 |
res.timing.elapsed.ms.toString :: |
|
259 |
res.timing.cpu.ms.toString :: |
|
260 |
res.out_lines.length.toString :: |
|
261 |
res.out_lines ::: res.err_lines |
|
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
262 |
} |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
263 |
} |
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73134
diff
changeset
|
264 |
} |
60991 | 265 |
} |