src/Pure/System/bash.ML
author wenzelm
Sun, 07 Feb 2021 15:32:57 +0100
changeset 73227 5cb4f7107add
parent 71692 f8e52c0152fe
child 73228 0575cfd2ecfc
permissions -rw-r--r--
clarified modules: less redundancy;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62584
6cd36a0d2a28 clarified files;
wenzelm
parents: 62569
diff changeset
     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
62879
4764473c9b8d back to static conditional compilation -- simplified bootstrap;
wenzelm
parents: 62850
diff changeset
     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
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
     7
signature BASH =
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
     8
sig
64304
96bc94c87a81 clarified modules;
wenzelm
parents: 62923
diff changeset
     9
  val string: string -> string
96bc94c87a81 clarified modules;
wenzelm
parents: 62923
diff changeset
    10
  val strings: string list -> string
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
    11
  val process: string -> {out: string, err: string, rc: int, terminate: unit -> unit}
44054
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
    12
end;
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
    13
73227
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    14
structure Bash: sig val terminate: int option -> unit end =
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    15
struct
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    16
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    17
fun terminate NONE = ()
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    18
  | terminate (SOME pid) =
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    19
      let
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    20
        val kill = Kill.kill_group pid;
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    21
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    22
        fun multi_kill count s =
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    23
          count = 0 orelse
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    24
            (kill s; kill Kill.SIGNONE) andalso
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    25
            (OS.Process.sleep (seconds 0.1); multi_kill (count - 1) s);
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    26
        val _ =
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    27
          multi_kill 10 Kill.SIGINT andalso
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    28
          multi_kill 10 Kill.SIGTERM andalso
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    29
          multi_kill 10 Kill.SIGKILL;
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    30
      in () end;
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    31
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    32
end;
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    33
62911
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    34
if ML_System.platform_is_windows then ML
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    35
\<open>
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    36
structure Bash: BASH =
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    37
struct
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    38
73227
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    39
open Bash;
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
    40
64304
96bc94c87a81 clarified modules;
wenzelm
parents: 62923
diff changeset
    41
val string = Bash_Syntax.string;
96bc94c87a81 clarified modules;
wenzelm
parents: 62923
diff changeset
    42
val strings = Bash_Syntax.strings;
96bc94c87a81 clarified modules;
wenzelm
parents: 62923
diff changeset
    43
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents: 62911
diff changeset
    44
val process = Thread_Attributes.uninterruptible (fn restore_attributes => fn script =>
62911
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    45
  let
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    46
    datatype result = Wait | Signal | Result of int;
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    47
    val result = Synchronized.var "bash_result" Wait;
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    48
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    49
    val id = serial_string ();
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    50
    val script_path = File.tmp_path (Path.basic ("bash_script" ^ id));
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    51
    val out_path = File.tmp_path (Path.basic ("bash_out" ^ id));
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    52
    val err_path = File.tmp_path (Path.basic ("bash_err" ^ id));
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    53
    val pid_path = File.tmp_path (Path.basic ("bash_pid" ^ id));
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    54
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    55
    fun cleanup_files () =
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    56
     (try File.rm script_path;
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    57
      try File.rm out_path;
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    58
      try File.rm err_path;
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    59
      try File.rm pid_path);
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    60
    val _ = cleanup_files ();
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    61
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    62
    val system_thread =
71692
f8e52c0152fe clarified names;
wenzelm
parents: 64304
diff changeset
    63
      Isabelle_Thread.fork {name = "bash", stack_limit = NONE, interrupts = false} (fn () =>
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents: 62911
diff changeset
    64
        Thread_Attributes.with_attributes Thread_Attributes.private_interrupts (fn _ =>
62911
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    65
          let
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    66
            val _ = File.write script_path script;
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    67
            val bash_script =
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    68
              "bash " ^ File.bash_path script_path ^
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    69
                " > " ^ File.bash_path out_path ^
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    70
                " 2> " ^ File.bash_path err_path;
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    71
            val bash_process = getenv_strict "ISABELLE_BASH_PROCESS";
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    72
            val rc =
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    73
              Windows.simpleExecute ("",
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    74
                quote (ML_System.platform_path bash_process) ^ " " ^
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    75
                quote (File.platform_path pid_path) ^  " \"\" bash -c " ^ quote bash_script)
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    76
              |> Windows.fromStatus |> SysWord.toInt;
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    77
            val res = if rc = 130 orelse rc = 512 then Signal else Result rc;
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    78
          in Synchronized.change result (K res) end
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    79
          handle exn =>
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    80
            (Synchronized.change result (fn Wait => Signal | res => res); Exn.reraise exn)));
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    81
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    82
    fun read_pid 0 = NONE
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    83
      | read_pid count =
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    84
          (case (Int.fromString (File.read pid_path) handle IO.Io _ => NONE) of
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    85
            NONE => (OS.Process.sleep (seconds 0.1); read_pid (count - 1))
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    86
          | some => some);
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    87
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    88
    fun cleanup () =
71692
f8e52c0152fe clarified names;
wenzelm
parents: 64304
diff changeset
    89
     (Isabelle_Thread.interrupt_unsynchronized system_thread;
62911
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    90
      cleanup_files ());
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    91
  in
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    92
    let
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    93
      val _ =
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    94
        restore_attributes (fn () =>
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    95
          Synchronized.guarded_access result (fn Wait => NONE | x => SOME ((), x))) ();
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    96
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    97
      val out = the_default "" (try File.read out_path);
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    98
      val err = the_default "" (try File.read err_path);
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
    99
      val rc = (case Synchronized.value result of Signal => Exn.interrupt () | Result rc => rc);
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
   100
      val pid = read_pid 1;
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
   101
      val _ = cleanup ();
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
   102
    in {out = out, err = err, rc = rc, terminate = fn () => terminate pid} end
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
   103
    handle exn => (terminate (read_pid 10); cleanup (); Exn.reraise exn)
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
   104
  end);
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
   105
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
   106
