Skip to content

Commit e28f3b7

Browse files
committed
fix: Make sure updater properly exists after launching update process
1 parent 53153ca commit e28f3b7

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

lib/libimhex/include/hex/helpers/utils.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ namespace hex {
9999
void startProgram(const std::vector<std::string> &command);
100100
int executeCommand(const std::string &command);
101101
std::optional<std::string> executeCommandWithOutput(const std::string &command);
102+
void executeCommandDetach(const std::string &command);
102103
void openWebpage(std::string url);
103104

104105
extern "C" void registerFont(const char *fontName, const char *fontPath);

lib/libimhex/source/helpers/utils.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
#elif defined(OS_LINUX)
3131
#include <unistd.h>
3232
#include <dlfcn.h>
33+
#include <spawn.h>
3334
#include <hex/helpers/utils_linux.hpp>
3435
#elif defined(OS_MACOS)
3536
#include <unistd.h>
3637
#include <dlfcn.h>
38+
#include <spawn.h>
3739
#include <hex/helpers/utils_macos.hpp>
3840
#include <CoreFoundation/CoreFoundation.h>
3941
#elif defined(OS_WEB)
@@ -354,6 +356,42 @@ namespace hex {
354356
return result;
355357
}
356358

359+
void executeCommandDetach(const std::string &command) {
360+
#if defined(OS_WINDOWS)
361+
STARTUPINFOA si = { };
362+
PROCESS_INFORMATION pi = { };
363+
si.cb = sizeof(si);
364+
365+
DWORD flags = CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW;
366+
std::string cmdCopy = command;
367+
368+
BOOL result = ::CreateProcessA(
369+
nullptr,
370+
cmdCopy.data(),
371+
nullptr,
372+
nullptr,
373+
false,
374+
flags,
375+
nullptr,
376+
nullptr,
377+
&si,
378+
&pi
379+
);
380+
381+
if (result) {
382+
::CloseHandle(pi.hProcess);
383+
::CloseHandle(pi.hThread);
384+
}
385+
#elif defined(OS_MACOS) || defined(OS_LINUX)
386+
pid_t pid;
387+
const char* argv[] = { "sh", "-c", command.c_str(), nullptr };
388+
389+
::posix_spawnp(&pid, "sh", nullptr, nullptr, const_cast<char* const*>(argv), nullptr);
390+
#elif defined(OS_WEB)
391+
std::ignore = command;
392+
#endif
393+
}
394+
357395
void openWebpage(std::string url) {
358396
if (!url.contains("://"))
359397
url = "https://" + url;

main/updater/source/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ auto updateCommand(std::string command) {
151151
const auto formattedCommand = fmt::format(fmt::runtime(command), updatePath.string());
152152

153153
hex::log::info("Starting update process with command: '{}'", formattedCommand);
154-
return hex::executeCommand(formattedCommand) == 0;
154+
155+
hex::executeCommandDetach(formattedCommand);
156+
157+
return 0;
155158
};
156159
}
157160

0 commit comments

Comments
 (0)