veyron/lib/modules/core: Remove reliance on rt.R().

Change-Id: I689e6bf794010c791b9db7cc1d2c4632862c5013
diff --git a/lib/modules/core/core.go b/lib/modules/core/core.go
index 214d127..e6519d9 100644
--- a/lib/modules/core/core.go
+++ b/lib/modules/core/core.go
@@ -50,20 +50,14 @@
 
 const (
 	// Functions
-	LSCommand                = "ls"
-	SetNamespaceRootsCommand = "setRoots"
-	ResolveCommand           = "resolve"
-	ResolveMTCommand         = "resolveMT"
-	SleepCommand             = "sleep"
-	TimeCommand              = "time"
-	MountCommand             = "mount"
-	NamespaceCacheCommand    = "cache"
+	SleepCommand = "sleep"
+	TimeCommand  = "time"
 	// Subprocesses
 	EchoServerCommand  = "echoServer"
 	EchoClientCommand  = "echoClient"
 	RootMTCommand      = "root"
 	MTCommand          = "mt"
-	LSExternalCommand  = "lse"
+	LSCommand          = "ls"
 	ProxyServerCommand = "proxyd"
 	WSPRCommand        = "wsprd"
 	ShellCommand       = "sh"
diff --git a/lib/modules/core/core_test.go b/lib/modules/core/core_test.go
index 86bbb0e..eeae40e 100644
--- a/lib/modules/core/core_test.go
+++ b/lib/modules/core/core_test.go
@@ -6,12 +6,9 @@
 	"reflect"
 	"sort"
 	"strconv"
-	"strings"
 	"testing"
 	"time"
 
-	"veyron.io/veyron/veyron2/naming"
-	"veyron.io/veyron/veyron2/rt"
 	"veyron.io/veyron/veyron2/vlog"
 
 	"veyron.io/veyron/veyron/lib/expect"
@@ -34,7 +31,6 @@
 
 func init() {
 	testutil.Init()
-	rt.Init()
 }
 
 func newShell(t *testing.T) (*modules.Shell, func()) {
@@ -135,44 +131,14 @@
 	defer fn()
 
 	mountPoints := []string{"a", "b", "c", "d", "e"}
-	mountAddrs, fn, err := startMountTables(t, sh, mountPoints...)
+	_, fn, err := startMountTables(t, sh, mountPoints...)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
 	defer fn()
 
-	rootName := mountAddrs["root"]
-	ls, err := sh.Start(core.LSCommand, nil, rootName+"/...")
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
-	lsSession := expect.NewSession(t, ls.Stdout(), time.Minute)
-	lsSession.SetVerbosity(testing.Verbose())
-
-	if got, want := lsSession.ExpectVar("RN"), strconv.Itoa(len(mountPoints)+1); got != want {
-		t.Fatalf("got %v, want %v", got, want)
-	}
-	lsSession.Expect("R0=" + rootName)
-
-	// Look for names that correspond to the mountpoints above (i.e, a, b or c)
-	pattern := ""
-	for _, n := range mountPoints {
-		pattern = pattern + "^R[\\d]+=(" + rootName + "/(" + n + ")$)|"
-	}
-	pattern = pattern[:len(pattern)-1]
-
-	found := []string{}
-	for i := 0; i < len(mountPoints); i++ {
-		found = append(found, getMatchingMountpoint(lsSession.ExpectRE(pattern, 1)))
-	}
-	sort.Strings(found)
-	sort.Strings(mountPoints)
-	if got, want := found, mountPoints; !reflect.DeepEqual(got, want) {
-		t.Errorf("got %v, want %v", got, want)
-	}
-
 	// Run the ls command in a subprocess, with consts.NamespaceRootPrefix as set above.
-	lse, err := sh.Start(core.LSExternalCommand, nil, "...")
+	lse, err := sh.Start(core.LSCommand, nil, "...")
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -183,7 +149,7 @@
 		t.Fatalf("got %v, want %v", got, want)
 	}
 
-	pattern = ""
+	pattern := ""
 	for _, n := range mountPoints {
 		// Since the LSExternalCommand runs in a subprocess with
 		// consts.NamespaceRootPrefix set to the name of the root mount
@@ -192,7 +158,7 @@
 		pattern = pattern + "^R[\\d]+=(" + n + "$)|"
 	}
 	pattern = pattern[:len(pattern)-1]
-	found = []string{}
+	found := []string{}
 	for i := 0; i < len(mountPoints); i++ {
 		found = append(found, getMatchingMountpoint(lseSession.ExpectRE(pattern, 1)))
 	}
@@ -225,90 +191,6 @@
 	srv.Shutdown(nil, nil)
 }
 
