metadata: add kdbus_meta_dup()
authorDaniel Mack <daniel@zonque.org>
Wed, 8 Oct 2014 12:47:44 +0000 (14:47 +0200)
committerDaniel Mack <daniel@zonque.org>
Wed, 8 Oct 2014 15:30:01 +0000 (17:30 +0200)
Add a way to duplicate a metadata object. This will be needed to dup
conn->owner_meta and attach that to messages, instead of new items
retrieved from 'current'.

Signed-off-by: Daniel Mack <daniel@zonque.org>
metadata.c
metadata.h

index 17f6b61ead447f43a97a8de95abfcdf446123eab..4bb5c0ae0402a2dc31074fd85caeca155d2e32ea 100644 (file)
@@ -61,6 +61,42 @@ int kdbus_meta_new(struct kdbus_meta **meta)
        return 0;
 }
 
+/**
+ * kdbus_meta_dup() - Duplicate a meta object
+ *
+ * @orig:      The meta object to duplicate
+ * @copy:      Return pointer for the duplicated object
+ *
+ * Return: 0 on success, -ENOMEM on memory allocation failures.
+ */
+int kdbus_meta_dup(const struct kdbus_meta *orig,
+                  struct kdbus_meta **copy)
+{
+       struct kdbus_meta *m;
+
+       BUG_ON(!orig || !copy);
+
+       m = kmalloc(sizeof(*m), GFP_KERNEL);
+       if (!m)
+               return -ENOMEM;
+
+       m->data = kmemdup(orig->data, orig->allocated_size, GFP_KERNEL);
+       if (!m->data) {
+               kfree(m);
+               return -ENOMEM;
+       }
+
+       m->pid_namespace = get_pid_ns(orig->pid_namespace);
+       m->user_namespace = get_user_ns(orig->user_namespace);
+
+       m->attached = orig->attached;
+       m->allocated_size = orig->allocated_size;
+       m->size = orig->size;
+
+       *copy = m;
+       return 0;
+}
+
 /**
  * kdbus_meta_ns_eq() - check whether the namespaces of two metadata objects
  *                     are equal.
index 98c7a3187005d378b7b18d389d23cc8e7bc264a4..a2728f57a06f5816977a5f7e02581520301d9a22 100644 (file)
@@ -37,6 +37,8 @@ struct kdbus_meta {
 struct kdbus_conn;
 
 int kdbus_meta_new(struct kdbus_meta **meta);
+int kdbus_meta_dup(const struct kdbus_meta *orig,
+                  struct kdbus_meta **copy);
 int kdbus_meta_append_data(struct kdbus_meta *meta, u64 type,
                           const void *buf, size_t len);
 int kdbus_meta_append(struct kdbus_meta *meta,