| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- /*
- Copyright 2020 Docker Compose CLI authors
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package mobycli
- import (
- "fmt"
- "unsafe"
- "golang.org/x/sys/windows"
- )
- func init() {
- if err := killSubProcessesOnClose(); err != nil {
- fmt.Println("failed to create job:", err)
- }
- }
- // killSubProcessesOnClose will ensure on windows that all child processes of the current process are killed if parent is killed.
- func killSubProcessesOnClose() error {
- job, err := windows.CreateJobObject(nil, nil)
- if err != nil {
- return err
- }
- info := windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION{
- BasicLimitInformation: windows.JOBOBJECT_BASIC_LIMIT_INFORMATION{
- LimitFlags: windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
- },
- }
- if _, err := windows.SetInformationJobObject(
- job,
- windows.JobObjectExtendedLimitInformation,
- uintptr(unsafe.Pointer(&info)),
- uint32(unsafe.Sizeof(info))); err != nil {
- _ = windows.CloseHandle(job)
- return err
- }
- if err := windows.AssignProcessToJobObject(job, windows.CurrentProcess()); err != nil {
- _ = windows.CloseHandle(job)
- return err
- }
- return nil
- }
|