From 14ef4a6506bd37b7d7acfda216d399d5f156819a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 21 Mar 2018 19:04:45 +0100 Subject: [PATCH] logind: rework manager_get_{user|session}_by_pid() a bit Let's make sure we always initialize the return value if we return non-negative. Just a matter of coding style: we should always initialize our return values when we return >= 0, and leave them unclobbered if we return < 0. --- src/login/logind-core.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/login/logind-core.c b/src/login/logind-core.c index 41b4d4d..8c022cf 100644 --- a/src/login/logind-core.c +++ b/src/login/logind-core.c @@ -282,7 +282,7 @@ int manager_process_button_device(Manager *m, struct udev_device *d) { return 0; } -int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) { +int manager_get_session_by_pid(Manager *m, pid_t pid, Session **ret) { _cleanup_free_ char *unit = NULL; Session *s; int r; @@ -294,38 +294,51 @@ int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) { r = cg_pid_get_unit(pid, &unit); if (r < 0) - return 0; + goto not_found; s = hashmap_get(m->session_units, unit); if (!s) - return 0; + goto not_found; + + if (ret) + *ret = s; - if (session) - *session = s; return 1; + +not_found: + if (ret) + *ret = NULL; + return 0; } -int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) { +int manager_get_user_by_pid(Manager *m, pid_t pid, User **ret) { _cleanup_free_ char *unit = NULL; User *u; int r; assert(m); - assert(user); if (!pid_is_valid(pid)) return -EINVAL; r = cg_pid_get_slice(pid, &unit); if (r < 0) - return 0; + goto not_found; u = hashmap_get(m->user_units, unit); if (!u) - return 0; + goto not_found; + + if (ret) + *ret = u; - *user = u; return 1; + +not_found: + if (ret) + *ret = NULL; + + return 0; } int manager_get_idle_hint(Manager *m, dual_timestamp *t) { -- 2.7.4