Index: uspace/srv/ns/ns.c
===================================================================
--- uspace/srv/ns/ns.c	(revision fcd7053c1b7bca76e2258a6b2b5ce5b3b416f00f)
+++ uspace/srv/ns/ns.c	(revision 826a4e8d659d128b34fce6e71bd9c822d63c2ddf)
@@ -41,4 +41,5 @@
 #include <ipc/services.h>
 #include <stdio.h>
+#include <bool.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -48,4 +49,5 @@
 #include <libadt/hash_table.h>
 #include <sysinfo.h>
+#include <loader/loader.h>
 #include <ddi.h>
 #include <as.h>
@@ -57,4 +59,9 @@
 static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call);
 static int connect_to_service(ipcarg_t service, ipc_call_t *call,
+    ipc_callid_t callid);
+
+void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call,
+    ipc_callid_t callid);
+void connect_to_clonable(ipcarg_t service, ipc_call_t *call,
     ipc_callid_t callid);
 
@@ -84,4 +91,21 @@
 static void *clockaddr = NULL;
 static void *klogaddr = NULL;
+
+/** Request for connection to a clonable service. */
+typedef struct {
+	link_t link;
+	ipcarg_t service;
+	ipc_call_t *call;
+	ipc_callid_t callid;
+} cs_req_t;
+
+/** List of clonable-service connection requests. */
+static link_t cs_req;
+
+/** Return true if @a service is clonable. */
+static bool service_clonable(int service)
+{
+	return service == SERVICE_LOAD;
+}
 
 static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name,
@@ -117,4 +141,6 @@
 		return ENOMEM;
 	}
+
+	list_initialize(&cs_req);
 	
 	printf(NAME ": Accepting connections\n");
@@ -143,6 +169,12 @@
 			 * Server requests service registration.
 			 */
-			retval = register_service(IPC_GET_ARG1(call),
-			    IPC_GET_ARG5(call), &call);
+			if (service_clonable(IPC_GET_ARG1(call))) {
+				register_clonable(IPC_GET_ARG1(call),
+				    IPC_GET_ARG5(call), &call, callid);
+				continue;
+			} else {
+				retval = register_service(IPC_GET_ARG1(call),
+				    IPC_GET_ARG5(call), &call);
+			}
 			break;
 		case IPC_M_CONNECT_ME_TO:
@@ -150,6 +182,12 @@
 			 * Client requests to be connected to a service.
 			 */
-			retval = connect_to_service(IPC_GET_ARG1(call), &call,
-			    callid);
+			if (service_clonable(IPC_GET_ARG1(call))) {
+				connect_to_clonable(IPC_GET_ARG1(call),
+				    &call, callid);
+				continue;
+			} else {
+				retval = connect_to_service(IPC_GET_ARG1(call),
+				    &call, callid);
+			}
 			break;
 		default:
@@ -168,9 +206,9 @@
 /** Register service.
  *
- * @param service Service to be registered.
- * @param phone Phone to be used for connections to the service.
- * @param call Pointer to call structure.
- *
- * @return Zero on success or a value from @ref errno.h.
+ * @param service	Service to be registered.
+ * @param phone		Phone to be used for connections to the service.
+ * @param call		Pointer to call structure.
+ *
+ * @return		Zero on success or a value from @ref errno.h.
  */
 int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
@@ -182,5 +220,5 @@
 	};
 	hashed_service_t *hs;
-			
+
 	if (hash_table_find(&ns_hash_table, keys)) {
 		return EEXISTS;
@@ -203,7 +241,7 @@
 /** Connect client to service.
  *
- * @param service Service to be connected to.
- * @param call Pointer to call structure.
- * @param callid Call ID of the request.
+ * @param service	Service to be connected to.
+ * @param call		Pointer to call structure.
+ * @param callid	Call ID of the request.
  *
  * @return Zero on success or a value from @ref errno.h.
@@ -214,5 +252,5 @@
 	link_t *hlp;
 	hashed_service_t *hs;
-			
+
 	hlp = hash_table_find(&ns_hash_table, keys);
 	if (!hlp) {
@@ -222,4 +260,80 @@
 	return ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call), 
 		IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
+}
+
+/** Register clonable service.
+ *
+ * @param service	Service to be registered.
+ * @param phone		Phone to be used for connections to the service.
+ * @param call		Pointer to call structure.
+ */
+void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call,
+    ipc_callid_t callid)
+{
+	int rc;
+	cs_req_t *csr;
+
+	if (list_empty(&cs_req)) {
+		/* There was no pending connection request. */
+		printf(NAME ": Unexpected clonable server.\n");
+		ipc_answer_0(callid, EBUSY);
+		return;
+	}
+
+	csr = list_get_instance(cs_req.next, cs_req_t, link);
+	list_remove(&csr->link);
+
+	/* Currently we can only handle a single type of clonable service. */
+	assert(csr->service == SERVICE_LOAD);
+
+	ipc_answer_0(callid, EOK);
+
+	rc = ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(*csr->call),
+		IPC_GET_ARG3(*csr->call), 0, IPC_FF_NONE);
+
+	free(csr);
+}
+
+/** Connect client to clonable service.
+ *
+ * @param service	Service to be connected to.
+ * @param call		Pointer to call structure.
+ * @param callid	Call ID of the request.
+ *
+ * @return		Zero on success or a value from @ref errno.h.
+ */
+void connect_to_clonable(ipcarg_t service, ipc_call_t *call,
+    ipc_callid_t callid)
+{
+	int rc;
+	cs_req_t *csr;
+
+	assert(service == SERVICE_LOAD);
+
+	csr = malloc(sizeof(cs_req_t));
+	if (csr == NULL) {
+		ipc_answer_0(callid, ENOMEM);
+		return;
+	}
+
+	/* Spawn a loader. */
+	rc = loader_spawn("loader");
+
+	if (rc < 0) {
+		free(csr);
+		ipc_answer_0(callid, rc);
+		return;
+	}
+
+	csr->service = service;
+	csr->call = call;
+	csr->callid = callid;
+
+	/*
+	 * We can forward the call only after the server we spawned connects
+	 * to us. Meanwhile we might need to service more connection requests.
+	 * Thus we store the call in a queue.
+	 */
+	list_append(&csr->link, &cs_req);
 }
 
