[systemd-devel] [PATCH 1/2] domain: move compose logic on its own kdbus_domain_compose_path() function

Djalal Harouni tixxdz at opendz.org
Wed Mar 19 13:24:52 PDT 2014


Signed-off-by: Djalal Harouni <tixxdz at opendz.org>
---
 domain.c | 68 +++++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 37 insertions(+), 31 deletions(-)

diff --git a/domain.c b/domain.c
index 2e05e90..d27cad2 100644
--- a/domain.c
+++ b/domain.c
@@ -223,12 +223,44 @@ struct kdbus_domain *kdbus_domain_find_by_major(unsigned int major)
 	return domain;
 }
 
+/* Caller has validated parent and name arguments */
+static int kdbus_domain_compose_path(struct kdbus_domain *domain,
+				     struct kdbus_domain *parent,
+				     const char *name)
+{
+	struct kdbus_domain *exists;
+
+	if (!parent) {
+		/* initial domain */
+		domain->devpath = kstrdup(KBUILD_MODNAME, GFP_KERNEL);
+		if (!domain->devpath)
+			return -ENOMEM;
+	} else {
+		exists = kdbus_domain_find(parent, name);
+		if (exists) {
+			kdbus_domain_unref(exists);
+			return -EEXIST;
+		}
+
+		domain->devpath = kasprintf(GFP_KERNEL, "%s/domain/%s",
+					    parent->devpath, name);
+		if (!domain->devpath)
+			return -ENOMEM;
+
+		domain->name = kstrdup(name, GFP_KERNEL);
+		if (!domain->name)
+			return -ENOMEM;
+	}
+
+	return 0;
+}
+
 /**
  * kdbus_domain_new() - create a new domain
  * @parent:		Parent domain, NULL for initial one
  * @name:		Name of the domain, NULL for the initial one
  * @mode:		The access mode for the "control" device node
- * @domain:			The returned domain
+ * @domain:		The returned domain
  *
  * Return: 0 on success, negative errno on failure
  */
@@ -258,39 +290,13 @@ int kdbus_domain_new(struct kdbus_domain *parent, const char *name,
 
 	if (parent)
 		mutex_lock(&parent->lock);
+
 	mutex_lock(&kdbus_subsys_lock);
 
 	/* compose name and path of base directory in /dev */
-	if (!parent) {
-		/* initial domain */
-		d->devpath = kstrdup(KBUILD_MODNAME, GFP_KERNEL);
-		if (!d->devpath) {
-			ret = -ENOMEM;
-			goto exit_unlock;
-		}
-	} else {
-		struct kdbus_domain *exists;
-
-		exists = kdbus_domain_find(parent, name);
-		if (exists) {
-			kdbus_domain_unref(exists);
-			ret = -EEXIST;
-			goto exit_unlock;
-		}
-
-		d->devpath = kasprintf(GFP_KERNEL, "%s/domain/%s",
-				       parent->devpath, name);
-		if (!d->devpath) {
-			ret = -ENOMEM;
-			goto exit_unlock;
-		}
-
-		d->name = kstrdup(name, GFP_KERNEL);
-		if (!d->name) {
-			ret = -ENOMEM;
-			goto exit_unlock;
-		}
-	}
+	ret = kdbus_domain_compose_path(d, parent, name);
+	if (ret < 0)
+		goto exit_unlock;
 
 	/* get dynamic major */
 	ret = register_chrdev(0, d->devpath, &kdbus_device_ops);
-- 
1.8.5.3



More information about the systemd-devel mailing list