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
15package signal
16
17import (
18	"os"
19	"os/signal"
20	"runtime/debug"
21	"syscall"
22
23	"android/soong/ui/logger"
24	"time"
25)
26
27// SetupSignals sets up signal handling to ensure all of our subprocesses are killed and that
28// our log/trace buffers are flushed to disk.
29//
30// All of our subprocesses are in the same process group, so they'll receive a SIGINT at the
31// same time we do. Most of the time this means we just need to ignore the signal and we'll
32// just see errors from all of our subprocesses. But in case that fails, when we get a signal:
33//
34//  1. Wait two seconds to exit normally.
35//  2. Call cancel() which is normally the cancellation of a Context. This will send a SIGKILL
36//     to any subprocesses attached to that context.
37//  3. Wait two seconds to exit normally.
38//  4. Call cleanup() to close the log/trace buffers, then panic.
39//  5. If another two seconds passes (if cleanup got stuck, etc), then panic.
40func SetupSignals(log logger.Logger, cancel, cleanup func()) {
41	signals := make(chan os.Signal, 5)
42	signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM)
43	go handleSignals(signals, log, cancel, cleanup)
44}
45
46func handleSignals(signals chan os.Signal, log logger.Logger, cancel, cleanup func()) {
47	var timeouts int
48	var timeout <-chan time.Time
49
50	handleTimeout := func() {
51		timeouts += 1
52		switch timeouts {
53		case 1:
54			// Things didn't exit cleanly, cancel our ctx (SIGKILL to subprocesses)
55			// Do this asynchronously to ensure it won't block and prevent us from
56			// taking more drastic measures.
57			log.Println("Still alive, killing subprocesses...")
58			go cancel()
59		case 2:
60			// Cancel didn't work. Try to run cleanup manually, then we'll panic
61			// at the next timer whether it finished or not.
62			log.Println("Still alive, cleaning up...")
63
64			// Get all stacktraces to see what was stuck
65			debug.SetTraceback("all")
66
67			go func() {
68				defer log.Panicln("Timed out exiting...")
69				cleanup()
70			}()
71		default:
72			// In case cleanup() deadlocks, the next tick will panic.
73			log.Panicln("Got signal, but timed out exiting...")
74		}
75	}
76
77	for {
78		select {
79		case s := <-signals:
80			log.Println("Got signal:", s)
81
82			// Another signal triggers our next timeout handler early
83			if timeout != nil {
84				handleTimeout()
85			}
86
87			// Wait 2 seconds for everything to exit cleanly.
88			timeout = time.Tick(time.Second * 2)
89		case <-timeout:
90			handleTimeout()
91		}
92	}
93}
94