gamemode-wine.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright (c) 2017-2019, Feral Interactive
  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 "helpers.h"
  29. #include "logging.h"
  30. #include <ctype.h>
  31. #include <fcntl.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. /**
  35. * Detect if the process is a wine preloader process
  36. */
  37. bool game_mode_detect_wine_preloader(const char *exe)
  38. {
  39. return (strtail(exe, "/wine-preloader") || strtail(exe, "/wine64-preloader"));
  40. }
  41. /**
  42. * Detect if the process is a wine loader process
  43. */
  44. bool game_mode_detect_wine_loader(const char *exe)
  45. {
  46. return (strtail(exe, "/wine") || strtail(exe, "/wine64"));
  47. }
  48. /**
  49. * Attempt to resolve the exe for wine-preloader.
  50. * This function is used if game_mode_context_find_exe() identified the
  51. * process as wine-preloader. Returns NULL when resolve fails.
  52. */
  53. char *game_mode_resolve_wine_preloader(const pid_t pid)
  54. {
  55. char buffer[PATH_MAX];
  56. char *proc_path = NULL, *wine_exe = NULL, *wineprefix = NULL;
  57. /* Open the directory, we are potentially reading multiple files from it */
  58. procfd_t proc_fd = game_mode_open_proc(pid);
  59. if (proc_fd == INVALID_PROCFD)
  60. goto fail_proc;
  61. /* Open the command line */
  62. int fd = openat(proc_fd, "cmdline", O_RDONLY | O_CLOEXEC);
  63. if (fd != -1) {
  64. FILE *stream = fdopen(fd, "r");
  65. if (stream) {
  66. char *argv = NULL;
  67. size_t args = 0;
  68. int argc = 0;
  69. while (!wine_exe && (argc++ < 2) && (getdelim(&argv, &args, 0, stream) != -1)) {
  70. /* If we see the wine loader here, we have to use the next argument */
  71. if (strtail(argv, "/wine") || strtail(argv, "/wine64"))
  72. continue;
  73. free(wine_exe); // just in case
  74. /* Check presence of the drive letter, we assume that below */
  75. wine_exe = args > 2 && argv[1] == ':' ? strndup(argv, args) : NULL;
  76. }
  77. free(argv);
  78. fclose(stream);
  79. } else
  80. close(fd);
  81. }
  82. /* Did we get wine exe from cmdline? */
  83. if (wine_exe)
  84. LOG_MSG("Detected wine exe for client %d [%s].\n", pid, wine_exe);
  85. else
  86. goto fail_cmdline;
  87. /* Open the process environment and find the WINEPREFIX */
  88. errno = 0;
  89. if (!(wineprefix = game_mode_lookup_proc_env(proc_fd, "WINEPREFIX"))) {
  90. /* Lookup user home instead only if there was no error */
  91. char *home = NULL;
  92. if (errno == 0)
  93. home = game_mode_lookup_user_home();
  94. /* Append "/.wine" if we found the user home */
  95. if (home)
  96. wineprefix = safe_snprintf(buffer, "%s/.wine", home);
  97. /* Cleanup and check result */
  98. free(home);
  99. if (!wineprefix)
  100. goto fail_env;
  101. }
  102. /* Wine prefix was detected, log this for diagnostics */
  103. LOG_MSG("Detected wine prefix for client %d: '%s'\n", pid, wineprefix);
  104. /* Convert Windows to Unix path separators */
  105. char *ix = wine_exe;
  106. while (ix != NULL)
  107. (ix = strchr(ix, '\\')) && (*ix++ = '/');
  108. /* Convert the drive letter to lcase because wine handles it this way in the prefix */
  109. wine_exe[0] = (char)tolower(wine_exe[0]);
  110. /* Convert relative wine exe path to full unix path */
  111. char *wine_path = buffered_snprintf(buffer, "%s/dosdevices/%s", wineprefix, wine_exe);
  112. free(wine_exe);
  113. wine_exe = wine_path ? realpath(wine_path, NULL) : NULL;
  114. /* Fine? Successo? Fortuna! */
  115. if (wine_exe)
  116. LOG_MSG("Successfully mapped wine client %d [%s].\n", pid, wine_exe);
  117. else
  118. goto fail;
  119. error_cleanup:
  120. game_mode_close_proc(proc_fd);
  121. free(wineprefix);
  122. free(proc_path);
  123. return wine_exe;
  124. fail:
  125. LOG_ERROR("Unable to find wine executable for client %d: %s\n", pid, strerror(errno));
  126. goto error_cleanup;
  127. fail_cmdline:
  128. LOG_ERROR("Wine loader has no accepted cmdline for client %d yet, deferring.\n", pid);
  129. goto error_cleanup;
  130. fail_env:
  131. LOG_ERROR("Failed to access process environment in '%s': %s\n", proc_path, strerror(errno));
  132. goto error_cleanup;
  133. fail_proc:
  134. LOG_ERROR("Failed to access process data in '%s': %s\n", proc_path, strerror(errno));
  135. goto error_cleanup;
  136. }