Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exec: Don't depend on internals of os.Process #4233

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions cli/azd/pkg/exec/cmdtree_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ type CmdTree struct {
jobObject windows.Handle
}

type winProcess struct {
_ uint32 //pid
hndl windows.Handle
}

func (o *CmdTree) Start() error {
o.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
Expand Down Expand Up @@ -62,9 +57,18 @@ func (o *CmdTree) Start() error {
return fmt.Errorf("failed to set job object info: %w", err)
}

err = windows.AssignProcessToJobObject(
o.jobObject,
(*winProcess)(unsafe.Pointer(o.Process)).hndl)
process, err := windows.OpenProcess(windows.PROCESS_SET_QUOTA|windows.PROCESS_TERMINATE, false, uint32(o.Process.Pid))
if err != nil {
return fmt.Errorf("failed to open process: %w", err)
}
defer func() {
err := windows.CloseHandle(process)
if err != nil {
log.Printf("failed to close process handle: %s\n", err)
}
}()

err = windows.AssignProcessToJobObject(o.jobObject, process)

if err != nil {
return fmt.Errorf("failed to assign process to job object: %w", err)
Expand All @@ -74,7 +78,7 @@ func (o *CmdTree) Start() error {
}

func (o *CmdTree) Kill() {
err := windows.TerminateJobObject(windows.Handle(o.jobObject), 0)
err := windows.TerminateJobObject(o.jobObject, 0)
if err != nil {
log.Printf("failed to terminate job object %d: %s\n", o.jobObject, err)
}
Expand Down
Loading