-func TestResolve(t *testing.T) {
-	sh, fn := newShell(t)
-	defer fn()
-
-	mountPoints := []string{"a", "b"}
-	mountAddrs, fn, err := startMountTables(t, sh, mountPoints...)
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
-	defer fn()
-	rootName := mountAddrs["root"]
-	mtName := "b"
-	echoName := naming.Join(mtName, "echo")
-	srv, err := sh.Start(core.EchoServerCommand, nil, testArgs("test", echoName)...)
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
-	srvSession := expect.NewSession(t, srv.Stdout(), time.Minute)
-	srvSession.ExpectVar("NAME")
-	addr := srvSession.ExpectVar("ADDR")
-	addr = naming.JoinAddressName(addr, "")
-	wsAddr := strings.Replace(addr, "@tcp@", "@ws@", 1)
-
-	// Resolve an object
-	resolver, err := sh.Start(core.ResolveCommand, nil, rootName+"/"+echoName)
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
-	resolverSession := expect.NewSession(t, resolver.Stdout(), time.Minute)
-	if got, want := resolverSession.ExpectVar("RN"), "2"; got != want {
-		t.Fatalf("got %v, want %v", got, want)
-	}
-	if got, want := resolverSession.ExpectVar("R0"), addr; got != want && got != wsAddr {
-		t.Errorf("got %v, want either %v or %v", got, want, wsAddr)
-	}
-	if err = resolver.Shutdown(nil, os.Stderr); err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
-
-	// Resolve to a mount table using a rooted name.
-	addr = naming.JoinAddressName(mountAddrs[mtName], "echo")
-	wsAddr = strings.Replace(addr, "@tcp@", "@ws@", 1)
-	resolver, err = sh.Start(core.ResolveMTCommand, nil, rootName+"/"+echoName)
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
-	resolverSession = expect.NewSession(t, resolver.Stdout(), time.Minute)
-	if got, want := resolverSession.ExpectVar("RN"), "2"; got != want {
-		t.Fatalf("got %v, want %v", got, want)
-	}
-	if got, want := resolverSession.ExpectVar("R0"), addr; got != want && got != wsAddr {
-		t.Fatalf("got %v, want either %v or %v", got, want, wsAddr)
-	}
-	if err := resolver.Shutdown(nil, os.Stderr); err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
-
-	// Resolve to a mount table, but using a relative name.
-	nsroots, err := sh.Start(core.SetNamespaceRootsCommand, nil, rootName)
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
-	if err := nsroots.Shutdown(nil, os.Stderr); err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
-
-	resolver, err = sh.Start(core.ResolveMTCommand, nil, echoName)
-	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
-	resolverSession = expect.NewSession(t, resolver.Stdout(), time.Minute)
-	if got, want := resolverSession.ExpectVar("RN"), "2"; got != want {
-		t.Fatalf("got %v, want %v", got, want)
-	}
-	if got, want := resolverSession.ExpectVar("R0"), addr; got != want && got != wsAddr {
-		t.Fatalf("got %v, want either %v or %v", got, want, wsAddr)
-	}
-	if err := resolver.Shutdown(nil, os.Stderr); err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
-	srv.Shutdown(nil, nil)
-	nsroots.Shutdown(nil, nil)
-}
-
 func TestHelperProcess(t *testing.T) {
 	modules.DispatchInTest()
 }
