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 <kai@kaishome.de>
This commit is contained in:
Kai Krakow 2018-10-03 01:17:32 +02:00
parent 3a6d258eae
commit 47ea4514cd

View File

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <linux/limits.h>
#include <linux/sched.h>
#include <pthread.h>
#include <pwd.h>
#include <sched.h>
#include <signal.h>
#include <stdatomic.h>
@ -48,6 +49,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <systemd/sd-daemon.h>
/* 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.