summaryrefslogtreecommitdiff
path: root/src/execute.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2022-08-24 22:44:05 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2022-08-24 22:44:05 +0200
commita2b83596438eac9353acf254ca91842697f6001a (patch)
tree6ac075ac6f6d30b5e30a62673b7d5968df91a5e9 /src/execute.cc
parentc8e7d8922310108f2d46189a8f48abcb68e72534 (diff)
WIP: Move
Diffstat (limited to 'src/execute.cc')
-rw-r--r--src/execute.cc99
1 files changed, 90 insertions, 9 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;