go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/tools/internal/apigen/command.go (about) 1 // Copyright 2015 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package apigen 16 17 import ( 18 "context" 19 "os" 20 "os/exec" 21 "time" 22 23 "go.chromium.org/luci/common/clock" 24 log "go.chromium.org/luci/common/logging" 25 ) 26 27 const shutdownWait = 20 * time.Second 28 29 // killableCommand is an extension of exec.Cmd that can be easily killed. 30 type killableCommand struct { 31 *exec.Cmd 32 } 33 34 func (cmd *killableCommand) kill(c context.Context) { 35 c = log.SetField(c, "cmd", cmd.Path) 36 37 // First, try and nicely kill the process. 38 log.Debugf(c, "Sending interrupt to process.") 39 cmd.Process.Signal(os.Interrupt) 40 41 processFinishedC := make(chan struct{}) 42 defer close(processFinishedC) 43 go func() { 44 total := time.Duration(0) 45 t := clock.NewTimer(c) 46 for total < shutdownWait { 47 t.Reset(time.Second) 48 select { 49 case <-processFinishedC: 50 return 51 52 case <-t.GetC(): 53 // Signal the process again. 54 total += time.Second 55 log.Debugf(c, "Process not terminated; sending interrupt to process.") 56 cmd.Process.Signal(os.Interrupt) 57 } 58 } 59 60 log.Debugf(c, "Process did not gracefully exit; sending KILL.") 61 cmd.Process.Kill() 62 }() 63 64 cmd.Wait() 65 log.Debugf(c, "Process terminated.") 66 }