From 41ce4614d46ca432384ef16e16f72e77067811c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 26 May 2020 03:00:13 +0200 Subject: [PATCH] Handle interrupts --- github-fast-env.rb | 67 +++++++++++++++++++++++++++++++++++++++++++-- github-fast-envd.rb | 8 ++++-- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/github-fast-env.rb b/github-fast-env.rb index 8eb7957..fad8d77 100755 --- a/github-fast-env.rb +++ b/github-fast-env.rb @@ -31,7 +31,6 @@ if ARGV.length > 1 end # TODO: support arguments -# TODO: forward signals script_path = File.realpath(ARGV[0]) control_socket_path = "/tmp/github-fast-envd.sock" @@ -48,6 +47,62 @@ def log(level, message) end end +$remote_process_id = nil + +Signal.trap("HUP") do + if $remote_process_id + Process.kill("HUP", $remote_process_id) + else + Signal.trap("HUP", "DEFAULT") + Process.kill("HUP", 0) + end +end + +Signal.trap("INT") do + if $remote_process_id + Process.kill("INT", $remote_process_id) + else + Signal.trap("INT", "DEFAULT") + Process.kill("INT", 0) + end +end + +Signal.trap("QUIT") do + if $remote_process_id + Process.kill("QUIT", $remote_process_id) + else + Signal.trap("QUIT", "DEFAULT") + Process.kill("QUIT", 0) + end +end + +Signal.trap("TERM") do + if $remote_process_id + Process.kill("TERM", $remote_process_id) + else + Signal.trap("TERM", "DEFAULT") + Process.kill("TERM", 0) + end +end + +Signal.trap("TSTP") do + if $remote_process_id + Process.kill("TSTP", $remote_process_id) + end + + Signal.trap("TSTP", "DEFAULT") + Process.kill("TSTP", 0) +end + +Signal.trap("CONT") do + if $remote_process_id + Process.kill("CONT", $remote_process_id) + end + + Signal.trap("CONT", "DEFAULT") + Process.kill("CONT", 0) +end + def read_command ready_ios = IO.select([$control_socket], [], [], 10) @@ -114,7 +169,15 @@ pipes = {"stdin" => nil, "stdout" => nil, "stderr" => nil} while true command, arguments = read_command - if command == "ready" + if command == "pid" + if arguments.empty? + log "error", "malformed response" + exit 1 + end + + $remote_process_id = arguments[0].to_i + log "trace", "remote process ID: #{$remote_process_id}" + elsif command == "ready" break elsif command == "named-pipes" if arguments.empty? diff --git a/github-fast-envd.rb b/github-fast-envd.rb index 9002273..6497ca1 100755 --- a/github-fast-envd.rb +++ b/github-fast-envd.rb @@ -177,6 +177,9 @@ while true connection_id += 1 child_process = fork { + process_id = Process.pid + control_socket.puts "pid #{process_id}" + exit_code = "unknown" if mode == "named-pipes" @@ -185,7 +188,7 @@ while true set_up_pseudoterminal(control_socket, pseudoterminal_path) end - $original_stderr.puts " executing script " + script_path + $original_stderr.puts " executing script #{script_path} (#{process_id})" begin begin @@ -224,13 +227,12 @@ while true end begin - # TODO: which exit code to return when interrupted? control_socket.puts "done #{exit_code}" rescue end control_socket.close - $original_stderr.puts " finished handling request" + $original_stderr.puts " finished handling request (#{process_id})" Kernel.exit! }