github.com/hernad/nomad@v1.6.112/drivers/shared/executor/executor_windows.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  //go:build windows
     5  
     6  package executor
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  	"syscall"
    12  )
    13  
    14  // configure new process group for child process
    15  func (e *UniversalExecutor) setNewProcessGroup() error {
    16  	// We need to check that as build flags includes windows for this file
    17  	if e.childCmd.SysProcAttr == nil {
    18  		e.childCmd.SysProcAttr = &syscall.SysProcAttr{}
    19  	}
    20  	e.childCmd.SysProcAttr.CreationFlags = syscall.CREATE_NEW_PROCESS_GROUP
    21  	return nil
    22  }
    23  
    24  // Cleanup any still hanging user processes
    25  func (e *UniversalExecutor) killProcessTree(proc *os.Process) error {
    26  	// We must first verify if the process is still running.
    27  	// (Windows process often lingered around after being reported as killed).
    28  	handle, err := syscall.OpenProcess(syscall.PROCESS_TERMINATE|syscall.SYNCHRONIZE|syscall.PROCESS_QUERY_INFORMATION, false, uint32(proc.Pid))
    29  	if err != nil {
    30  		return os.NewSyscallError("OpenProcess", err)
    31  	}
    32  	defer syscall.CloseHandle(handle)
    33  
    34  	result, err := syscall.WaitForSingleObject(syscall.Handle(handle), 0)
    35  
    36  	switch result {
    37  	case syscall.WAIT_OBJECT_0:
    38  		return nil
    39  	case syscall.WAIT_TIMEOUT:
    40  		// Process still running.  Just kill it.
    41  		return proc.Kill()
    42  	default:
    43  		return os.NewSyscallError("WaitForSingleObject", err)
    44  	}
    45  }
    46  
    47  // Send a Ctrl-Break signal for shutting down the process,
    48  // like in https://golang.org/src/os/signal/signal_windows_test.go
    49  func sendCtrlBreak(pid int) error {
    50  	dll, err := syscall.LoadDLL("kernel32.dll")
    51  	if err != nil {
    52  		return fmt.Errorf("Error loading kernel32.dll: %v", err)
    53  	}
    54  	proc, err := dll.FindProc("GenerateConsoleCtrlEvent")
    55  	if err != nil {
    56  		return fmt.Errorf("Cannot find procedure GenerateConsoleCtrlEvent: %v", err)
    57  	}
    58  	result, _, err := proc.Call(syscall.CTRL_BREAK_EVENT, uintptr(pid))
    59  	if result == 0 {
    60  		return fmt.Errorf("Error sending ctrl-break event: %v", err)
    61  	}
    62  	return nil
    63  }
    64  
    65  // Send the process a Ctrl-Break event, allowing it to shutdown by itself
    66  // before being Terminate.
    67  func (e *UniversalExecutor) shutdownProcess(_ os.Signal, proc *os.Process) error {
    68  	if err := sendCtrlBreak(proc.Pid); err != nil {
    69  		return fmt.Errorf("executor shutdown error: %v", err)
    70  	}
    71  	e.logger.Debug("sent Ctrl-Break to process", "pid", proc.Pid)
    72  
    73  	return nil
    74  }