From 47ea4514cd3d485e68bb0069a81933e9ad86eb10 Mon Sep 17 00:00:00 2001 From: Kai Krakow Date: Wed, 3 Oct 2018 01:17:32 +0200 Subject: [PATCH] gamemode: Add function to lookup user home directory We first try to use `$HOME`, and only then fall back to passwd lookup. Signed-off-by: Kai Krakow --- daemon/gamemode.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/daemon/gamemode.c b/daemon/gamemode.c index 4e20aae..22e3d2e 100644 --- a/daemon/gamemode.c +++ b/daemon/gamemode.c @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include #include #include @@ -48,6 +49,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include /* SCHED_ISO may not be defined as it is a reserved value not yet @@ -671,6 +673,25 @@ GameModeContext *game_mode_context_instance() return &instance; } +/** + * Lookup the home directory of the user in a safe way. + */ +static char *game_mode_lookup_user_home(void) +{ + /* Try loading env HOME first */ + const char *home = secure_getenv("HOME"); + if (!home) { + /* If HOME is not defined (or out of context), fall back to passwd */ + struct passwd *pw = getpwuid(getuid()); + if (!pw) + return NULL; + home = pw->pw_dir; + } + + /* Try to allocate into our heap */ + return home ? strdup(home) : NULL; +} + /** * Attempt to locate the exe for the process. * We might run into issues if the process is running under an odd umask.