Refactor named pipe handling

This commit is contained in:
Patrick Lühne 2020-05-25 16:50:05 +02:00
parent a42417aed3
commit aa17fe4e00
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
2 changed files with 35 additions and 31 deletions

View File

@ -98,26 +98,31 @@ while true
if command == "ready" if command == "ready"
break break
end elsif command == "named-pipes"
if arguments.empty?
if not ["stdin", "stdout", "stderr"].include?(command) or arguments.empty?
log "error", "malformed response" log "error", "malformed response"
exit 1 exit 1
end end
pipe_path = arguments[0] pipe_base_path = Base64.decode64(arguments[0])
log "info", "connecting to #{pipe_path}" log "info", "connecting to named pipes at #{pipe_base_path}"
if command == "stdin" pipes["stdin"] = File::open("#{pipe_base_path}.stdin", "w")
pipes[command] = File::open(pipe_path, "w") pipes["stdin"].sync = true
pipes["stdout"] = File::open("#{pipe_base_path}.stdout", "r")
pipes["stdout"].sync = true
pipes["stderr"] = File::open("#{pipe_base_path}.stderr", "r")
pipes["stderr"].sync = true
log "info", "connected via named pipes"
elsif command == "pseudoterminal"
log "error", "not yet implemented"
exit 1
else else
pipes[command] = File::open(pipe_path, "r") log "error", "malformed response"
exit 1
end end
pipes[command].sync = true
log "info", "connected"
end end
log "info", "ready" log "info", "ready"

View File

@ -39,16 +39,14 @@ $stderr.puts "serving control socket"
connection_id = 0 connection_id = 0
def open_pipe(connection_id, name) def open_pipe(path)
pipe_path = "/tmp/github-fast-envd.#{connection_id}.#{name}" if File.exist?(path) and File.pipe?(path)
File.unlink(path)
if File.exist?(pipe_path) and File.pipe?(pipe_path)
File.unlink(pipe_path)
end end
File.mkfifo(pipe_path, mode = 0600) File.mkfifo(path, mode = 0600)
pipe_path path
end end
def read_command(control_socket) def read_command(control_socket)
@ -86,18 +84,19 @@ def read_command(control_socket)
end end
def set_up_named_pipes(control_socket, connection_id) def set_up_named_pipes(control_socket, connection_id)
stdin = open_pipe(connection_id, "stdin") pipe_base_path = "/tmp/github-fast-envd.#{connection_id}"
control_socket.puts "stdin #{stdin}" pipe_base_path_encoded = Base64.encode64(pipe_base_path).delete("\n")
stdin = open_pipe("#{pipe_base_path}.stdin")
stdout = open_pipe("#{pipe_base_path}.stdout")
stderr = open_pipe("#{pipe_base_path}.stderr")
control_socket.puts "named-pipes #{pipe_base_path_encoded}"
stdin = File::open(stdin, "r") stdin = File::open(stdin, "r")
stdin.sync = true stdin.sync = true
stdout = open_pipe(connection_id, "stdout")
control_socket.puts "stdout #{stdout}"
stdout = File::open(stdout, "w") stdout = File::open(stdout, "w")
stdout.sync = true stdout.sync = true
stderr = open_pipe(connection_id, "stderr")
control_socket.puts "stderr #{stderr}"
stderr = File::open(stderr, "w") stderr = File::open(stderr, "w")
stderr.sync = true stderr.sync = true