On windows 10, I would like to suspend a whole process tree using NtSuspendProcess on each of the children/grand-children processes.
I am starting the parent process using exec.Command("java", ...) and the process tree should look like:
myapp (executes cmd.Start())
\__java (parent)
\__java
|__proc1
\__proc2
I am able to retrieve the process parent handle uintptr with:
cmd := exec.Command("java", ...)
cmd.Start()
// mirror os.Process struct
type osprocmirror struct {
Pid int
handle uintptr
isdone uint32
sigMu sync.RWMutex
}
handle := (*osprocmirror)(unsafe.Pointer(cmd.Process))
I can't figure out how I can retrieve the handles of the child processes from the parent handle (or pid), in order to suspend them individually.
(since the option to suspend all together parent and children processes does not exist, I should suspend one by one right?)
- I tried by listing all processes and then selecting only the ones with the the correct parent pid , but I cannot find a way to reliably get the parent pid by knowing the child pid (plus there would be problems in case of grandchildren processes, plus I would prefer working exclusively with handles which should be more reliable).
- I tried to create a job into which the first process is spawned, in order to be able to list all the child pids/handles using the job's handle, but it's quite complex and would need some low level implementations (which do not fit well with
os.execpackage). - I found a windows function from
user32.dllcalledEnumChildWindowsand started to implement it, but since I did not understand how it works completely (does it only work with processes that have graphical windows? because I'm not working with GUI at all), I wanted to know other possible solutions (maybe easier).
Is there a simpler way of getting all the children processes handles from the parent handle?
If not, do you have some good example for this sort of implementation?
Thank you