| author | wenzelm | 
| Wed, 16 Mar 2016 11:45:25 +0100 | |
| changeset 62634 | aa3b47b32100 | 
| parent 62584 | 6cd36a0d2a28 | 
| child 62850 | 1f1a2c33ccf4 | 
| permissions | -rw-r--r-- | 
| 62584 | 1  | 
(* Title: Pure/System/bash.ML  | 
| 
40748
 
591b6778d076
removed bash from ML system bootstrap, and past the Secure ML barrier;
 
wenzelm 
parents:  
diff
changeset
 | 
2  | 
Author: Makarius  | 
| 
 
591b6778d076
removed bash from ML system bootstrap, and past the Secure ML barrier;
 
wenzelm 
parents:  
diff
changeset
 | 
3  | 
|
| 60976 | 4  | 
GNU bash processes, with propagation of interrupts -- POSIX version.  | 
| 
40748
 
591b6778d076
removed bash from ML system bootstrap, and past the Secure ML barrier;
 
wenzelm 
parents:  
diff
changeset
 | 
5  | 
*)  | 
| 
 
591b6778d076
removed bash from ML system bootstrap, and past the Secure ML barrier;
 
wenzelm 
parents:  
diff
changeset
 | 
6  | 
|
| 44054 | 7  | 
signature BASH =  | 
8  | 
sig  | 
|
| 
47499
 
4b0daca2bf88
redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
 
wenzelm 
parents: 
44112 
diff
changeset
 | 
9  | 
  val process: string -> {out: string, err: string, rc: int, terminate: unit -> unit}
 | 
| 44054 | 10  | 
end;  | 
11  | 
||
12  | 
structure Bash: BASH =  | 
|
| 
43850
 
7f2cbc713344
moved bash operations to Isabelle_System (cf. Scala version);
 
wenzelm 
parents: 
43847 
diff
changeset
 | 
13  | 
struct  | 
| 
 
7f2cbc713344
moved bash operations to Isabelle_System (cf. Scala version);
 
wenzelm 
parents: 
43847 
diff
changeset
 | 
14  | 
|
| 
 
7f2cbc713344
moved bash operations to Isabelle_System (cf. Scala version);
 
wenzelm 
parents: 
43847 
diff
changeset
 | 
15  | 
val process = uninterruptible (fn restore_attributes => fn script =>  | 
| 40749 | 16  | 
let  | 
17  | 
datatype result = Wait | Signal | Result of int;  | 
|
18  | 
val result = Synchronized.var "bash_result" Wait;  | 
|
| 
40748
 
591b6778d076
removed bash from ML system bootstrap, and past the Secure ML barrier;
 
wenzelm 
parents:  
diff
changeset
 | 
19  | 
|
| 40749 | 20  | 
val id = serial_string ();  | 
21  | 
    val script_path = File.tmp_path (Path.basic ("bash_script" ^ id));
 | 
|
| 
47499
 
4b0daca2bf88
redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
 
wenzelm 
parents: 
44112 
diff
changeset
 | 
22  | 
    val out_path = File.tmp_path (Path.basic ("bash_out" ^ id));
 | 
| 
 
4b0daca2bf88
redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
 
wenzelm 
parents: 
44112 
diff
changeset
 | 
23  | 
    val err_path = File.tmp_path (Path.basic ("bash_err" ^ id));
 | 
| 40749 | 24  | 
    val pid_path = File.tmp_path (Path.basic ("bash_pid" ^ id));
 | 
| 
40748
 
591b6778d076
removed bash from ML system bootstrap, and past the Secure ML barrier;
 
wenzelm 
parents:  
diff
changeset
 | 
25  | 
|
| 
54651
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
26  | 
fun cleanup_files () =  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
27  | 
(try File.rm script_path;  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
28  | 
try File.rm out_path;  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
29  | 
try File.rm err_path;  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
30  | 
try File.rm pid_path);  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
31  | 
val _ = cleanup_files ();  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
32  | 
|
| 40749 | 33  | 
val system_thread =  | 
| 61556 | 34  | 
      Standard_Thread.fork {name = "bash", stack_limit = NONE, interrupts = false} (fn () =>
 | 
| 40749 | 35  | 
Multithreading.with_attributes Multithreading.private_interrupts (fn _ =>  | 
36  | 
let  | 
|
37  | 
val _ = File.write script_path script;  | 
|
| 62295 | 38  | 
val _ = getenv_strict "ISABELLE_BASH_PROCESS";  | 
| 40749 | 39  | 
val status =  | 
40  | 
OS.Process.system  | 
|
| 62569 | 41  | 
                ("exec \"$ISABELLE_BASH_PROCESS\" " ^ File.bash_path pid_path ^ " \"\"" ^
 | 
| 
62549
 
9498623b27f0
File.bash_string operations in ML as in Scala -- exclusively for GNU bash, not perl and not user output;
 
wenzelm 
parents: 
62505 
diff
changeset
 | 
42  | 
" bash " ^ File.bash_path script_path ^  | 
| 
 
9498623b27f0
File.bash_string operations in ML as in Scala -- exclusively for GNU bash, not perl and not user output;
 
wenzelm 
parents: 
62505 
diff
changeset
 | 
43  | 
" > " ^ File.bash_path out_path ^  | 
| 
 
9498623b27f0
File.bash_string operations in ML as in Scala -- exclusively for GNU bash, not perl and not user output;
 
wenzelm 
parents: 
62505 
diff
changeset
 | 
44  | 
" 2> " ^ File.bash_path err_path);  | 
| 40749 | 45  | 
val res =  | 
46  | 
(case Posix.Process.fromStatus status of  | 
|
47  | 
Posix.Process.W_EXITED => Result 0  | 
|
48  | 
| Posix.Process.W_EXITSTATUS 0wx82 => Signal  | 
|
49  | 
| Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w)  | 
|
50  | 
| Posix.Process.W_SIGNALED s =>  | 
|
51  | 
if s = Posix.Signal.int then Signal  | 
|
52  | 
else Result (256 + LargeWord.toInt (Posix.Signal.toWord s))  | 
|
53  | 
| Posix.Process.W_STOPPED s =>  | 
|
54  | 
Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));  | 
|
55  | 
in Synchronized.change result (K res) end  | 
|
| 
40783
 
21f7e8d66a39
more conventional exception propagation -- taking into account Simple_Thread.fork wrapping;
 
wenzelm 
parents: 
40750 
diff
changeset
 | 
56  | 
handle exn =>  | 
| 62505 | 57  | 
(Synchronized.change result (fn Wait => Signal | res => res); Exn.reraise exn)));  | 
| 
40748
 
