Support a headless server mode

Add a -headless flag which runs only the debugger server until
a SIGINT is received.
This commit is contained in:
Dan Mace 2015-04-17 16:49:49 -04:00 committed by Derek Parker
parent ca2508af6b
commit 064462e5b5
2 changed files with 24 additions and 13 deletions

@ -3,11 +3,13 @@ package main
import (
"flag"
"fmt"
sys "golang.org/x/sys/unix"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strconv"
@ -36,10 +38,12 @@ func main() {
var printv, printhelp bool
var addr string
var logEnabled bool
var headless bool
flag.BoolVar(&printv, "version", false, "Print version number and exit.")
flag.StringVar(&addr, "addr", "localhost:0", "Debugging server listen address.")
flag.BoolVar(&logEnabled, "log", false, "Enable debugging server logging.")
flag.BoolVar(&headless, "headless", false, "Run in headless mode.")
flag.Parse()
if flag.NFlag() == 0 && len(flag.Args()) == 0 {
@ -120,15 +124,22 @@ func main() {
})
go server.Run()
status := 0
if !headless {
// Create and start a terminal
client := rest.NewClient(listener.Addr().String())
term := terminal.New(client)
err, status := term.Run()
err, status = term.Run()
} else {
ch := make(chan os.Signal)
signal.Notify(ch, sys.SIGINT)
<-ch
err = server.Stop(true)
}
if err != nil {
fmt.Println(err)
}
// Clean up and exit
fmt.Println("[Hope I was of service hunting your bug!]")
os.Exit(status)
}

@ -98,8 +98,13 @@ func (s *RESTServer) Run() error {
return http.Serve(s.listener, container)
}
// Stop detaches from the debugger and waits for it to stop.
func (s *RESTServer) Stop(kill bool) error {
return s.debugger.Detach(kill)
err := s.debugger.Detach(kill)
if err != nil {
return err
}
return <-s.debuggerStopped
}
// writeError writes a simple error response.
@ -117,17 +122,12 @@ func (s *RESTServer) detach(request *restful.Request, response *restful.Response
return
}
err = s.debugger.Detach(kill)
err = s.Stop(kill)
if err != nil {
writeError(response, http.StatusInternalServerError, err.Error())
return
}
err = <-s.debuggerStopped
if err != nil {
writeError(response, http.StatusInternalServerError, err.Error())
return
}
response.WriteHeader(http.StatusOK)
}