veyron/security/agent/agentd: kill child command when the agent is killed
If the agent receives a signal like TERM or INT, we want the agent to first kill
its child before exiting. The change makes agent listen for ShutdownOnSignals
and relay the received signal to the child (so that the child has a chance to
shut down cleanly).
Change-Id: I9301ff092b7b7cdce1d6b7bf655e3ff41811630a
diff --git a/security/agent/agentd/main.go b/security/agent/agentd/main.go
index faa8c0d..3ca5ee0 100644
--- a/security/agent/agentd/main.go
+++ b/security/agent/agentd/main.go
@@ -12,6 +12,7 @@
"code.google.com/p/go.crypto/ssh/terminal"
"veyron.io/veyron/veyron/lib/flags/consts"
+ vsignals "veyron.io/veyron/veyron/lib/signals"
_ "veyron.io/veyron/veyron/profiles"
vsecurity "veyron.io/veyron/veyron/security"
"veyron.io/veyron/veyron/security/agent"
@@ -102,7 +103,22 @@
log.Fatalf("Error starting child: %v", err)
}
sock.Close()
+ shutdown := make(chan struct{})
+ go func() {
+ select {
+ case sig := <-vsignals.ShutdownOnSignals(runtime):
+ // TODO(caprita): Should we also relay double signal to
+ // the child? That currently just force exits the
+ // current process.
+ if sig == vsignals.STOP {
+ sig = syscall.SIGTERM
+ }
+ cmd.Process.Signal(sig)
+ case <-shutdown:
+ }
+ }()
cmd.Wait()
+ close(shutdown)
status := cmd.ProcessState.Sys().(syscall.WaitStatus)
os.Exit(status.ExitStatus())
}