startup_checks: Use fmt::print, fix exec error handling

Uses fmt::print opposed to std::fprintf for error printing.

Call exit instead of returning to caller to prevent a like issue the
previous commit was trying to solve.

Removes unneeded comment.

Co-authored-by: liamwhite <liamwhite@users.noreply.github.com>
Co-authored-by: Lioncash <mathew1800@gmail.com>
This commit is contained in:
lat9nq 2022-11-23 21:57:28 -05:00
parent 3e6c835a64
commit 35319ca3a5

View File

@ -4,11 +4,11 @@
#include "video_core/vulkan_common/vulkan_wrapper.h" #include "video_core/vulkan_common/vulkan_wrapper.h"
#ifdef _WIN32 #ifdef _WIN32
#include <cstring> // for memset, strncpy, strncmp #include <cstring>
#include <processthreadsapi.h> #include <processthreadsapi.h>
#include <windows.h> #include <windows.h>
#elif defined(YUZU_UNIX) #elif defined(YUZU_UNIX)
#include <cstring> // for strncmp #include <cstring>
#include <errno.h> #include <errno.h>
#include <spawn.h> #include <spawn.h>
#include <sys/types.h> #include <sys/types.h>
@ -16,7 +16,7 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <cstdio> #include <fmt/core.h>
#include "video_core/vulkan_common/vulkan_instance.h" #include "video_core/vulkan_common/vulkan_instance.h"
#include "video_core/vulkan_common/vulkan_library.h" #include "video_core/vulkan_common/vulkan_library.h"
#include "yuzu/startup_checks.h" #include "yuzu/startup_checks.h"
@ -30,7 +30,7 @@ void CheckVulkan() {
Vulkan::CreateInstance(library, dld, VK_API_VERSION_1_0); Vulkan::CreateInstance(library, dld, VK_API_VERSION_1_0);
} catch (const Vulkan::vk::Exception& exception) { } catch (const Vulkan::vk::Exception& exception) {
std::fprintf(stderr, "Failed to initialize Vulkan: %s\n", exception.what()); fmt::print(stderr, "Failed to initialize Vulkan: {}\n", exception.what());
} }
} }
@ -52,7 +52,7 @@ bool CheckEnvVars(bool* is_child) {
*is_child = true; *is_child = true;
return false; return false;
} else if (!SetEnvironmentVariableA(IS_CHILD_ENV_VAR, ENV_VAR_ENABLED_TEXT)) { } else if (!SetEnvironmentVariableA(IS_CHILD_ENV_VAR, ENV_VAR_ENABLED_TEXT)) {
std::fprintf(stderr, "SetEnvironmentVariableA failed to set %s with error %lu\n", fmt::print(stderr, "SetEnvironmentVariableA failed to set {} with error {}\n",
IS_CHILD_ENV_VAR, GetLastError()); IS_CHILD_ENV_VAR, GetLastError());
return true; return true;
} }
@ -72,7 +72,7 @@ bool StartupChecks(const char* arg0, bool* has_broken_vulkan, bool perform_vulka
// Set the startup variable for child processes // Set the startup variable for child processes
const bool env_var_set = SetEnvironmentVariableA(STARTUP_CHECK_ENV_VAR, ENV_VAR_ENABLED_TEXT); const bool env_var_set = SetEnvironmentVariableA(STARTUP_CHECK_ENV_VAR, ENV_VAR_ENABLED_TEXT);
if (!env_var_set) { if (!env_var_set) {
std::fprintf(stderr, "SetEnvironmentVariableA failed to set %s with error %lu\n", fmt::print(stderr, "SetEnvironmentVariableA failed to set {} with error {}\n",
STARTUP_CHECK_ENV_VAR, GetLastError()); STARTUP_CHECK_ENV_VAR, GetLastError());
return false; return false;
} }
@ -91,22 +91,22 @@ bool StartupChecks(const char* arg0, bool* has_broken_vulkan, bool perform_vulka
DWORD exit_code = STILL_ACTIVE; DWORD exit_code = STILL_ACTIVE;
const int err = GetExitCodeProcess(process_info.hProcess, &exit_code); const int err = GetExitCodeProcess(process_info.hProcess, &exit_code);
if (err == 0) { if (err == 0) {
std::fprintf(stderr, "GetExitCodeProcess failed with error %lu\n", GetLastError()); fmt::print(stderr, "GetExitCodeProcess failed with error {}\n", GetLastError());
} }
// Vulkan is broken if the child crashed (return value is not zero) // Vulkan is broken if the child crashed (return value is not zero)
*has_broken_vulkan = (exit_code != 0); *has_broken_vulkan = (exit_code != 0);
if (CloseHandle(process_info.hProcess) == 0) { if (CloseHandle(process_info.hProcess) == 0) {
std::fprintf(stderr, "CloseHandle failed with error %lu\n", GetLastError()); fmt::print(stderr, "CloseHandle failed with error {}\n", GetLastError());
} }
if (CloseHandle(process_info.hThread) == 0) { if (CloseHandle(process_info.hThread) == 0) {
std::fprintf(stderr, "CloseHandle failed with error %lu\n", GetLastError()); fmt::print(stderr, "CloseHandle failed with error {}\n", GetLastError());
} }
} }
if (!SetEnvironmentVariableA(STARTUP_CHECK_ENV_VAR, nullptr)) { if (!SetEnvironmentVariableA(STARTUP_CHECK_ENV_VAR, nullptr)) {
std::fprintf(stderr, "SetEnvironmentVariableA failed to clear %s with error %lu\n", fmt::print(stderr, "SetEnvironmentVariableA failed to clear {} with error {}\n",
STARTUP_CHECK_ENV_VAR, GetLastError()); STARTUP_CHECK_ENV_VAR, GetLastError());
} }
@ -114,7 +114,7 @@ bool StartupChecks(const char* arg0, bool* has_broken_vulkan, bool perform_vulka
const int env_var_set = setenv(STARTUP_CHECK_ENV_VAR, ENV_VAR_ENABLED_TEXT, 1); const int env_var_set = setenv(STARTUP_CHECK_ENV_VAR, ENV_VAR_ENABLED_TEXT, 1);
if (env_var_set == -1) { if (env_var_set == -1) {
const int err = errno; const int err = errno;
std::fprintf(stderr, "setenv failed to set %s with error %d\n", STARTUP_CHECK_ENV_VAR, err); fmt::print(stderr, "setenv failed to set {} with error {}\n", STARTUP_CHECK_ENV_VAR, err);
return false; return false;
} }
@ -129,7 +129,7 @@ bool StartupChecks(const char* arg0, bool* has_broken_vulkan, bool perform_vulka
const int r_val = waitpid(pid, &status, 0); const int r_val = waitpid(pid, &status, 0);
if (r_val == -1) { if (r_val == -1) {
const int err = errno; const int err = errno;
std::fprintf(stderr, "wait failed with error %d\n", err); fmt::print(stderr, "wait failed with error {}\n", err);
return false; return false;
} }
// Vulkan is broken if the child crashed (return value is not zero) // Vulkan is broken if the child crashed (return value is not zero)
@ -139,7 +139,7 @@ bool StartupChecks(const char* arg0, bool* has_broken_vulkan, bool perform_vulka
const int env_var_cleared = unsetenv(STARTUP_CHECK_ENV_VAR); const int env_var_cleared = unsetenv(STARTUP_CHECK_ENV_VAR);
if (env_var_cleared == -1) { if (env_var_cleared == -1) {
const int err = errno; const int err = errno;
std::fprintf(stderr, "unsetenv failed to clear %s with error %d\n", STARTUP_CHECK_ENV_VAR, fmt::print(stderr, "unsetenv failed to clear {} with error {}\n", STARTUP_CHECK_ENV_VAR,
err); err);
} }
#endif #endif
@ -169,7 +169,7 @@ bool SpawnChild(const char* arg0, PROCESS_INFORMATION* pi, int flags) {
pi // lpProcessInformation pi // lpProcessInformation
); );
if (!process_created) { if (!process_created) {
std::fprintf(stderr, "CreateProcessA failed with error %lu\n", GetLastError()); fmt::print(stderr, "CreateProcessA failed with error {}\n", GetLastError());
return false; return false;
} }
@ -182,14 +182,14 @@ pid_t SpawnChild(const char* arg0) {
if (pid == -1) { if (pid == -1) {
// error // error
const int err = errno; const int err = errno;
std::fprintf(stderr, "fork failed with error %d\n", err); fmt::print(stderr, "fork failed with error {}\n", err);
return pid; return pid;
} else if (pid == 0) { } else if (pid == 0) {
// child // child
execl(arg0, arg0, nullptr); execl(arg0, arg0, nullptr);
const int err = errno; const int err = errno;
std::fprintf(stderr, "execl failed with error %d\n", err); fmt::print(stderr, "execl failed with error {}\n", err);
return -1; _exit(0);
} }
return pid; return pid;