summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/execute.cc99
-rw-r--r--src/libctor.h8
2 files changed, 94 insertions, 13 deletions
diff --git a/src/execute.cc b/src/execute.cc
index 85c1da1..2111f49 100644
--- a/src/execute.cc
+++ b/src/execute.cc
@@ -13,6 +13,15 @@
#include <processthreadsapi.h>
#include <synchapi.h>
#include <processenv.h>
+#include <handleapi.h>
+#include <libloaderapi.h>
+
+//#include <winbase.h>
+#define INHERIT_PARENT_AFFINITY 0x00010000
+#define DETACHED_PROCESS 0x00000008
+
+#define WINDOWS_LEAN_AND_MEAN
+#include <windows.h>
#endif
#include <iostream>
@@ -163,23 +172,95 @@ int execute(const std::string& command,
env_str += '\0';
}
env_str += '\0';
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- ZeroMemory(&si,sizeof(si));
- si.cb=sizeof(si);
- ZeroMemory(&pi,sizeof(pi));
+
+ STARTUPINFO si{};
+// si.hStdInput = GetStdHandle(((DWORD)-10)/*STD_INPUT_HANDLE*/);
+// si.hStdOutput = GetStdHandle(((DWORD)-11)/*STD_OUTPUT_HANDLE*/);
+// si.hStdError = GetStdHandle(((DWORD)-12)/*STD_ERROR_HANDLE*/);
+// si.dwFlags = /*STARTF_USESTDHANDLES*/0x00000100;
+
+ PROCESS_INFORMATION pi{};
+
+ si.cb = sizeof(si);
+
// TODO: Use SetDllDirectory(...) to set DLL search directory
- if(!CreateProcess(nullptr, //"C:/WINDOWS/notepad.exe",
- //"notepad.exe c:/readme.txt",0,0,0,0,0,0,&si,&pi))
- (char*)cmd.data(),0,0,0,0,env_str.data(),0,&si,&pi))
+
+ if(!CreateProcess(nullptr, // lpApplicationName
+ (char*)cmd.data(), // lpCommandLine
+ nullptr, // lpProcessAttributes
+ nullptr, // lpThreadAttributes
+ TRUE, // bInheritHandles
+ INHERIT_PARENT_AFFINITY |
+ //DETACHED_PROCESS |
+ ///*DEBUG_PROCESS*/ 0x00000001 |
+ ///*CREATE_NO_WINDOW*/ 0x08000000 |
+ ///*CREATE_BREAKAWAY_FROM_JOB*/ 0x01000000 |
+ /*CREATE_NEW_PROCESS_GROUP*/ 0x00000200 |
+ 0, // dwCreationFlags
+ env_str.data(), // lpEnvironment
+ nullptr, // lpCurrentDirectory
+ &si, // lpStartupInfo
+ &pi)) // lpProcessInformation
{
return 1; // Could not start process;
}
if(terminate)
{
- ExitProcess(0);
+ char target[MAX_PATH+1];
+ char tmpdir[MAX_PATH+1];
+ int cnt{0};
+
+ // The returned string ends with a backslash
+ if(GetTempPathA(sizeof(tmpdir), tmpdir) == 0)
+ {
+ std::cerr << "Could not read TMP folder\n";
+ return GetLastError();
+ }
+
+ char source[MAX_PATH];
+ HMODULE module = GetModuleHandle(0);
+ GetModuleFileNameA(module, source, MAX_PATH);
+
+ while(true)
+ {
+ if(cnt > 10) // If we need to try more than 10 times something is wrong
+ {
+ return 1;
+ }
+
+ sprintf(target, "%s.tmp-%d", source, cnt);
+ if(MoveFileA(source, target))
+ {
+ break; // success
+ }
+
+ auto err = GetLastError();
+ if(err == ERROR_ALREADY_EXISTS)
+ {
+ if(DeleteFileA(target))
+ {
+ continue; // Try again
+ }
+
+ auto err = GetLastError();
+ if(err != ERROR_ACCESS_DENIED)
+ {
+ std::cerr << "Could not delete file\n";
+ return err;
+ }
+
+ cnt++;
+ continue; // Increment and try again
+ }
+ else
+ {
+ std::cerr << "Could not move file\n";
+ return err;
+ }
+ }
}
+
// Now 'pi.hProcess' contains the process HANDLE, which you can use to
// wait for it like this:
const DWORD infinite = 0xFFFFFFFF;
diff --git a/src/libctor.h b/src/libctor.h
index e4ea7e7..c91777e 100644
--- a/src/libctor.h
+++ b/src/libctor.h
@@ -116,10 +116,10 @@ int reg(ExternalConfigurations (*cb)(const Settings&),
const std::source_location location = std::source_location::current());
// Convenience macro - ugly but keeps things simple(r)
-#define CONCAT(a, b) CONCAT_INNER(a, b)
-#define CONCAT_INNER(a, b) a ## b
-#define UNIQUE_NAME(base) CONCAT(base, __LINE__)
-#define REG(cb) namespace { int UNIQUE_NAME(unique) = reg(cb); }
+#define CTOR_CONCAT(a, b) CTOR_CONCAT_INNER(a, b)
+#define CTOR_CONCAT_INNER(a, b) a ## b
+#define CTOR_UNIQUE_NAME(base) CTOR_CONCAT(base, __LINE__)
+#define REG(cb) namespace { int CTOR_UNIQUE_NAME(unique) = reg(cb); }
// Predefined configuration keys
namespace cfg