9
9
*
10
10
*****************************************************************************/
11
11
12
+ #define WIN32_NO_STATUS
12
13
#define WIN32_LEAN_AND_MEAN
13
14
#include < Windows.h>
15
+ #undef WIN32_NO_STATUS
16
+ #include < ntstatus.h>
17
+ #include < winnt.h>
18
+ #include < winternl.h>
14
19
#include < delayimp.h>
15
20
#include " CCefApp.h"
16
21
#include < string>
21
26
#pragma comment(lib, "cef_sandbox.lib")
22
27
#endif
23
28
29
+ DWORD WINAPI CheckParentProcessAliveness (LPVOID);
30
+
24
31
int _declspec (dllexport) InitCEF()
25
32
{
26
33
// Get absolute CEFLauncher.exe path
@@ -46,20 +53,62 @@ int _declspec(dllexport) InitCEF()
46
53
sandboxInfo = scopedSandbox.sandbox_info ();
47
54
#endif
48
55
49
- if (HANDLE job = CreateJobObjectW (nullptr , nullptr ); job != nullptr )
56
+ const HANDLE parentCheckThread = CreateThread (nullptr , 0 , CheckParentProcessAliveness, nullptr , 0 , nullptr );
57
+
58
+ const int exitCode = CefExecuteProcess (mainArgs, app, sandboxInfo);
59
+
60
+ if (parentCheckThread != nullptr )
50
61
{
51
- JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits{};
52
- limits.BasicLimitInformation .LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
62
+ TerminateThread (parentCheckThread, 0 );
63
+ CloseHandle (parentCheckThread);
64
+ }
53
65
54
- if (SetInformationJobObject (job, JobObjectExtendedLimitInformation, &limits, sizeof (limits)))
55
- {
56
- AssignProcessToJobObject (job, GetCurrentProcess ());
57
- }
58
- else
66
+ return exitCode;
67
+ }
68
+
69
+ static DWORD WINAPI CheckParentProcessAliveness (LPVOID)
70
+ {
71
+ NTSTATUS (NTAPI * queryInformation)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG) = nullptr ;
72
+
73
+ if (HMODULE const ntdll = GetModuleHandleW (L" ntdll.dll" ); ntdll != nullptr )
74
+ {
75
+ queryInformation = reinterpret_cast <decltype (queryInformation)>(GetProcAddress (ntdll, " NtQueryInformationProcess" ));
76
+ }
77
+
78
+ if (queryInformation == nullptr )
79
+ return 1 ;
80
+
81
+ PROCESS_BASIC_INFORMATION info{};
82
+
83
+ ULONG returnLength = 0 ;
84
+ NTSTATUS status = queryInformation (GetCurrentProcess (), ProcessBasicInformation, &info, sizeof (info), &returnLength);
85
+
86
+ if (!NT_SUCCESS (status) || returnLength < sizeof (PROCESS_BASIC_INFORMATION))
87
+ return 2 ;
88
+
89
+ const auto parentProcessId = static_cast <DWORD>(reinterpret_cast <ULONG_PTR>(info.Reserved3 ));
90
+ const HANDLE parentProcess = OpenProcess (SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE , parentProcessId);
91
+
92
+ if (parentProcess == nullptr )
93
+ {
94
+ if (GetLastError () == ERROR_INVALID_PARAMETER)
95
+ ExitProcess (0 );
96
+
97
+ return 3 ;
98
+ }
99
+
100
+ while (true )
101
+ {
102
+ DWORD exitCode{};
103
+
104
+ if (!GetExitCodeProcess (parentProcess, &exitCode) || exitCode != STILL_ACTIVE)
59
105
{
60
- CloseHandle (job);
106
+ CloseHandle (parentProcess);
107
+ ExitProcess (exitCode);
61
108
}
109
+
110
+ Sleep (1000 );
62
111
}
63
112
64
- return CefExecuteProcess (mainArgs, app, sandboxInfo) ;
113
+ return 0 ;
65
114
}
0 commit comments