|
| 1 | +// +build windows |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright The containerd Authors. |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package logging |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "net" |
| 25 | + "os" |
| 26 | + "os/signal" |
| 27 | + "syscall" |
| 28 | + |
| 29 | + "github.com/Microsoft/go-winio" |
| 30 | + "github.com/pkg/errors" |
| 31 | +) |
| 32 | + |
| 33 | +// Run the logging driver |
| 34 | +func Run(fn LoggerFunc) { |
| 35 | + err := runInternal(fn) |
| 36 | + if err != nil { |
| 37 | + fmt.Fprintln(os.Stderr, err) |
| 38 | + os.Exit(1) |
| 39 | + } |
| 40 | + os.Exit(0) |
| 41 | +} |
| 42 | + |
| 43 | +func runInternal(fn LoggerFunc) error { |
| 44 | + ctx, cancel := context.WithCancel(context.Background()) |
| 45 | + defer cancel() |
| 46 | + |
| 47 | + var ( |
| 48 | + soutPipe, serrPipe, waitPipe string |
| 49 | + sout, serr, wait net.Conn |
| 50 | + ok bool |
| 51 | + err error |
| 52 | + ) |
| 53 | + |
| 54 | + if soutPipe, ok = os.LookupEnv("CONTAINER_STDOUT"); !ok { |
| 55 | + return errors.New("'CONTAINER_STDOUT' environment variable missing") |
| 56 | + } |
| 57 | + if sout, err = winio.DialPipeContext(ctx, soutPipe); err != nil { |
| 58 | + return errors.Wrap(err, "unable to dial stdout pipe") |
| 59 | + } |
| 60 | + |
| 61 | + if serrPipe, ok = os.LookupEnv("CONTAINER_STDERR"); !ok { |
| 62 | + return errors.New("'CONTAINER_STDERR' environment variable missing") |
| 63 | + } |
| 64 | + if serr, err = winio.DialPipeContext(ctx, serrPipe); err != nil { |
| 65 | + return errors.Wrap(err, "unable to dial stderr pipe") |
| 66 | + } |
| 67 | + |
| 68 | + waitPipe = os.Getenv("CONTAINER_WAIT") |
| 69 | + if wait, err = winio.DialPipeContext(ctx, waitPipe); err != nil { |
| 70 | + return errors.Wrap(err, "unable to dial wait pipe") |
| 71 | + } |
| 72 | + |
| 73 | + config := &Config{ |
| 74 | + ID: os.Getenv("CONTAINER_ID"), |
| 75 | + Namespace: os.Getenv("CONTAINER_NAMESPACE"), |
| 76 | + Stdout: sout, |
| 77 | + Stderr: serr, |
| 78 | + } |
| 79 | + |
| 80 | + var ( |
| 81 | + sigCh = make(chan os.Signal, 2) |
| 82 | + errCh = make(chan error, 1) |
| 83 | + ) |
| 84 | + |
| 85 | + signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) |
| 86 | + |
| 87 | + go func() { |
| 88 | + errCh <- fn(ctx, config, wait.Close) |
| 89 | + }() |
| 90 | + |
| 91 | + for { |
| 92 | + select { |
| 93 | + case <-sigCh: |
| 94 | + cancel() |
| 95 | + case err = <-errCh: |
| 96 | + return err |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments