createKillOnCloseJob creates a Windows Job Object with KILL_ON_JOB_CLOSE flag. When the job handle is closed, all processes in the job will be terminated.
()
| 18 | // createKillOnCloseJob creates a Windows Job Object with KILL_ON_JOB_CLOSE flag. |
| 19 | // When the job handle is closed, all processes in the job will be terminated. |
| 20 | func createKillOnCloseJob() (windows.Handle, error) { |
| 21 | job, err := windows.CreateJobObject(nil, nil) |
| 22 | if err != nil { |
| 23 | return 0, err |
| 24 | } |
| 25 | |
| 26 | var info windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION |
| 27 | // Enable KILL_ON_JOB_CLOSE flag |
| 28 | info.BasicLimitInformation.LimitFlags = windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE |
| 29 | |
| 30 | _, err = windows.SetInformationJobObject( |
| 31 | job, |
| 32 | windows.JobObjectExtendedLimitInformation, |
| 33 | uintptr(unsafe.Pointer(&info)), |
| 34 | uint32(unsafe.Sizeof(info)), |
| 35 | ) |
| 36 | if err != nil { |
| 37 | _ = windows.CloseHandle(job) |
| 38 | return 0, err |
| 39 | } |
| 40 | return job, nil |
| 41 | } |
| 42 | |
| 43 | // assignProcessToJob assigns a process to a Job Object. |
| 44 | // Returns the process handle which should be closed by the caller. |
no outgoing calls