Resolve own absolute path for subprocess spawn

This commit is contained in:
Luke Street 2025-09-29 09:25:57 -06:00
parent 195f6c1408
commit 4a2ba45620
4 changed files with 24 additions and 12 deletions

View File

@ -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<uint16_t> commandLineW;
extern bool debugEnabled;
extern unsigned int debugIndent;

View File

@ -1068,7 +1068,7 @@ namespace kernel32 {
*/
LPSTR WIN_FUNC GetCommandLineA() {
DEBUG_LOG("GetCommandLineA\n");
return wibo::commandLine;
return const_cast<LPSTR>(wibo::commandLine.c_str());
}
LPWSTR WIN_FUNC GetCommandLineW() {

View File

@ -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<uint16_t> 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;

View File

@ -192,9 +192,9 @@ namespace processes {
std::vector<char *> nativeArgs;
nativeArgs.reserve(storage.size() + 2);
nativeArgs.push_back(wibo::executableName);
nativeArgs.push_back(const_cast<char *>(wibo::executableName.c_str()));
for (auto &entry : storage) {
nativeArgs.push_back(entry.data());
nativeArgs.push_back(const_cast<char *>(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;