Index: uspace/srv/ns/ns.c
===================================================================
--- uspace/srv/ns/ns.c	(revision a32c9bbabb841efbe2362e1a1795c93c564a3224)
+++ uspace/srv/ns/ns.c	(revision 1fcfc940d5ec6d6e9be0da114b9324a0b30b41c6)
@@ -29,9 +29,9 @@
 /** @addtogroup ns
  * @{
- */ 
+ */
 
 /**
- * @file	ns.c
- * @brief	Naming service for HelenOS IPC.
+ * @file  ns.c
+ * @brief Naming service for HelenOS IPC.
  */
 
@@ -53,10 +53,10 @@
 #include <as.h>
 
-#define NAME	"ns"
-
-#define NS_HASH_TABLE_CHAINS	20
+#define NAME  "ns"
+
+#define NS_HASH_TABLE_CHAINS  20
 
 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,
+static void connect_to_service(ipcarg_t service, ipc_call_t *call,
     ipc_callid_t callid);
 
@@ -65,4 +65,5 @@
 void connect_to_clonable(ipcarg_t service, ipc_call_t *call,
     ipc_callid_t callid);
+
 
 /* Static functions implementing NS hash table operations. */
@@ -84,11 +85,19 @@
 typedef struct {
 	link_t link;
-	ipcarg_t service;		/**< Number of the service. */
-	ipcarg_t phone;			/**< Phone registered with the service. */
-	ipcarg_t in_phone_hash;		/**< Incoming phone hash. */
+	ipcarg_t service;        /**< Number of the service. */
+	ipcarg_t phone;          /**< Phone registered with the service. */
+	ipcarg_t in_phone_hash;  /**< Incoming phone hash. */
 } hashed_service_t;
 
-static void *clockaddr = NULL;
-static void *klogaddr = NULL;
+/** Pending connection structure. */
+typedef struct {
+	link_t link;
+	ipcarg_t service;        /**< Number of the service. */
+	ipc_callid_t callid;     /**< Call ID waiting for the connection */
+	ipcarg_t arg2;           /**< Second argument */
+	ipcarg_t arg3;           /**< Third argument */
+} pending_req_t;
+
+static link_t pending_req;
 
 /** Request for connection to a clonable service. */
@@ -103,8 +112,11 @@
 static link_t cs_req;
 
+static void *clockaddr = NULL;
+static void *klogaddr = NULL;
+
 /** Return true if @a service is clonable. */
 static bool service_clonable(int service)
 {
-	return service == SERVICE_LOAD;
+	return (service == SERVICE_LOAD);
 }
 
@@ -129,24 +141,57 @@
 }
 
+/** Process pending connection requests */
+static void process_pending_req()
+{
+	link_t *cur;
+	
+loop:
+	for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
+		pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
+		
+		unsigned long keys[3] = {
+			pr->service,
+			0,
+			0
+		};
+		
+		link_t *link = hash_table_find(&ns_hash_table, keys);
+		if (!link)
+			continue;
+		
+		hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
+		ipcarg_t retval = ipc_forward_fast(pr->callid, hs->phone,
+		    pr->arg2, pr->arg3, 0, IPC_FF_NONE);
+		
+		if (!(pr->callid & IPC_CALLID_NOTIFICATION))
+			ipc_answer_0(pr->callid, retval);
+		
+		list_remove(cur);
+		free(pr);
+		goto loop;
+	}
+}
+
 int main(int argc, char **argv)
 {
 	printf(NAME ": HelenOS IPC Naming Service\n");
 	
-	ipc_call_t call;
-	ipc_callid_t callid;
-	
-	ipcarg_t retval;
-
 	if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3,
 	    &ns_hash_table_ops)) {
-		printf(NAME ": No memory available\n");
+		printf(NAME ": No memory available for services\n");
 		return ENOMEM;
 	}
-
+	
+	list_initialize(&pending_req);
 	list_initialize(&cs_req);
 	
 	printf(NAME ": Accepting connections\n");
-	while (1) {
-		callid = ipc_wait_for_call(&call);
+	while (true) {
+		process_pending_req();
+		
+		ipc_call_t call;
+		ipc_callid_t callid = ipc_wait_for_call(&call);
+		ipcarg_t retval;
+		
 		switch (IPC_GET_METHOD(call)) {
 		case IPC_M_SHARE_IN:
@@ -187,6 +232,7 @@
 				continue;
 			} else {
-				retval = connect_to_service(IPC_GET_ARG1(call),
-				    &call, callid);
+				connect_to_service(IPC_GET_ARG1(call), &call,
+				    callid);
+				continue;
 			}
 			break;
@@ -195,7 +241,7 @@
 			break;
 		}
-		if (!(callid & IPC_CALLID_NOTIFICATION)) {
+		
+		if (!(callid & IPC_CALLID_NOTIFICATION))
 			ipc_answer_0(callid, retval);
-		}
 	}
 	
