Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Todd Wang | 8c4e5cc | 2015-04-09 11:30:52 -0700 | [diff] [blame] | 5 | // Package signals implements utilities for managing process shutdown with |
| 6 | // support for signal-handling. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 7 | package signals |
| 8 | |
Todd Wang | 8c4e5cc | 2015-04-09 11:30:52 -0700 | [diff] [blame] | 9 | // TODO(caprita): Rename the function to Shutdown() and the package to shutdown |
| 10 | // since it's not just signals anymore. |
| 11 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 12 | import ( |
| 13 | "os" |
| 14 | "os/signal" |
| 15 | "syscall" |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 16 | "time" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 17 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 18 | "v.io/v23" |
| 19 | "v.io/v23/context" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | type stopSignal string |
| 23 | |
| 24 | func (stopSignal) Signal() {} |
| 25 | func (s stopSignal) String() string { return string(s) } |
| 26 | |
| 27 | const ( |
| 28 | STOP = stopSignal("") |
| 29 | DoubleStopExitCode = 1 |
| 30 | ) |
| 31 | |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 32 | // TODO(caprita): Needless to say, this is a hack. The motivator was getting |
| 33 | // the device manager (run by the security agent) to shut down cleanly when the |
| 34 | // process group containing both the agent and device manager receives a signal |
| 35 | // (and the agent propagates that signal to the child). We should be able to |
| 36 | // finesse this by demonizing the device manager and/or being smarter about how |
| 37 | // and when the agent sends the signal to the child. |
| 38 | |
| 39 | // SameSignalTimeWindow specifies the time window during which multiple |
| 40 | // deliveries of the same signal are counted as one signal. If set to zero, no |
| 41 | // such de-duping occurs. This is useful in situations where a process receives |
| 42 | // a signal explicitly sent by its parent when the parent receives the signal, |
| 43 | // but also receives it independently by virtue of being part of the same |
| 44 | // process group. |
| 45 | // |
| 46 | // This is a variable, so that I can be set appropriately. Note, there is no |
| 47 | // locking around it, the assumption being that it's set during initialization |
| 48 | // and never reset afterwards. |
| 49 | var SameSignalTimeWindow time.Duration |
| 50 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 51 | // defaultSignals returns a set of platform-specific signals that an application |
| 52 | // is encouraged to listen on. |
| 53 | func defaultSignals() []os.Signal { |
| 54 | return []os.Signal{syscall.SIGTERM, syscall.SIGINT, STOP} |
| 55 | } |
| 56 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 57 | // ShutdownOnSignals registers signal handlers for the specified signals, or, if |
| 58 | // none are specified, the default signals. The first signal received will be |
| 59 | // made available on the returned channel; upon receiving a second signal, the |
| 60 | // process will exit. |
Suharsh Sivakumar | 946f64d | 2015-01-08 10:48:13 -0800 | [diff] [blame] | 61 | func ShutdownOnSignals(ctx *context.T, signals ...os.Signal) <-chan os.Signal { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 62 | if len(signals) == 0 { |
| 63 | signals = defaultSignals() |
| 64 | } |
| 65 | // At least a buffer of length two so that we don't drop the first two |
| 66 | // signals we get on account of the channel being full. |
| 67 | ch := make(chan os.Signal, 2) |
| 68 | sawStop := false |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 69 | var signalsNoStop []os.Signal |
| 70 | for _, s := range signals { |
| 71 | switch s { |
| 72 | case STOP: |
| 73 | if !sawStop { |
| 74 | sawStop = true |
Suharsh Sivakumar | 946f64d | 2015-01-08 10:48:13 -0800 | [diff] [blame] | 75 | if ctx != nil { |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 76 | stopWaiter := make(chan string, 1) |
Cosmos Nicolaou | e9c622d | 2015-07-10 11:09:42 -0700 | [diff] [blame] | 77 | v23.GetAppCycle(ctx).WaitForStop(ctx, stopWaiter) |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 78 | go func() { |
| 79 | for { |
| 80 | ch <- stopSignal(<-stopWaiter) |
| 81 | } |
| 82 | }() |
| 83 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 84 | } |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 85 | default: |
| 86 | signalsNoStop = append(signalsNoStop, s) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 89 | if len(signalsNoStop) > 0 { |
| 90 | signal.Notify(ch, signalsNoStop...) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 91 | } |
| 92 | // At least a buffer of length one so that we don't block on ret <- sig. |
| 93 | ret := make(chan os.Signal, 1) |
| 94 | go func() { |
| 95 | // First signal received. |
| 96 | sig := <-ch |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 97 | sigTime := time.Now() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 98 | ret <- sig |
| 99 | // Wait for a second signal, and force an exit if the process is |
| 100 | // still executing cleanup code. |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 101 | for { |
| 102 | secondSig := <-ch |
| 103 | // If signal de-duping is enabled, ignore the signal if |
| 104 | // it's the same signal and has occured within the |
| 105 | // specified time window. |
| 106 | if SameSignalTimeWindow <= 0 || secondSig.String() != sig.String() || sigTime.Add(SameSignalTimeWindow).Before(time.Now()) { |
| 107 | os.Exit(DoubleStopExitCode) |
| 108 | } |
| 109 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 110 | }() |
| 111 | return ret |
| 112 | } |