Refactoring to support pseudoterminal mode

This commit is contained in:
Patrick Lühne 2020-05-25 16:28:59 +02:00
parent d6da017a76
commit a42417aed3
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
2 changed files with 56 additions and 39 deletions

View File

@ -89,7 +89,7 @@ end
log "info", "connected to control socket"
encoded_script_path = Base64.encode64(script_path).delete("\n")
$control_socket.puts "new v1 #{encoded_script_path}"
$control_socket.puts "new v1 named-pipes #{encoded_script_path}"
pipes = {"stdin" => nil, "stdout" => nil, "stderr" => nil}

View File

@ -3,7 +3,7 @@
require "base64"
require "socket"
require "/data/github/current/config/environment"
#require "/data/github/current/config/environment"
class ClientError < StandardError
def initialize(message)
@ -85,39 +85,10 @@ def read_command(control_socket)
return command, arguments
end
while true
control_socket = control_server.accept
$stderr.puts "- new connection"
begin
command, arguments = read_command(control_socket)
if command != "new"
raise ClientError.new "unexpected command"
end
if arguments.length != 2
raise ClientError.new "malformed command"
end
if arguments[0] != "v1"
raise ClientError.new "unsupported protocol version"
end
connection_id += 1
child_process = fork {
original_stdin = $stdin.dup
original_stdout = $stdout.dup
original_stderr = $stderr.dup
exit_code = "unknown"
begin
def set_up_named_pipes(control_socket, connection_id)
stdin = open_pipe(connection_id, "stdin")
control_socket.puts "stdin #{stdin}"
stdin = File::open(stdin, "r")
# TODO: reconsider
stdin.sync = true
stdout = open_pipe(connection_id, "stdout")
@ -130,7 +101,8 @@ while true
stderr = File::open(stderr, "w")
stderr.sync = true
$stderr.puts " set up pipes"
$stderr.puts " set up named pipes"
control_socket.puts "ready"
response = control_socket.readline.strip
@ -140,19 +112,64 @@ while true
Kernel.exit!
end
script_path = Base64.decode64(arguments[1])
$stderr.puts " executing script " + script_path
$stdin.reopen(stdin)
$stdout.reopen(stdout)
$stderr.reopen(stderr)
end
while true
control_socket = control_server.accept
$stderr.puts "- new connection"
begin
command, arguments = read_command(control_socket)
if command != "new"
raise ClientError.new "unexpected command"
end
if arguments.length != 3
raise ClientError.new "malformed command"
end
protocol_version = arguments[0]
if protocol_version != "v1"
raise ClientError.new "unsupported protocol version (#{protocol_version})"
end
mode = arguments[1]
if not ["named-pipes", "pseudoterminal"].include?(mode)
raise ClientError.new "unknown mode (#{mode})"
end
script_path = Base64.decode64(arguments[2])
connection_id += 1
child_process = fork {
original_stdin = $stdin.dup
original_stdout = $stdout.dup
original_stderr = $stderr.dup
exit_code = "unknown"
if mode == "named-pipes"
set_up_named_pipes(control_socket, connection_id)
else
raise ClientError.new "not yet implemented"
end
original_stderr.puts " executing script " + script_path
begin
begin
load script_path, true
rescue SystemExit => error
original_stderr.puts " exit code: #{error.status}"
exit_code = error.status
rescue => error
rescue StandardError => error
$stdin = original_stdin
$stdout = original_stdout
$stderr = original_stderr