Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 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 build |
| 16 | |
| 17 | import ( |
| 18 | "os" |
| 19 | "os/signal" |
| 20 | "runtime/debug" |
| 21 | "syscall" |
| 22 | |
| 23 | "android/soong/ui/logger" |
| 24 | ) |
| 25 | |
| 26 | // SetupSignals sets up signal handling to kill our children and allow us to cleanly finish |
| 27 | // writing our log/trace files. |
| 28 | // |
| 29 | // Currently, on the first SIGINT|SIGALARM we call the cancel() function, which is usually |
| 30 | // the CancelFunc returned by context.WithCancel, which will kill all the commands running |
| 31 | // within that Context. Usually that's enough, and you'll run through your normal error paths. |
| 32 | // |
| 33 | // If another signal comes in after the first one, we'll trigger a panic with full stacktraces |
| 34 | // from every goroutine so that it's possible to debug what is stuck. Just before the process |
| 35 | // exits, we'll call the cleanup() function so that you can flush your log files. |
| 36 | func SetupSignals(log logger.Logger, cancel, cleanup func()) { |
| 37 | signals := make(chan os.Signal, 5) |
| 38 | // TODO: Handle other signals |
| 39 | signal.Notify(signals, os.Interrupt, syscall.SIGALRM) |
| 40 | go handleSignals(signals, log, cancel, cleanup) |
| 41 | } |
| 42 | |
| 43 | func handleSignals(signals chan os.Signal, log logger.Logger, cancel, cleanup func()) { |
| 44 | defer cleanup() |
| 45 | |
| 46 | var force bool |
| 47 | |
| 48 | for { |
| 49 | s := <-signals |
| 50 | if force { |
| 51 | // So that we can better see what was stuck |
| 52 | debug.SetTraceback("all") |
| 53 | log.Panicln("Second signal received:", s) |
| 54 | } else { |
| 55 | log.Println("Got signal:", s) |
| 56 | cancel() |
| 57 | force = true |
| 58 | } |
| 59 | } |
| 60 | } |