1*333d2b36SAndroid Build Coastguard Worker// Copyright 2017 Google Inc. All rights reserved. 2*333d2b36SAndroid Build Coastguard Worker// 3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*333d2b36SAndroid Build Coastguard Worker// 7*333d2b36SAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*333d2b36SAndroid Build Coastguard Worker// 9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*333d2b36SAndroid Build Coastguard Worker// limitations under the License. 14*333d2b36SAndroid Build Coastguard Worker 15*333d2b36SAndroid Build Coastguard Workerpackage signal 16*333d2b36SAndroid Build Coastguard Worker 17*333d2b36SAndroid Build Coastguard Workerimport ( 18*333d2b36SAndroid Build Coastguard Worker "os" 19*333d2b36SAndroid Build Coastguard Worker "os/signal" 20*333d2b36SAndroid Build Coastguard Worker "runtime/debug" 21*333d2b36SAndroid Build Coastguard Worker "syscall" 22*333d2b36SAndroid Build Coastguard Worker 23*333d2b36SAndroid Build Coastguard Worker "android/soong/ui/logger" 24*333d2b36SAndroid Build Coastguard Worker "time" 25*333d2b36SAndroid Build Coastguard Worker) 26*333d2b36SAndroid Build Coastguard Worker 27*333d2b36SAndroid Build Coastguard Worker// SetupSignals sets up signal handling to ensure all of our subprocesses are killed and that 28*333d2b36SAndroid Build Coastguard Worker// our log/trace buffers are flushed to disk. 29*333d2b36SAndroid Build Coastguard Worker// 30*333d2b36SAndroid Build Coastguard Worker// All of our subprocesses are in the same process group, so they'll receive a SIGINT at the 31*333d2b36SAndroid Build Coastguard Worker// same time we do. Most of the time this means we just need to ignore the signal and we'll 32*333d2b36SAndroid Build Coastguard Worker// just see errors from all of our subprocesses. But in case that fails, when we get a signal: 33*333d2b36SAndroid Build Coastguard Worker// 34*333d2b36SAndroid Build Coastguard Worker// 1. Wait two seconds to exit normally. 35*333d2b36SAndroid Build Coastguard Worker// 2. Call cancel() which is normally the cancellation of a Context. This will send a SIGKILL 36*333d2b36SAndroid Build Coastguard Worker// to any subprocesses attached to that context. 37*333d2b36SAndroid Build Coastguard Worker// 3. Wait two seconds to exit normally. 38*333d2b36SAndroid Build Coastguard Worker// 4. Call cleanup() to close the log/trace buffers, then panic. 39*333d2b36SAndroid Build Coastguard Worker// 5. If another two seconds passes (if cleanup got stuck, etc), then panic. 40*333d2b36SAndroid Build Coastguard Workerfunc SetupSignals(log logger.Logger, cancel, cleanup func()) { 41*333d2b36SAndroid Build Coastguard Worker signals := make(chan os.Signal, 5) 42*333d2b36SAndroid Build Coastguard Worker signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM) 43*333d2b36SAndroid Build Coastguard Worker go handleSignals(signals, log, cancel, cleanup) 44*333d2b36SAndroid Build Coastguard Worker} 45*333d2b36SAndroid Build Coastguard Worker 46*333d2b36SAndroid Build Coastguard Workerfunc handleSignals(signals chan os.Signal, log logger.Logger, cancel, cleanup func()) { 47*333d2b36SAndroid Build Coastguard Worker var timeouts int 48*333d2b36SAndroid Build Coastguard Worker var timeout <-chan time.Time 49*333d2b36SAndroid Build Coastguard Worker 50*333d2b36SAndroid Build Coastguard Worker handleTimeout := func() { 51*333d2b36SAndroid Build Coastguard Worker timeouts += 1 52*333d2b36SAndroid Build Coastguard Worker switch timeouts { 53*333d2b36SAndroid Build Coastguard Worker case 1: 54*333d2b36SAndroid Build Coastguard Worker // Things didn't exit cleanly, cancel our ctx (SIGKILL to subprocesses) 55*333d2b36SAndroid Build Coastguard Worker // Do this asynchronously to ensure it won't block and prevent us from 56*333d2b36SAndroid Build Coastguard Worker // taking more drastic measures. 57*333d2b36SAndroid Build Coastguard Worker log.Println("Still alive, killing subprocesses...") 58*333d2b36SAndroid Build Coastguard Worker go cancel() 59*333d2b36SAndroid Build Coastguard Worker case 2: 60*333d2b36SAndroid Build Coastguard Worker // Cancel didn't work. Try to run cleanup manually, then we'll panic 61*333d2b36SAndroid Build Coastguard Worker // at the next timer whether it finished or not. 62*333d2b36SAndroid Build Coastguard Worker log.Println("Still alive, cleaning up...") 63*333d2b36SAndroid Build Coastguard Worker 64*333d2b36SAndroid Build Coastguard Worker // Get all stacktraces to see what was stuck 65*333d2b36SAndroid Build Coastguard Worker debug.SetTraceback("all") 66*333d2b36SAndroid Build Coastguard Worker 67*333d2b36SAndroid Build Coastguard Worker go func() { 68*333d2b36SAndroid Build Coastguard Worker defer log.Panicln("Timed out exiting...") 69*333d2b36SAndroid Build Coastguard Worker cleanup() 70*333d2b36SAndroid Build Coastguard Worker }() 71*333d2b36SAndroid Build Coastguard Worker default: 72*333d2b36SAndroid Build Coastguard Worker // In case cleanup() deadlocks, the next tick will panic. 73*333d2b36SAndroid Build Coastguard Worker log.Panicln("Got signal, but timed out exiting...") 74*333d2b36SAndroid Build Coastguard Worker } 75*333d2b36SAndroid Build Coastguard Worker } 76*333d2b36SAndroid Build Coastguard Worker 77*333d2b36SAndroid Build Coastguard Worker for { 78*333d2b36SAndroid Build Coastguard Worker select { 79*333d2b36SAndroid Build Coastguard Worker case s := <-signals: 80*333d2b36SAndroid Build Coastguard Worker log.Println("Got signal:", s) 81*333d2b36SAndroid Build Coastguard Worker 82*333d2b36SAndroid Build Coastguard Worker // Another signal triggers our next timeout handler early 83*333d2b36SAndroid Build Coastguard Worker if timeout != nil { 84*333d2b36SAndroid Build Coastguard Worker handleTimeout() 85*333d2b36SAndroid Build Coastguard Worker } 86*333d2b36SAndroid Build Coastguard Worker 87*333d2b36SAndroid Build Coastguard Worker // Wait 2 seconds for everything to exit cleanly. 88*333d2b36SAndroid Build Coastguard Worker timeout = time.Tick(time.Second * 2) 89*333d2b36SAndroid Build Coastguard Worker case <-timeout: 90*333d2b36SAndroid Build Coastguard Worker handleTimeout() 91*333d2b36SAndroid Build Coastguard Worker } 92*333d2b36SAndroid Build Coastguard Worker } 93*333d2b36SAndroid Build Coastguard Worker} 94