end;
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
   107
\<close>
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
   108
else ML
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
   109
\<open>
44054
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
   110
structure Bash: BASH =
43850
7f2cbc713344 moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents: 43847
diff changeset
   111
struct
7f2cbc713344 moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents: 43847
diff changeset
   112
73227
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
   113
open Bash;
5cb4f7107add clarified modules: less redundancy;
wenzelm
parents: 71692
diff changeset
   114
64304
96bc94c87a81 clarified modules;
wenzelm
parents: 62923
diff changeset
   115
val string = Bash_Syntax.string;
96bc94c87a81 clarified modules;
wenzelm
parents: 62923
diff changeset
   116
val strings = Bash_Syntax.strings;
96bc94c87a81 clarified modules;
wenzelm
parents: 62923
diff changeset
   117
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents: 62911
diff changeset
   118
val process = Thread_Attributes.uninterruptible (fn restore_attributes => fn script =>
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   119
  let
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   120
    datatype result = Wait | Signal | Result of int;
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   121
    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
   122
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   123
    val id = serial_string ();
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   124
    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
   125
    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
   126
    val err_path = File.tmp_path (Path.basic ("bash_err" ^ id));
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   127
    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
   128
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
   129
    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
   130
     (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
   131
      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
   132
      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
   133
      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
   134
    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
   135
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   136
    val system_thread =
71692
f8e52c0152fe clarified names;
wenzelm
parents: 64304
diff changeset
   137
      Isabelle_Thread.fork {name = "bash", stack_limit = NONE, interrupts = false} (fn () =>
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents: 62911
diff changeset
   138
        Thread_Attributes.with_attributes Thread_Attributes.private_interrupts (fn _ =>
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   139
          let
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   140
            val _ = File.write script_path script;
62295
4f2fb9adfae5 clarified bash process;
wenzelm
parents: 61556
diff changeset
   141
            val _ = getenv_strict "ISABELLE_BASH_PROCESS";
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   142
            val status =
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   143
              OS.Process.system
62569
5db10482f4cf bash process with builtin timing;
wenzelm
parents: 62549
diff changeset
   144
                ("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
   145
                  " 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
   146
                  " > " ^ 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
   147
                  " 2> " ^ File.bash_path err_path);
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   148
            val res =
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   149
              (case Posix.Process.fromStatus status of
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   150
                Posix.Process.W_EXITED => Result 0
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   151
              | Posix.Process.W_EXITSTATUS 0wx82 => Signal
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   152
              | Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w)
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   153
              | Posix.Process.W_SIGNALED s =>
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   154
                  if s = Posix.Signal.int then Signal
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   155
                  else Result (256 + LargeWord.toInt (Posix.Signal.toWord s))
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   156
              | Posix.Process.W_STOPPED s =>
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   157
                  Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   158
          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
   159
          handle exn =>
62505
9e2a65912111 clarified modules;
wenzelm
parents: 62483
diff changeset
   160
            (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
   161
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
   162
    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
   163
      | 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
   164
          (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
   165
            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
   166
          | some => some);
43847
529159f81d06 more general bash_process, which allows to terminate background processes as well;
wenzelm
parents: 40896
diff changeset
   167
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   168
    fun cleanup () =
71692
f8e52c0152fe clarified names;
wenzelm
parents: 64304
diff changeset
   169
     (Isabelle_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
   170
      cleanup_files ());
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   171
  in
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   172
    let
40748
591b6778d076 removed bash from ML system bootstrap, and past the Secure ML barrier;
wenzelm
parents:
diff changeset
   173
      val _ =
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   174
        restore_attributes (fn () =>
40750
2064991db2ac more thorough process termination (cf. Scala version);
wenzelm
parents: 40749
diff changeset
   175
          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
   176
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
   177
      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
   178
      val err = the_default "" (try File.read err_path);
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   179
      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
   180
      val pid = read_pid 1;
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   181
      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
   182
    in {out = out, err = err, rc = rc, terminate = fn () => terminate pid} end
62505
9e2a65912111 clarified modules;
wenzelm
parents: 62483
diff changeset
   183
    handle exn => (terminate (read_pid 10); cleanup (); Exn.reraise exn)
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
   184
  end);
40748
591b6778d076 removed bash from ML system bootstrap, and past the Secure ML barrier;
wenzelm
parents:
diff changeset
   185
43850
7f2cbc713344 moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents: 43847
diff changeset
   186
end;
62911
78e03d8bf1c4 back to dynamic conditional compilation (reverting 4764473c9b8d) via recursive ML name space;
wenzelm
parents: 62891
diff changeset
   187
\<close>