1/*
2   Copyright The containerd Authors.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17package main
18
19import (
20	"os"
21	"os/signal"
22
23	"github.com/containerd/containerd/sys/reaper"
24	runc "github.com/containerd/go-runc"
25	"github.com/containerd/ttrpc"
26	"golang.org/x/sys/unix"
27)
28
29// setupSignals creates a new signal handler for all signals and sets the shim as a
30// sub-reaper so that the container processes are reparented
31func setupSignals() (chan os.Signal, error) {
32	signals := make(chan os.Signal, 32)
33	signal.Notify(signals, unix.SIGTERM, unix.SIGINT, unix.SIGCHLD, unix.SIGPIPE)
34	// make sure runc is setup to use the monitor
35	// for waiting on processes
36	runc.Monitor = reaper.Default
37	// set the shim as the subreaper for all orphaned processes created by the container
38	if err := reaper.SetSubreaper(1); err != nil {
39		return nil, err
40	}
41	return signals, nil
42}
43
44func newServer() (*ttrpc.Server, error) {
45	return ttrpc.NewServer(ttrpc.WithServerHandshaker(ttrpc.UnixSocketRequireSameUser()))
46}
47