123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- #define _GNU_SOURCE
- #include "gamemode.h"
- #include "common-helpers.h"
- #include "common-logging.h"
- #include <ctype.h>
- #include <fcntl.h>
- #include <pwd.h>
- static bool game_mode_detect_wine_preloader(const char *exe)
- {
- return (strtail(exe, "/wine-preloader") || strtail(exe, "/wine64-preloader"));
- }
- static bool game_mode_detect_wine_loader(const char *exe)
- {
- return (strtail(exe, "/wine") || strtail(exe, "/wine64"));
- }
- 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;
- }
- static int game_mode_close_proc(const procfd_t procfd)
- {
- return close(procfd);
- }
- 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) {
-
- char *line = NULL;
- size_t len = 0;
- size_t pos = strlen(var) + 1;
- while (!environ && (getdelim(&line, &len, 0, stream) != -1)) {
-
- 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 (environ && !strlen(environ)) {
- free(environ);
- environ = NULL;
- }
- return environ;
- }
- static char *game_mode_lookup_user_home(void)
- {
-
- const char *home = secure_getenv("HOME");
- if (!home) {
-
- struct passwd *pw = getpwuid(getuid());
- if (!pw)
- return NULL;
- home = pw->pw_dir;
- }
-
- return home ? strdup(home) : NULL;
- }
- char *game_mode_resolve_wine_preloader(const char *exe, const pid_t pid)
- {
-
- if (game_mode_detect_wine_preloader(exe) || game_mode_detect_wine_loader(exe)) {
- LOG_MSG("Detected wine for client %d [%s].\n", pid, exe);
- } else {
- return NULL;
- }
- char buffer[PATH_MAX];
- char *wine_exe = NULL, *wineprefix = NULL;
-
- procfd_t proc_fd = game_mode_open_proc(pid);
- if (proc_fd == INVALID_PROCFD)
- goto fail_proc;
-
- int fd = openat(proc_fd, "cmdline", O_RDONLY | O_CLOEXEC);
- if (fd != -1) {
- FILE *stream = fdopen(fd, "r");
- if (stream) {
- char *argv = NULL;
- size_t args = 0;
- int argc = 0;
- while (!wine_exe && (argc++ < 2) && (getdelim(&argv, &args, 0, stream) != -1)) {
-
- if (strtail(argv, "/wine") || strtail(argv, "/wine64"))
- continue;
- free(wine_exe);
-
- wine_exe = args > 2 && argv[1] == ':' ? strndup(argv, args) : NULL;
- }
- free(argv);
- fclose(stream);
- } else
- close(fd);
- }
-
- if (wine_exe)
- LOG_MSG("Detected wine exe for client %d [%s].\n", pid, wine_exe);
- else
- goto fail_cmdline;
-
- errno = 0;
- if (!(wineprefix = game_mode_lookup_proc_env(proc_fd, "WINEPREFIX"))) {
-
- char *home = NULL;
- if (errno == 0)
- home = game_mode_lookup_user_home();
-
- if (home)
- wineprefix = safe_snprintf(buffer, "%s/.wine", home);
-
- free(home);
- if (!wineprefix)
- goto fail_env;
- }
-
- LOG_MSG("Detected wine prefix for client %d: '%s'\n", pid, wineprefix);
-
- char *ix = wine_exe;
- while (ix != NULL)
- (ix = strchr(ix, '\\')) && (*ix++ = '/');
-
- wine_exe[0] = (char)tolower(wine_exe[0]);
-
- char *wine_path = buffered_snprintf(buffer, "%s/dosdevices/%s", wineprefix, wine_exe);
- free(wine_exe);
- wine_exe = wine_path ? realpath(wine_path, NULL) : NULL;
-
- if (wine_exe)
- LOG_MSG("Successfully mapped wine client %d [%s].\n", pid, wine_exe);
- else
- goto fail;
- error_cleanup:
- game_mode_close_proc(proc_fd);
- free(wineprefix);
- return wine_exe;
- fail:
- LOG_ERROR("Unable to find wine executable for client %d: %s\n", pid, strerror(errno));
- goto error_cleanup;
- fail_cmdline:
- LOG_ERROR("Wine loader has no accepted cmdline for client %d yet, deferring.\n", pid);
- goto error_cleanup;
- fail_env:
- LOG_ERROR("Failed to access process environment for client %d: %s\n", pid, strerror(errno));
- goto error_cleanup;
- fail_proc:
- LOG_ERROR("Failed to access process data for client %d: %s\n", pid, strerror(errno));
- goto error_cleanup;
- }
|