@@ -206,9 +252,10 @@
 /** 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)
@@ -219,15 +266,12 @@
 		0
 	};
-	hashed_service_t *hs;
-
-	if (hash_table_find(&ns_hash_table, keys)) {
+	
+	if (hash_table_find(&ns_hash_table, keys))
 		return EEXISTS;
-	}
-			
-	hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
-	if (!hs) {
+	
+	hashed_service_t *hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
+	if (!hs)
 		return ENOMEM;
-	}
-			
+	
 	link_initialize(&hs->link);
 	hs->service = service;
@@ -235,43 +279,66 @@
 	hs->in_phone_hash = call->in_phone_hash;
 	hash_table_insert(&ns_hash_table, keys, &hs->link);
+	
+	return 0;
+}
+
+/** Connect client to 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_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
+{
+	ipcarg_t retval;
+	unsigned long keys[3] = {
+		service,
+		0,
+		0
+	};
+	
+	link_t *link = hash_table_find(&ns_hash_table, keys);
+	if (!link) {
+		if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) {
+			/* Blocking connection, add to pending list */
+			pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t));
+			if (!pr) {
+				retval = ENOMEM;
+				goto out;
+			}
 			
-	return 0;
-}
-
-/** Connect client to 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.
- */
-int connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
-{
-	unsigned long keys[3] = { service, 0, 0 };
-	link_t *hlp;
-	hashed_service_t *hs;
-
-	hlp = hash_table_find(&ns_hash_table, keys);
-	if (!hlp) {
-		return ENOENT;
-	}
-	hs = hash_table_get_instance(hlp, hashed_service_t, link);
-	return ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call), 
-		IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
+			pr->service = service;
+			pr->callid = callid;
+			pr->arg2 = IPC_GET_ARG2(*call);
+			pr->arg3 = IPC_GET_ARG3(*call);
+			list_append(&pr->link, &pending_req);
+			return;
+		}
+		retval = ENOENT;
+		goto out;
+	}
+	
+	hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
+	retval = ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
+	    IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
+	
+out:
+	if (!(callid & IPC_CALLID_NOTIFICATION))
+		ipc_answer_0(callid, retval);
 }
 
 /** 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.
+ * @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. */
@@ -280,16 +347,16 @@
 		return;
 	}
-
-	csr = list_get_instance(cs_req.next, cs_req_t, link);
+	
+	cs_req_t *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),
+	
+	int rc = ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(csr->call),
 		IPC_GET_ARG3(csr->call), 0, IPC_FF_NONE);
-
+	
 	free(csr);
 }
@@ -297,27 +364,25 @@
 /** 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.
+ * @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));
+	
+	cs_req_t *csr = malloc(sizeof(cs_req_t));
 	if (csr == NULL) {
 		ipc_answer_0(callid, ENOMEM);
 		return;
 	}
-
+	
 	/* Spawn a loader. */
-	rc = loader_spawn("loader");
-
+	int rc = loader_spawn("loader");
+	
 	if (rc < 0) {
 		free(csr);
@@ -325,9 +390,9 @@
 		return;
 	}
-
+	
 	csr->service = service;
 	csr->call = *call;
 	csr->callid = callid;
-
+	
 	/*
 	 * We can forward the call only after the server we spawned connects
@@ -341,11 +406,13 @@
  *
  * @param key Pointer keys. However, only the first key (i.e. service number)
- * 	      is used to compute the hash index.
+ *            is used to compute the hash index.
+ *
  * @return Hash index corresponding to key[0].
+ *
  */
 hash_index_t ns_hash(unsigned long *key)
 {
 	assert(key);
-	return *key % NS_HASH_TABLE_CHAINS;
+	return (*key % NS_HASH_TABLE_CHAINS);
 }
 
@@ -358,18 +425,18 @@
  * as a nasty hack.
  *
- * @param key Array of keys.
+ * @param key  Array of keys.
  * @param keys Must be lesser or equal to 3.
  * @param item Pointer to a hash table item.
+ *
  * @return Non-zero if the key matches the item, zero otherwise.
+ *
  */
 int ns_compare(unsigned long key[], hash_count_t keys, link_t *item)
 {
-	hashed_service_t *hs;
-
 	assert(key);
 	assert(keys <= 3);
 	assert(item);
 	
-	hs = hash_table_get_instance(item, hashed_service_t, link);
+	hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link);
 	
 	if (keys == 2)
@@ -382,4 +449,5 @@
  *
  * @param item Item that was removed from the hash table.
+ *
  */
 void ns_remove(link_t *item)
@@ -389,5 +457,5 @@
 }
 
-/** 
+/**
  * @}
  */
