1
0

gamemode-wine.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. Copyright (c) 2017-2025, Feral Interactive and the GameMode contributors
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Feral Interactive nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #define _GNU_SOURCE
  27. #include "gamemode.h"
  28. #include "common-helpers.h"
  29. #include "common-logging.h"
  30. #include <ctype.h>
  31. #include <fcntl.h>
  32. #include <pwd.h>
  33. /**
  34. * Detect if the process is a wine preloader process
  35. */
  36. static bool game_mode_detect_wine_preloader(const char *exe)
  37. {
  38. return (strtail(exe, "/wine-preloader") || strtail(exe, "/wine64-preloader"));
  39. }
  40. /**
  41. * Detect if the process is a wine loader process
  42. */
  43. static bool game_mode_detect_wine_loader(const char *exe)
  44. {
  45. return (strtail(exe, "/wine") || strtail(exe, "/wine64"));
  46. }
  47. /**
  48. * Opens the process environment for a specific PID and returns
  49. * a file descriptor to the directory /proc/PID. Doing it that way prevents
  50. * the directory going MIA when a process exits while we are looking at it
  51. * and allows us to handle fewer error cases.
  52. */
  53. static procfd_t game_mode_open_proc(const pid_t pid)
  54. {
  55. char buffer[PATH_MAX];
  56. const char *proc_path = buffered_snprintf(buffer, "/proc/%d", pid);
  57. return proc_path ? open(proc_path, O_RDONLY | O_CLOEXEC) : INVALID_PROCFD;
  58. }
  59. /**
  60. * Closes the process environment.
  61. */
  62. static int game_mode_close_proc(const procfd_t procfd)
  63. {
  64. return close(procfd);
  65. }
  66. /**
  67. * Lookup the process environment for a specific variable or return NULL.
  68. * Requires an open directory FD from /proc/PID.
  69. */
  70. static char *game_mode_lookup_proc_env(const procfd_t proc_fd, const char *var)
  71. {
  72. char *environ = NULL;
  73. int fd = openat(proc_fd, "environ", O_RDONLY | O_CLOEXEC);
  74. if (fd != -1) {
  75. FILE *stream = fdopen(fd, "r");
  76. if (stream) {
  77. /* Read every \0 terminated line from the environment */
  78. char *line = NULL;
  79. size_t len = 0;
  80. size_t pos = strlen(var) + 1;
  81. while (!environ && (getdelim(&line, &len, 0, stream) != -1)) {
  82. /* Find a match including the "=" suffix */
  83. if ((len > pos) && (strncmp(line, var, strlen(var)) == 0) && (line[pos - 1] == '='))
  84. environ = strndup(line + pos, len - pos);
  85. }
  86. free(line);
  87. fclose(stream);
  88. } else
  89. close(fd);
  90. }
  91. /* If found variable is empty, skip it */
  92. if (environ && !strlen(environ)) {
  93. free(environ);
  94. environ = NULL;
  95. }
  96. return environ;
  97. }
  98. /**
  99. * Lookup the home directory of the user in a safe way.
  100. */
  101. static char *game_mode_lookup_user_home(void)
  102. {
  103. /* Try loading env HOME first */
  104. const char *home = secure_getenv("HOME");
  105. if (!home) {
  106. /* If HOME is not defined (or out of context), fall back to passwd */
  107. struct passwd *pw = getpwuid(getuid());
  108. if (!pw)
  109. return NULL;
  110. home = pw->pw_dir;
  111. }
  112. /* Try to allocate into our heap */
  113. return home ? strdup(home) : NULL;
  114. }
  115. /**
  116. * Attempt to resolve the exe for wine-preloader.
  117. * This function is used if game_mode_context_find_exe() identified the
  118. * process as wine-preloader. Returns NULL when resolve fails.
  119. */
  120. char *game_mode_resolve_wine_preloader(const char *exe, const pid_t pid)
  121. {
  122. /* Detect if the process is a wine loader process */
  123. if (game_mode_detect_wine_preloader(exe) || game_mode_detect_wine_loader(exe)) {
  124. LOG_MSG("Detected wine for client %d [%s].\n", pid, exe);
  125. } else {
  126. return NULL;
  127. }
  128. char buffer[PATH_MAX];
  129. char *wine_exe = NULL, *wineprefix = NULL;
  130. /* Open the directory, we are potentially reading multiple files from it */
  131. procfd_t proc_fd = game_mode_open_proc(pid);
  132. if (proc_fd == INVALID_PROCFD)
  133. goto fail_proc;
  134. /* Open the command line */
  135. int fd = openat(proc_fd, "cmdline", O_RDONLY | O_CLOEXEC);
  136. if (fd != -1) {
  137. FILE *stream = fdopen(fd, "r");
  138. if (stream) {
  139. char *argv = NULL;
  140. size_t args = 0;
  141. int argc = 0;
  142. while (!wine_exe && (argc++ < 2) && (getdelim(&argv, &args, 0, stream) != -1)) {
  143. /* If we see the wine loader here, we have to use the next argument */
  144. if (strtail(argv, "/wine") || strtail(argv, "/wine64"))
  145. continue;
  146. free(wine_exe); // just in case
  147. /* Check presence of the drive letter, we assume that below */
  148. wine_exe = args > 2 && argv[1] == ':' ? strndup(argv, args) : NULL;
  149. }
  150. free(argv);
  151. fclose(stream);
  152. } else
  153. close(fd);
  154. }
  155. /* Did we get wine exe from cmdline? */
  156. if (wine_exe)
  157. LOG_MSG("Detected wine exe for client %d [%s].\n", pid, wine_exe);
  158. else
  159. goto fail_cmdline;
  160. /* Open the process environment and find the WINEPREFIX */
  161. errno = 0;
  162. if (!(wineprefix = game_mode_lookup_proc_env(proc_fd, "WINEPREFIX"))) {
  163. /* Lookup user home instead only if there was no error */
  164. char *home = NULL;
  165. if (errno == 0)
  166. home = game_mode_lookup_user_home();
  167. /* Append "/.wine" if we found the user home */
  168. if (home)
  169. wineprefix = safe_snprintf(buffer, "%s/.wine", home);
  170. /* Cleanup and check result */
  171. free(home);
  172. if (!wineprefix)
  173. goto fail_env;
  174. }
  175. /* Wine prefix was detected, log this for diagnostics */
  176. LOG_MSG("Detected wine prefix for client %d: '%s'\n", pid, wineprefix);
  177. /* Convert Windows to Unix path separators */
  178. char *ix = wine_exe;
  179. while (ix != NULL)
  180. (ix = strchr(ix, '\\')) && (*ix++ = '/');
  181. /* Convert the drive letter to lcase because wine handles it this way in the prefix */
  182. wine_exe[0] = (char)tolower(wine_exe[0]);
  183. /* Convert relative wine exe path to full unix path */
  184. char *wine_path = buffered_snprintf(buffer, "%s/dosdevices/%s", wineprefix, wine_exe);
  185. free(wine_exe);
  186. wine_exe = wine_path ? realpath(wine_path, NULL) : NULL;
  187. /* Fine? Successo? Fortuna! */
  188. if (wine_exe)
  189. LOG_MSG("Successfully mapped wine client %d [%s].\n", pid, wine_exe);
  190. else
  191. goto fail;
  192. error_cleanup:
  193. if (proc_fd != INVALID_PROCFD)
  194. game_mode_close_proc(proc_fd);
  195. free(wineprefix);
  196. return wine_exe;
  197. fail:
  198. LOG_ERROR("Unable to find wine executable for client %d: %s\n", pid, strerror(errno));
  199. goto error_cleanup;
  200. fail_cmdline:
  201. LOG_ERROR("Wine loader has no accepted cmdline for client %d yet, deferring.\n", pid);
  202. goto error_cleanup;
  203. fail_env:
  204. LOG_ERROR("Failed to access process environment for client %d: %s\n", pid, strerror(errno));
  205. goto error_cleanup;
  206. fail_proc:
  207. LOG_ERROR("Failed to access process data for client %d: %s\n", pid, strerror(errno));
  208. goto error_cleanup;
  209. }