Skip to content

Commit

Permalink
Add command line argument to ignore all unix based exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
fxfactorial committed May 11, 2016
1 parent 377e3af commit 36d9a19
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/app/gandalf_args.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ let be_verbose =
in
Arg.(value & flag & info ["v"; "verbose"] ~doc)

let ignore_all_unix_errors =
let doc = "Ignore all Unix exceptions, most likely raised by Usbmuxd. \
Warning: this may hide errors"
in
Arg.(value & flag & info ["ignore_all_unix_exceptions"] ~doc)

let forward_connection_file =
let doc =
"A JSON based mapping from udid to local ports to forward, \
Expand Down
18 changes: 13 additions & 5 deletions src/app/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ let begin_program
log_async_exn
log_plugged_inout
log_everything_else
stats_server =
stats_server
ignore_unix_exn_ =
let starting_place = Sys.getcwd () in
if do_daemonize then begin
(* This order matters, must get this done before anything Lwt
Expand Down Expand Up @@ -148,7 +149,11 @@ let begin_program
in
Usbmux.Logging.(
let relay_with =
R.make_tunnels ?stats_server ?tunnel_timeout ~device_map
R.make_tunnels
~ignore_unix_exn:ignore_unix_exn_
?stats_server
?tunnel_timeout
~device_map
in
if very_loud
then relay_with
Expand All @@ -175,7 +180,7 @@ let begin_program
| e ->
Printexc.to_string e
|> Lwt_io.printlf "Error: Please report: Unknown exception: %s" >>
exit 9
exit 10
))

let entry_point = let open Gandalf_args in
Expand All @@ -192,7 +197,8 @@ let entry_point = let open Gandalf_args in
$ log_async_exceptions
$ log_plugged_action
$ log_everything_else
$ status_server_port)
$ status_server_port
$ ignore_all_unix_errors)

let top_level_info =
let doc = "Control TCP forwarding for iDevices" in
Expand Down Expand Up @@ -243,7 +249,9 @@ let top_level_info =
`P "6 -> Check if $(b,$(tname)) is even running";
`P "7 -> Check if usbmuxd is running";
`P "8 -> Error in mapping file, check your json";
`P "9 -> Unknown reason, please report";
`P "9 -> Some kind of Unix error, most likely caused by usbmuxd, \
check misc section logs";
`P "10 -> Unknown reason, please report";
`S "AUTHOR";
`P "Edgar Aroutiounian <edgar.factorial@gmail.com>"]
in
Expand Down
17 changes: 13 additions & 4 deletions src/lib/usbmux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ module Relay = struct
relay_timeout,
lazy_exceptions,
tunnels_created,
tunnel_timeouts) =
Hashtbl.create 24, ref "", ref None, ref 0, ref 0, ref 0
tunnel_timeouts,
unix_exn_exit_program) =
Hashtbl.create 24, ref "", ref None, ref 0, ref 0, ref 0, ref false

let status_server port =
P.sprintf "http://127.0.0.1:%d" port
Expand Down Expand Up @@ -215,8 +216,8 @@ module Relay = struct
(fun () ->
Lwt.pick
(* Either get data off the stream or timeout after a period
of time, we don't want to keep relays open that have no
activity on them *)
of time, we don't want to keep relays open that have no
activity on them *)
[Lwt_stream.get stream; timeout_task ~after_timeout read_timeout])
|> Lwt_stream.from

Expand Down Expand Up @@ -352,6 +353,9 @@ module Relay = struct
lazy_exceptions := !lazy_exceptions + 1;
"(Safe to ignore) OCaml lazy value exception from TCP tunneling"
|> log `misc
| Unix_error(e, _, _) ->
error_message e |> P.sprintf "Unix based error: %s" |> log `misc;
if !unix_exn_exit_program then exit 9
| exn ->
"Please report, this is an unhandled async exception (A bug)"
|> log (`exn exn);
Expand Down Expand Up @@ -455,6 +459,7 @@ module Relay = struct
|> Lwt.async

let rec make_tunnels
?(ignore_unix_exn=false)
?(log_opts=(!Logging.logging_opts))
?stats_server
?tunnel_timeout
Expand All @@ -467,6 +472,9 @@ module Relay = struct
else Lwt.return_unit)
|> Lwt_main.at_exit;

(* Should Unix exceptions exit the program? *)
unix_exn_exit_program := ignore_unix_exn;

(* Set the logging options *)
Logging.logging_opts := log_opts;
(* Ask for larger internal buffers for Lwt_io function rather than
Expand Down Expand Up @@ -531,6 +539,7 @@ module Relay = struct
(* Spin it up again *)
make_tunnels
(* Use existing status server *)
~ignore_unix_exn:!unix_exn_exit_program
~log_opts:!Logging.logging_opts
?stats_server:None
?tunnel_timeout:None
Expand Down
1 change: 1 addition & 0 deletions src/lib/usbmux.mli
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ module Relay : sig
creates the tunnels requested for in the {b device_map} and creates a
HTTP status server that is accessible at localport 5000. *)
val make_tunnels :
?ignore_unix_exn:bool ->
?log_opts:Logging.log_opts ->
?stats_server:int ->
?tunnel_timeout:int ->
Expand Down

0 comments on commit 36d9a19

Please sign in to comment.