veyron/lib/signals: Remove dependence on rt.R()

Change-Id: I451318d807336a157ee3b92cb85921f7f2ce02c4
diff --git a/lib/signals/signals.go b/lib/signals/signals.go
index e24e1c3..35d2965 100644
--- a/lib/signals/signals.go
+++ b/lib/signals/signals.go
@@ -5,7 +5,7 @@
 	"os/signal"
 	"syscall"
 
-	"veyron.io/veyron/veyron2/rt"
+	"veyron.io/veyron/veyron2"
 )
 
 type stopSignal string
@@ -31,7 +31,7 @@
 // none are specified, the default signals.  The first signal received will be
 // made available on the returned channel; upon receiving a second signal, the
 // process will exit.
-func ShutdownOnSignals(signals ...os.Signal) <-chan os.Signal {
+func ShutdownOnSignals(runtime veyron2.Runtime, signals ...os.Signal) <-chan os.Signal {
 	if len(signals) == 0 {
 		signals = defaultSignals()
 	}
@@ -45,9 +45,9 @@
 		case STOP:
 			if !sawStop {
 				sawStop = true
-				if r := rt.R(); r != nil {
+				if runtime != nil {
 					stopWaiter := make(chan string, 1)
-					r.AppCycle().WaitForStop(stopWaiter)
+					runtime.AppCycle().WaitForStop(stopWaiter)
 					go func() {
 						for {
 							ch <- stopSignal(<-stopWaiter)
diff --git a/lib/signals/signals_test.go b/lib/signals/signals_test.go
index e48e373..4220712 100644
--- a/lib/signals/signals_test.go
+++ b/lib/signals/signals_test.go
@@ -41,7 +41,7 @@
 	modules.RegisterChild("handleDefaultsIgnoreChan", "", handleDefaultsIgnoreChan)
 }
 
-func stopLoop(stdin io.Reader, ch chan<- struct{}) {
+func stopLoop(runtime veyron2.Runtime, stdin io.Reader, ch chan<- struct{}) {
 	scanner := bufio.NewScanner(stdin)
 	for scanner.Scan() {
 		switch scanner.Text() {
@@ -49,19 +49,23 @@
 			close(ch)
 			return
 		case "stop":
-			rt.R().AppCycle().Stop()
+			runtime.AppCycle().Stop()
 		}
 	}
 }
 
 func program(stdin io.Reader, stdout io.Writer, signals ...os.Signal) {
-	r := rt.Init()
+	runtime, err := rt.New()
+	if err != nil {
+		panic(err)
+	}
+
 	closeStopLoop := make(chan struct{})
-	go stopLoop(stdin, closeStopLoop)
-	wait := ShutdownOnSignals(signals...)
+	go stopLoop(runtime, stdin, closeStopLoop)
+	wait := ShutdownOnSignals(runtime, signals...)
 	fmt.Fprintf(stdout, "ready\n")
 	fmt.Fprintf(stdout, "received signal %s\n", <-wait)
-	r.Cleanup()
+	runtime.Cleanup()
 	<-closeStopLoop
 }
 
@@ -81,10 +85,14 @@
 }
 
 func handleDefaultsIgnoreChan(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
-	defer rt.Init().Cleanup()
+	runtime, err := rt.New()
+	if err != nil {
+		panic(err)
+	}
+	defer runtime.Cleanup()
 	closeStopLoop := make(chan struct{})
-	go stopLoop(stdin, closeStopLoop)
-	ShutdownOnSignals()
+	go stopLoop(runtime, stdin, closeStopLoop)
+	ShutdownOnSignals(runtime)
 	fmt.Fprintf(stdout, "ready\n")
 	<-closeStopLoop
 	return nil
@@ -287,7 +295,7 @@
 // the input list of signals.
 func TestParseSignalsList(t *testing.T) {
 	list := []os.Signal{STOP, syscall.SIGTERM}
-	ShutdownOnSignals(list...)
+	ShutdownOnSignals(nil, list...)
 	if !isSignalInSet(syscall.SIGTERM, list) {
 		t.Errorf("signal %s not in signal set, as expected: %v", syscall.SIGTERM, list)
 	}
@@ -309,8 +317,8 @@
 
 }
 
-func createConfigServer(t *testing.T) (ipc.Server, string, <-chan string) {
-	server, err := rt.R().NewServer()
+func createConfigServer(t *testing.T, runtime veyron2.Runtime) (ipc.Server, string, <-chan string) {
+	server, err := runtime.NewServer()
 	if err != nil {
 		t.Fatalf("Got error: %v", err)
 	}
@@ -328,8 +336,11 @@
 
 // TestCleanRemoteShutdown verifies that remote shutdown works correctly.
 func TestCleanRemoteShutdown(t *testing.T) {
-	r := rt.Init()
-	defer r.Cleanup()
+	runtime, err := rt.New()
+	if err != nil {
+		panic(err)
+	}
+	defer runtime.Cleanup()
 
 	sh, err := modules.NewShell(nil)
 	if err != nil {
@@ -339,9 +350,9 @@
 
 	// Set the child process up with a blessing from the parent so that
 	// the default authorization works for RPCs between the two.
-	childcreds := security.NewVeyronCredentials(r.Principal(), "child")
+	childcreds := security.NewVeyronCredentials(runtime.Principal(), "child")
 	defer os.RemoveAll(childcreds)
-	configServer, configServiceName, ch := createConfigServer(t)
+	configServer, configServiceName, ch := createConfigServer(t, runtime)
 	defer configServer.Stop()
 	sh.SetVar(consts.VeyronCredentials, childcreds)
 	sh.SetConfigKey(mgmt.ParentNameConfigKey, configServiceName)
@@ -355,7 +366,7 @@
 	appCycleName := <-ch
 	s.Expect("ready")
 	appCycle := appcycle.AppCycleClient(appCycleName)
-	stream, err := appCycle.Stop(r.NewContext())
+	stream, err := appCycle.Stop(runtime.NewContext())
 	if err != nil {
 		t.Fatalf("Got error: %v", err)
 	}
diff --git a/runtimes/google/ipc/benchmarks/bmclient/main.go b/runtimes/google/ipc/benchmarks/bmclient/main.go
index aee94e8..874afb2 100644
--- a/runtimes/google/ipc/benchmarks/bmclient/main.go
+++ b/runtimes/google/ipc/benchmarks/bmclient/main.go
@@ -19,6 +19,8 @@
 
 func main() {
 	r := rt.Init()
+	defer r.Cleanup()
+
 	ctx := r.NewContext()
 	if *chunkCount == 0 {
 		benchmarks.CallEcho(ctx, *server, *count, *payloadSize, os.Stdout)
diff --git a/runtimes/google/ipc/benchmarks/bmserver/main.go b/runtimes/google/ipc/benchmarks/bmserver/main.go
index f07afef..7d09e63 100644
--- a/runtimes/google/ipc/benchmarks/bmserver/main.go
+++ b/runtimes/google/ipc/benchmarks/bmserver/main.go
@@ -12,8 +12,10 @@
 
 func main() {
 	r := rt.Init()
+	defer r.Cleanup()
+
 	addr, stop := benchmarks.StartServer(r, roaming.ListenSpec)
 	vlog.Infof("Listening on %s", addr)
 	defer stop()
-	<-signals.ShutdownOnSignals()
+	<-signals.ShutdownOnSignals(r)
 }
diff --git a/runtimes/google/rt/shutdown_servers_test.go b/runtimes/google/rt/shutdown_servers_test.go
index 32ead79..cba2006 100644
--- a/runtimes/google/rt/shutdown_servers_test.go
+++ b/runtimes/google/rt/shutdown_servers_test.go
@@ -238,7 +238,7 @@
 	// Note, if the developer wants to exit immediately upon receiving a
 	// signal or stop command, they can skip this, in which case the default
 	// behavior is for the process to exit.
-	waiter := signals.ShutdownOnSignals()
+	waiter := signals.ShutdownOnSignals(r)
 
 	// This communicates to the parent test driver process in our unit test
 	// that this server is ready and waiting on signals or stop commands.
diff --git a/security/agent/pingpong/main.go b/security/agent/pingpong/main.go
index dcc38d0..31fcd8a 100644
--- a/security/agent/pingpong/main.go
+++ b/security/agent/pingpong/main.go
@@ -22,6 +22,8 @@
 
 func clientMain() {
 	runtime := rt.Init()
+	defer runtime.Cleanup()
+
 	log := runtime.Logger()
 	log.Info("Pinging...")
 
@@ -55,7 +57,7 @@
 	}
 
 	// Wait forever.
-	<-signals.ShutdownOnSignals()
+	<-signals.ShutdownOnSignals(r)
 }
 
 type allowEveryone struct{}
diff --git a/services/identity/identityd/main.go b/services/identity/identityd/main.go
index 55e0b87..5e9f3dc 100644
--- a/services/identity/identityd/main.go
+++ b/services/identity/identityd/main.go
@@ -142,7 +142,7 @@
 	})
 	vlog.Infof("Running HTTP server at: %v", httpaddress())
 	go runHTTPSServer(*httpaddr)
-	<-signals.ShutdownOnSignals()
+	<-signals.ShutdownOnSignals(r)
 }
 
 func appendSuffixTo(objectname []string, suffix string) []string {
diff --git a/services/mgmt/application/applicationd/main.go b/services/mgmt/application/applicationd/main.go
index ab42449..af11153 100644
--- a/services/mgmt/application/applicationd/main.go
+++ b/services/mgmt/application/applicationd/main.go
@@ -50,5 +50,5 @@
 		vlog.Infof("Application repository serving at %q", epName)
 	}
 	// Wait until shutdown.
-	<-signals.ShutdownOnSignals()
+	<-signals.ShutdownOnSignals(runtime)
 }
diff --git a/services/mgmt/binary/binaryd/main.go b/services/mgmt/binary/binaryd/main.go
index 9b87655..d65984f 100644
--- a/services/mgmt/binary/binaryd/main.go
+++ b/services/mgmt/binary/binaryd/main.go
@@ -129,5 +129,5 @@
 		vlog.Infof("Binary repository serving at %q", epName)
 	}
 	// Wait until shutdown.
-	<-signals.ShutdownOnSignals()
+	<-signals.ShutdownOnSignals(runtime)
 }
diff --git a/services/mgmt/build/buildd/main.go b/services/mgmt/build/buildd/main.go
index bba50c4..deb5f37 100644
--- a/services/mgmt/build/buildd/main.go
+++ b/services/mgmt/build/buildd/main.go
@@ -42,5 +42,5 @@
 	vlog.Infof("Build server running at endpoint=%q", endpoint)
 
 	// Wait until shutdown.
-	<-signals.ShutdownOnSignals()
+	<-signals.ShutdownOnSignals(runtime)
 }
diff --git a/services/mgmt/node/impl/impl_test.go b/services/mgmt/node/impl/impl_test.go
index d753fd5..ad669d5 100644
--- a/services/mgmt/node/impl/impl_test.go
+++ b/services/mgmt/node/impl/impl_test.go
@@ -173,7 +173,7 @@
 
 	fmt.Fprintf(stdout, "ready:%d\n", os.Getpid())
 
-	<-signals.ShutdownOnSignals()
+	<-signals.ShutdownOnSignals(rt.R())
 
 	if val, present := env["PAUSE_BEFORE_STOP"]; present && val == "1" {
 		modules.WaitForEOF(stdin)
@@ -240,7 +240,7 @@
 	vlog.FlushLog()
 	ping()
 
-	<-signals.ShutdownOnSignals()
+	<-signals.ShutdownOnSignals(rt.R())
 	if err := ioutil.WriteFile("testfile", []byte("goodbye world"), 0600); err != nil {
 		vlog.Fatalf("Failed to write testfile: %v", err)
 	}
diff --git a/services/mgmt/node/noded/main.go b/services/mgmt/node/noded/main.go
index eea0702..c40f2a9 100644
--- a/services/mgmt/node/noded/main.go
+++ b/services/mgmt/node/noded/main.go
@@ -76,5 +76,5 @@
 	impl.InvokeCallback(name)
 
 	// Wait until shutdown.
-	<-signals.ShutdownOnSignals()
+	<-signals.ShutdownOnSignals(runtime)
 }
diff --git a/services/mgmt/profile/profiled/main.go b/services/mgmt/profile/profiled/main.go
index d01b803..a811f12 100644
--- a/services/mgmt/profile/profiled/main.go
+++ b/services/mgmt/profile/profiled/main.go
@@ -45,5 +45,5 @@
 	vlog.Infof("Profile repository running at endpoint=%q", endpoint)
 
 	// Wait until shutdown.
-	<-signals.ShutdownOnSignals()
+	<-signals.ShutdownOnSignals(runtime)
 }
diff --git a/services/mgmt/root/rootd/main.go b/services/mgmt/root/rootd/main.go
index ff23fdc..45f9205 100644
--- a/services/mgmt/root/rootd/main.go
+++ b/services/mgmt/root/rootd/main.go
@@ -32,5 +32,5 @@
 	}
 
 	// Wait until shutdown.
-	<-signals.ShutdownOnSignals()
+	<-signals.ShutdownOnSignals(r)
 }
diff --git a/services/mounttable/mounttabled/mounttable.go b/services/mounttable/mounttabled/mounttable.go
index 7234a09..3286652 100644
--- a/services/mounttable/mounttabled/mounttable.go
+++ b/services/mounttable/mounttabled/mounttable.go
@@ -84,5 +84,5 @@
 	}
 
 	// Wait until signal is received.
-	vlog.Info("Received signal ", <-signals.ShutdownOnSignals())
+	vlog.Info("Received signal ", <-signals.ShutdownOnSignals(r))
 }
diff --git a/tools/debug/impl.go b/tools/debug/impl.go
index e36d950..9c5effc 100644
--- a/tools/debug/impl.go
+++ b/tools/debug/impl.go
@@ -554,7 +554,7 @@
 	fmt.Fprintln(cmd.Stdout())
 	fmt.Fprintln(cmd.Stdout(), "Hit CTRL-C to exit")
 
-	<-signals.ShutdownOnSignals()
+	<-signals.ShutdownOnSignals(runtime)
 	return nil
 }