src/Pure/Concurrent/bash.ML
author wenzelm
Wed, 09 Mar 2016 14:54:51 +0100
changeset 62569 5db10482f4cf
parent 62549 9498623b27f0
permissions -rw-r--r--
bash process with builtin timing;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40748
591b6778d076 removed bash from ML system bootstrap, and past the Secure ML barrier;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Concurrent/bash.ML
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
wenzelm
parents: 60764
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
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
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
    10
end;
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
    11
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
    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
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    16
  let
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    17
    datatype result = Wait | Signal | Result of int;
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    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
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    20
    val id = serial_string ();
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    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
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    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
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    33
    val system_thread =
61556
0d4ee4168e41 clarified modules;
wenzelm
parents: 60976
diff changeset
    34
      Standard_Thread.fork {name = "bash", stack_limit = NONE, interrupts = false} (fn () =>
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    35
        Multithreading.with_attributes Multithreading.private_interrupts (fn _ =>
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    36
          let
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    37
            val _ = File.write script_path script;
62295
4f2fb9adfae5 clarified bash process;
wenzelm
parents: 61556
diff changeset
    38
            val _ = getenv_strict "ISABELLE_BASH_PROCESS";
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    39
            val status =
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    40
              OS.Process.system
62569
5db10482f4cf bash process with builtin timing;
wenzelm
parents: 62549
diff changeset
    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
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    45
            val res =
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    46
              (case Posix.Process.fromStatus status of
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    47
                Posix.Process.W_EXITED => Result 0
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    48
              | Posix.Process.W_EXITSTATUS 0wx82 => Signal
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    49
              | Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w)
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    50
              | Posix.Process.W_SIGNALED s =>
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    51
                  if s = Posix.Signal.int then Signal
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    52
                  else Result (256 + LargeWord.toInt (Posix.Signal.toWord s))
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    53
              | Posix.Process.W_STOPPED s =>
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    54
                  Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    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
9e2a65912111 clarified modules;
wenzelm
parents: 62483
diff changeset
    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
wenzelm
parents: 60764
diff changeset
    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
wenzelm
parents: 60764
diff changeset
    75
                (kill s; kill (Posix.Signal.fromWord 0w0)) andalso
wenzelm
parents: 60764
diff changeset
    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
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    83
    fun cleanup () =
61556
0d4ee4168e41 clarified modules;
wenzelm
parents: 60976
diff changeset
    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
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    86
  in
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    87
    let
40748
591b6778d076 removed bash from ML system bootstrap, and past the Secure ML barrier;
wenzelm
parents:
diff changeset
    88
      val _ =
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    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
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    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
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    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
9e2a65912111 clarified modules;
wenzelm
parents: 62483
diff changeset
    98
    handle exn => (terminate (read_pid 10); cleanup (); Exn.reraise exn)
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    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