[mxio][ns] wire up mxio_ns_install()

Currently it only supports installation in processes without
an active namespace, but that's the only place we need to do
this right now.

Change-Id: If6cdc2a14cd6a654d491019afd92016655022287
diff --git a/system/ulib/mxio/namespace.c b/system/ulib/mxio/namespace.c
index 12ee6a6..1080144 100644
--- a/system/ulib/mxio/namespace.c
+++ b/system/ulib/mxio/namespace.c
@@ -661,12 +661,6 @@
     return MX_OK;
 }
 
-mx_status_t mxio_ns_install(mxio_ns_t* ns) {
-    //TODO
-    return MX_ERR_NOT_SUPPORTED;
-}
-
-
 static mx_status_t ns_enum_callback(mxvn_t* vn, void* cookie,
                                     mx_status_t (*func)(void* cookie, const char* path,
                                                         size_t len, mx_handle_t h)) {
diff --git a/system/ulib/mxio/unistd.c b/system/ulib/mxio/unistd.c
index 29ee670..f0cc685 100644
--- a/system/ulib/mxio/unistd.c
+++ b/system/ulib/mxio/unistd.c
@@ -595,6 +595,37 @@
     atexit(mxio_exit);
 }
 
+
+mx_status_t mxio_ns_install(mxio_ns_t* ns) {
+    mxio_t* io = mxio_ns_open_root(ns);
+    if (io == NULL) {
+        return MX_ERR_IO;
+    }
+
+    mxio_t* old_root = NULL;
+    mx_status_t status;
+
+    mtx_lock(&mxio_lock);
+    if (mxio_root_ns != NULL) {
+        //TODO: support replacing an active namespace
+        status = MX_ERR_ALREADY_EXISTS;
+    } else {
+        if (mxio_root_handle) {
+            old_root = mxio_root_handle;
+        }
+        mxio_root_handle = io;
+        status = MX_OK;
+    }
+    mtx_unlock(&mxio_lock);
+
+    if (old_root) {
+        mxio_close(old_root);
+        mxio_release(old_root);
+    }
+    return status;
+}
+
+
 mx_status_t mxio_clone_root(mx_handle_t* handles, uint32_t* types) {
     // The root handle is established in the init hook called from
     // libc startup (or, in the special case of devmgr, installed