diff --git a/common.h b/common.h index 687905c..c182475 100644 --- a/common.h +++ b/common.h @@ -102,8 +102,8 @@ namespace wibo { extern uint32_t lastError; extern char **argv; extern int argc; - extern char *executableName; - extern char *commandLine; + extern std::string executableName; + extern std::string commandLine; extern std::vector commandLineW; extern bool debugEnabled; extern unsigned int debugIndent; diff --git a/dll/kernel32.cpp b/dll/kernel32.cpp index c584943..6a31f34 100644 --- a/dll/kernel32.cpp +++ b/dll/kernel32.cpp @@ -1068,7 +1068,7 @@ namespace kernel32 { */ LPSTR WIN_FUNC GetCommandLineA() { DEBUG_LOG("GetCommandLineA\n"); - return wibo::commandLine; + return const_cast(wibo::commandLine.c_str()); } LPWSTR WIN_FUNC GetCommandLineW() { diff --git a/main.cpp b/main.cpp index 8123c5c..aeb35a7 100644 --- a/main.cpp +++ b/main.cpp @@ -17,8 +17,8 @@ uint32_t wibo::lastError = 0; char** wibo::argv; int wibo::argc; -char *wibo::executableName; -char *wibo::commandLine; +std::string wibo::executableName; +std::string wibo::commandLine; std::vector wibo::commandLineW; wibo::Executable *wibo::mainModule = 0; bool wibo::debugEnabled = false; @@ -254,6 +254,18 @@ int main(int argc, char **argv) { return argc <= 1 ? 0 : 1; } + // Try to resolve our own executable path + std::error_code ec; + auto resolved = std::filesystem::read_symlink("/proc/self/exe", ec); + std::string executablePath; + if (!ec) { + executablePath = resolved.string(); + } else { + const char *selfArg = argv[0] ? argv[0] : ""; + auto absCandidate = std::filesystem::absolute(selfArg, ec); + executablePath = ec ? std::string(selfArg) : absCandidate.string(); + } + if (!chdirPath.empty()) { if (chdir(chdirPath.c_str()) != 0) { std::string message = std::string("Failed to chdir to ") + chdirPath; @@ -340,11 +352,11 @@ int main(int argc, char **argv) { } cmdLine += '\0'; - wibo::commandLine = cmdLine.data(); - wibo::commandLineW = stringToWideString(wibo::commandLine); - DEBUG_LOG("Command line: %s\n", wibo::commandLine); + wibo::commandLine = cmdLine; + wibo::commandLineW = stringToWideString(wibo::commandLine.c_str()); + DEBUG_LOG("Command line: %s\n", wibo::commandLine.c_str()); - wibo::executableName = argv[0]; + wibo::executableName = executablePath; wibo::argv = guestArgv; wibo::argc = guestArgc; diff --git a/processes.cpp b/processes.cpp index 93ba0e1..ff584ce 100644 --- a/processes.cpp +++ b/processes.cpp @@ -192,9 +192,9 @@ namespace processes { std::vector nativeArgs; nativeArgs.reserve(storage.size() + 2); - nativeArgs.push_back(wibo::executableName); + nativeArgs.push_back(const_cast(wibo::executableName.c_str())); for (auto &entry : storage) { - nativeArgs.push_back(entry.data()); + nativeArgs.push_back(const_cast(entry.c_str())); } nativeArgs.push_back(nullptr); @@ -205,7 +205,7 @@ namespace processes { setenv("WIBO_DEBUG_INDENT", indent.c_str(), 1); pid_t pid = -1; - int spawnResult = posix_spawn(&pid, wibo::executableName, &actions, nullptr, nativeArgs.data(), environ); + int spawnResult = posix_spawn(&pid, wibo::executableName.c_str(), &actions, nullptr, nativeArgs.data(), environ); posix_spawn_file_actions_destroy(&actions); if (spawnResult != 0) { return spawnResult;