From 84d6508d803d8488106d4815c00ba62155c839da Mon Sep 17 00:00:00 2001 From: Kai Krakow Date: Fri, 25 Sep 2020 00:39:21 +0200 Subject: [PATCH 1/5] Revert "Move code only used for Wine detection into wine file" Actually, it wasn't planned to only use this code by the wine detection, there's more code in my queue that uses this. This reverts commit 42a803ce8584b2d2b83fa4b2912291f0223abf93. --- common/common-helpers.h | 1 + daemon/gamemode-env.c | 96 +++++++++++++++++++++++++++++++++++++++++ daemon/gamemode-proc.c | 61 ++++++++++++++++++++++++++ daemon/gamemode-wine.c | 79 +-------------------------------- daemon/gamemode.h | 14 ++++++ daemon/meson.build | 10 +++-- 6 files changed, 180 insertions(+), 81 deletions(-) create mode 100644 daemon/gamemode-env.c create mode 100644 daemon/gamemode-proc.c diff --git a/common/common-helpers.h b/common/common-helpers.h index d079b3fd..29b60153 100644 --- a/common/common-helpers.h +++ b/common/common-helpers.h @@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE. #pragma once +#include #include #include #include diff --git a/daemon/gamemode-env.c b/daemon/gamemode-env.c new file mode 100644 index 00000000..6a1786b1 --- /dev/null +++ b/daemon/gamemode-env.c @@ -0,0 +1,96 @@ +/* + +Copyright (c) 2017-2019, Feral Interactive +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Feral Interactive nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + + */ + +#define _GNU_SOURCE + +#include "gamemode.h" + +#include +#include +#include +#include +#include +#include + +/** + * Lookup the process environment for a specific variable or return NULL. + * Requires an open directory FD from /proc/PID. + */ +char *game_mode_lookup_proc_env(const procfd_t proc_fd, const char *var) +{ + char *environ = NULL; + + int fd = openat(proc_fd, "environ", O_RDONLY | O_CLOEXEC); + if (fd != -1) { + FILE *stream = fdopen(fd, "r"); + if (stream) { + /* Read every \0 terminated line from the environment */ + char *line = NULL; + size_t len = 0; + size_t pos = strlen(var) + 1; + while (!environ && (getdelim(&line, &len, 0, stream) != -1)) { + /* Find a match including the "=" suffix */ + if ((len > pos) && (strncmp(line, var, strlen(var)) == 0) && (line[pos - 1] == '=')) + environ = strndup(line + pos, len - pos); + } + free(line); + fclose(stream); + } else + close(fd); + } + + /* If found variable is empty, skip it */ + if (environ && !strlen(environ)) { + free(environ); + environ = NULL; + } + + return environ; +} + +/** + * Lookup the home directory of the user in a safe way. + */ +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; +} diff --git a/daemon/gamemode-proc.c b/daemon/gamemode-proc.c new file mode 100644 index 00000000..ca6a176b --- /dev/null +++ b/daemon/gamemode-proc.c @@ -0,0 +1,61 @@ +/* + +Copyright (c) 2017-2019, Feral Interactive +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Feral Interactive nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + + */ + +#define _GNU_SOURCE + +#include "gamemode.h" +#include "common-helpers.h" + +#include +#include +#include + +/** + * Opens the process environment for a specific PID and returns + * a file descriptor to the directory /proc/PID. Doing it that way prevents + * the directory going MIA when a process exits while we are looking at it + * and allows us to handle fewer error cases. + */ +procfd_t game_mode_open_proc(const pid_t pid) +{ + char buffer[PATH_MAX]; + const char *proc_path = buffered_snprintf(buffer, "/proc/%d", pid); + + return proc_path ? open(proc_path, O_RDONLY | O_CLOEXEC) : INVALID_PROCFD; +} + +/** + * Closes the process environment. + */ +int game_mode_close_proc(const procfd_t procfd) +{ + return close(procfd); +} diff --git a/daemon/gamemode-wine.c b/daemon/gamemode-wine.c index 056b50ad..55e1f42c 100644 --- a/daemon/gamemode-wine.c +++ b/daemon/gamemode-wine.c @@ -38,6 +38,8 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include +#include /** * Detect if the process is a wine preloader process @@ -55,83 +57,6 @@ static bool game_mode_detect_wine_loader(const char *exe) return (strtail(exe, "/wine") || strtail(exe, "/wine64")); } -/** - * Opens the process environment for a specific PID and returns - * a file descriptor to the directory /proc/PID. Doing it that way prevents - * the directory going MIA when a process exits while we are looking at it - * and allows us to handle fewer error cases. - */ -static procfd_t game_mode_open_proc(const pid_t pid) -{ - char buffer[PATH_MAX]; - const char *proc_path = buffered_snprintf(buffer, "/proc/%d", pid); - - return proc_path ? open(proc_path, O_RDONLY | O_CLOEXEC) : INVALID_PROCFD; -} - -/** - * Closes the process environment. - */ -static int game_mode_close_proc(const procfd_t procfd) -{ - return close(procfd); -} - -/** - * Lookup the process environment for a specific variable or return NULL. - * Requires an open directory FD from /proc/PID. - */ -static char *game_mode_lookup_proc_env(const procfd_t proc_fd, const char *var) -{ - char *environ = NULL; - - int fd = openat(proc_fd, "environ", O_RDONLY | O_CLOEXEC); - if (fd != -1) { - FILE *stream = fdopen(fd, "r"); - if (stream) { - /* Read every \0 terminated line from the environment */ - char *line = NULL; - size_t len = 0; - size_t pos = strlen(var) + 1; - while (!environ && (getdelim(&line, &len, 0, stream) != -1)) { - /* Find a match including the "=" suffix */ - if ((len > pos) && (strncmp(line, var, strlen(var)) == 0) && (line[pos - 1] == '=')) - environ = strndup(line + pos, len - pos); - } - free(line); - fclose(stream); - } else - close(fd); - } - - /* If found variable is empty, skip it */ - if (environ && !strlen(environ)) { - free(environ); - environ = NULL; - } - - return environ; -} - -/** - * 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 resolve the exe for wine-preloader. * This function is used if game_mode_context_find_exe() identified the diff --git a/daemon/gamemode.h b/daemon/gamemode.h index 255aafb5..f7a12aac 100644 --- a/daemon/gamemode.h +++ b/daemon/gamemode.h @@ -168,6 +168,13 @@ GameModeConfig *game_mode_config_from_context(const GameModeContext *context); */ int game_mode_reload_config(GameModeContext *context); +/** gamemode-env.c + * Provides internal API functions specific to working environment + * variables. + */ +char *game_mode_lookup_proc_env(const procfd_t proc_fd, const char *var); +char *game_mode_lookup_user_home(void); + /** gamemode-ioprio.c * Provides internal API functions specific to adjusting process * IO priorities. @@ -175,6 +182,13 @@ int game_mode_reload_config(GameModeContext *context); int game_mode_get_ioprio(const pid_t client); void game_mode_apply_ioprio(const GameModeContext *self, const pid_t client, int expected); +/** gamemode-proc.c + * Provides internal API functions specific to working with process + * environments. + */ +procfd_t game_mode_open_proc(const pid_t pid); +int game_mode_close_proc(const procfd_t procfd); + /** gamemode-sched.c * Provides internal API functions specific to adjusting process * scheduling. diff --git a/daemon/meson.build b/daemon/meson.build index b5bb5fb7..7c4acfbb 100644 --- a/daemon/meson.build +++ b/daemon/meson.build @@ -1,14 +1,16 @@ # Main daemon daemon_sources = [ 'gamemoded.c', + 'gamemode-config.c', 'gamemode-context.c', + 'gamemode-dbus.c', + 'gamemode-env.c', + 'gamemode-gpu.c', 'gamemode-ioprio.c', + 'gamemode-proc.c', 'gamemode-sched.c', - 'gamemode-wine.c', 'gamemode-tests.c', - 'gamemode-gpu.c', - 'gamemode-dbus.c', - 'gamemode-config.c', + 'gamemode-wine.c', ] gamemoded_includes = gamemode_headers_includes From b434e9acc5f7acb768ada6fe0935e559628e5142 Mon Sep 17 00:00:00 2001 From: Kai Krakow Date: Fri, 12 Oct 2018 19:38:41 +0200 Subject: [PATCH 2/5] gamemode-env: Abstract out opening the environment This enables us to reuse the open function for different purposes. Signed-off-by: Kai Krakow --- daemon/gamemode-env.c | 57 ++++++++++++++++++++++++++++++------------- daemon/gamemode.h | 3 +++ 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/daemon/gamemode-env.c b/daemon/gamemode-env.c index 6a1786b1..354f826a 100644 --- a/daemon/gamemode-env.c +++ b/daemon/gamemode-env.c @@ -40,6 +40,33 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +/** + * Open the process environment for enumerating or lookup. Requires an open + * directory FD from /proc/PID. + */ +FILE *game_mode_open_proc_env(const procfd_t proc_fd) +{ + /* Try to open the environ file of the process */ + int fd = openat(proc_fd, "environ", O_RDONLY | O_CLOEXEC); + if (fd != -1) { + FILE *stream = fdopen(fd, "r"); + if (stream) + return stream; + else + close(fd); + } + /* We failed */ + return NULL; +} + +/** + * Close the process enviroment opened by game_mode_open_proc_env(). + */ +int game_mode_close_proc_env(FILE *stream) +{ + return fclose(stream); +} + /** * Lookup the process environment for a specific variable or return NULL. * Requires an open directory FD from /proc/PID. @@ -48,23 +75,19 @@ char *game_mode_lookup_proc_env(const procfd_t proc_fd, const char *var) { char *environ = NULL; - int fd = openat(proc_fd, "environ", O_RDONLY | O_CLOEXEC); - if (fd != -1) { - FILE *stream = fdopen(fd, "r"); - if (stream) { - /* Read every \0 terminated line from the environment */ - char *line = NULL; - size_t len = 0; - size_t pos = strlen(var) + 1; - while (!environ && (getdelim(&line, &len, 0, stream) != -1)) { - /* Find a match including the "=" suffix */ - if ((len > pos) && (strncmp(line, var, strlen(var)) == 0) && (line[pos - 1] == '=')) - environ = strndup(line + pos, len - pos); - } - free(line); - fclose(stream); - } else - close(fd); + FILE *stream = game_mode_open_proc_env(proc_fd); + if (stream) { + /* Read every \0 terminated line from the environment */ + char *line = NULL; + size_t len = 0; + size_t pos = strlen(var) + 1; + while (!environ && (getdelim(&line, &len, 0, stream) != -1)) { + /* Find a match including the "=" suffix */ + if ((len > pos) && (strncmp(line, var, strlen(var)) == 0) && (line[pos - 1] == '=')) + environ = strndup(line + pos, len - pos); + } + free(line); + game_mode_close_proc_env(stream); } /* If found variable is empty, skip it */ diff --git a/daemon/gamemode.h b/daemon/gamemode.h index f7a12aac..e1b8d9fd 100644 --- a/daemon/gamemode.h +++ b/daemon/gamemode.h @@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#include #include #define INVALID_PROCFD -1 @@ -173,6 +174,8 @@ int game_mode_reload_config(GameModeContext *context); * variables. */ char *game_mode_lookup_proc_env(const procfd_t proc_fd, const char *var); +FILE *game_mode_open_proc_env(const procfd_t proc_fd); +int game_mode_open_close_env(FILE *stream); char *game_mode_lookup_user_home(void); /** gamemode-ioprio.c From 0c1c001b5adde4a96144ac675f1a7440faf0e74c Mon Sep 17 00:00:00 2001 From: Kai Krakow Date: Mon, 24 Sep 2018 20:05:37 +0200 Subject: [PATCH 3/5] gamemode-context: Never touch wineserver With the arrival of SteamPlay Proton, we probably don't want to touch wineserver at all. With staging patches, wineserver has it's own means of gaining realtime priority and handling spawned processes. Optimally, gamemode support would be integrated right into Proton itself by changing the process spawning functions of wine when launching the actual game exe. I'm currently working on such an idea. This could be extended to ignore other well-known binaries which should never be handled by gamemode (i.e. shells of scripted game launchers). Signed-off-by: Kai Krakow --- daemon/gamemode-context.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/daemon/gamemode-context.c b/daemon/gamemode-context.c index ef301465..419d2afa 100644 --- a/daemon/gamemode-context.c +++ b/daemon/gamemode-context.c @@ -580,6 +580,13 @@ int game_mode_context_register(GameModeContext *self, pid_t client, pid_t reques if (!executable) goto error_cleanup; + /* Check for forced exceptions */ + if (strstr(executable, "/wineserver") != NULL) { + /* wineserver from wine-staging has its own means of setting priorities */ + LOG_MSG("Client [%s] was rejected (forced to ignore wineserver)\n", executable); + goto error_cleanup; + } + /* Check our blacklist and whitelist */ if (!config_get_client_whitelisted(self->config, executable)) { LOG_MSG("Client [%s] was rejected (not in whitelist)\n", executable); From 2fe8ad64081c48031d6d73a4ac632dd0fa80e543 Mon Sep 17 00:00:00 2001 From: Kai Krakow Date: Mon, 24 Sep 2018 21:17:08 +0200 Subject: [PATCH 4/5] gamemode-context: Ignore Steam client processes If you want to run your whole Steam client under gamemode to not adjust each game individually, we certainly should leave the Steam client processes alone as we don't want them to take resources away from the game. Otherwise, Steam client processes may run with `SCHED_ISO`. Closes: https://github.com/FeralInteractive/gamemode/issues/67 Signed-off-by: Kai Krakow --- daemon/gamemode-context.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/daemon/gamemode-context.c b/daemon/gamemode-context.c index 419d2afa..8e8621af 100644 --- a/daemon/gamemode-context.c +++ b/daemon/gamemode-context.c @@ -581,10 +581,19 @@ int game_mode_context_register(GameModeContext *self, pid_t client, pid_t reques goto error_cleanup; /* Check for forced exceptions */ + char *exe = NULL; if (strstr(executable, "/wineserver") != NULL) { /* wineserver from wine-staging has its own means of setting priorities */ LOG_MSG("Client [%s] was rejected (forced to ignore wineserver)\n", executable); goto error_cleanup; + } else if (((exe = strstr(executable, "/steam")) != NULL) && (strlen(exe) == 6)) { + /* we don't want to touch steam if the whole steam client runs in gamemode */ + LOG_MSG("Client [%s] was rejected (forced to ignore steam)\n", executable); + goto error_cleanup; + } else if (strstr(executable, "/steamwebhelper") != NULL) { + /* we don't want to touch steamwebhelper if the whole steam client runs in gamemode */ + LOG_MSG("Client [%s] was rejected (forced to ignore steamwebhelper)\n", executable); + goto error_cleanup; } /* Check our blacklist and whitelist */ From fcdae41b39de4a3c869f8c4f347c1e752ad394b3 Mon Sep 17 00:00:00 2001 From: Kai Krakow Date: Mon, 24 Sep 2018 21:37:06 +0200 Subject: [PATCH 5/5] README: Document the new Steam and Wine support Let's document the changes to automatically handle Steam, SteamPlay, and Wine. Also, give an example how to run the Steam client or Wine games in GameMode. Signed-off-by: Kai Krakow --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 2d2cf7dd..e4bd19d8 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,14 @@ The following games are known to integrate GameMode support (meaning they don't * Total War: Three Kingdoms * Total War: WARHAMMER II +### Running the Steam client itself or Wine in GameMode +The Linux Steam client and Wine can be run in GameMode. GameMode will automatically disable handling key processes of Steam or Wine which may negatively affect your gaming experience. Just prepend `gamemoderun` to your Steam or Wine launching command line: +```bash +gamemoderun steam # for Steam +gamemoderun wine ./game.exe # for Wine +``` +Running the whole Steam client itself in GameMode removes the need for individually adjusting the start option of each game. The automatic handling of Wine games is important for the new SteamPlay feature to run Wine games natively from the Linux Steam client. + ### Others Other apps which can integrate with GameMode include: * [ATLauncher](https://atlauncher.com/downloads) Minecraft launcher