Index: uspace/srv/devmap/devmap.c
===================================================================
--- uspace/srv/devmap/devmap.c	(revision 19f857a3b361739b06d4f547174ae4ac1b99c651)
+++ uspace/srv/devmap/devmap.c	(revision 957cfa58e9f28dbcaf9efd98ce73ec541a8166fe)
@@ -172,5 +172,11 @@
 		
 		*name = str_dup(fqdn);
-		if ((*name == NULL) || (str_cmp(*name, "") == 0)) {
+		if (*name == NULL) {
+			free(*ns_name);
+			return false;
+		}
+		
+		if (str_cmp(*name, "") == 0) {
+			free(*name);
 			free(*ns_name);
 			return false;
@@ -186,9 +192,15 @@
 	
 	*name = str_dup(fqdn + slash_after);
-	if ((*name == NULL) || (str_cmp(*name, "") == 0)) {
+	if (*name == NULL) {
 		free(*ns_name);
 		return false;
 	}
 	
+	if (str_cmp(*name, "") == 0) {
+		free(*name);
+		free(*ns_name);
+		return false;
+	}
+	
 	return true;
 }
@@ -202,11 +214,10 @@
 static devmap_namespace_t *devmap_namespace_find_name(const char *name)
 {
-	link_t *item = namespaces_list.next;
-	
-	while (item != &namespaces_list) {
-		devmap_namespace_t *namespace = list_get_instance(item, devmap_namespace_t, namespaces);
+	link_t *item;
+	for (item = namespaces_list.next; item != &namespaces_list; item = item->next) {
+		devmap_namespace_t *namespace =
+		    list_get_instance(item, devmap_namespace_t, namespaces);
 		if (str_cmp(namespace->name, name) == 0)
 			return namespace;
-		item = item->next;
 	}
 	
@@ -224,12 +235,10 @@
 static devmap_namespace_t *devmap_namespace_find_handle(dev_handle_t handle)
 {
-	link_t *item = namespaces_list.next;
-	
-	while (item != &namespaces_list) {
-		devmap_namespace_t *namespace = list_get_instance(item, devmap_namespace_t, namespaces);
+	link_t *item;
+	for (item = namespaces_list.next; item != &namespaces_list; item = item->next) {
+		devmap_namespace_t *namespace =
+		    list_get_instance(item, devmap_namespace_t, namespaces);
 		if (namespace->handle == handle)
 			return namespace;
-		
-		item = item->next;
 	}
 	
@@ -246,11 +255,11 @@
     const char *name)
 {
-	link_t *item = devices_list.next;
-	
-	while (item != &devices_list) {
-		devmap_device_t *device = list_get_instance(item, devmap_device_t, devices);
-		if ((str_cmp(device->namespace->name, ns_name) == 0) && (str_cmp(device->name, name) == 0))
+	link_t *item;
+	for (item = devices_list.next; item != &devices_list; item = item->next) {
+		devmap_device_t *device =
+		    list_get_instance(item, devmap_device_t, devices);
+		if ((str_cmp(device->namespace->name, ns_name) == 0)
+		    && (str_cmp(device->name, name) == 0))
 			return device;
-		item = item->next;
 	}
 	
@@ -268,12 +277,10 @@
 static devmap_device_t *devmap_device_find_handle(dev_handle_t handle)
 {
-	link_t *item = devices_list.next;
-	
-	while (item != &devices_list) {
-		devmap_device_t *device = list_get_instance(item, devmap_device_t, devices);
+	link_t *item;
+	for (item = devices_list.next; item != &devices_list; item = item->next) {
+		devmap_device_t *device =
+		    list_get_instance(item, devmap_device_t, devices);
 		if (device->handle == handle)
 			return device;
-		
-		item = item->next;
 	}
 	
@@ -367,5 +374,4 @@
 	list_remove(&(device->driver_devices));
 	
-	free(device->namespace);
 	free(device->name);
 	free(device);
@@ -386,6 +392,6 @@
 	}
 	
-	devmap_driver_t *driver = (devmap_driver_t *) malloc(sizeof(devmap_driver_t));
-	
+	devmap_driver_t *driver =
+	    (devmap_driver_t *) malloc(sizeof(devmap_driver_t));
 	if (driver == NULL) {
 		ipc_answer_0(iid, ENOMEM);
@@ -404,5 +410,25 @@
 	}
 	
-	/* Initialize mutex for list of devices owned by this driver */
+	/*
+	 * Create connection to the driver
+	 */
+	ipc_call_t call;
+	ipc_callid_t callid = async_get_call(&call);
+	
+	if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
+		free(driver->name);
+		free(driver);
+		ipc_answer_0(callid, ENOTSUP);
+		ipc_answer_0(iid, ENOTSUP);
+		return NULL;
+	}
+	
+	driver->phone = IPC_GET_ARG5(call);
+	ipc_answer_0(callid, EOK);
+	
+	/*
+	 * Initialize mutex for list of devices
+	 * owned by this driver
+	 */
 	fibril_mutex_initialize(&driver->devices_mutex);
 	
@@ -411,24 +437,4 @@
 	 */
 	list_initialize(&driver->devices);
-	
-	/*
-	 * Create connection to the driver
-	 */
-	ipc_call_t call;
-	ipc_callid_t callid = async_get_call(&call);
-	
-	if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
-		ipc_answer_0(callid, ENOTSUP);
-		
-		free(driver->name);
-		free(driver);
-		ipc_answer_0(iid, ENOTSUP);
-		return NULL;
-	}
-	
-	driver->phone = IPC_GET_ARG5(call);
-	
-	ipc_answer_0(callid, EOK);
-	
 	list_initialize(&(driver->drivers));
 	
@@ -436,5 +442,6 @@
 	
 	/* TODO:
-	 * check that no driver with name equal to driver->name is registered
+	 * Check that no driver with name equal to
+	 * driver->name is registered
 	 */
 	
@@ -482,5 +489,5 @@
 	fibril_mutex_unlock(&drivers_list_mutex);
 	
-	/* free name and driver */
+	/* Free name and driver */
 	if (driver->name != NULL)
 		free(driver->name);
@@ -503,5 +510,6 @@
 	
 	/* Create new device entry */
-	devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t));
+	devmap_device_t *device =
+	    (devmap_device_t *) malloc(sizeof(devmap_device_t));
 	if (device == NULL) {
 		ipc_answer_0(iid, ENOMEM);
@@ -533,6 +541,7 @@
 	devmap_namespace_t *namespace = devmap_namespace_create(ns_name);
 	free(ns_name);
-	if (!namespace) {
+	if (namespace == NULL) {
 		fibril_mutex_unlock(&devices_list_mutex);
+		free(device->name);
 		free(device);
 		ipc_answer_0(iid, ENOMEM);
@@ -545,8 +554,8 @@
 	/* Check that device is not already registered */
 	if (devmap_device_find_name(namespace->name, device->name) != NULL) {
-		printf(NAME ": Device '%s/%s' already registered\n", device->namespace, device->name);
+		printf("%s: Device '%s/%s' already registered\n", NAME,
+		    device->namespace, device->name);
 		devmap_namespace_destroy(namespace);
 		fibril_mutex_unlock(&devices_list_mutex);
-		free(device->namespace);
 		free(device->name);
 		free(device);
@@ -603,4 +612,5 @@
 	
 	if ((dev == NULL) || (dev->driver == NULL) || (dev->driver->phone == 0)) {
+		fibril_mutex_unlock(&devices_list_mutex);
 		ipc_answer_0(callid, ENOENT);
 		return;
@@ -668,7 +678,8 @@
 		return;
 	}
+	
+	ipc_answer_1(iid, EOK, dev->handle);
+	
 	fibril_mutex_unlock(&devices_list_mutex);
-	
-	ipc_answer_1(iid, EOK, dev->handle);
 	free(ns_name);
 	free(name);
@@ -719,7 +730,8 @@
 		return;
 	}
+	
+	ipc_answer_1(iid, EOK, namespace->handle);
+	
 	fibril_mutex_unlock(&devices_list_mutex);
-	
-	ipc_answer_1(iid, EOK, namespace->handle);
 	free(name);
 }
@@ -729,7 +741,9 @@
 	fibril_mutex_lock(&devices_list_mutex);
 	
-	devmap_namespace_t *namespace = devmap_namespace_find_handle(IPC_GET_ARG1(*icall));
+	devmap_namespace_t *namespace =
+	    devmap_namespace_find_handle(IPC_GET_ARG1(*icall));
 	if (namespace == NULL) {
-		devmap_device_t *dev = devmap_device_find_handle(IPC_GET_ARG1(*icall));
+		devmap_device_t *dev =
+		    devmap_device_find_handle(IPC_GET_ARG1(*icall));
 		if (dev == NULL)
 			ipc_answer_1(iid, EOK, DEV_HANDLE_NONE);
@@ -753,5 +767,6 @@
 	fibril_mutex_lock(&devices_list_mutex);
 	
-	devmap_namespace_t *namespace = devmap_namespace_find_handle(IPC_GET_ARG1(*icall));
+	devmap_namespace_t *namespace =
+	    devmap_namespace_find_handle(IPC_GET_ARG1(*icall));
 	if (namespace == NULL)
 		ipc_answer_0(iid, EEXISTS);
@@ -782,4 +797,5 @@
 	size_t count = size / sizeof(dev_desc_t);
 	if (count != list_count(&namespaces_list)) {
+		fibril_mutex_unlock(&devices_list_mutex);
 		ipc_answer_0(callid, EOVERFLOW);
 		ipc_answer_0(iid, EOVERFLOW);
@@ -789,4 +805,5 @@
 	dev_desc_t *desc = (dev_desc_t *) malloc(size);
 	if (desc == NULL) {
+		fibril_mutex_unlock(&devices_list_mutex);
 		ipc_answer_0(callid, ENOMEM);
 		ipc_answer_0(iid, ENOMEM);
@@ -794,14 +811,14 @@
 	}
 	
-	link_t *item = namespaces_list.next;
+	link_t *item;
 	size_t pos = 0;
-	while (item != &namespaces_list) {
-		devmap_namespace_t *namespace = list_get_instance(item, devmap_namespace_t, namespaces);
+	for (item = namespaces_list.next; item != &namespaces_list;
+	    item = item->next) {
+		devmap_namespace_t *namespace =
+		    list_get_instance(item, devmap_namespace_t, namespaces);
 		
 		desc[pos].handle = namespace->handle;
 		str_cpy(desc[pos].name, DEVMAP_NAME_MAXLEN, namespace->name);
 		pos++;
-		
-		item = item->next;
 	}
 	
@@ -835,5 +852,6 @@
 	fibril_mutex_lock(&devices_list_mutex);
 	
-	devmap_namespace_t *namespace = devmap_namespace_find_handle(IPC_GET_ARG1(*icall));
+	devmap_namespace_t *namespace =
+	    devmap_namespace_find_handle(IPC_GET_ARG1(*icall));
 	if (namespace == NULL) {
 		fibril_mutex_unlock(&devices_list_mutex);
@@ -845,4 +863,5 @@
 	size_t count = size / sizeof(dev_desc_t);
 	if (count != namespace->refcnt) {
+		fibril_mutex_unlock(&devices_list_mutex);
 		ipc_answer_0(callid, EOVERFLOW);
 		ipc_answer_0(iid, EOVERFLOW);
@@ -852,4 +871,5 @@
 	dev_desc_t *desc = (dev_desc_t *) malloc(size);
 	if (desc == NULL) {
+		fibril_mutex_unlock(&devices_list_mutex);
 		ipc_answer_0(callid, ENOMEM);
 		ipc_answer_0(iid, EREFUSED);
@@ -857,8 +877,9 @@
 	}
 	
-	link_t *item = devices_list.next;
+	link_t *item;
 	size_t pos = 0;
-	while (item != &devices_list) {
-		devmap_device_t *device = list_get_instance(item, devmap_device_t, devices);
+	for (item = devices_list.next; item != &devices_list; item = item->next) {
+		devmap_device_t *device =
+		    list_get_instance(item, devmap_device_t, devices);
 		
 		if (device->namespace == namespace) {
@@ -867,6 +888,4 @@
 			pos++;
 		}
-		
-		item = item->next;
 	}
 	
@@ -909,5 +928,6 @@
 	}
 	
-	devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t));
+	devmap_device_t *device =
+	    (devmap_device_t *) malloc(sizeof(devmap_device_t));
 	if (device == NULL) {
 		fibril_mutex_unlock(&null_devices_mutex);
@@ -919,5 +939,5 @@
 	
 	devmap_namespace_t *namespace = devmap_namespace_create("null");
-	if (!namespace) {
+	if (namespace == NULL) {
 		fibril_mutex_lock(&devices_list_mutex);
 		fibril_mutex_unlock(&null_devices_mutex);
@@ -949,9 +969,14 @@
 static void devmap_null_destroy(ipc_callid_t iid, ipc_call_t *icall)
 {
+	ipcarg_t i = IPC_GET_ARG1(*icall);
+	if (i >= NULL_DEVICES) {
+		ipc_answer_0(iid, ELIMIT);
+		return;
+	}
+	
 	fibril_mutex_lock(&null_devices_mutex);
 	
-	ipcarg_t i = IPC_GET_ARG1(*icall);
-	
 	if (null_devices[i] == NULL) {
+		fibril_mutex_unlock(&null_devices_mutex);
 		ipc_answer_0(iid, ENOENT);
 		return;
@@ -965,5 +990,4 @@
 	
 	fibril_mutex_unlock(&null_devices_mutex);
-	
 	ipc_answer_0(iid, EOK);
 }
@@ -1121,8 +1145,8 @@
 int main(int argc, char *argv[])
 {
-	printf(NAME ": HelenOS Device Mapper\n");
+	printf("%s: HelenOS Device Mapper\n", NAME);
 	
 	if (!devmap_init()) {
-		printf(NAME ": Error while initializing service\n");
+		printf("%s: Error while initializing service\n", NAME);
 		return -1;
 	}
@@ -1136,5 +1160,5 @@
 		return -1;
 	
-	printf(NAME ": Accepting connections\n");
+	printf("%s: Accepting connections\n", NAME);
 	async_manager();
 	
