|
1 |
| -// +build !linux,!windows,!darwin |
| 1 | +// +build !windows |
2 | 2 |
|
3 | 3 | /*
|
4 | 4 | Copyright The containerd Authors.
|
|
19 | 19 | package shim
|
20 | 20 |
|
21 | 21 | import (
|
| 22 | + "bytes" |
| 23 | + "context" |
| 24 | + "net" |
22 | 25 | "os"
|
| 26 | + "os/exec" |
23 | 27 | "os/signal"
|
| 28 | + "syscall" |
24 | 29 |
|
25 |
| - "github.com/containerd/ttrpc" |
| 30 | + "github.com/containerd/containerd/events" |
| 31 | + "github.com/containerd/containerd/namespaces" |
| 32 | + "github.com/containerd/typeurl" |
| 33 | + "github.com/pkg/errors" |
| 34 | + "github.com/sirupsen/logrus" |
| 35 | + "golang.org/x/sys/unix" |
26 | 36 | )
|
27 | 37 |
|
28 | 38 | // setupSignals creates a new signal handler for all signals and sets the shim as a
|
29 | 39 | // sub-reaper so that the container processes are reparented
|
30 | 40 | func setupSignals() (chan os.Signal, error) {
|
31 |
| - signals := make(chan os.Signal, 2048) |
32 |
| - signal.Notify(signals) |
| 41 | + signals := make(chan os.Signal, 32) |
| 42 | + signal.Notify(signals, unix.SIGTERM, unix.SIGINT, unix.SIGCHLD, unix.SIGPIPE) |
33 | 43 | return signals, nil
|
34 | 44 | }
|
35 | 45 |
|
36 |
| -func newServer() (*ttrpc.Server, error) { |
37 |
| - return ttrpc.NewServer(ttrpc.WithServerHandshaker(ttrpc.UnixSocketRequireSameUser())) |
| 46 | +func setupDumpStacks(dump chan<- os.Signal) { |
| 47 | + signal.Notify(dump, syscall.SIGUSR1) |
38 | 48 | }
|
39 | 49 |
|
40 |
| -func subreaper() error { |
| 50 | +func serveListener(path string) (net.Listener, string, error) { |
| 51 | + var ( |
| 52 | + l net.Listener |
| 53 | + err error |
| 54 | + ) |
| 55 | + if path == "" { |
| 56 | + l, err = net.FileListener(os.NewFile(3, "socket")) |
| 57 | + path = "[inherited from parent]" |
| 58 | + } else { |
| 59 | + if len(path) > 106 { |
| 60 | + return nil, path, errors.Errorf("%q: unix socket path too long (> 106)", path) |
| 61 | + } |
| 62 | + l, err = net.Listen("unix", "\x00"+path) |
| 63 | + } |
| 64 | + if err != nil { |
| 65 | + return nil, path, err |
| 66 | + } |
| 67 | + return l, path, nil |
| 68 | +} |
| 69 | + |
| 70 | +func handleSignals(logger *logrus.Entry, signals chan os.Signal) error { |
| 71 | + logger.Info("starting signal loop") |
| 72 | + for { |
| 73 | + select { |
| 74 | + case s := <-signals: |
| 75 | + switch s { |
| 76 | + case unix.SIGCHLD: |
| 77 | + if err := Reap(); err != nil { |
| 78 | + logger.WithError(err).Error("reap exit status") |
| 79 | + } |
| 80 | + case unix.SIGPIPE: |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func (l *remoteEventsPublisher) Publish(ctx context.Context, topic string, event events.Event) error { |
| 87 | + ns, _ := namespaces.Namespace(ctx) |
| 88 | + encoded, err := typeurl.MarshalAny(event) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + data, err := encoded.Marshal() |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + cmd := exec.CommandContext(ctx, l.containerdBinaryPath, "--address", l.address, "publish", "--topic", topic, "--namespace", ns) |
| 97 | + cmd.Stdin = bytes.NewReader(data) |
| 98 | + c, err := Default.Start(cmd) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + status, err := Default.Wait(cmd, c) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + if status != 0 { |
| 107 | + return errors.New("failed to publish event") |
| 108 | + } |
41 | 109 | return nil
|
42 | 110 | }
|
0 commit comments