package shell

  1. Overview
  2. Docs
Yet another implementation of fork&exec and related functionality

Install

dune-project
 Dependency

Authors

Maintainers

Sources

shell-v0.14.0.tar.gz
sha256=dea47dfd44f8dd736b6ea0394bad5e9302c65c4c7243e73be2e05fe4381aef4f
md5=a91101aef477f2bd563c24f218ae0bd3

doc/src/shell/shell.ml.html

Source file shell.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
(* TODO: Ron wants the ability to run interactive commands and to expose the fd
   version of process handling.*)
open Core
open Poly

module Line_buffer = Shell__line_buffer

let extra_path = Shell_internal.extra_path

module Process = struct

  exception Early_exit [@@deriving sexp]

  type status = [ `Timeout of Time.Span.t | Low_level_process.Status.t ] [@@deriving sexp_of]
  (*  type status = (unit, error) Result.t with sexp_of *)

  type t = {
    program   : string;
    arguments : string list;
  } [@@deriving sexp_of]

  type result = {
    command : t;
    status  : status;
    stdout  : string;
    stderr  : string;
  } [@@deriving sexp_of]

  exception Failed of result [@@deriving sexp]

  let to_string {program=prog; arguments=args} =
    let f s =
      if not (String.contains s ' ') &&
         not (String.contains s '"') then
        s
      else
        sprintf "%S" s
    in
    String.concat ~sep:" " (List.map ~f (prog::args))

  let status_to_string = function
    | `Timeout t -> sprintf !"Timed out (ran for %{Time.Span})" t
    | #Low_level_process.Status.t as s -> Low_level_process.Status.to_string s

  let format_failed c =
    String.concat ~sep:" " ["Command failed:";
                            to_string c.command;
                            "Exit status:";
                            status_to_string c.status;
                            "stderr:";
                            c.stderr]

  let () = Caml.Printexc.register_printer (function
    | Failed r -> Some (format_failed r)
    | _ -> None)

  module Defaults = struct
    let timeout = ref None
    let verbose = ref false
    let echo = ref false
    let preserve_euid = ref false
  end

  let set_defaults ?timeout ?verbose ?echo ?preserve_euid () =
    Option.iter ~f:(fun v -> Defaults.verbose := v) verbose;
    Option.iter ~f:(fun v -> Defaults.timeout := v) timeout;
    Option.iter ~f:(fun v -> Defaults.echo := v) echo;
    Option.iter ~f:(fun v -> Defaults.preserve_euid := v) preserve_euid


  let cmd program arguments = {
    program   = program;
    arguments = arguments;
  }

  let shell s =
    let addtl_args = if !Defaults.preserve_euid then [ "-p" ] else [] in
    {
      program   = "/bin/bash";
      arguments = addtl_args @ [ "-c" ; s ]
    }

  (* avoid asking for the password at all costs. *)
  let noninteractive_ssh_options = ["-o";"BatchMode yes"]
  let noninteractive_no_hostkey_checking_options = [
    "-n"; "-q"; "-x";
    "-o"; "ConnectTimeout=10";
    "-o"; "CheckHostIP=no";
    "-o"; "StrictHostKeyChecking=no";
    "-o"; "BatchMode=yes";
  ]

  (* Passes the remote command to ssh *)

  let make_ssh_command
        ?(ssh_options = noninteractive_ssh_options) ?(quote_args=true)
        ?user ~host args =
    (* quote_args quotes all arguments to the shell.  We need to escape all the
       arguments because ssh is passing this to the remote shell which will
       unescape all of that before passing it over to our program.*)
    let url = match user with
      | None      -> host
      | Some user -> user ^"@"^host
    in
    let args =
      if quote_args then List.map ~f:Filename.quote args
      else args
    in
    { program   = "/usr/bin/ssh";
      arguments = ssh_options @ [url; "--"] @ args;
    }

  let remote ?ssh_options ?quote_args ?user ~host cmd =
    make_ssh_command ?ssh_options ?quote_args ?user ~host
      (cmd.program :: cmd.arguments)

  type 'res acc =
    { add_stdout    : Bytes.t -> int -> [`Stop | `Continue];
      add_stderr    : Bytes.t -> int -> [`Stop | `Continue];
      flush         : unit -> 'res; }

  type 'res reader = unit -> 'res acc

  let run_k' k
        ?use_extra_path
        ?(timeout = !Defaults.timeout)
        ?working_dir ?setuid ?setgid ?env
        ?(verbose = !Defaults.verbose)
        ?(echo = !Defaults.echo)
        ?input
        ?keep_open
        ?tail_len
    =
    k (fun cmd stdoutf stderrf ->
      if echo then
        Console.Ansi.printf [`Underscore] !"Shell: %{}\n%!" cmd;
      let stderrf =
        if verbose then
          (fun s len -> Console.Ansi.output [`Red] stderr s 0 len)
        else stderrf
      and stdoutf =
        if verbose then
          (fun s len ->
             Console.Ansi.output [`Green] stdout s 0 len;
             stdoutf s len)
        else stdoutf
      in
      (Low_level_process.run
         ?timeout ?input ?keep_open ?working_dir
         ?setuid ?setgid ?use_extra_path ?env
         ?tail_len
         ~stdoutf
         ~stderrf
         ~prog:cmd.program
         ~args:cmd.arguments ()))

  let run_k k ?(expect = [0]) = run_k' (fun f ->
    k (fun cmd reader ->
      let acc = reader () in
      let stdoutf s len =
        match acc.add_stdout s len with
        | `Continue -> ()
        | `Stop -> raise Early_exit
      in
      let stderrf s len =
        match acc.add_stderr s len with
        | `Continue -> ()
        | `Stop -> raise Early_exit
      in
      try
        let r = f cmd stdoutf stderrf in
        let module Res = Low_level_process.Command_result in
        match r.Res.status with
        | `Exited i when List.mem expect i ~equal:Int.equal -> acc.flush ()
        | status ->
          raise (Failed
                   { command = cmd;
                     status  = (status :> status);
                     stderr  = r.Res.stderr_tail;
                     stdout  = r.Res.stdout_tail;
                   })
      with Early_exit -> acc.flush ()))


  let run ?expect = run_k (fun f cmd reader -> f cmd reader) ?expect

  let test_k k ?(true_v = [0]) ?(false_v = [1]) = run_k' (fun f ->
    k (fun cmd ->
      let r = f cmd (fun _ _ -> ()) (fun _ _ -> ()) in
      let module Res = Low_level_process.Command_result in
      match r.Res.status with
      | `Exited i when List.mem true_v  i ~equal:Int.equal -> true
      | `Exited i when List.mem false_v i ~equal:Int.equal -> false
      | #status as status ->
        raise (Failed
                 { command = cmd;
                   status  = (status :> status);
                   stderr  = r.Res.stderr_tail;
                   stdout  = r.Res.stdout_tail })))

  let test ?true_v = test_k (fun f cmd -> f cmd) ?true_v

  let discard () = {
    add_stdout = (fun _ _ -> `Continue);
    add_stderr = (fun _ _ -> `Continue);
    flush = (fun () -> ())
  }

  let callback ~add ~flush () = {
    add_stdout = (fun s len -> add s len;`Continue);
    add_stderr = (fun _ _ -> `Continue);
    flush
  }

  let callback_with_stderr ~add ~add_err ~flush () = {
    add_stdout = (fun s len -> add s len;`Continue);
    add_stderr = (fun s len -> add_err s len; `Continue);
    flush
  }

  let content () =
    let buffer = Buffer.create 16 in
    {
      add_stdout = (fun s len -> Buffer.add_subbytes buffer s ~pos:0 ~len; `Continue);
      add_stderr = (fun _ _ -> `Continue);
      flush = (fun () -> Buffer.contents buffer);
    }

  let content_and_stderr () =
    let stdout_buffer = Buffer.create 16 in
    let buffer_stderr = Buffer.create 16 in
    {
      add_stdout = (fun s len -> Buffer.add_subbytes stdout_buffer s ~pos:0 ~len; `Continue);
      add_stderr = (fun s len -> Buffer.add_subbytes buffer_stderr s ~pos:0 ~len; `Continue);
      flush = (fun () ->
        Buffer.contents stdout_buffer,
        Buffer.contents buffer_stderr
      );
    }

  let fold_lines (type ret) (type v)
        ?eol
        ~(init:v)
        ~(f: v -> string -> (v * [`Continue | `Stop]))
        ~(flush:v -> ret)
        () : ret acc
    =
    let acc = ref init
    and continue = ref `Continue in
    let lb =
      Line_buffer.create
        ?eol
        (fun line ->
           match !continue with
           | `Stop -> ()
           | `Continue ->
             let acc_v,continue_v = f !acc line in
             acc := acc_v;
             continue := continue_v)
    in
    { add_stdout =
        (fun s len ->
           Line_buffer.add_subbytes lb s ~pos:0 ~len;
           !continue);
      add_stderr = (fun _ _ -> `Continue);
      flush = (fun () ->
        Line_buffer.flush lb;
        flush !acc) }

  let lines ?eol () =
    fold_lines
      ?eol
      ~flush:List.rev
      ~init:[]
      ~f:(fun acc line -> (line::acc) , `Continue)

  let aux_head ~flush ?eol () =
    fold_lines
      ?eol
      ~flush
      ~init:None
      ~f:(fun _acc line -> Some line,`Stop)

  let head ?eol ()     = aux_head ~flush:(fun x -> x) ?eol ()

  exception Empty_head

  let head_exn ?eol () =
    aux_head ~flush:(function Some x -> x | None -> raise Empty_head) ?eol ()

  let aux_one_line ~flush ?eol () =
    fold_lines
      ?eol
      ~flush:(function
        | Some result -> flush result
        | None -> flush (Or_error.error_s [%message
                           "expected one line, got empty output"]))
      ~init:None
      ~f:(fun acc line -> match acc with
        | Some (Ok first_line) ->
          let second_line = line in
          Some (Or_error.error_s [%message
                  "One line expected, got at least two lines of output"
                    ~first_line
                    ~second_line]), `Stop
        | Some (Error _e) ->
          (* didn't we say `Stop?! *)
          assert false
        | None ->
          Some (Ok line), `Continue)

  let one_line_exn ?eol () = aux_one_line ~flush:Or_error.ok_exn ?eol ()
  let one_line     ?eol () = aux_one_line ~flush:Fn.id           ?eol ()
end

let%test_unit _ =
  [%test_result: string] ~expect:"hello"
    (Process.run
       (Process.cmd "echo" ["hello\nworld"])
       (Process.head_exn ()))

type 'a with_process_flags =
  ?use_extra_path:bool
  -> ?timeout:Time.Span.t option
  -> ?working_dir:string (* rename to run_in? *)
  -> ?setuid:int
  -> ?setgid:int
  -> ?env:[`Extend of (string * string) list
          |`Replace of (string * string) list]
  -> ?verbose:bool
  -> ?echo:bool
  -> ?input:string
  -> ?keep_open:bool
  -> ?tail_len:int
  -> 'a

type 'a with_run_flags =
  (* Defaults to [0]*)
  ?expect:int list -> ('a with_process_flags)

type 'a with_test_flags =
  ?true_v:int list -> ?false_v:int list -> ('a with_process_flags)

type 'a cmd = string -> string list -> 'a

type ('a,'ret) sh_cmd = (('a, unit, string,'ret) format4 -> 'a)

let run_gen reader =
  Process.run_k (fun f prog args -> f (Process.cmd prog args) reader)

let run                     = run_gen  Process.discard
let run_lines ?eol          = run_gen (Process.lines        ?eol ())
let run_one ?eol            = run_gen (Process.head         ?eol ())
let run_one_exn ?eol        = run_gen (Process.head_exn     ?eol ())
let run_first_line ?eol     = run_gen (Process.head         ?eol ())
let run_first_line_exn ?eol = run_gen (Process.head_exn     ?eol ())
let run_one_line ?eol       = run_gen (Process.one_line     ?eol ())
let run_one_line_exn ?eol   = run_gen (Process.one_line_exn ?eol ())
let run_full                = run_gen  Process.content
let run_fold ?eol ~init ~f  = run_gen (Process.fold_lines ?eol ~init ~f ~flush:Fn.id)
(*
   TEST_UNIT =
   (* This should not hand because the stdin is closed... *)
   run ~timeout:(Some (sec 0.5)) "cat" []
   TEST_UNIT =
   try
   run ~timeout:(Some (sec 0.5)) "cat" []
   with Process.
*)

let test =
  Process.test_k (fun f prog args -> f (Process.cmd prog args))

let k_shell_command k f fmt =
  ksprintf (fun command -> k f (Process.shell command)) fmt

let sh_gen reader =
  Process.run_k (k_shell_command (fun f cmd -> f cmd reader))

let sh                ?expect = sh_gen  Process.discard          ?expect
let sh_lines          ?expect = sh_gen (Process.lines ())        ?expect
let sh_full           ?expect = sh_gen  Process.content          ?expect
let sh_one            ?expect = sh_gen (Process.head ())         ?expect
let sh_one_exn        ?expect = sh_gen (Process.head_exn ())     ?expect
let sh_first_line     ?expect = sh_gen (Process.head ())         ?expect
let sh_first_line_exn ?expect = sh_gen (Process.head_exn ())     ?expect
let sh_one_line       ?expect = sh_gen (Process.one_line ())     ?expect
let sh_one_line_exn   ?expect = sh_gen (Process.one_line_exn ()) ?expect

let%test _ =
  sh_lines "yes yes | head -n 200000" =
  List.init 200_000 ~f:(fun _num -> "yes")

let sh_test ?true_v =
  Process.test_k (k_shell_command (fun f cmd -> f cmd)) ?true_v

type 'a with_ssh_flags =
  ?ssh_options:string list
  -> ?user:string -> host:string -> 'a

let noninteractive_ssh_options = Process.noninteractive_ssh_options
let noninteractive_no_hostkey_checking_options =
  Process.noninteractive_no_hostkey_checking_options

let k_remote_command k f ?ssh_options ?user ~host fmt =
  ksprintf (fun command ->
    k f (Process.make_ssh_command ~quote_args:false
           ?ssh_options ?user ~host [command]))
    fmt

let ssh_gen reader ?ssh_options ?user ~host =
  Process.run_k (k_remote_command (fun f cmd -> f cmd reader)
                   ?ssh_options ?user ~host)

let ssh                ?ssh_options = ssh_gen  Process.discard          ?ssh_options
let ssh_lines          ?ssh_options = ssh_gen (Process.lines ())        ?ssh_options
let ssh_full           ?ssh_options = ssh_gen  Process.content          ?ssh_options
let ssh_one            ?ssh_options = ssh_gen (Process.head ())         ?ssh_options
let ssh_one_exn        ?ssh_options = ssh_gen (Process.head_exn ())     ?ssh_options
let ssh_first_line     ?ssh_options = ssh_gen (Process.head ())         ?ssh_options
let ssh_first_line_exn ?ssh_options = ssh_gen (Process.head_exn ())     ?ssh_options
let ssh_one_line       ?ssh_options = ssh_gen (Process.one_line ())     ?ssh_options
let ssh_one_line_exn   ?ssh_options = ssh_gen (Process.one_line_exn ()) ?ssh_options

let ssh_test ?ssh_options ?user ~host =
  Process.test_k (k_remote_command (fun f cmd -> f cmd)
                    ?ssh_options ?user ~host)

let whoami = Shell_internal.whoami
let which = Shell_internal.which

let ln ?s ?f src dst =
  let s = Option.map s ~f:(fun () -> "-s") in
  let f = Option.map f ~f:(fun () -> "-f") in
  run "/bin/ln" (List.filter_map ~f:ident [s; f] @ ["-n"; "--"; src; dst])

let rm ?r ?f path =
  let r = Option.map r ~f:(fun () -> "-r") in
  let f = Option.map f ~f:(fun () -> "-f") in
  run "/bin/rm" (List.filter_map ~f:ident [r; f; Some "--"; Some path])

let mv src dst =
  run "/bin/mv" ["--";src;dst]

let mkdir ?p ?perm path =
  let p = Option.map p ~f:(fun () -> "-p") in
  let mode = Option.map perm ~f:(sprintf "--mode=%o") in
  run "/bin/mkdir" (List.filter_map ~f:ident [p; mode;Some "--";Some path])

(* TODO: Deal with atomicity  *)
let cp ?(overwrite=true) ?perm src dst =
  let perm = match perm with
    | Some p -> p
    | None -> (Unix.lstat src).Unix.st_perm
  in
  let dst = if Sys.is_directory dst = `Yes then
      dst ^/ (Filename.basename src)
    else
      dst
  in
  let out_mode =
    if overwrite then
      [ Unix.O_WRONLY; Unix.O_NOCTTY; Unix.O_CREAT; Unix.O_TRUNC ]
    else
      [ Unix.O_WRONLY; Unix.O_NOCTTY; Unix.O_CREAT; Unix.O_EXCL ]
  in
  protectx (Unix.openfile src ~mode:[ Unix.O_RDONLY; Unix.O_NOCTTY ] ~perm:0)
    ~f:(fun infh ->
      protectx (Unix.openfile dst ~mode:out_mode ~perm)
        ~f:(fun outfh ->
          let buflen = 4096 in
          let buf = Bytes.create buflen in
          let rec loop () =
            let rlen = Unix.read infh ~buf ~pos:0 ~len:buflen in
            if rlen <> 0 then
              let wlen = Unix.write outfh ~buf ~pos:0 ~len:rlen in
              if rlen <> wlen then
                failwithf "Short write: tried to write %d bytes, \
                           only wrote %d bytes" rlen wlen ();
              loop ()
          in
          loop ();
        )
        ~finally:Unix.close
    )
    ~finally:Unix.close
;;

let scp ?(compress=false) ?(recurse=false) ?user ~host f t =
  let user_arg = Option.value_map user ~default:"" ~f:(fun user -> user ^ "@") in
  let args = [f; user_arg ^ host ^ ":" ^ t] in
  let args = if recurse then "-r"::args else args in
  let args = if compress then "-C"::args else args in
  run "scp" args
;;
OCaml

Innovation. Community. Security.