Windows: fix reexec not working when spaces in exe path

This commit is contained in:
Jean-Francois Dockes 2022-01-19 14:43:38 +00:00
parent fbcf0f42e9
commit fdb18a7c4b
5 changed files with 27 additions and 9 deletions

View File

@ -73,12 +73,14 @@
#include "closefrom.h" #include "closefrom.h"
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include "safeunistd.h"
#include <fcntl.h> #include <fcntl.h>
#include <string.h> #include <string.h>
#ifndef _WIN32
#include <sys/param.h> #include <sys/param.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
#endif
/* #define DEBUG_CLOSEFROM */ /* #define DEBUG_CLOSEFROM */

View File

@ -1611,13 +1611,13 @@ int Pidfile::write_pid()
} }
char pidstr[20]; char pidstr[20];
sprintf(pidstr, "%u", int(getpid())); sprintf(pidstr, "%u", int(getpid()));
lseek(fd, 0, 0); ::lseek(fd, 0, 0);
if (::write(fd, pidstr, strlen(pidstr)) != (PATHUT_SSIZE_T)strlen(pidstr)) { if (::write(fd, pidstr, strlen(pidstr)) != (PATHUT_SSIZE_T)strlen(pidstr)) {
m_reason = "write failed"; m_reason = "write failed";
return -1; return -1;
} }
#ifdef _WIN32 #ifdef _WIN32
close(fd); ::close(fd);
#endif #endif
return 0; return 0;
} }

View File

@ -32,6 +32,7 @@
#include <psapi.h> #include <psapi.h>
#include "smallut.h" #include "smallut.h"
#include "pathut.h" #include "pathut.h"
#include "closefrom.h"
using namespace std; using namespace std;
@ -1242,13 +1243,26 @@ void ReExec::reexec()
return; return;
} }
// Fill up argv // Fill up argv. Can you believe that we need to quote the args !!
int i = 0; // Esp. argv[0] if it's c:\program files\...
std::vector<std::unique_ptr<wchar_t[]>> ptrs; std::vector<std::unique_ptr<wchar_t[]>> ptrs;
for (const auto& arg: m_argv) { std::unique_ptr<wchar_t[]> cmd = utf8towchar(m_argv[0]);
unsigned int i = 0;
for (; i < m_argv.size(); i++) {
LOGDEB2("argv[" << i << "] " << m_argv[i] << "\n");
std::string arg;
argQuote(m_argv[i], arg);
LOGDEB2(" Quoted: [" << arg << "]\n");
ptrs.push_back(utf8towchar(arg)); ptrs.push_back(utf8towchar(arg));
argv[i++] = ptrs.back().get(); argv[i] = ptrs.back().get();
} }
argv[i] = nullptr; argv[i] = nullptr;
_wexecvp(argv[0], argv);
// Close all descriptors except 0,1,2
// Does not work under windows for some reason, the new process does not start
//libclf_closefrom(3);
auto ret = _wexecvp(cmd.get(), argv);
LOGERR("_WEXECVP ["<<argv[0]<< "] FAILED: returned: " << ret << " errno " << errno << "\n");
std::cerr<<"_WEXECVP ["<<argv[0]<< "] FAILED: returned: " << ret << " errno " << errno << "\n";
} }

View File

@ -367,5 +367,6 @@ copyunrtf
copymutagen copymutagen
copywpd copywpd
copypff copypff
copypython copypython
echo "MKINSTDIR OK"

View File

@ -88,6 +88,7 @@ SOURCES += \
../../utils/cancelcheck.cpp \ ../../utils/cancelcheck.cpp \
../../utils/chrono.cpp \ ../../utils/chrono.cpp \
../../utils/circache.cpp \ ../../utils/circache.cpp \
../../utils/closefrom.cpp \
../../utils/cmdtalk.cpp \ ../../utils/cmdtalk.cpp \
../../utils/conftree.cpp \ ../../utils/conftree.cpp \
../../utils/copyfile.cpp \ ../../utils/copyfile.cpp \