veyron/runtimes/google/rt: Split runtime initialization into two parts
so some initialization can use the veyron2.* functions.

Change-Id: If17dfd4c86f5fcc75b6d40763549131dbc48e984
diff --git a/runtimes/fake/runtime.go b/runtimes/fake/runtime.go
index baa6386..39e3396 100644
--- a/runtimes/fake/runtime.go
+++ b/runtimes/fake/runtime.go
@@ -28,6 +28,10 @@
 	return &Runtime{}, ctx, func() {}, nil
 }
 
+func (r *Runtime) Init(ctx *context.T) error {
+	return nil
+}
+
 func (r *Runtime) SetPrincipal(ctx *context.T, principal security.Principal) (*context.T, error) {
 	return context.WithValue(ctx, principalKey, principal), nil
 }
diff --git a/runtimes/google/rt/mgmt.go b/runtimes/google/rt/mgmt.go
index ca0f7e3..ccaad62 100644
--- a/runtimes/google/rt/mgmt.go
+++ b/runtimes/google/rt/mgmt.go
@@ -14,13 +14,17 @@
 	"v.io/core/veyron/lib/exec"
 )
 
-func (rt *Runtime) initMgmt(ctx *context.T, appCycle veyron2.AppCycle, handle *exec.ChildHandle) error {
-	// Do not initialize the mgmt runtime if the process has not
-	// been started through the veyron exec library by a device
-	// manager.
-	if handle == nil {
+func (rt *Runtime) initMgmt(ctx *context.T) error {
+	handle, err := exec.GetChildHandle()
+	if err == exec.ErrNoVersion {
+		// Do not initialize the mgmt runtime if the process has not
+		// been started through the veyron exec library by a device
+		// manager.
 		return nil
+	} else if err != nil {
+		return err
 	}
+
 	parentName, err := handle.Config.Get(mgmt.ParentNameConfigKey)
 	if err != nil {
 		return nil
@@ -45,7 +49,7 @@
 	if err != nil {
 		return err
 	}
-	if err := server.Serve("", appCycle.Remote(), nil); err != nil {
+	if err := server.Serve("", veyron2.GetAppCycle(ctx).Remote(), nil); err != nil {
 		server.Stop()
 		return err
 	}
@@ -55,6 +59,13 @@
 		return err
 	}
 
+	// Note(mattr): that we ignore the error here.
+	// As far as I can tell this is because the modules framework actually calls
+	// SetReady(), so we would then call it here a second time and get
+	// an error.
+	// TODO(mattr): We should change the modules framework so we can properly check
+	// errors here.
+	handle.SetReady()
 	return nil
 }
 
@@ -79,7 +90,7 @@
 
 func (rt *Runtime) callbackToParent(ctx *context.T, parentName, myName string) error {
 	ctx, _ = context.WithTimeout(ctx, time.Minute)
-	call, err := rt.GetClient(ctx).StartCall(ctx, parentName, "Set", []interface{}{mgmt.AppCycleManagerConfigKey, myName}, options.NoResolve{})
+	call, err := rt.GetClient(ctx).StartCall(ctx, parentName, "Set", []interface{}{mgmt.AppCycleManagerConfigKey, myName})
 
 	if err != nil {
 		return err
diff --git a/runtimes/google/rt/runtime.go b/runtimes/google/rt/runtime.go
index b74bc10..976e61b 100644
--- a/runtimes/google/rt/runtime.go
+++ b/runtimes/google/rt/runtime.go
@@ -21,7 +21,6 @@
 	"v.io/core/veyron2/vlog"
 	"v.io/core/veyron2/vtrace"
 
-	"v.io/core/veyron/lib/exec"
 	"v.io/core/veyron/lib/flags"
 	_ "v.io/core/veyron/lib/stats/sysstats"
 	iipc "v.io/core/veyron/runtimes/google/ipc"
@@ -68,22 +67,11 @@
 	reservedDispatcher ipc.Dispatcher, dispatcherOpts ...ipc.ServerOpt) (*Runtime, *context.T, veyron2.Shutdown, error) {
 	r := &Runtime{deps: dependency.NewGraph()}
 
-	handle, err := exec.GetChildHandle()
-	switch err {
-	case exec.ErrNoVersion:
-		// The process has not been started through the veyron exec
-		// library. No further action is needed.
-	case nil:
-		// The process has been started through the veyron exec
-		// library.
-	default:
-		return nil, nil, nil, err
-	}
-
 	r.initLogging(ctx)
 	ctx = context.WithValue(ctx, loggerKey, vlog.Log)
 
 	// Setup the initial trace.
+	var err error
 	ctx, err = ivtrace.Init(ctx, flags.Vtrace)
 	if err != nil {
 		return nil, nil, nil, err
@@ -148,7 +136,7 @@
 	}
 
 	// Initialize security.
-	principal, err := initSecurity(ctx, handle, flags.Credentials, client)
+	principal, err := initSecurity(ctx, flags.Credentials, client)
 	if err != nil {
 		return nil, nil, nil, err
 	}
@@ -160,20 +148,11 @@
 		return nil, nil, nil, err
 	}
 
-	// Initialize management.
-	if err := r.initMgmt(ctx, r.GetAppCycle(ctx), handle); err != nil {
-		return nil, nil, nil, err
-	}
-
 	ctx = r.SetBackgroundContext(ctx)
 
 	// TODO(suharshs,mattr): Go through the rt.Cleanup function and make sure everything
 	// gets cleaned up.
 
-	if handle != nil {
-		handle.SetReady()
-	}
-
 	return r, ctx, r.shutdown, nil
 }
 
@@ -192,6 +171,10 @@
 	return nil
 }
 
+func (r *Runtime) Init(ctx *context.T) error {
+	return r.initMgmt(ctx)
+}
+
 func (r *Runtime) shutdown() {
 	r.deps.CloseAndWaitForAll()
 	vlog.FlushLog()
diff --git a/runtimes/google/rt/security.go b/runtimes/google/rt/security.go
index ab640df..b4a3aef 100644
--- a/runtimes/google/rt/security.go
+++ b/runtimes/google/rt/security.go
@@ -18,8 +18,8 @@
 	"v.io/core/veyron/security/agent"
 )
 
-func initSecurity(ctx *context.T, handle *exec.ChildHandle, credentials string, client ipc.Client) (security.Principal, error) {
-	principal, err := setupPrincipal(ctx, handle, credentials, client)
+func initSecurity(ctx *context.T, credentials string, client ipc.Client) (security.Principal, error) {
+	principal, err := setupPrincipal(ctx, credentials, client)
 	if err != nil {
 		return nil, err
 	}
@@ -31,13 +31,13 @@
 	return principal, nil
 }
 
-func setupPrincipal(ctx *context.T, handle *exec.ChildHandle, credentials string, client ipc.Client) (security.Principal, error) {
+func setupPrincipal(ctx *context.T, credentials string, client ipc.Client) (security.Principal, error) {
 	var err error
 	var principal security.Principal
 	if principal, _ = ctx.Value(principalKey).(security.Principal); principal != nil {
 		return principal, nil
 	}
-	if fd, err := agentFD(handle); err != nil {
+	if fd, err := agentFD(); err != nil {
 		return nil, err
 	} else if fd >= 0 {
 		return connectToAgent(ctx, fd, client)
@@ -66,7 +66,11 @@
 // agentFD returns a non-negative file descriptor to be used to communicate with
 // the security agent if the current process has been configured to use the
 // agent.
-func agentFD(handle *exec.ChildHandle) (int, error) {
+func agentFD() (int, error) {
+	handle, err := exec.GetChildHandle()
+	if err != nil && err != exec.ErrNoVersion {
+		return -1, err
+	}
 	var fd string
 	if handle != nil {
 		// We were started by a parent (presumably, device manager).