func sendTTYToCommand(commandUsock *unixsocket.Usock, clientFile *os.File, err error) error { if err != nil { return err } return commandUsock.WriteFD(int(clientFile.Fd())) }
func receiveExitStatus(commandUsock *unixsocket.Usock, err error) (string, error) { if err != nil { return "", err } return commandUsock.ReadMessage() }
func sendExitStatus(usock *unixsocket.Usock, exitStatus string, err error) error { if err != nil { return err } _, err = usock.WriteMessage(exitStatus) return err }
func sendClientPidAndArgumentsToCommand(commandUsock *unixsocket.Usock, clientPid int, arguments string, err error) error { if err != nil { return err } msg := messages.CreatePidAndArgumentsMessage(clientPid, arguments) _, err = commandUsock.WriteMessage(msg) return err }
func sendCommandPidToClient(usock *unixsocket.Usock, pid int, err error) error { if err != nil { return err } strPid := strconv.Itoa(pid) _, err = usock.WriteMessage(strPid) return err }
func receivePidFromCommand(commandUsock *unixsocket.Usock, err error) (int, error) { if err != nil { return -1, err } msg, err := commandUsock.ReadMessage() if err != nil { return -1, err } intPid, _, _ := messages.ParsePidMessage(msg) return intPid, err }
func receiveTTY(usock *unixsocket.Usock, err error) (*os.File, error) { if err != nil { return nil, err } clientFd, err := usock.ReadFD() if err != nil { return nil, errors.New("Expected FD, none received!") } fileName := strconv.Itoa(rand.Int()) clientFile := unixsocket.FdToFile(clientFd, fileName) return clientFile, nil }
func receiveCommandArgumentsAndPid(usock *unixsocket.Usock, err error) (string, int, string, error) { if err != nil { return "", -1, "", err } msg, err := usock.ReadMessage() if err != nil { return "", -1, "", err } command, clientPid, arguments, err := messages.ParseClientCommandRequestMessage(msg) if err != nil { return "", -1, "", err } return command, clientPid, arguments, err }
// see docs/client_master_handshake.md func handleClientConnection(tree *processtree.ProcessTree, usock *unixsocket.Usock) { defer usock.Close() // we have established first contact to the client. command, clientPid, arguments, err := receiveCommandArgumentsAndPid(usock, nil) commandNode, slaveNode, err := findCommandAndSlaveNodes(tree, command, err) if err != nil { // connection was established, no data was sent. Ignore. return } command = commandNode.Name // resolve aliases clientFile, err := receiveTTY(usock, err) defer clientFile.Close() if err == nil && slaveNode.Error != "" { // we can skip steps 3-5 as they deal with the command process we're not spawning. // Write a fake pid (step 6) usock.WriteMessage("0") // Write the error message to the terminal clientFile.Write([]byte(slaveNode.Error)) // Skip step 7, and write an exit code to the client (step 8) usock.WriteMessage("1") return } commandUsock, err := bootNewCommand(slaveNode, command, err) defer commandUsock.Close() err = sendClientPidAndArgumentsToCommand(commandUsock, clientPid, arguments, err) err = sendTTYToCommand(commandUsock, clientFile, err) cmdPid, err := receivePidFromCommand(commandUsock, err) err = sendCommandPidToClient(usock, cmdPid, err) exitStatus, err := receiveExitStatus(commandUsock, err) err = sendExitStatus(usock, exitStatus, err) if err != nil { slog.Error(err) } // Done! Hooray! }