From 88d219e62b03a56767b1fb9f41693033701e589e Mon Sep 17 00:00:00 2001 From: Daniel Mack Date: Fri, 31 Oct 2014 10:49:35 +0100 Subject: [PATCH] convert *_new() functions to return the new object directly Al Viro writes: BTW, the calling conventions for your foo_new() are annoying - instead of "return -E... or 0, storing the reference to new object in var parameter passed as the last argument", could you please just return ERR_PTR(-E...) on error, a pointer to new object on success and to hell with those struct foo **foo in the argument lists? Signed-off-by: Daniel Mack --- bus.c | 46 ++++++++++++------------- bus.h | 11 +++--- connection.c | 97 +++++++++++++++++++++++++++------------------------- connection.h | 7 ++-- domain.c | 20 +++++------ domain.h | 4 +-- endpoint.c | 19 +++++----- endpoint.h | 6 ++-- handle.c | 40 ++++++++++++++-------- main.c | 5 +-- match.c | 10 +++--- match.h | 2 +- message.c | 35 +++++++------------ message.h | 7 ++-- metadata.c | 27 ++++++--------- metadata.h | 5 ++- names.c | 10 +++--- names.h | 2 +- notify.c | 27 +++++++-------- pool.c | 14 +++----- pool.h | 2 +- 21 files changed, 188 insertions(+), 208 deletions(-) diff --git a/bus.c b/bus.c index 6dcaf22..592f819 100644 --- a/bus.c +++ b/bus.c @@ -268,41 +268,37 @@ exit_free_slice: * @mode: The access mode for the device node * @uid: The uid of the device node * @gid: The gid of the device node - * @bus: Pointer to a reference where the new bus is stored * * This function will allocate a new kdbus_bus and link it to the given * domain. * - * Return: 0 on success, negative errno on failure. + * Return: a new kdbus_bus object on success, ERR_PTR value on failure. */ -int kdbus_bus_new(struct kdbus_domain *domain, - const struct kdbus_cmd_make *make, - const char *name, - const struct kdbus_bloom_parameter *bloom, - umode_t mode, kuid_t uid, kgid_t gid, - struct kdbus_bus **bus) +struct kdbus_bus *kdbus_bus_new(struct kdbus_domain *domain, + const struct kdbus_cmd_make *make, + const char *name, + const struct kdbus_bloom_parameter *bloom, + umode_t mode, kuid_t uid, kgid_t gid) { struct kdbus_bus *b; char prefix[16]; int ret; - BUG_ON(*bus); - /* enforce "$UID-" prefix */ snprintf(prefix, sizeof(prefix), "%u-", from_kuid(current_user_ns(), uid)); if (strncmp(name, prefix, strlen(prefix) != 0)) - return -EINVAL; + return ERR_PTR(-EINVAL); b = kdbus_bus_find(domain, name); if (b) { kdbus_bus_unref(b); - return -EEXIST; + return ERR_PTR(-EEXIST); } b = kzalloc(sizeof(*b), GFP_KERNEL); if (!b) - return -ENOMEM; + return ERR_PTR(-ENOMEM); kref_init(&b->kref); b->uid_owner = uid; @@ -324,9 +320,9 @@ int kdbus_bus_new(struct kdbus_domain *domain, generate_random_uuid(b->id128); /* cache the metadata/credentials of the creator */ - ret = kdbus_meta_new(&b->meta); - if (ret < 0) - return ret; + b->meta = kdbus_meta_new(); + if (IS_ERR(b->meta)) + return ERR_PTR(PTR_ERR(b->meta)); ret = kdbus_meta_append(b->meta, NULL, 0, KDBUS_ATTACH_CREDS | @@ -347,13 +343,17 @@ int kdbus_bus_new(struct kdbus_domain *domain, goto exit_free; } - ret = kdbus_name_registry_new(&b->name_registry); - if (ret < 0) + b->name_registry = kdbus_name_registry_new(); + if (IS_ERR(b->name_registry)) { + ret = PTR_ERR(b->name_registry); goto exit_free_name; + } - ret = kdbus_ep_new(b, "bus", mode, uid, gid, false, &b->ep); - if (ret < 0) + b->ep = kdbus_ep_new(b, "bus", mode, uid, gid, false); + if (IS_ERR(b->ep) < 0) { + ret = PTR_ERR(b->ep); goto exit_free_reg; + } /* link into domain */ mutex_lock(&domain->lock); @@ -378,8 +378,7 @@ int kdbus_bus_new(struct kdbus_domain *domain, list_add_tail(&b->domain_entry, &domain->bus_list); mutex_unlock(&domain->lock); - *bus = b; - return 0; + return b; exit_unref_user_unlock: mutex_unlock(&domain->lock); @@ -395,7 +394,8 @@ exit_free: kdbus_policy_db_clear(&b->policy_db); kdbus_domain_unref(b->domain); kfree(b); - return ret; + + return ERR_PTR(ret); } /** diff --git a/bus.h b/bus.h index fd9d843..427c267 100644 --- a/bus.h +++ b/bus.h @@ -88,12 +88,11 @@ struct kdbus_bus { int kdbus_bus_make_user(const struct kdbus_cmd_make *make, char **name, struct kdbus_bloom_parameter *bloom); -int kdbus_bus_new(struct kdbus_domain *domain, - const struct kdbus_cmd_make *make, - const char *name, - const struct kdbus_bloom_parameter *bloom, - umode_t mode, kuid_t uid, kgid_t gid, - struct kdbus_bus **bus); +struct kdbus_bus *kdbus_bus_new(struct kdbus_domain *domain, + const struct kdbus_cmd_make *make, + const char *name, + const struct kdbus_bloom_parameter *bloom, + umode_t mode, kuid_t uid, kgid_t gid); int kdbus_cmd_bus_creator_info(struct kdbus_conn *conn, struct kdbus_cmd_info *cmd_info); struct kdbus_bus *kdbus_bus_ref(struct kdbus_bus *bus); diff --git a/connection.c b/connection.c index 5b1f3ed..ec121d6 100644 --- a/connection.c +++ b/connection.c @@ -77,10 +77,10 @@ struct kdbus_conn_reply { int err; }; -static int kdbus_conn_reply_new(struct kdbus_conn_reply **reply_wait, - struct kdbus_conn *reply_dst, - const struct kdbus_msg *msg, - struct kdbus_name_entry *name_entry) +static struct kdbus_conn_reply * +kdbus_conn_reply_new(struct kdbus_conn *reply_dst, + const struct kdbus_msg *msg, + struct kdbus_name_entry *name_entry) { bool sync = msg->flags & KDBUS_MSG_FLAGS_SYNC_REPLY; struct kdbus_conn_reply *r; @@ -109,13 +109,13 @@ static int kdbus_conn_reply_new(struct kdbus_conn_reply **reply_wait, r->waiting = true; } - *reply_wait = r; - exit_dec_reply_count: - if (ret < 0) + if (ret < 0) { atomic_dec(&reply_dst->reply_count); + return ERR_PTR(ret); + } - return ret; + return r; } static void __kdbus_conn_reply_free(struct kref *kref) @@ -752,12 +752,12 @@ int kdbus_conn_kmsg_send(struct kdbus_ep *ep, * and take care not to augment it by attaching any new items. */ if (conn_src->owner_meta) - ret = kdbus_meta_dup(conn_src->owner_meta, &kmsg->meta); + kmsg->meta = kdbus_meta_dup(conn_src->owner_meta); else - ret = kdbus_meta_new(&kmsg->meta); + kmsg->meta = kdbus_meta_new(); - if (ret < 0) - return ret; + if (IS_ERR(kmsg->meta)) + return PTR_ERR(kmsg->meta); } if (msg->dst_id == KDBUS_DST_ID_BROADCAST) { @@ -854,10 +854,12 @@ int kdbus_conn_kmsg_send(struct kdbus_ep *ep, if (ret < 0) goto exit_unref; - ret = kdbus_conn_reply_new(&reply_wait, conn_src, msg, - name_entry); - if (ret < 0) + reply_wait = kdbus_conn_reply_new(conn_src, msg, + name_entry); + if (IS_ERR(reply_wait)) { + ret = PTR_ERR(reply_wait); goto exit_unref; + } } else { ret = kdbus_conn_check_access(ep, msg, conn_src, conn_dst, &reply_wake); @@ -1328,9 +1330,11 @@ int kdbus_cmd_info(struct kdbus_conn *conn, */ flags = cmd_info->flags & (KDBUS_ATTACH_NAMES | KDBUS_ATTACH_CONN_NAME); if (flags) { - ret = kdbus_meta_new(&meta); - if (ret < 0) + meta = kdbus_meta_new(); + if (IS_ERR(meta)) { + ret = PTR_ERR(meta); goto exit; + } ret = kdbus_meta_append(meta, owner_conn, 0, flags); if (ret < 0) @@ -1444,14 +1448,12 @@ int kdbus_cmd_conn_update(struct kdbus_conn *conn, * @ep: The endpoint the connection is connected to * @hello: The kdbus_cmd_hello as passed in by the user * @meta: The metadata gathered at open() time of the handle - * @c: Returned connection * - * Return: 0 on success, negative errno on failure + * Return: a new kdbus_conn on success, ERR_PTR on failure */ -int kdbus_conn_new(struct kdbus_ep *ep, - struct kdbus_cmd_hello *hello, - struct kdbus_meta *meta, - struct kdbus_conn **c) +struct kdbus_conn *kdbus_conn_new(struct kdbus_ep *ep, + struct kdbus_cmd_hello *hello, + struct kdbus_meta *meta) { #ifdef CONFIG_DEBUG_LOCK_ALLOC static struct lock_class_key __key; @@ -1469,37 +1471,35 @@ int kdbus_conn_new(struct kdbus_ep *ep, bool is_monitor; int ret; - BUG_ON(*c); - is_monitor = hello->flags & KDBUS_HELLO_MONITOR; is_activator = hello->flags & KDBUS_HELLO_ACTIVATOR; is_policy_holder = hello->flags & KDBUS_HELLO_POLICY_HOLDER; /* can't be activator or policy holder and monitor at the same time */ if (is_monitor && (is_activator || is_policy_holder)) - return -EINVAL; + return ERR_PTR(-EINVAL); /* can't be policy holder and activator at the same time */ if (is_activator && is_policy_holder) - return -EINVAL; + return ERR_PTR(-EINVAL); /* only privileged connections can activate and monitor */ if (!kdbus_bus_uid_is_privileged(bus) && (is_activator || is_policy_holder || is_monitor)) - return -EPERM; + return ERR_PTR(-EPERM); KDBUS_ITEMS_FOREACH(item, hello->items, KDBUS_ITEMS_SIZE(hello, items)) { switch (item->type) { case KDBUS_ITEM_NAME: if (!is_activator && !is_policy_holder) - return -EINVAL; + return ERR_PTR(-EINVAL); if (name) - return -EINVAL; + return ERR_PTR(-EINVAL); if (!kdbus_name_is_valid(item->str, true)) - return -EINVAL; + return ERR_PTR(-EINVAL); name = item->str; break; @@ -1507,10 +1507,10 @@ int kdbus_conn_new(struct kdbus_ep *ep, case KDBUS_ITEM_CREDS: /* privileged processes can impersonate somebody else */ if (!kdbus_bus_uid_is_privileged(bus)) - return -EPERM; + return ERR_PTR(-EPERM); if (item->size != KDBUS_ITEM_SIZE(sizeof(*creds))) - return -EINVAL; + return ERR_PTR(-EINVAL); creds = &item->creds; break; @@ -1518,7 +1518,7 @@ int kdbus_conn_new(struct kdbus_ep *ep, case KDBUS_ITEM_SECLABEL: /* privileged processes can impersonate somebody else */ if (!kdbus_bus_uid_is_privileged(bus)) - return -EPERM; + return ERR_PTR(-EPERM); seclabel = item->str; seclabel_len = item->size - KDBUS_ITEM_HEADER_SIZE; @@ -1527,7 +1527,7 @@ int kdbus_conn_new(struct kdbus_ep *ep, case KDBUS_ITEM_CONN_NAME: /* human-readable connection name (debugging) */ if (conn_name) - return -EINVAL; + return ERR_PTR(-EINVAL); conn_name = item->str; break; @@ -1535,11 +1535,11 @@ int kdbus_conn_new(struct kdbus_ep *ep, } if ((is_activator || is_policy_holder) && !name) - return -EINVAL; + return ERR_PTR(-EINVAL); conn = kzalloc(sizeof(*conn), GFP_KERNEL); if (!conn) - return -ENOMEM; + return ERR_PTR(-ENOMEM); if (is_activator || is_policy_holder) { /* @@ -1580,13 +1580,17 @@ int kdbus_conn_new(struct kdbus_ep *ep, /* init entry, so we can unconditionally remove it */ INIT_LIST_HEAD(&conn->monitor_entry); - ret = kdbus_pool_new(conn->name, &conn->pool, hello->pool_size); - if (ret < 0) + conn->pool = kdbus_pool_new(conn->name, hello->pool_size); + if (IS_ERR(conn->pool)) { + ret = PTR_ERR(conn->pool); goto exit_unref_cred; + } - ret = kdbus_match_db_new(&conn->match_db); - if (ret < 0) + conn->match_db = kdbus_match_db_new(); + if (IS_ERR(conn->match_db)) { + ret = PTR_ERR(conn->match_db); goto exit_free_pool; + } conn->bus = kdbus_bus_ref(ep->bus); conn->ep = kdbus_ep_ref(ep); @@ -1622,9 +1626,11 @@ int kdbus_conn_new(struct kdbus_ep *ep, /* privileged processes can impersonate somebody else */ if (creds || seclabel) { - ret = kdbus_meta_new(&conn->owner_meta); - if (ret < 0) + conn->owner_meta = kdbus_meta_new(); + if (IS_ERR(conn->owner_meta)) { + ret = PTR_ERR(conn->owner_meta); goto exit_release_names; + } if (creds) { ret = kdbus_meta_append_data(conn->owner_meta, @@ -1698,8 +1704,7 @@ int kdbus_conn_new(struct kdbus_ep *ep, kdbus_notify_flush(conn->bus); - *c = conn; - return 0; + return conn; exit_unref_user_unlock: up_write(&bus->conn_rwlock); @@ -1723,7 +1728,7 @@ exit_free_conn: kfree(conn->name); kfree(conn); - return ret; + return ERR_PTR(ret); } /** diff --git a/connection.h b/connection.h index 01a5bd8..ed7f635 100644 --- a/connection.h +++ b/connection.h @@ -103,10 +103,9 @@ struct kdbus_conn { struct kdbus_kmsg; struct kdbus_name_registry; -int kdbus_conn_new(struct kdbus_ep *ep, - struct kdbus_cmd_hello *hello, - struct kdbus_meta *meta, - struct kdbus_conn **conn); +struct kdbus_conn *kdbus_conn_new(struct kdbus_ep *ep, + struct kdbus_cmd_hello *hello, + struct kdbus_meta *meta); struct kdbus_conn *kdbus_conn_ref(struct kdbus_conn *conn); struct kdbus_conn *kdbus_conn_unref(struct kdbus_conn *conn); int kdbus_conn_acquire(struct kdbus_conn *conn); diff --git a/domain.c b/domain.c index eb2ce72..561b0bf 100644 --- a/domain.c +++ b/domain.c @@ -192,24 +192,21 @@ static struct kdbus_domain *kdbus_domain_find(struct kdbus_domain *parent, * @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 * - * Return: 0 on success, negative errno on failure + * Return: a new kdbus_domain on success, ERR_PTR on failure */ -int kdbus_domain_new(struct kdbus_domain *parent, const char *name, - umode_t mode, struct kdbus_domain **domain) +struct kdbus_domain *kdbus_domain_new(struct kdbus_domain *parent, + const char *name, umode_t mode) { struct kdbus_domain *d; int ret; - BUG_ON(*domain); - if ((parent && !name) || (!parent && name)) - return -EINVAL; + return ERR_PTR(-EINVAL); d = kzalloc(sizeof(*d), GFP_KERNEL); if (!d) - return -ENOMEM; + return ERR_PTR(-ENOMEM); d->disconnected = true; INIT_LIST_HEAD(&d->bus_list); @@ -297,15 +294,14 @@ int kdbus_domain_new(struct kdbus_domain *parent, const char *name, if (ret < 0) { kdbus_domain_disconnect(d); kdbus_domain_unref(d); - return ret; + return ERR_PTR(ret); } - *domain = d; - return 0; + return d; exit_put: put_device(&d->dev); - return ret; + return ERR_PTR(ret); } /** diff --git a/domain.h b/domain.h index f51cdb5..116fda9 100644 --- a/domain.h +++ b/domain.h @@ -89,8 +89,8 @@ extern struct bus_type kdbus_subsys; struct kdbus_domain *kdbus_domain_ref(struct kdbus_domain *domain); struct kdbus_domain *kdbus_domain_unref(struct kdbus_domain *domain); void kdbus_domain_disconnect(struct kdbus_domain *domain); -int kdbus_domain_new(struct kdbus_domain *parent, const char *name, - umode_t mode, struct kdbus_domain **domain); +struct kdbus_domain *kdbus_domain_new(struct kdbus_domain *parent, + const char *name, umode_t mode); int kdbus_domain_get_user_unlocked(struct kdbus_domain *domain, kuid_t uid, diff --git a/endpoint.c b/endpoint.c index 8304360..8254cfc 100644 --- a/endpoint.c +++ b/endpoint.c @@ -150,23 +150,22 @@ static struct kdbus_ep *kdbus_ep_find(struct kdbus_bus *bus, const char *name) * @uid: The uid of the device node * @gid: The gid of the device node * @policy: Whether or not the endpoint should have a policy db - * @ep: Pointer to a reference where the new endpoint is stored * * This function will create a new enpoint with the given * name and properties for a given bus. * - * Return: 0 on success, negative errno on failure. + * Return: a new kdbus_ep on success, ERR_PTR on failure. */ -int kdbus_ep_new(struct kdbus_bus *bus, const char *name, - umode_t mode, kuid_t uid, kgid_t gid, - bool policy, struct kdbus_ep **ep) +struct kdbus_ep *kdbus_ep_new(struct kdbus_bus *bus, const char *name, + umode_t mode, kuid_t uid, kgid_t gid, + bool policy) { struct kdbus_ep *e; int ret; e = kzalloc(sizeof(*e), GFP_KERNEL); if (!e) - return -ENOMEM; + return ERR_PTR(-ENOMEM); e->disconnected = true; mutex_init(&e->lock); @@ -231,16 +230,14 @@ int kdbus_ep_new(struct kdbus_bus *bus, const char *name, if (ret < 0) { kdbus_ep_disconnect(e); kdbus_ep_unref(e); - return ret; + return ERR_PTR(ret); } - if (ep) - *ep = e; - return 0; + return e; exit_put: put_device(&e->dev); - return ret; + return ERR_PTR(ret); } /** diff --git a/endpoint.h b/endpoint.h index 19cb2d3..80a3feb 100644 --- a/endpoint.h +++ b/endpoint.h @@ -59,9 +59,9 @@ struct kdbus_ep { bool has_policy : 1; }; -int kdbus_ep_new(struct kdbus_bus *bus, const char *name, - umode_t mode, kuid_t uid, kgid_t gid, - bool policy, struct kdbus_ep **ep); +struct kdbus_ep *kdbus_ep_new(struct kdbus_bus *bus, const char *name, + umode_t mode, kuid_t uid, kgid_t gid, + bool policy); struct kdbus_ep *kdbus_ep_ref(struct kdbus_ep *ep); struct kdbus_ep *kdbus_ep_unref(struct kdbus_ep *ep); void kdbus_ep_disconnect(struct kdbus_ep *ep); diff --git a/handle.c b/handle.c index 1481057..f1c5a8d 100644 --- a/handle.c +++ b/handle.c @@ -285,9 +285,11 @@ static int kdbus_handle_open(struct inode *inode, struct file *file) handle->domain = kdbus_domain_ref(handle->ep->bus->domain); /* cache the metadata/credentials of the creator */ - ret = kdbus_meta_new(&handle->meta); - if (ret < 0) + handle->meta = kdbus_meta_new(); + if (IS_ERR(handle->meta)) { + ret = PTR_ERR(handle->meta); goto exit_free; + } ret = kdbus_meta_append(handle->meta, NULL, 0, KDBUS_ATTACH_CREDS | @@ -481,10 +483,12 @@ static long kdbus_handle_ioctl_control(struct file *file, unsigned int cmd, gid = current_fsgid(); } - ret = kdbus_bus_new(handle->domain, make, name, &bloom, - mode, current_fsuid(), gid, &bus); - if (ret < 0) + bus = kdbus_bus_new(handle->domain, make, name, &bloom, + mode, current_fsuid(), gid); + if (IS_ERR(bus)) { + ret = PTR_ERR(bus); break; + } /* turn the control fd into a new bus owner device */ ret = kdbus_handle_transform(handle, KDBUS_HANDLE_CONTROL, @@ -534,9 +538,11 @@ static long kdbus_handle_ioctl_control(struct file *file, unsigned int cmd, if (make->flags & KDBUS_MAKE_ACCESS_WORLD) mode = 0666; - ret = kdbus_domain_new(handle->domain, name, mode, &domain); - if (ret < 0) + domain = kdbus_domain_new(handle->domain, name, mode); + if (IS_ERR(domain)) { + ret = PTR_ERR(domain); break; + } /* turn the control fd into a new domain owner device */ ret = kdbus_handle_transform(handle, KDBUS_HANDLE_CONTROL, @@ -615,10 +621,12 @@ static long kdbus_handle_ioctl_ep(struct file *file, unsigned int cmd, } /* custom endpoints always have a policy db */ - ret = kdbus_ep_new(handle->ep->bus, name, mode, - current_fsuid(), gid, true, &ep); - if (ret < 0) + ep = kdbus_ep_new(handle->ep->bus, name, mode, + current_fsuid(), gid, true); + if (IS_ERR(ep)) { + ret = PTR_ERR(ep); break; + } ret = kdbus_ep_policy_set(ep, make->items, KDBUS_ITEMS_SIZE(make, items)); @@ -683,9 +691,11 @@ static long kdbus_handle_ioctl_ep(struct file *file, unsigned int cmd, break; } - ret = kdbus_conn_new(handle->ep, hello, handle->meta, &conn); - if (ret < 0) + conn = kdbus_conn_new(handle->ep, hello, handle->meta); + if (IS_ERR(conn)) { + ret = PTR_ERR(conn); break; + } /* turn the ep fd into a new connection */ ret = kdbus_handle_transform(handle, KDBUS_HANDLE_EP, @@ -985,9 +995,11 @@ static long kdbus_handle_ioctl_ep_connected(struct file *file, unsigned int cmd, break; } - ret = kdbus_kmsg_new_from_user(conn, buf, &kmsg); - if (ret < 0) + kmsg = kdbus_kmsg_new_from_user(conn, buf); + if (IS_ERR(kmsg)) { + ret = PTR_ERR(kmsg); break; + } ret = kdbus_conn_kmsg_send(conn->ep, conn, kmsg); if (ret < 0) { diff --git a/main.c b/main.c index caa4aab..5b855c1 100644 --- a/main.c +++ b/main.c @@ -40,8 +40,9 @@ static int __init kdbus_init(void) * Create the initial domain; it is world-accessible and * provides the /dev/kdbus/control device node. */ - ret = kdbus_domain_new(NULL, NULL, 0666, &kdbus_domain_init); - if (ret < 0) { + kdbus_domain_init = kdbus_domain_new(NULL, NULL, 0666); + if (IS_ERR(kdbus_domain_init) < 0) { + ret = PTR_ERR(kdbus_domain_init); pr_err("failed to initialize, error=%i\n", ret); goto exit_minor; } diff --git a/match.c b/match.c index 86458a6..8b9fc9f 100644 --- a/match.c +++ b/match.c @@ -148,23 +148,21 @@ void kdbus_match_db_free(struct kdbus_match_db *db) /** * kdbus_match_db_new() - create a new match database - * @db: Pointer location for the returned database * - * Return: 0 on success, negative errno on failure. + * Return: a new kdbus_match_db on success, ERR_PTR on failure. */ -int kdbus_match_db_new(struct kdbus_match_db **db) +struct kdbus_match_db *kdbus_match_db_new(void) { struct kdbus_match_db *d; d = kzalloc(sizeof(*d), GFP_KERNEL); if (!d) - return -ENOMEM; + return ERR_PTR(-ENOMEM); mutex_init(&d->entries_lock); INIT_LIST_HEAD(&d->entries_list); - *db = d; - return 0; + return d; } static bool kdbus_match_bloom(const struct kdbus_bloom_filter *filter, diff --git a/match.h b/match.h index 7288808..2237fb7 100644 --- a/match.h +++ b/match.h @@ -18,7 +18,7 @@ struct kdbus_conn; struct kdbus_kmsg; struct kdbus_match_db; -int kdbus_match_db_new(struct kdbus_match_db **db); +struct kdbus_match_db *kdbus_match_db_new(void); void kdbus_match_db_free(struct kdbus_match_db *db); int kdbus_match_db_add(struct kdbus_conn *conn, struct kdbus_cmd_match *cmd); diff --git a/message.c b/message.c index 8550d62..f726af7 100644 --- a/message.c +++ b/message.c @@ -56,27 +56,23 @@ void kdbus_kmsg_free(struct kdbus_kmsg *kmsg) /** * kdbus_kmsg_new() - allocate message * @extra_size: additional size to reserve for data - * @kmsg: Returned Message * - * Return: 0 on success, negative errno on failure. + * Return: new kdbus_kmsg on success, ERR_PTR on failure. */ -int kdbus_kmsg_new(size_t extra_size, struct kdbus_kmsg **kmsg) +struct kdbus_kmsg *kdbus_kmsg_new(size_t extra_size) { struct kdbus_kmsg *m; size_t size; - BUG_ON(*kmsg); - size = sizeof(struct kdbus_kmsg) + KDBUS_ITEM_SIZE(extra_size); m = kzalloc(size, GFP_KERNEL); if (!m) - return -ENOMEM; + return ERR_PTR(-ENOMEM); m->msg.size = size - KDBUS_KMSG_HEADER_SIZE; m->msg.items[0].size = KDBUS_ITEM_SIZE(extra_size); - *kmsg = m; - return 0; + return m; } static int kdbus_handle_check_file(struct file *file) @@ -324,34 +320,30 @@ static int kdbus_msg_scan_items(struct kdbus_conn *conn, * kdbus_kmsg_new_from_user() - copy message from user memory * @conn: Connection * @msg: User-provided message - * @kmsg: Copy of message * - * Return: 0 on success, negative errno on failure. + * Return: a new kdbus_kmsg on success, ERR_PTR on failure. */ -int kdbus_kmsg_new_from_user(struct kdbus_conn *conn, - struct kdbus_msg __user *msg, - struct kdbus_kmsg **kmsg) +struct kdbus_kmsg *kdbus_kmsg_new_from_user(struct kdbus_conn *conn, + struct kdbus_msg __user *msg) { struct kdbus_kmsg *m; u64 size, alloc_size; int ret; - BUG_ON(*kmsg); - if (!KDBUS_IS_ALIGNED8((unsigned long)msg)) - return -EFAULT; + return ERR_PTR(-EFAULT); if (kdbus_size_get_user(&size, msg, struct kdbus_msg)) - return -EFAULT; + return ERR_PTR(-EFAULT); if (size < sizeof(struct kdbus_msg) || size > KDBUS_MSG_MAX_SIZE) - return -EMSGSIZE; + return ERR_PTR(-EMSGSIZE); alloc_size = size + KDBUS_KMSG_HEADER_SIZE; m = kmalloc(alloc_size, GFP_KERNEL); if (!m) - return -ENOMEM; + return ERR_PTR(-ENOMEM); memset(m, 0, KDBUS_KMSG_HEADER_SIZE); if (copy_from_user(&m->msg, msg, size)) { @@ -411,10 +403,9 @@ int kdbus_kmsg_new_from_user(struct kdbus_conn *conn, } m->msg.src_id = conn->id; - *kmsg = m; - return 0; + return m; exit_free: kdbus_kmsg_free(m); - return ret; + return ERR_PTR(ret); } diff --git a/message.h b/message.h index 2c85734..abd04a4 100644 --- a/message.h +++ b/message.h @@ -64,9 +64,8 @@ struct kdbus_kmsg { struct kdbus_ep; struct kdbus_conn; -int kdbus_kmsg_new(size_t extra_size, struct kdbus_kmsg **kmsg); -int kdbus_kmsg_new_from_user(struct kdbus_conn *conn, - struct kdbus_msg __user *msg, - struct kdbus_kmsg **kmsg); +struct kdbus_kmsg *kdbus_kmsg_new(size_t extra_size); +struct kdbus_kmsg *kdbus_kmsg_new_from_user(struct kdbus_conn *conn, + struct kdbus_msg __user *msg); void kdbus_kmsg_free(struct kdbus_kmsg *kmsg); #endif diff --git a/metadata.c b/metadata.c index 8323e6d..77f8e33 100644 --- a/metadata.c +++ b/metadata.c @@ -35,19 +35,16 @@ /** * kdbus_meta_new() - create new metadata object - * @meta: New metadata object * - * Return: 0 on success, negative errno on failure. + * Return: a new kdbus_meta object on success, ERR_PTR on failure. */ -int kdbus_meta_new(struct kdbus_meta **meta) +struct kdbus_meta *kdbus_meta_new(void) { struct kdbus_meta *m; - BUG_ON(*meta); - m = kzalloc(sizeof(*m), GFP_KERNEL); if (!m) - return -ENOMEM; + return ERR_PTR(-ENOMEM); /* * Remember the PID and user namespaces our credentials belong to; @@ -57,33 +54,30 @@ int kdbus_meta_new(struct kdbus_meta **meta) m->pid_namespace = get_pid_ns(task_active_pid_ns(current)); m->user_namespace = get_user_ns(current_user_ns()); - *meta = m; - return 0; + return m; } /** * 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. + * Return: a new kdbus_meta object on success, ERR_PTR on failure. */ -int kdbus_meta_dup(const struct kdbus_meta *orig, - struct kdbus_meta **copy) +struct kdbus_meta *kdbus_meta_dup(const struct kdbus_meta *orig) { struct kdbus_meta *m; - BUG_ON(!orig || !copy); + BUG_ON(!orig); m = kmalloc(sizeof(*m), GFP_KERNEL); if (!m) - return -ENOMEM; + return ERR_PTR(-ENOMEM); m->data = kmemdup(orig->data, orig->allocated_size, GFP_KERNEL); if (!m->data) { kfree(m); - return -ENOMEM; + return ERR_PTR(-ENOMEM); } m->pid_namespace = get_pid_ns(orig->pid_namespace); @@ -93,8 +87,7 @@ int kdbus_meta_dup(const struct kdbus_meta *orig, m->allocated_size = orig->allocated_size; m->size = orig->size; - *copy = m; - return 0; + return m; } /** diff --git a/metadata.h b/metadata.h index a2728f5..0b96580 100644 --- a/metadata.h +++ b/metadata.h @@ -36,9 +36,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); +struct kdbus_meta *kdbus_meta_new(void); +struct kdbus_meta *kdbus_meta_dup(const struct kdbus_meta *orig); 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, diff --git a/names.c b/names.c index 5f8853c..bc1308a 100644 --- a/names.c +++ b/names.c @@ -76,23 +76,21 @@ void kdbus_name_registry_free(struct kdbus_name_registry *reg) /** * kdbus_name_registry_new() - create a new name registry - * @reg: The returned name registry * - * Return: 0 on success, negative errno on failure. + * Return: a new kdbus_name_registry on success, ERR_PTR on failure. */ -int kdbus_name_registry_new(struct kdbus_name_registry **reg) +struct kdbus_name_registry *kdbus_name_registry_new(void) { struct kdbus_name_registry *r; r = kzalloc(sizeof(*r), GFP_KERNEL); if (!r) - return -ENOMEM; + return ERR_PTR(-ENOMEM); hash_init(r->entries_hash); init_rwsem(&r->rwlock); - *reg = r; - return 0; + return r; } static struct kdbus_name_entry * diff --git a/names.h b/names.h index 594d1bd..fa527cd 100644 --- a/names.h +++ b/names.h @@ -52,7 +52,7 @@ struct kdbus_name_entry { struct kdbus_conn *activator; }; -int kdbus_name_registry_new(struct kdbus_name_registry **reg); +struct kdbus_name_registry *kdbus_name_registry_new(void); void kdbus_name_registry_free(struct kdbus_name_registry *reg); int kdbus_name_acquire(struct kdbus_name_registry *reg, diff --git a/notify.c b/notify.c index c68add6..0dd54e6 100644 --- a/notify.c +++ b/notify.c @@ -31,13 +31,12 @@ static int kdbus_notify_reply(struct kdbus_bus *bus, u64 id, u64 cookie, u64 msg_type) { struct kdbus_kmsg *kmsg = NULL; - int ret; BUG_ON(id == 0); - ret = kdbus_kmsg_new(0, &kmsg); - if (ret < 0) - return ret; + kmsg = kdbus_kmsg_new(0); + if (IS_ERR(kmsg)) + return PTR_ERR(kmsg); /* * a kernel-generated notification can only contain one @@ -54,7 +53,7 @@ static int kdbus_notify_reply(struct kdbus_bus *bus, u64 id, spin_lock(&bus->notify_lock); list_add_tail(&kmsg->queue_entry, &bus->notify_list); spin_unlock(&bus->notify_lock); - return ret; + return 0; } /** @@ -109,13 +108,12 @@ int kdbus_notify_name_change(struct kdbus_bus *bus, u64 type, { struct kdbus_kmsg *kmsg = NULL; size_t name_len, extra_size; - int ret; name_len = strlen(name) + 1; extra_size = sizeof(struct kdbus_notify_name_change) + name_len; - ret = kdbus_kmsg_new(extra_size, &kmsg); - if (ret < 0) - return ret; + kmsg = kdbus_kmsg_new(extra_size); + if (IS_ERR(kmsg)) + return PTR_ERR(kmsg); kmsg->msg.dst_id = KDBUS_DST_ID_BROADCAST; kmsg->msg.src_id = KDBUS_SRC_ID_KERNEL; @@ -134,7 +132,7 @@ int kdbus_notify_name_change(struct kdbus_bus *bus, u64 type, spin_lock(&bus->notify_lock); list_add_tail(&kmsg->queue_entry, &bus->notify_list); spin_unlock(&bus->notify_lock); - return ret; + return 0; } /** @@ -150,11 +148,10 @@ int kdbus_notify_name_change(struct kdbus_bus *bus, u64 type, int kdbus_notify_id_change(struct kdbus_bus *bus, u64 type, u64 id, u64 flags) { struct kdbus_kmsg *kmsg = NULL; - int ret; - ret = kdbus_kmsg_new(sizeof(struct kdbus_notify_id_change), &kmsg); - if (ret < 0) - return ret; + kmsg = kdbus_kmsg_new(sizeof(struct kdbus_notify_id_change)); + if (IS_ERR(kmsg)) + return PTR_ERR(kmsg); kmsg->msg.dst_id = KDBUS_DST_ID_BROADCAST; kmsg->msg.src_id = KDBUS_SRC_ID_KERNEL; @@ -181,7 +178,7 @@ int kdbus_notify_id_change(struct kdbus_bus *bus, u64 type, u64 id, u64 flags) spin_lock(&bus->notify_lock); list_add_tail(&kmsg->queue_entry, &bus->notify_list); spin_unlock(&bus->notify_lock); - return ret; + return 0; } /** diff --git a/pool.c b/pool.c index ef181d7..58cc215 100644 --- a/pool.c +++ b/pool.c @@ -373,12 +373,11 @@ void kdbus_pool_slice_make_public(struct kdbus_pool_slice *slice) * kdbus_pool_new() - create a new pool * @name: Name of the (deleted) file which shows up in * /proc, used for debugging - * @pool: Newly allocated pool * @size: Maximum size of the pool * - * Return: 0 on success, negative errno on failure. + * Return: a new kdbus_pool on success, ERR_PTR on failure. */ -int kdbus_pool_new(const char *name, struct kdbus_pool **pool, size_t size) +struct kdbus_pool *kdbus_pool_new(const char *name, size_t size) { struct kdbus_pool_slice *s; struct kdbus_pool *p; @@ -386,11 +385,9 @@ int kdbus_pool_new(const char *name, struct kdbus_pool **pool, size_t size) char *n = NULL; int ret; - BUG_ON(*pool); - p = kzalloc(sizeof(*p), GFP_KERNEL); if (!p) - return -ENOMEM; + return ERR_PTR(-ENOMEM); if (name) { n = kasprintf(GFP_KERNEL, KBUILD_MODNAME "-conn:%s", name); @@ -430,8 +427,7 @@ int kdbus_pool_new(const char *name, struct kdbus_pool **pool, size_t size) list_add(&s->entry, &p->slices); kdbus_pool_add_free_slice(p, s); - *pool = p; - return 0; + return p; exit_put_write: put_write_access(file_inode(f)); @@ -439,7 +435,7 @@ exit_put_shmem: fput(f); exit_free: kfree(p); - return ret; + return ERR_PTR(ret); } /** diff --git a/pool.h b/pool.h index 745161b..408edd2 100644 --- a/pool.h +++ b/pool.h @@ -17,7 +17,7 @@ struct kdbus_pool; struct kdbus_pool_slice; -int kdbus_pool_new(const char *name, struct kdbus_pool **pool, size_t size); +struct kdbus_pool *kdbus_pool_new(const char *name, size_t size); void kdbus_pool_free(struct kdbus_pool *pool); size_t kdbus_pool_remain(struct kdbus_pool *pool); int kdbus_pool_mmap(const struct kdbus_pool *pool, struct vm_area_struct *vma); -- 2.34.1