author | wenzelm |
Wed, 09 Mar 2016 19:30:09 +0100 | |
changeset 62573 | 27f90319a499 |
parent 62569 | 5db10482f4cf |
child 62574 | ec382bc689e5 |
permissions | -rw-r--r-- |
60991 | 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 |
{ |
|
62543 | 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 |
||
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
|
29 |
def process(script: String, |
62573 | 30 |
cwd: JFile = null, |
31 |
env: Map[String, String] = Map.empty, |
|
32 |
redirect: Boolean = false): Process = |
|
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
|
33 |
new Process(script, cwd, env, redirect) |
60991 | 34 |
|
35 |
class Process private [Bash]( |
|
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
|
36 |
script: String, cwd: JFile, env: Map[String, String], redirect: Boolean) |
60991 | 37 |
extends Prover.System_Process |
38 |
{ |
|
62569 | 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") |
|
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
|
43 |
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
|
44 |
|
62296 | 45 |
private val proc = |
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
|
46 |
Isabelle_System.process(cwd, Isabelle_System.settings(env), redirect, |
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
|
47 |
File.platform_path(Path.variable("ISABELLE_BASH_PROCESS")), "-", |
62569 | 48 |
File.standard_path(timing_file), "bash", File.standard_path(script_file)) |
60991 | 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 |
||
62558
c46418f12ee1
clarified process interrupt: exactly one signal (like thread interrupt);
wenzelm
parents:
62545
diff
changeset
|
67 |
def interrupt() |
c46418f12ee1
clarified process interrupt: exactly one signal (like thread interrupt);
wenzelm
parents:
62545
diff
changeset
|
68 |
{ Exn.Interrupt.postpone { Isabelle_System.kill("INT", pid) } } |
c46418f12ee1
clarified process interrupt: exactly one signal (like thread interrupt);
wenzelm
parents:
62545
diff
changeset
|
69 |
|
60991 | 70 |
private def kill(signal: String): Boolean = |
61025 | 71 |
Exn.Interrupt.postpone { |
72 |
Isabelle_System.kill(signal, pid) |
|
73 |
Isabelle_System.kill("0", pid)._2 == 0 } getOrElse true |
|
60991 | 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() { multi_kill("INT") && multi_kill("TERM") && kill("KILL"); proc.destroy } |
|
92 |
||
93 |
||
94 |
// JVM shutdown hook |
|
95 |
||
96 |
private val shutdown_hook = new Thread { override def run = terminate() } |
|
97 |
||
98 |
try { Runtime.getRuntime.addShutdownHook(shutdown_hook) } |
|
99 |
catch { case _: IllegalStateException => } |
|
100 |
||
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
|
101 |
|
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
|
102 |
/* join */ |
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
|
103 |
|
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
|
104 |
def join: Int = |
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
|
105 |
{ |
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
|
106 |
val rc = proc.waitFor |
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
|
107 |
|
60991 | 108 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
109 |
catch { case _: IllegalStateException => } |
|
110 |
||
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
|
111 |
script_file.delete |
60991 | 112 |
|
62569 | 113 |
timing.change { |
114 |
case None => |
|
115 |
val t = |
|
116 |
Word.explode(File.read(timing_file)) match { |
|
117 |
case List(Properties.Value.Long(elapsed), Properties.Value.Long(cpu)) => |
|
118 |
Timing(Time.ms(elapsed), Time.ms(cpu), Time.zero) |
|
119 |
case _ => Timing.zero |
|
120 |
} |
|
121 |
timing_file.delete |
|
122 |
Some(t) |
|
123 |
case some => some |
|
124 |
} |
|
125 |
||
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
|
126 |
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
|
127 |
} |
62543 | 128 |
|
129 |
||
130 |
/* result */ |
|
131 |
||
132 |
def result( |
|
133 |
progress_stdout: String => Unit = (_: String) => (), |
|
134 |
progress_stderr: String => Unit = (_: String) => (), |
|
135 |
progress_limit: Option[Long] = None, |
|
136 |
strict: Boolean = true): Process_Result = |
|
137 |
{ |
|
138 |
stdin.close |
|
139 |
||
140 |
val limited = new Limited_Progress(this, progress_limit) |
|
141 |
val out_lines = |
|
142 |
Future.thread("bash_stdout") { File.read_lines(stdout, limited(progress_stdout)) } |
|
143 |
val err_lines = |
|
144 |
Future.thread("bash_stderr") { File.read_lines(stderr, limited(progress_stderr)) } |
|
145 |
||
146 |
val rc = |
|
147 |
try { join } |
|
148 |
catch { case Exn.Interrupt() => terminate; Exn.Interrupt.return_code } |
|
149 |
if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt() |
|
150 |
||
62569 | 151 |
Process_Result(rc, out_lines.join, err_lines.join, false, timing.value.get) |
62543 | 152 |
} |
60991 | 153 |
} |
154 |
} |