Created
June 4, 2020 08:34
-
-
Save fcharlie/f322c9a06292f8ce44a92c96f02c60dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
#include <Windows.h> | |
class ProcessGroup { | |
public: | |
ProcessGroup() = default; | |
ProcessGroup(const ProcessGroup &) = delete; | |
ProcessGroup &operator=(const ProcessGroup &) = delete; | |
~ProcessGroup() { | |
if (hJob != nullptr) { | |
CloseHandle(hJob); | |
} | |
} | |
bool Initialize(const wchar_t *name) { | |
if (hJob = CreateJobObjectW(nullptr, name); hJob == nullptr) { | |
// dump last error code | |
return false; | |
} | |
JOBOBJECT_EXTENDED_LIMIT_INFORMATION limit = {0}; | |
JOBOBJECT_BASIC_LIMIT_INFORMATION &basic_limit = | |
limit.BasicLimitInformation; | |
basic_limit.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_TIME | | |
JOB_OBJECT_LIMIT_PROCESS_MEMORY | | |
JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION; | |
limit.ProcessMemoryLimit = memory_limit; | |
basic_limit.PerProcessUserTimeLimit.QuadPart = ms * 10000; | |
if (!SetInformationJobObject(hJOB, JobObjectExtendedLimitInformation, | |
&limit, sizeof(limit))) { | |
return false; | |
} | |
return true; | |
} | |
void Terminate(UINT exitcode) { | |
if (TerminateJobObject(hJob, exitcode) != TRUE) { | |
/// show error | |
} | |
} | |
BOOL Exec(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, | |
LPSECURITY_ATTRIBUTES lpProcessAttributes, | |
LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, | |
DWORD dwCreationFlags, LPVOID lpEnvironment, | |
LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, | |
LPPROCESS_INFORMATION lpProcessInformation) { | |
if (CreateProcessW(lpApplicationName, lpCommandLine, lpProcessAttributes, | |
lpThreadAttributes, bInheritHandles, | |
CREATE_SUSPENDED | dwCreationFlags, lpEnvironment, | |
lpCurrentDirectory, lpStartupInfo, | |
lpProcessInformation) != TRUE) { | |
return false; | |
} | |
if (!AssignProcessToJobObject(hJob, pi.hProcess)) { | |
CloseHandle(pi.hThread); | |
TerminateProcess(pi.hProcess, 0); | |
CloseHandle(pi.hProcess); | |
return false; | |
} | |
if (-1 == ResumeThread(pi.hThread)) { | |
CloseHandle(pi.hThread); | |
TerminateProcess(pi.hProcess, 0); | |
CloseHandle(pi.hProcess); | |
return false; | |
} | |
return true; | |
} | |
private: | |
HANDLE hJob{nullptr}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://unit42.paloaltonetworks.com/what-i-learned-from-reverse-engineering-windows-containers/