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