| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package supervisor |
| 18 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 19 | // Supporting infrastructure to allow running some non-Go payloads under |
| 20 | // supervision. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 21 | |
| 22 | import ( |
| 23 | "context" |
| Lorenz Brun | e3032bd | 2023-04-13 14:43:41 +0200 | [diff] [blame] | 24 | "errors" |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 25 | "net" |
| Lorenz Brun | e3032bd | 2023-04-13 14:43:41 +0200 | [diff] [blame] | 26 | "os" |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 27 | "os/exec" |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 28 | |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 29 | "google.golang.org/grpc" |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 30 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 31 | "source.monogon.dev/osbase/logtree" |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 32 | ) |
| 33 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 34 | // GRPCServer creates a Runnable that serves gRPC requests as longs as it's not |
| 35 | // canceled. |
| 36 | // If graceful is set to true, the server will be gracefully stopped instead of |
| 37 | // plain stopped. This means all pending RPCs will finish, but also requires |
| 38 | // streaming gRPC handlers to check their context liveliness and exit |
| 39 | // accordingly. If the server code does not support this, `graceful` should be |
| 40 | // false and the server will be killed violently instead. |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 41 | func GRPCServer(srv *grpc.Server, lis net.Listener, graceful bool) Runnable { |
| 42 | return func(ctx context.Context) error { |
| 43 | Signal(ctx, SignalHealthy) |
| Jan Schär | 23e5230 | 2024-03-21 16:50:15 +0100 | [diff] [blame] | 44 | defer func() { |
| 45 | if graceful { |
| 46 | srv.GracefulStop() |
| 47 | } else { |
| 48 | srv.Stop() |
| 49 | } |
| 50 | }() |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 51 | errC := make(chan error) |
| 52 | go func() { |
| 53 | errC <- srv.Serve(lis) |
| 54 | }() |
| 55 | select { |
| 56 | case <-ctx.Done(): |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 57 | return ctx.Err() |
| 58 | case err := <-errC: |
| 59 | return err |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 64 | // RunCommand will create a Runnable that starts a long-running command, whose |
| 65 | // exit is determined to be a failure. |
| Jan Schär | bdbb9c2 | 2024-12-18 15:14:02 +0100 | [diff] [blame^] | 66 | // cmd should be created with [exec.CommandContext] so that it will be killed |
| 67 | // when the context is canceled. |
| Serge Bazanski | 0560429 | 2021-03-12 17:47:21 +0100 | [diff] [blame] | 68 | func RunCommand(ctx context.Context, cmd *exec.Cmd, opts ...RunCommandOption) error { |
| Serge Bazanski | 967be21 | 2020-11-02 11:26:59 +0100 | [diff] [blame] | 69 | Signal(ctx, SignalHealthy) |
| Serge Bazanski | 0560429 | 2021-03-12 17:47:21 +0100 | [diff] [blame] | 70 | |
| 71 | var parseKLog bool |
| Lorenz Brun | e3032bd | 2023-04-13 14:43:41 +0200 | [diff] [blame] | 72 | var signal <-chan os.Signal |
| Serge Bazanski | 0560429 | 2021-03-12 17:47:21 +0100 | [diff] [blame] | 73 | for _, opt := range opts { |
| 74 | if opt.parseKlog { |
| 75 | parseKLog = true |
| 76 | } |
| Lorenz Brun | e3032bd | 2023-04-13 14:43:41 +0200 | [diff] [blame] | 77 | if opt.signal != nil { |
| 78 | signal = opt.signal |
| 79 | } |
| Serge Bazanski | 0560429 | 2021-03-12 17:47:21 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if parseKLog { |
| 83 | // We make two klogs, one for each of stdout/stderr. This is to prevent |
| 84 | // accidental interleaving of both. |
| 85 | klogStdout := logtree.KLogParser(Logger(ctx)) |
| 86 | defer klogStdout.Close() |
| 87 | klogStderr := logtree.KLogParser(Logger(ctx)) |
| 88 | defer klogStderr.Close() |
| 89 | |
| 90 | cmd.Stdout = klogStdout |
| 91 | cmd.Stderr = klogStderr |
| 92 | } else { |
| 93 | cmd.Stdout = RawLogger(ctx) |
| 94 | cmd.Stderr = RawLogger(ctx) |
| 95 | } |
| Lorenz Brun | e3032bd | 2023-04-13 14:43:41 +0200 | [diff] [blame] | 96 | err := cmd.Start() |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | exited := make(chan struct{}) |
| 102 | if signal != nil { |
| 103 | go func() { |
| 104 | for { |
| 105 | var err error |
| 106 | select { |
| 107 | case s := <-signal: |
| 108 | err = cmd.Process.Signal(s) |
| 109 | case <-exited: |
| 110 | return |
| 111 | } |
| 112 | if err != nil && !errors.Is(err, os.ErrProcessDone) { |
| 113 | Logger(ctx).Warningf("Failed sending signal to process: %v", err) |
| 114 | } |
| 115 | } |
| 116 | }() |
| 117 | } |
| 118 | |
| 119 | err = cmd.Wait() |
| 120 | if signal != nil { |
| 121 | exited <- struct{}{} |
| 122 | } |
| Serge Bazanski | 967be21 | 2020-11-02 11:26:59 +0100 | [diff] [blame] | 123 | Logger(ctx).Infof("Command returned: %v", err) |
| 124 | return err |
| Serge Bazanski | 9c09c4e | 2020-03-24 13:58:01 +0100 | [diff] [blame] | 125 | } |
| Serge Bazanski | 0560429 | 2021-03-12 17:47:21 +0100 | [diff] [blame] | 126 | |
| 127 | type RunCommandOption struct { |
| 128 | parseKlog bool |
| Lorenz Brun | e3032bd | 2023-04-13 14:43:41 +0200 | [diff] [blame] | 129 | signal <-chan os.Signal |
| Serge Bazanski | 0560429 | 2021-03-12 17:47:21 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 132 | // ParseKLog signals that the command being run will return klog-compatible |
| 133 | // logs to stdout and/or stderr, and these will be re-interpreted as structured |
| Serge Bazanski | 0560429 | 2021-03-12 17:47:21 +0100 | [diff] [blame] | 134 | // logging and emitted to the supervisor's logger. |
| 135 | func ParseKLog() RunCommandOption { |
| 136 | return RunCommandOption{ |
| 137 | parseKlog: true, |
| 138 | } |
| 139 | } |
| Lorenz Brun | e3032bd | 2023-04-13 14:43:41 +0200 | [diff] [blame] | 140 | |
| 141 | // SignalChan takes a channel which can be used to send signals to the |
| 142 | // supervised process. |
| 143 | // |
| 144 | // The given channel will be read from as long as the underlying process is |
| 145 | // running. If the process doesn't start successfully the channel will not be |
| 146 | // read. When the process exits, the channel will stop being read. |
| 147 | // |
| 148 | // With the above in mind, and also taking into account the inherent lack of |
| 149 | // reliability in delivering any process-handled signals in POSIX/Linux, it is |
| 150 | // recommended to use unbuffered channels, always write to them in a non-blocking |
| 151 | // fashion (eg. in a select { ... default: } block), and to not rely only on the |
| 152 | // signal delivery mechanism for the intended behaviour. |
| 153 | // |
| 154 | // For example, if the signals are used to trigger some configuration reload, |
| 155 | // these configuration reloads should either be verified and signal delivery should |
| 156 | // be retried until confirmed successful, or there should be a backup periodic |
| 157 | // reload performed by the target process independently of signal-based reload |
| 158 | // triggers. |
| 159 | // |
| 160 | // Another example: if the signal delivered is a SIGTERM used to gracefully |
| 161 | // terminate some process, it should be attempted to be delivered a number of |
| 162 | // times before finally SIGKILLing the process. |
| 163 | func SignalChan(s <-chan os.Signal) RunCommandOption { |
| 164 | return RunCommandOption{ |
| 165 | signal: s, |
| 166 | } |
| 167 | } |