-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle Agent Child processes on windows (#18)
* Added missing skipTests param * Added Windows Job Object to handle the child processes when agent process exits. Co-authored-by: cciutea <[email protected]>
- Loading branch information
1 parent
d05a63f
commit 1c06e93
Showing
3 changed files
with
103 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2020 New Relic Corporation. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
// JobObject on Windows allows groups of processes to be managed as a unit. | ||
type JobObject struct { | ||
handle windows.Handle | ||
} | ||
|
||
// NewJob creates a new instance of a Windows Job Object. | ||
func NewJob() (*JobObject, error) { | ||
jobObjectHandle, err := windows.CreateJobObject(nil, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create job object: %v", err) | ||
} | ||
|
||
jobInfo := windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION{ | ||
BasicLimitInformation: windows.JOBOBJECT_BASIC_LIMIT_INFORMATION{ | ||
LimitFlags: windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, | ||
}, | ||
} | ||
_, err = windows.SetInformationJobObject( | ||
jobObjectHandle, | ||
windows.JobObjectExtendedLimitInformation, | ||
uintptr(unsafe.Pointer(&jobInfo)), | ||
uint32(unsafe.Sizeof(jobInfo)), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to set job object info: %v", err) | ||
} | ||
return &JobObject{ | ||
handle: jobObjectHandle, | ||
}, nil | ||
} | ||
|
||
// AddProcess to the JobObject. | ||
func (jo *JobObject) AddProcess(process *os.Process) error { | ||
|
||
// Using unsafe operation we are using the handle inside os.Process. | ||
processHandle := (*struct { | ||
pid int | ||
handle windows.Handle | ||
})(unsafe.Pointer(process)).handle | ||
|
||
err := windows.AssignProcessToJobObject(jo.handle, processHandle) | ||
if err != nil { | ||
return fmt.Errorf("failed to assign process to job object: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
// Close will terminate the JobObject and also stop all the subprocesses. | ||
func (jo *JobObject) Close() error { | ||
err := windows.CloseHandle(jo.handle) | ||
if err != nil { | ||
return fmt.Errorf("failed to close the job object: %v", err) | ||
} | ||
return nil | ||
} |
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
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