func (mon *SlaveMonitor) slaveDidBeginRegistration(fd int) { // Having just started the process, we expect an IO, which we convert to a UNIX domain socket fileName := strconv.Itoa(rand.Int()) slaveFile := unixsocket.FdToFile(fd, fileName) slaveUsock, err := unixsocket.NewUsockFromFile(slaveFile) if err != nil { fmt.Println(err) } if err = slaveUsock.Conn.SetReadBuffer(1024); err != nil { fmt.Println(err) } if err = slaveUsock.Conn.SetWriteBuffer(1024); err != nil { fmt.Println(err) } // We now expect the slave to use this fd they send us to send a Pid&Identifier Message msg, err := slaveUsock.ReadMessage() if err != nil { fmt.Println(err) } pid, identifier, err := ParsePidMessage(msg) slaveNode := mon.tree.FindSlaveByName(identifier) if slaveNode == nil { panic("slavemonitor.go:slaveDidBeginRegistration:Unknown identifier:" + identifier) } go slaveNode.Run(identifier, pid, slaveUsock) }
func bootNewCommand(slaveNode *processtree.SlaveNode, command string, err error) (*unixsocket.Usock, error) { if err != nil { return nil, err } request := &processtree.CommandRequest{command, make(chan *os.File)} slaveNode.RequestCommandBoot(request) commandFile := <-request.Retchan // TODO: don't really want to wait indefinitely. // defer commandFile.Close() // TODO: can't do this here anymore. return unixsocket.NewUsockFromFile(commandFile) }
func Run(color bool) { if !color { slog.DisableColor() DisableErrorColor() } slog.StartingZeus() var tree *ProcessTree = BuildProcessTree() exitNow = make(chan int) localMasterFile, remoteMasterFile, err := unixsocket.Socketpair(syscall.SOCK_DGRAM) if err != nil { panic(err) } localMasterSocket, err := unixsocket.NewUsockFromFile(localMasterFile) if err != nil { panic(err) } quit1 := make(chan bool) quit2 := make(chan bool) quit3 := make(chan bool) go StartSlaveMonitor(tree, localMasterSocket, remoteMasterFile, quit1) go StartClientHandler(tree, quit2) go StartFileMonitor(tree, quit3) quit := make(chan bool, 1) c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func() { <-c // FIXME: Unprecedented levels of jank, right here. terminateComponents(quit1, quit2, quit3, quit) }() go func() { exitStatus = <-exitNow terminateComponents(quit1, quit2, quit3, quit) }() <-quit <-quit <-quit os.Exit(exitStatus) }
func bootNewCommand(slaveNode *processtree.SlaveNode, command string, err error) (*unixsocket.Usock, error) { if err != nil { return nil, err } request := &processtree.CommandRequest{command, make(chan *processtree.CommandReply)} slaveNode.RequestCommandBoot(request) reply := <-request.Retchan // TODO: don't really want to wait indefinitely. // defer commandFile.Close() // TODO: can't do this here anymore. if reply.State == processtree.SCrashed { return nil, errors.New("Process has crashed") } return unixsocket.NewUsockFromFile(reply.File) }
func StartSlaveMonitor(tree *ProcessTree, quit chan bool) { monitor := &SlaveMonitor{tree} localMasterFile, remoteMasterFile, err := unixsocket.Socketpair(syscall.SOCK_DGRAM) if err != nil { panic(err) } localMasterSocket, err := unixsocket.NewUsockFromFile(localMasterFile) if err != nil { panic(err) } // We just want this unix socket to be a channel so we can select on it... registeringFds := make(chan int, 3) go func() { for { fd, err := localMasterSocket.ReadFD() if err != nil { fmt.Println(err) } registeringFds <- fd } }() for _, slave := range monitor.tree.SlavesByName { if slave.Parent == nil { go monitor.startInitialProcess(remoteMasterFile) } else { go monitor.bootSlave(slave) } } for { select { case <-quit: quit <- true monitor.cleanupChildren() return case fd := <-registeringFds: monitor.slaveDidBeginRegistration(fd) } } }
func StartSlaveMonitor(tree *ProcessTree, done chan bool) chan bool { quit := make(chan bool) go func() { localMasterFile, remoteMasterFile, err := unixsocket.Socketpair(syscall.SOCK_DGRAM) if err != nil { Error("Couldn't create socketpair") } monitor := &SlaveMonitor{tree, remoteMasterFile} localMasterSocket, err := unixsocket.NewUsockFromFile(localMasterFile) if err != nil { Error("Couldn't Open UNIXSocket") } // We just want this unix socket to be a channel so we can select on it... registeringFds := make(chan int, 3) go func() { for { fd, err := localMasterSocket.ReadFD() if err != nil { slog.Error(err) } registeringFds <- fd } }() for _, slave := range monitor.tree.SlavesByName { go slave.Run(monitor) } for { select { case <-quit: monitor.cleanupChildren() done <- true return case fd := <-registeringFds: go monitor.slaveDidBeginRegistration(fd) } } }() return quit }
func (mon *SlaveMonitor) slaveDidBeginRegistration(fd int) { // Having just started the process, we expect an IO, which we convert to a UNIX domain socket fileName := strconv.Itoa(rand.Int()) slaveFile := unixsocket.FdToFile(fd, fileName) slaveUsock, err := unixsocket.NewUsockFromFile(slaveFile) if err != nil { slog.Error(err) } if err = slaveUsock.Conn.SetReadBuffer(1024); err != nil { slog.Error(err) } if err = slaveUsock.Conn.SetWriteBuffer(1024); err != nil { slog.Error(err) } // We now expect the slave to use this fd they send us to send a Pid&Identifier Message msg, err := slaveUsock.ReadMessage() if err != nil { slog.Error(err) } pid, identifier, err := messages.ParsePidMessage(msg) // And the last step before executing its action, the slave sends us a pipe it will later use to // send us all the features it's loaded. featurePipeFd, err := slaveUsock.ReadFD() if err != nil { slog.Error(err) } slaveNode := mon.tree.FindSlaveByName(identifier) if slaveNode == nil { Error("slavemonitor.go:slaveDidBeginRegistration:Unknown identifier:" + identifier) } slaveNode.SlaveWasInitialized(pid, slaveUsock, featurePipeFd) }
// see docs/client_master_handshake.md func handleClientConnection(tree *ProcessTree, usock *unixsocket.Usock) { defer usock.Close() // we have established first contact to the client. // we first read the command and arguments specified from the connection. (step 1) msg, err := usock.ReadMessage() if err != nil { fmt.Println("clienthandler.go:handleClientConnection:read command and arguments:", err) return } command, arguments, err := ParseClientCommandRequestMessage(msg) if err != nil { fmt.Println("clienthandler.go:handleClientConnection:parse command and arguments:", err) return } commandNode := tree.FindCommand(command) if commandNode == nil { fmt.Println("ERROR: Node not found!: ", command) return } command = commandNode.Name // resolve aliases slaveNode := commandNode.Parent // Now we read the terminal IO socket to use for raw IO (step 2) clientFd, err := usock.ReadFD() if err != nil { fmt.Println("Expected FD, none received!") return } fileName := strconv.Itoa(rand.Int()) clientFile := unixsocket.FdToFile(clientFd, fileName) defer clientFile.Close() // We now need to fork a new command process. // For now, we naively assume it's running... if 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 } // boot a command process and establish a socket connection to it. slaveNode.WaitUntilBooted() msg = CreateSpawnCommandMessage(command) slaveNode.mu.Lock() unixsocket.NewUsock(slaveNode.Socket).WriteMessage(msg) slaveNode.mu.Unlock() // TODO: deadline? how to respond if this is never sent? commandFd := <-slaveNode.ClientCommandPTYFileDescriptor if err != nil { fmt.Println("Couldn't start command process!", err) } fileName = strconv.Itoa(rand.Int()) commandFile := unixsocket.FdToFile(commandFd, fileName) defer commandFile.Close() commandUsock, err := unixsocket.NewUsockFromFile(commandFile) if err != nil { fmt.Println("MakeUnixSocket", err) } defer commandUsock.Close() // Send the arguments to the command process (step 3) commandUsock.WriteMessage(arguments) // Send the client terminal connection to the command process (step 4) commandUsock.WriteFD(clientFd) // Receive the pid from the command process (step 5) msg, err = commandUsock.ReadMessage() if err != nil { fmt.Println(err) } intPid, _, _ := ParsePidMessage(msg) // Send the pid to the client process (step 6) strPid := strconv.Itoa(intPid) usock.WriteMessage(strPid) // Receive the exit status from the command (step 7) msg, err = commandUsock.ReadMessage() if err != nil { fmt.Println("clienthandler.go:handleClientConnection:receive exit status:", err) return } // Forward the exit status to the Client (step 8) usock.WriteMessage(msg) // Done! Hooray! }