startup_checks.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "video_core/vulkan_common/vulkan_wrapper.h"
  4. #ifdef _WIN32
  5. #include <cstring>
  6. #include <processthreadsapi.h>
  7. #include <windows.h>
  8. #elif defined(YUZU_UNIX)
  9. #include <cstring>
  10. #include <errno.h>
  11. #include <spawn.h>
  12. #include <sys/types.h>
  13. #include <sys/wait.h>
  14. #include <unistd.h>
  15. #endif
  16. #include <fmt/core.h>
  17. #include "video_core/vulkan_common/vulkan_instance.h"
  18. #include "video_core/vulkan_common/vulkan_library.h"
  19. #include "yuzu/startup_checks.h"
  20. void CheckVulkan() {
  21. // Just start the Vulkan loader, this will crash if something is wrong
  22. try {
  23. Vulkan::vk::InstanceDispatch dld;
  24. const auto library = Vulkan::OpenLibrary();
  25. const Vulkan::vk::Instance instance =
  26. Vulkan::CreateInstance(*library, dld, VK_API_VERSION_1_1);
  27. } catch (const Vulkan::vk::Exception& exception) {
  28. fmt::print(stderr, "Failed to initialize Vulkan: {}\n", exception.what());
  29. }
  30. }
  31. bool CheckEnvVars(bool* is_child) {
  32. #ifdef _WIN32
  33. // Check environment variable to see if we are the child
  34. char variable_contents[8];
  35. const DWORD startup_check_var =
  36. GetEnvironmentVariableA(STARTUP_CHECK_ENV_VAR, variable_contents, 8);
  37. if (startup_check_var > 0 && std::strncmp(variable_contents, ENV_VAR_ENABLED_TEXT, 8) == 0) {
  38. CheckVulkan();
  39. return true;
  40. }
  41. // Don't perform startup checks if we are a child process
  42. char is_child_s[8];
  43. const DWORD is_child_len = GetEnvironmentVariableA(IS_CHILD_ENV_VAR, is_child_s, 8);
  44. if (is_child_len > 0 && std::strncmp(is_child_s, ENV_VAR_ENABLED_TEXT, 8) == 0) {
  45. *is_child = true;
  46. return false;
  47. } else if (!SetEnvironmentVariableA(IS_CHILD_ENV_VAR, ENV_VAR_ENABLED_TEXT)) {
  48. fmt::print(stderr, "SetEnvironmentVariableA failed to set {} with error {}\n",
  49. IS_CHILD_ENV_VAR, GetLastError());
  50. return true;
  51. }
  52. #elif defined(YUZU_UNIX)
  53. const char* startup_check_var = getenv(STARTUP_CHECK_ENV_VAR);
  54. if (startup_check_var != nullptr &&
  55. std::strncmp(startup_check_var, ENV_VAR_ENABLED_TEXT, 8) == 0) {
  56. CheckVulkan();
  57. return true;
  58. }
  59. #endif
  60. return false;
  61. }
  62. bool StartupChecks(const char* arg0, bool* has_broken_vulkan, bool perform_vulkan_check) {
  63. #ifdef _WIN32
  64. // Set the startup variable for child processes
  65. const bool env_var_set = SetEnvironmentVariableA(STARTUP_CHECK_ENV_VAR, ENV_VAR_ENABLED_TEXT);
  66. if (!env_var_set) {
  67. fmt::print(stderr, "SetEnvironmentVariableA failed to set {} with error {}\n",
  68. STARTUP_CHECK_ENV_VAR, GetLastError());
  69. return false;
  70. }
  71. if (perform_vulkan_check) {
  72. // Spawn child process that performs Vulkan check
  73. PROCESS_INFORMATION process_info;
  74. std::memset(&process_info, '\0', sizeof(process_info));
  75. if (!SpawnChild(arg0, &process_info, 0)) {
  76. return false;
  77. }
  78. // Wait until the process exits and get exit code from it
  79. WaitForSingleObject(process_info.hProcess, INFINITE);
  80. DWORD exit_code = STILL_ACTIVE;
  81. const int err = GetExitCodeProcess(process_info.hProcess, &exit_code);
  82. if (err == 0) {
  83. fmt::print(stderr, "GetExitCodeProcess failed with error {}\n", GetLastError());
  84. }
  85. // Vulkan is broken if the child crashed (return value is not zero)
  86. *has_broken_vulkan = (exit_code != 0);
  87. if (CloseHandle(process_info.hProcess) == 0) {
  88. fmt::print(stderr, "CloseHandle failed with error {}\n", GetLastError());
  89. }
  90. if (CloseHandle(process_info.hThread) == 0) {
  91. fmt::print(stderr, "CloseHandle failed with error {}\n", GetLastError());
  92. }
  93. }
  94. if (!SetEnvironmentVariableA(STARTUP_CHECK_ENV_VAR, nullptr)) {
  95. fmt::print(stderr, "SetEnvironmentVariableA failed to clear {} with error {}\n",
  96. STARTUP_CHECK_ENV_VAR, GetLastError());
  97. }
  98. #elif defined(YUZU_UNIX)
  99. const int env_var_set = setenv(STARTUP_CHECK_ENV_VAR, ENV_VAR_ENABLED_TEXT, 1);
  100. if (env_var_set == -1) {
  101. const int err = errno;
  102. fmt::print(stderr, "setenv failed to set {} with error {}\n", STARTUP_CHECK_ENV_VAR, err);
  103. return false;
  104. }
  105. if (perform_vulkan_check) {
  106. const pid_t pid = SpawnChild(arg0);
  107. if (pid == -1) {
  108. return false;
  109. }
  110. // Get exit code from child process
  111. int status;
  112. const int r_val = waitpid(pid, &status, 0);
  113. if (r_val == -1) {
  114. const int err = errno;
  115. fmt::print(stderr, "wait failed with error {}\n", err);
  116. return false;
  117. }
  118. // Vulkan is broken if the child crashed (return value is not zero)
  119. *has_broken_vulkan = (status != 0);
  120. }
  121. const int env_var_cleared = unsetenv(STARTUP_CHECK_ENV_VAR);
  122. if (env_var_cleared == -1) {
  123. const int err = errno;
  124. fmt::print(stderr, "unsetenv failed to clear {} with error {}\n", STARTUP_CHECK_ENV_VAR,
  125. err);
  126. }
  127. #endif
  128. return false;
  129. }
  130. #ifdef _WIN32
  131. bool SpawnChild(const char* arg0, PROCESS_INFORMATION* pi, int flags) {
  132. STARTUPINFOA startup_info;
  133. std::memset(&startup_info, '\0', sizeof(startup_info));
  134. startup_info.cb = sizeof(startup_info);
  135. char p_name[255];
  136. std::strncpy(p_name, arg0, 254);
  137. p_name[254] = '\0';
  138. const bool process_created = CreateProcessA(nullptr, // lpApplicationName
  139. p_name, // lpCommandLine
  140. nullptr, // lpProcessAttributes
  141. nullptr, // lpThreadAttributes
  142. false, // bInheritHandles
  143. flags, // dwCreationFlags
  144. nullptr, // lpEnvironment
  145. nullptr, // lpCurrentDirectory
  146. &startup_info, // lpStartupInfo
  147. pi // lpProcessInformation
  148. );
  149. if (!process_created) {
  150. fmt::print(stderr, "CreateProcessA failed with error {}\n", GetLastError());
  151. return false;
  152. }
  153. return true;
  154. }
  155. #elif defined(YUZU_UNIX)
  156. pid_t SpawnChild(const char* arg0) {
  157. const pid_t pid = fork();
  158. if (pid == -1) {
  159. // error
  160. const int err = errno;
  161. fmt::print(stderr, "fork failed with error {}\n", err);
  162. return pid;
  163. } else if (pid == 0) {
  164. // child
  165. execlp(arg0, arg0, nullptr);
  166. const int err = errno;
  167. fmt::print(stderr, "execl failed with error {}\n", err);
  168. _exit(0);
  169. }
  170. return pid;
  171. }
  172. #endif