diff --git a/lib/modules/core/echo.go b/lib/modules/core/echo.go
index 9d4b819..5f82c34 100644
--- a/lib/modules/core/echo.go
+++ b/lib/modules/core/echo.go
@@ -48,6 +48,12 @@
 }
 
 func echoServer(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
+	runtime, err := rt.New()
+	if err != nil {
+		panic(err)
+	}
+	defer runtime.Cleanup()
+
 	fl, args, err := parseListenFlags(args)
 	if err != nil {
 		return fmt.Errorf("failed to parse args: %s", err)
@@ -57,7 +63,7 @@
 	}
 	id, mp := args[0], args[1]
 	disp := &treeDispatcher{id: id}
-	server, err := rt.R().NewServer()
+	server, err := runtime.NewServer()
 	if err != nil {
 		return err
 	}
@@ -77,12 +83,18 @@
 }
 
 func echoClient(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
+	runtime, err := rt.New()
+	if err != nil {
+		panic(err)
+	}
+	defer runtime.Cleanup()
+
 	args = args[1:]
 	name := args[0]
 	args = args[1:]
-	client := rt.R().Client()
+	client := runtime.Client()
 	for _, a := range args {
-		ctxt := rt.R().NewContext()
+		ctxt := runtime.NewContext()
 		h, err := client.StartCall(ctxt, name, "Echo", []interface{}{a})
 		if err != nil {
 			return err
diff --git a/lib/modules/core/misc.go b/lib/modules/core/misc.go
index 35b54c2..df75b59 100644
--- a/lib/modules/core/misc.go
+++ b/lib/modules/core/misc.go
@@ -5,23 +5,14 @@
 	"io"
 	"time"
 
-	"veyron.io/veyron/veyron2/naming"
-	"veyron.io/veyron/veyron2/rt"
-
 	"veyron.io/veyron/veyron/lib/modules"
 )
 
 func init() {
-
-	modules.RegisterFunction(NamespaceCacheCommand, `on|off
-	turns the namespace cache on or off`, namespaceCache)
-	modules.RegisterFunction(MountCommand, `<mountpoint> <server> <ttl> [M][R]
-	invokes namespace.Mount(<mountpoint>, <server>, <ttl>)`, mountServer)
 	modules.RegisterFunction(SleepCommand, `[duration]
 	sleep for a time(in go time.Duration format): defaults to 1s`, sleep)
 	modules.RegisterFunction(TimeCommand, `
 	prints the current time`, now)
-
 }
 
 func sleep(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
@@ -53,48 +44,3 @@
 	fmt.Fprintf(stdout, "%s\n", time.Now())
 	return nil
 }
-
-func mountServer(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
-	if err := checkArgs(args[1:], -3, "<mount point> <server> <ttl> [M][R]"); err != nil {
-		return err
-	}
-	var opts []naming.MountOpt
-	for _, arg := range args[4:] {
-		for _, c := range arg {
-			switch c {
-			case 'R':
-				opts = append(opts, naming.ReplaceMountOpt(true))
-			case 'M':
-				opts = append(opts, naming.ServesMountTableOpt(true))
-			}
-		}
-	}
-	mp, server, ttlstr := args[1], args[2], args[3]
-	ttl, err := time.ParseDuration(ttlstr)
-	if err != nil {
-		return fmt.Errorf("failed to parse time from %q", ttlstr)
-	}
-	ns := rt.R().Namespace()
-	if err := ns.Mount(rt.R().NewContext(), mp, server, ttl, opts...); err != nil {
-		return err
-	}
-	fmt.Fprintf(stdout, "Mount(%s, %s, %s, %v)\n", mp, server, ttl, opts)
-	return nil
-}
-
-func namespaceCache(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
-	if err := checkArgs(args[1:], 1, "on|off"); err != nil {
-		return err
-	}
-	disable := true
-	switch args[1] {
-	case "on":
-		disable = false
-	case "off":
-		disable = true
-	default:
-		return fmt.Errorf("arg must be 'on' or 'off'")
-	}
-	rt.R().Namespace().CacheCtl(naming.DisableCache(disable))
-	return nil
-}
diff --git a/lib/modules/core/mounttable.go b/lib/modules/core/mounttable.go
index bee0e5d..066cf10 100644
--- a/lib/modules/core/mounttable.go
+++ b/lib/modules/core/mounttable.go
@@ -6,7 +6,6 @@
 	"os"
 	"strings"
 
-	"veyron.io/veyron/veyron2/context"
 	"veyron.io/veyron/veyron2/naming"
 	"veyron.io/veyron/veyron2/options"
 	"veyron.io/veyron/veyron2/rt"
@@ -19,21 +18,9 @@
 	modules.RegisterChild(RootMTCommand, "", rootMountTable)
 	modules.RegisterChild(MTCommand, `<mount point>
 	reads NAMESPACE_ROOT from its environment and mounts a new mount table at <mount point>`, mountTable)
-	modules.RegisterChild(LSExternalCommand, `<glob>...
+	modules.RegisterChild(LSCommand, `<glob>...
 	issues glob requests using the current processes namespace library`,
 		ls)
-	modules.RegisterFunction(LSCommand, `<glob>...
-	issues glob requests using the current processes namespace library`, ls)
-	modules.RegisterFunction(ResolveCommand, `<name>
-	resolves name to obtain an object server address`, resolveObject)
-	modules.RegisterFunction(ResolveMTCommand, `<name>
-	resolves name to obtain a mount table address`, resolveMT)
-	modules.RegisterFunction(SetNamespaceRootsCommand, `<name>...
-	set the in-process namespace roots to <name>...`, setNamespaceRoots)
-	modules.RegisterFunction(NamespaceCacheCommand, `on|off
-	turns the namespace cache on or off`, namespaceCache)
-	modules.RegisterFunction(MountCommand, `<mountpoint> <server> <ttl> [M][R]
-	invokes namespace.Mount(<mountpoint>, <server>, <ttl>)`, mountServer)
 }
 
 func mountTable(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
@@ -45,7 +32,12 @@
 }
 
 func runMT(root bool, stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
-	r := rt.R()
+	r, err := rt.New()
+	if err != nil {
+		panic(err)
+	}
+	defer r.Cleanup()
+
 	fl, args, err := parseListenFlags(args)
 	if err != nil {
 		return fmt.Errorf("failed to parse args: %s", err)
@@ -82,17 +74,23 @@
 }
 
 func ls(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
+	r, err := rt.New()
+	if err != nil {
+		panic(err)
+	}
+	defer r.Cleanup()
+
 	details := false
 	args = args[1:] // skip over command name
 	if len(args) > 0 && args[0] == "-l" {
 		details = true
 		args = args[1:]
 	}
-	ns := rt.R().Namespace()
+	ns := r.Namespace()
 	entry := 0
 	output := ""
 	for _, pattern := range args {
-		ch, err := ns.Glob(rt.R().NewContext(), pattern)
+		ch, err := ns.Glob(r.NewContext(), pattern)
 		if err != nil {
 			return err
 		}
@@ -119,34 +117,3 @@
 	fmt.Fprint(stdout, output)
 	return nil
 }
-
-type resolver func(ctx context.T, name string, opts ...naming.ResolveOpt) (names []string, err error)
-
-func resolve(fn resolver, stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
-	if err := checkArgs(args[1:], 1, "<name>"); err != nil {
-		return err
-	}
-	name := args[1]
-	servers, err := fn(rt.R().NewContext(), name)
-	if err != nil {
-		fmt.Fprintf(stdout, "RN=0\n")
-		return err
-	}
-	fmt.Fprintf(stdout, "RN=%d\n", len(servers))
-	for i, s := range servers {
-		fmt.Fprintf(stdout, "R%d=%s\n", i, s)
-	}
-	return nil
-}
-
-func resolveObject(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
-	return resolve(rt.R().Namespace().Resolve, stdin, stdout, stderr, env, args...)
-}
-
-func resolveMT(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
-	return resolve(rt.R().Namespace().ResolveToMountTable, stdin, stdout, stderr, env, args...)
-}
-
-func setNamespaceRoots(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
-	return rt.R().Namespace().SetRoots(args[1:]...)
-}
diff --git a/lib/modules/core/proxy.go b/lib/modules/core/proxy.go
index 4cba574..d1d2477 100644
--- a/lib/modules/core/proxy.go
+++ b/lib/modules/core/proxy.go
@@ -44,7 +44,13 @@
 	pname := naming.JoinAddressName(proxy.Endpoint().String(), "")
 	fmt.Fprintf(stdout, "PROXY_ADDR=%s\n", proxy.Endpoint().String())
 	fmt.Fprintf(stdout, "PROXY_NAME=%s\n", pname)
-	r := rt.R()
+
+	r, err := rt.New()
+	if err != nil {
+		panic(err)
+	}
+	defer r.Cleanup()
+
 	pub := publisher.New(r.NewContext(), r.Namespace(), time.Minute)
 	defer pub.WaitForStop()
 	defer pub.Stop()
diff --git a/runtimes/google/ipc/client_test.go b/runtimes/google/ipc/client_test.go
index bad7a3d..a98eca7 100644
--- a/runtimes/google/ipc/client_test.go
+++ b/runtimes/google/ipc/client_test.go
@@ -29,7 +29,11 @@
 
 func init() {
 	modules.RegisterChild("ping", "<name>", childPing)
-	r = rt.Init()
+	var err error
+	if r, err = rt.New(); err != nil {
+		panic(err)
+	}
+
 	r.Namespace().CacheCtl(naming.DisableCache(true))
 }
 
@@ -55,7 +59,9 @@
 		t.Fatalf("%s", rootSession.Error())
 	}
 	sh.SetVar(consts.NamespaceRootPrefix, rootName)
-	r.Namespace().SetRoots(rootName)
+	if err = r.Namespace().SetRoots(rootName); err != nil {
+		t.Fatalf("unexpected error setting namespace roots: %s", err)
+	}
 
 	deferFn := func() {
 		if testing.Verbose() {
@@ -84,20 +90,12 @@
 	return nil
 }
 
-func numServers(t *testing.T, sh *modules.Shell, name string) string {
-	r, err := sh.Start(core.ResolveCommand, nil, "echoServer")
+func numServers(t *testing.T, name string) int {
+	servers, err := r.Namespace().Resolve(r.NewContext(), name)
 	if err != nil {
-		t.Fatalf("unexpected error: %s", err)
+		return 0
 	}
-	s := expect.NewSession(t, r.Stdout(), time.Minute)
-	rn := s.ExpectVar("RN")
-	return rn
-}
-
-func mount(t *testing.T, r veyron2.Runtime, name, server string) {
-	if err := r.Namespace().Mount(r.NewContext(), name, server, time.Hour); err != nil {
-		t.Fatalf("unexpected error: %s", err)
-	}
+	return len(servers)
 }
 
 // TODO(cnicolaou): figure out how to test and see what the internals
@@ -115,16 +113,19 @@
 	runClient(t, sh)
 
 	// Create a fake set of 100 entries in the mount table
+	ctx := r.NewContext()
 	for i := 0; i < 100; i++ {
 		// 203.0.113.0 is TEST-NET-3 from RFC5737
 		ep := naming.FormatEndpoint("tcp", fmt.Sprintf("203.0.113.%d:443", i))
 		n := naming.JoinAddressName(ep, "")
-		mount(t, r, "echoServer", n)
+		if err := r.Namespace().Mount(ctx, "echoServer", n, time.Hour); err != nil {
+			t.Fatalf("unexpected error: %s", err)
+		}
 	}
 
 	// Verify that there are 102 entries for echoServer in the mount table.
-	if got, want := numServers(t, sh, "echoServer"), "102"; got != want {
-		t.Fatalf("got: %q, want: %q", got, want)
+	if got, want := numServers(t, "echoServer"), 102; got != want {
+		t.Fatalf("got: %d, want: %d", got, want)
 	}
 
 	// TODO(cnicolaou): ok, so it works, but I'm not sure how
@@ -137,16 +138,13 @@
 	srv.CloseStdin()
 	srv.Shutdown(nil, nil)
 
-	// TODO(cnicolaou,p): figure out why the real entry isn't removed
-	// from the mount table.
 	// Verify that there are 100 entries for echoServer in the mount table.
-	if got, want := numServers(t, sh, "echoServer"), "100"; got != want {
-		t.Fatalf("got: %q, want: %q", got, want)
+	if got, want := numServers(t, "echoServer"), 100; got != want {
+		t.Fatalf("got: %d, want: %d", got, want)
 	}
 }
 
 func TestTimeoutCall(t *testing.T) {
-	r := rt.Init()
 	client := r.Client()
 	ctx, _ := r.NewContext().WithTimeout(100 * time.Millisecond)
 	name := naming.JoinAddressName(naming.FormatEndpoint("tcp", "203.0.113.10:443"), "")
@@ -198,10 +196,6 @@
 
 func childPing(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
 	name := args[1]
-	r, err := rt.New()
-	if err != nil {
-		return fmt.Errorf("unexpected error: %s", err)
-	}
 	call, err := r.Client().StartCall(r.NewContext(), name, "Ping", nil)
 	if err != nil {
 		fmt.Errorf("unexpected error: %s", err)
diff --git a/services/mgmt/node/impl/util_test.go b/services/mgmt/node/impl/util_test.go
index 0d5acf2..de14eb0 100644
--- a/services/mgmt/node/impl/util_test.go
+++ b/services/mgmt/node/impl/util_test.go
@@ -66,15 +66,10 @@
 	return creds, []string{consts.VeyronCredentials + "=" + creds}
 }
 
-// setNSRoots sets the roots for the local runtime's namespace using the modules
-// function SetNamesaceRootCommand.
-func setNSRoots(t *testing.T, sh *modules.Shell, roots ...string) {
-	setRootsHandle, err := sh.Start(core.SetNamespaceRootsCommand, nil, roots...)
-	if err != nil {
-		t.Fatalf("%s: unexpected error: %s", loc(2), err)
-	}
-	if err := setRootsHandle.Shutdown(nil, os.Stderr); err != nil {
-		t.Fatalf("%s: SetNamespaceRootsCommand failed with %v", loc(2), err)
+// setNSRoots sets the roots for the local runtime's namespace.
+func setNSRoots(t *testing.T, roots ...string) {
+	if err := rt.R().Namespace().SetRoots(roots...); err != nil {
+		t.Fatalf("%s: SetRoots(%v) failed with %v", loc(2), roots, err)
 	}
 }
 
@@ -110,12 +105,9 @@
 		}
 		vlog.VI(1).Info("--(done shutting down root mt)---")
 		vlog.VI(1).Info("--------- DONE CLEANUP ----------")
-		// Calling sh.Start after sh.Cleanup is not a good idea.
-		// TODO(caprita): set the roots by hand and avoid running the
-		// corresponding modules command.
-		setNSRoots(t, sh, oldNamespaceRoots...)
+		setNSRoots(t, oldNamespaceRoots...)
 	}
-	setNSRoots(t, sh, mtName)
+	setNSRoots(t, mtName)
 	sh.SetVar(consts.NamespaceRootPrefix, mtName)
 	return sh, fn
 }
diff --git a/tools/naming/simulator/ambiguity.scr b/tools/naming/simulator/ambiguity.scr
index 8af8cad..5db56ad 100644
--- a/tools/naming/simulator/ambiguity.scr
+++ b/tools/naming/simulator/ambiguity.scr
@@ -46,7 +46,7 @@
 ls $s1/...
 set l=$_
 eval $l
-assert $RN 4
+assert $RN 5
 wait $l
 
 # Returns the mount at s1/a.
diff --git a/tools/naming/simulator/driver.go b/tools/naming/simulator/driver.go
index 410877d..32e9ff9 100644
--- a/tools/naming/simulator/driver.go
+++ b/tools/naming/simulator/driver.go
@@ -16,6 +16,7 @@
 	"time"
 	"unicode"
 
+	"veyron.io/veyron/veyron2"
 	"veyron.io/veyron/veyron2/rt"
 
 	"veyron.io/veyron/veyron/lib/expect"
@@ -94,8 +95,14 @@
 	}
 }
 
+var runtime veyron2.Runtime
+
 func main() {
-	rt.Init()
+	var err error
+	if runtime, err = rt.New(); err != nil {
+		panic(err)
+	}
+	defer runtime.Cleanup()
 
 	// Subprocesses commands are run by fork/execing this binary
 	// so we must test to see if this instance is a subprocess or the
diff --git a/tools/naming/simulator/mt_complex.scr b/tools/naming/simulator/mt_complex.scr
index 103d5f0..4a1757f 100644
--- a/tools/naming/simulator/mt_complex.scr
+++ b/tools/naming/simulator/mt_complex.scr
@@ -87,12 +87,12 @@
 ls ...
 set l=$_
 eval $l
-assert $RN 1
+assert $RN 2
 
 ls $root/...
 set l=$_
 eval $l
-assert $RN 2
+assert $RN 3
 
 echoClient tl test
 read $_ o
@@ -257,7 +257,7 @@
 mount tl/b/symlink /$mt_b_addr/$symlink_target 1h M
 wait $_
 
-ls -l tl/b/symlink
+ls -- -l tl/b/symlink
 wait $_
 
 resolve tl/b/symlink
diff --git a/tools/naming/simulator/mt_simple.scr b/tools/naming/simulator/mt_simple.scr
index b051c2f..8c4d9c0 100644
--- a/tools/naming/simulator/mt_simple.scr
+++ b/tools/naming/simulator/mt_simple.scr
@@ -36,7 +36,7 @@
 assert $RN 7
 wait $l
 
-ls -l $root/...
+ls -- -l $root/...
 wait $_
 
 resolve $root/usa
diff --git a/tools/naming/simulator/proxy.scr b/tools/naming/simulator/proxy.scr
index 8421133..ec514f3 100644
--- a/tools/naming/simulator/proxy.scr
+++ b/tools/naming/simulator/proxy.scr
@@ -88,9 +88,9 @@
 assertOneOf $PROXY_ADDR $ep1_addr $ep2_addr $ep3_addr $ep4_addr
 assert $rid $ECHOS_RID
 
-ls -l echo/withproxy
+ls -- -l echo/withproxy
 wait $_
-ls -l echo/noproxy
+ls -- -l echo/noproxy
 wait $_
 
 echoClient echo/withproxy "ohh"
diff --git a/tools/naming/simulator/shell_functions.go b/tools/naming/simulator/shell_functions.go
new file mode 100644
index 0000000..56875cc
--- /dev/null
+++ b/tools/naming/simulator/shell_functions.go
@@ -0,0 +1,118 @@
+package main
+
+import (
+	"fmt"
+	"io"
+	"time"
+
+	"veyron.io/veyron/veyron2/context"
+	"veyron.io/veyron/veyron2/naming"
+
+	"veyron.io/veyron/veyron/lib/modules"
+)
+
+func init() {
+	modules.RegisterFunction("cache", `on|off
+turns the namespace cache on or off`, namespaceCache)
+	modules.RegisterFunction("mount", `<mountpoint> <server> <ttl> [M][R]
+invokes namespace.Mount(<mountpoint>, <server>, <ttl>)`, mountServer)
+	modules.RegisterFunction("resolve", `<name>
+resolves name to obtain an object server address`, resolveObject)
+	modules.RegisterFunction("resolveMT", `<name>
+resolves name to obtain a mount table address`, resolveMT)
+	modules.RegisterFunction("setRoots", `<name>...
+set the in-process namespace roots to <name>...`, setNamespaceRoots)
+}
+
+// checkArgs checks for the expected number of args in args. A negative
+// value means at least that number of args are expected.
+func checkArgs(args []string, expected int, usage string) error {
+	got := len(args)
+	if expected < 0 {
+		expected = -expected
+		if got < expected {
+			return fmt.Errorf("wrong # args (got %d, expected >=%d) expected: %q got: %v", got, expected, usage, args)
+		}
+	} else {
+		if got != expected {
+			return fmt.Errorf("wrong # args (got %d, expected %d) expected: %q got: %v", got, expected, usage, args)
+		}
+	}
+	return nil
+}
+
+func mountServer(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
+	if err := checkArgs(args[1:], -3, "<mount point> <server> <ttl> [M][R]"); err != nil {
+		return err
+	}
+	var opts []naming.MountOpt
+	for _, arg := range args[4:] {
+		for _, c := range arg {
+			switch c {
+			case 'R':
+				opts = append(opts, naming.ReplaceMountOpt(true))
+			case 'M':
+				opts = append(opts, naming.ServesMountTableOpt(true))
+			}
+		}
+	}
+	mp, server, ttlstr := args[1], args[2], args[3]
+	ttl, err := time.ParseDuration(ttlstr)
+	if err != nil {
+		return fmt.Errorf("failed to parse time from %q", ttlstr)
+	}
+	ns := runtime.Namespace()
+	if err := ns.Mount(runtime.NewContext(), mp, server, ttl, opts...); err != nil {
+		return err
+	}
+	fmt.Fprintf(stdout, "Mount(%s, %s, %s, %v)\n", mp, server, ttl, opts)
+	return nil
+}
+
+func namespaceCache(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
+	if err := checkArgs(args[1:], 1, "on|off"); err != nil {
+		return err
+	}
+	disable := true
+	switch args[1] {
+	case "on":
+		disable = false
+	case "off":
+		disable = true
+	default:
+		return fmt.Errorf("arg must be 'on' or 'off'")
+	}
+	runtime.Namespace().CacheCtl(naming.DisableCache(disable))
+	return nil
+}
+
+type resolver func(ctx context.T, name string, opts ...naming.ResolveOpt) (names []string, err error)
+
+func resolve(fn resolver, stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
+	if err := checkArgs(args[1:], 1, "<name>"); err != nil {
+		return err
+	}
+	name := args[1]
+	servers, err := fn(runtime.NewContext(), name)
+	if err != nil {
+		fmt.Fprintf(stdout, "RN=0\n")
+		return err
+	}
+	fmt.Fprintf(stdout, "RN=%d\n", len(servers))
+	for i, s := range servers {
+		fmt.Fprintf(stdout, "R%d=%s\n", i, s)
+	}
+	return nil
+}
+
+func resolveObject(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
+	return resolve(runtime.Namespace().Resolve, stdin, stdout, stderr, env, args...)
+}
+
+func resolveMT(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
+	return resolve(runtime.Namespace().ResolveToMountTable, stdin, stdout, stderr, env, args...)
+}
+
+func setNamespaceRoots(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
+	return runtime.Namespace().SetRoots(args[1:]...)
+}