591b6778d076
removed bash from ML system bootstrap, and past the Secure ML barrier;
 
wenzelm 
parents:  
diff
changeset
 | 
58  | 
|
| 
54651
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
59  | 
fun read_pid 0 = NONE  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
60  | 
| read_pid count =  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
61  | 
(case (Int.fromString (File.read pid_path) handle IO.Io _ => NONE) of  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
62  | 
NONE => (OS.Process.sleep (seconds 0.1); read_pid (count - 1))  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
63  | 
| some => some);  | 
| 
43847
 
529159f81d06
more general bash_process, which allows to terminate background processes as well;
 
wenzelm 
parents: 
40896 
diff
changeset
 | 
64  | 
|
| 
54651
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
65  | 
fun terminate NONE = ()  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
66  | 
| terminate (SOME pid) =  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
67  | 
let  | 
| 60976 | 68  | 
fun kill s =  | 
| 
54651
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
69  | 
(Posix.Process.kill  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
70  | 
(Posix.Process.K_GROUP (Posix.Process.wordToPid (LargeWord.fromInt pid)), s); true)  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
71  | 
handle OS.SysErr _ => false;  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
72  | 
|
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
73  | 
fun multi_kill count s =  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
74  | 
count = 0 orelse  | 
| 60976 | 75  | 
(kill s; kill (Posix.Signal.fromWord 0w0)) andalso  | 
76  | 
(OS.Process.sleep (seconds 0.1); multi_kill (count - 1) s);  | 
|
| 
54651
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
77  | 
val _ =  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
78  | 
multi_kill 10 Posix.Signal.int andalso  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
79  | 
multi_kill 10 Posix.Signal.term andalso  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
80  | 
multi_kill 10 Posix.Signal.kill;  | 
| 
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
81  | 
in () end;  | 
| 
40750
 
2064991db2ac
more thorough process termination (cf. Scala version);
 
wenzelm 
parents: 
40749 
diff
changeset
 | 
82  | 
|
| 40749 | 83  | 
fun cleanup () =  | 
| 61556 | 84  | 
(Standard_Thread.interrupt_unsynchronized system_thread;  | 
| 
54651
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
85  | 
cleanup_files ());  | 
| 40749 | 86  | 
in  | 
87  | 
let  | 
|
| 
40748
 
591b6778d076
removed bash from ML system bootstrap, and past the Secure ML barrier;
 
wenzelm 
parents:  
diff
changeset
 | 
88  | 
val _ =  | 
| 40749 | 89  | 
restore_attributes (fn () =>  | 
| 
40750
 
2064991db2ac
more thorough process termination (cf. Scala version);
 
wenzelm 
parents: 
40749 
diff
changeset
 | 
90  | 
Synchronized.guarded_access result (fn Wait => NONE | x => SOME ((), x))) ();  | 
| 
40748
 
591b6778d076
removed bash from ML system bootstrap, and past the Secure ML barrier;
 
wenzelm 
parents:  
diff
changeset
 | 
91  | 
|
| 
47499
 
4b0daca2bf88
redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
 
wenzelm 
parents: 
44112 
diff
changeset
 | 
92  | 
val out = the_default "" (try File.read out_path);  | 
| 
 
4b0daca2bf88
redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
 
wenzelm 
parents: 
44112 
diff
changeset
 | 
93  | 
val err = the_default "" (try File.read err_path);  | 
| 40749 | 94  | 
val rc = (case Synchronized.value result of Signal => Exn.interrupt () | Result rc => rc);  | 
| 
54651
 
d71e7908eec3
more thorough read_pid with extra delay, to give external process a chance to write pid file before attempting to terminate it -- especially relevant for PIDE document processing, where interrupts can happen in the range of milliseconds;
 
wenzelm 
parents: 
47764 
diff
changeset
 | 
95  | 
val pid = read_pid 1;  | 
| 40749 | 96  | 
val _ = cleanup ();  | 
| 
47499
 
4b0daca2bf88
redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
 
wenzelm 
parents: 
44112 
diff
changeset
 | 
97  | 
    in {out = out, err = err, rc = rc, terminate = fn () => terminate pid} end
 | 
| 62505 | 98  | 
handle exn => (terminate (read_pid 10); cleanup (); Exn.reraise exn)  | 
| 40749 | 99  | 
end);  | 
| 
40748
 
591b6778d076
removed bash from ML system bootstrap, and past the Secure ML barrier;
 
wenzelm 
parents:  
diff
changeset
 | 
100  | 
|
| 
43850
 
7f2cbc713344
moved bash operations to Isabelle_System (cf. Scala version);
 
wenzelm 
parents: 
43847 
diff
changeset
 | 
101  | 
end;  | 
| 
 
7f2cbc713344
moved bash operations to Isabelle_System (cf. Scala version);
 
wenzelm 
parents: 
43847 
diff
changeset
 | 
102  |