Skip to main content

Module socket

Module socket 

Source
Expand description

The attach sockets themselves: accepting the clients, reading their stdin and writing the output to them.

ConstantsΒ§

DONE_PACKET πŸ”’
The packet indicating that we’re done writing.
PACKET_BUF_SIZE πŸ”’
The most data of an output packet, after its pipe byte. Like for conmon, CRI-O and the Go client read one byte more than this, so that a full read of a pipe goes out as one packet.
STDIN_PACKET_MAX_SIZE πŸ”’
The maximum size of a stdin packet from an attach client, a larger one gets truncated. The Go client writes at most 32 KiB at once, like CRI-O does for conmon, plus the start of a detach key sequence which it held back, a few bytes which the extra KiB leaves room for. The scratch buffer of the thread keeps this size after the first stdin, see with_scratch.

FunctionsΒ§

accept_connections πŸ”’
Accept connections until the container exits, which cancels token, or another listener replaces this one, which cancels stop.
client_gone πŸ”’
Whether a send failed because the client closed its end. A client which leaves output unread gets the connection reset.
create_listener πŸ”’
Listen on the attach socket at path, unless a listener of this server does already.
hangup πŸ”’
Wait until the client closed the connection. Without output, a send never notices, and a client which only closed its write side still receives the output. The hangup gets reported by an epoll instance of its own, which watches the socket for nothing but the hangups and errors epoll always reports. Neither input nor output wakes it up, and its readiness is separate from the one of the socket, which the read and write loops use. If that fails, only a failing send ends the loop.
input_ended πŸ”’
Check if no more input will arrive after a zero byte read. That is the case once the peer closed its write side, which a SOCK_SEQPACKET socket reports as POLLRDHUP, and no data is queued anymore. Checking the flag first makes sure that no data arrives in between.
next_message πŸ”’
The next output message, bounded by the drain once the container exited. If the output did not end after the exit, the client gets the done packet anyway.
read_loop πŸ”’
Forward the stdin of a client until it closes, the stdin of the container got closed or the container exited, which cancels token.
recv_stdin πŸ”’
Receive a stdin packet into the scratch buffer of the thread, see with_scratch, and copy it out, so that a connection holds no buffer while it waits and the queued stdin only the data of the packets. Stdin is binary, null bytes are part of the data. Returns whether the packet got truncated as well.
send_packet πŸ”’
Send a packet, false if the client went away or does not take it in time. A client which does not read keeps a send waiting, until it hangs up or, once the container exited, for at most DRAIN_MAX_WAIT. Each packet gets that long, so that a slow client still gets the output which was there already.
write_loop πŸ”’
Forward the container output of attach to a client until the output ended or the client went away. Once the container exited, which cancels token, a process left behind may keep the output open, so forward only what arrives shortly after the exit, see Drain::recv_attach.