github.com/demonoid81/containerd@v1.3.4/runtime/v2/shim/shim_unix.go (about)

     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 shim
    20  
    21  import (
    22  	"context"
    23  	"io"
    24  	"net"
    25  	"os"
    26  	"os/signal"
    27  	"syscall"
    28  
    29  	"github.com/containerd/containerd/sys/reaper"
    30  	"github.com/containerd/fifo"
    31  	"github.com/pkg/errors"
    32  	"github.com/sirupsen/logrus"
    33  	"golang.org/x/sys/unix"
    34  )
    35  
    36  // setupSignals creates a new signal handler for all signals and sets the shim as a
    37  // sub-reaper so that the container processes are reparented
    38  func setupSignals(config Config) (chan os.Signal, error) {
    39  	signals := make(chan os.Signal, 32)
    40  	smp := []os.Signal{unix.SIGTERM, unix.SIGINT, unix.SIGPIPE}
    41  	if !config.NoReaper {
    42  		smp = append(smp, unix.SIGCHLD)
    43  	}
    44  	signal.Notify(signals, smp...)
    45  	return signals, nil
    46  }
    47  
    48  func setupDumpStacks(dump chan<- os.Signal) {
    49  	signal.Notify(dump, syscall.SIGUSR1)
    50  }
    51  
    52  func serveListener(path string) (net.Listener, error) {
    53  	var (
    54  		l   net.Listener
    55  		err error
    56  	)
    57  	if path == "" {
    58  		l, err = net.FileListener(os.NewFile(3, "socket"))
    59  		path = "[inherited from parent]"
    60  	} else {
    61  		if len(path) > 106 {
    62  			return nil, errors.Errorf("%q: unix socket path too long (> 106)", path)
    63  		}
    64  		l, err = net.Listen("unix", "\x00"+path)
    65  	}
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	logrus.WithField("socket", path).Debug("serving api on abstract socket")
    70  	return l, nil
    71  }
    72  
    73  func handleSignals(ctx context.Context, logger *logrus.Entry, signals chan os.Signal) error {
    74  	logger.Info("starting signal loop")
    75  
    76  	for {
    77  		select {
    78  		case <-ctx.Done():
    79  			return ctx.Err()
    80  		case s := <-signals:
    81  			switch s {
    82  			case unix.SIGCHLD:
    83  				if err := reaper.Reap(); err != nil {
    84  					logger.WithError(err).Error("reap exit status")
    85  				}
    86  			case unix.SIGPIPE:
    87  			}
    88  		}
    89  	}
    90  }
    91  
    92  func openLog(ctx context.Context, _ string) (io.Writer, error) {
    93  	return fifo.OpenFifo(ctx, "log", unix.O_WRONLY, 0700)
    94  }