Revert "TBR: Revert "IPC implementation of agentd. The client library supports both IPC and RPC for now.""

This reverts commit 8b59ca39c5f4b98d07a4f7473fdd6e1d585cd275.

Change-Id: Ia5ce2249229955fc6ced331cacdf4181b847c905
diff --git a/test/modules/shell.go b/test/modules/shell.go
index 0f4106a..4b3400e 100644
--- a/test/modules/shell.go
+++ b/test/modules/shell.go
@@ -138,6 +138,7 @@
 	"io"
 	"io/ioutil"
 	"os"
+	"path/filepath"
 	"sync"
 	"syscall"
 	"time"
@@ -152,6 +153,7 @@
 	"v.io/x/ref"
 	"v.io/x/ref/internal/logger"
 	"v.io/x/ref/lib/exec"
+	"v.io/x/ref/services/agent"
 	"v.io/x/ref/services/agent/agentlib"
 	"v.io/x/ref/services/agent/keymgr"
 	"v.io/x/ref/test/expect"
@@ -184,7 +186,7 @@
 	tempCredDir      string
 	config           exec.Config
 	principal        security.Principal
-	agent            *keymgr.Agent
+	agent            agent.KeyManager
 	ctx              *context.T
 	logger           logging.Logger
 	sessionVerbosity bool
@@ -230,7 +232,7 @@
 	if sh.tempCredDir, err = ioutil.TempDir("", "shell_credentials-"); err != nil {
 		return nil, err
 	}
-	if sh.agent, err = keymgr.NewLocalAgent(ctx, sh.tempCredDir, nil); err != nil {
+	if sh.agent, err = keymgr.NewLocalAgent(sh.tempCredDir, nil); err != nil {
 		return nil, err
 	}
 	sh.principal = p
@@ -253,9 +255,8 @@
 // CustomCredentials encapsulates a Principal which can be shared with
 // one or more processes run by a Shell.
 type CustomCredentials struct {
-	p     security.Principal
-	agent *keymgr.Agent
-	id    []byte
+	p    security.Principal
+	path string
 }
 
 // Principal returns the Principal.
@@ -263,11 +264,10 @@
 	return c.p
 }
 
-// File returns a socket which can be used to connect to the agent
-// managing this principal. Typically you would pass this to a child
-// process.
-func (c *CustomCredentials) File() (*os.File, error) {
-	return c.agent.NewConnection(c.id)
+// Path returns the path to the credential's agent.
+// Typically you would pass this to a child process in EnvAgentPath.
+func (c *CustomCredentials) Path() string {
+	return c.path
 }
 
 func dup(conn *os.File) (int, error) {
@@ -289,26 +289,23 @@
 	if sh.ctx == nil {
 		return nil, nil
 	}
-	id, conn, err := sh.agent.NewPrincipal(sh.ctx, true)
+	id, err := sh.agent.NewPrincipal(true)
 	if err != nil {
 		return nil, err
 	}
-	fd, err := dup(conn)
-	conn.Close()
+	dir, err := ioutil.TempDir(sh.tempCredDir, "agent")
 	if err != nil {
 		return nil, err
 	}
-	ep, err := v23.NewEndpoint(agentlib.AgentEndpoint(fd))
-	if err != nil {
-		syscall.Close(fd)
+	path := filepath.Join(dir, "sock")
+	if err := sh.agent.ServePrincipal(id, path); err != nil {
 		return nil, err
 	}
-	p, err := agentlib.NewAgentPrincipal(sh.ctx, ep, v23.GetClient(sh.ctx))
+	p, err := agentlib.NewAgentPrincipalX(path)
 	if err != nil {
-		syscall.Close(fd)
 		return nil, err
 	}
-	return &CustomCredentials{p, sh.agent, id}, nil
+	return &CustomCredentials{p, path}, nil
 }
 
 // NewChildCredentials creates a new principal, served via the security agent
@@ -549,16 +546,13 @@
 		}
 	}
 
-	var p *os.File
+	var agentPath string
 	if opts.Credentials != nil {
-		p, err = opts.Credentials.File()
-		if err != nil {
-			return nil, err
-		}
+		agentPath = opts.Credentials.Path()
 	}
 
 	handle := info.factory()
-	h, err := handle.start(sh, p, &opts, sh.setupProgramEnv(env), sh.expand(args))
+	h, err := handle.start(sh, agentPath, &opts, sh.setupProgramEnv(env), sh.expand(args))
 	if err != nil {
 		return h, err
 	}
@@ -730,6 +724,7 @@
 	// by the shell's VeyronCredentials.
 	delete(m1, ref.EnvCredentials)
 	delete(m1, ref.EnvAgentEndpoint)
+	delete(m1, ref.EnvAgentPath)
 
 	m2 := envvar.MergeMaps(m1, evmap)
 	return envvar.MapToSlice(m2)