Explorar o código

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>
Kai Krakow %!s(int64=6) %!d(string=hai) anos
pai
achega
47ea4514cd
Modificáronse 1 ficheiros con 21 adicións e 0 borrados
  1. 21 0
      daemon/gamemode.c

+ 21 - 0
daemon/gamemode.c

@@ -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.