github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/libcontainerd/remote/client_linux.go (about)

     1  package remote // import "github.com/docker/docker/libcontainerd/remote"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/containerd/containerd"
    11  	"github.com/containerd/containerd/cio"
    12  	libcontainerdtypes "github.com/docker/docker/libcontainerd/types"
    13  	"github.com/docker/docker/pkg/idtools"
    14  	"github.com/opencontainers/runtime-spec/specs-go"
    15  	"github.com/sirupsen/logrus"
    16  )
    17  
    18  const runtimeName = "io.containerd.runtime.v1.linux"
    19  
    20  func summaryFromInterface(i interface{}) (*libcontainerdtypes.Summary, error) {
    21  	return &libcontainerdtypes.Summary{}, nil
    22  }
    23  
    24  func (c *client) UpdateResources(ctx context.Context, containerID string, resources *libcontainerdtypes.Resources) error {
    25  	p, err := c.getProcess(containerID, libcontainerdtypes.InitProcessName)
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	// go doesn't like the alias in 1.8, this means this need to be
    31  	// platform specific
    32  	return p.(containerd.Task).Update(ctx, containerd.WithResources((*specs.LinuxResources)(resources)))
    33  }
    34  
    35  func hostIDFromMap(id uint32, mp []specs.LinuxIDMapping) int {
    36  	for _, m := range mp {
    37  		if id >= m.ContainerID && id <= m.ContainerID+m.Size-1 {
    38  			return int(m.HostID + id - m.ContainerID)
    39  		}
    40  	}
    41  	return 0
    42  }
    43  
    44  func getSpecUser(ociSpec *specs.Spec) (int, int) {
    45  	var (
    46  		uid int
    47  		gid int
    48  	)
    49  
    50  	for _, ns := range ociSpec.Linux.Namespaces {
    51  		if ns.Type == specs.UserNamespace {
    52  			uid = hostIDFromMap(0, ociSpec.Linux.UIDMappings)
    53  			gid = hostIDFromMap(0, ociSpec.Linux.GIDMappings)
    54  			break
    55  		}
    56  	}
    57  
    58  	return uid, gid
    59  }
    60  
    61  func prepareBundleDir(bundleDir string, ociSpec *specs.Spec) (string, error) {
    62  	uid, gid := getSpecUser(ociSpec)
    63  	if uid == 0 && gid == 0 {
    64  		return bundleDir, idtools.MkdirAllAndChownNew(bundleDir, 0755, idtools.Identity{UID: 0, GID: 0})
    65  	}
    66  
    67  	p := string(filepath.Separator)
    68  	components := strings.Split(bundleDir, string(filepath.Separator))
    69  	for _, d := range components[1:] {
    70  		p = filepath.Join(p, d)
    71  		fi, err := os.Stat(p)
    72  		if err != nil && !os.IsNotExist(err) {
    73  			return "", err
    74  		}
    75  		if os.IsNotExist(err) || fi.Mode()&1 == 0 {
    76  			p = fmt.Sprintf("%s.%d.%d", p, uid, gid)
    77  			if err := idtools.MkdirAndChown(p, 0700, idtools.Identity{UID: uid, GID: gid}); err != nil && !os.IsExist(err) {
    78  				return "", err
    79  			}
    80  		}
    81  	}
    82  
    83  	return p, nil
    84  }
    85  
    86  func newFIFOSet(bundleDir, processID string, withStdin, withTerminal bool) *cio.FIFOSet {
    87  	config := cio.Config{
    88  		Terminal: withTerminal,
    89  		Stdout:   filepath.Join(bundleDir, processID+"-stdout"),
    90  	}
    91  	paths := []string{config.Stdout}
    92  
    93  	if withStdin {
    94  		config.Stdin = filepath.Join(bundleDir, processID+"-stdin")
    95  		paths = append(paths, config.Stdin)
    96  	}
    97  	if !withTerminal {
    98  		config.Stderr = filepath.Join(bundleDir, processID+"-stderr")
    99  		paths = append(paths, config.Stderr)
   100  	}
   101  	closer := func() error {
   102  		for _, path := range paths {
   103  			if err := os.RemoveAll(path); err != nil {
   104  				logrus.Warnf("libcontainerd: failed to remove fifo %v: %v", path, err)
   105  			}
   106  		}
   107  		return nil
   108  	}
   109  
   110  	return cio.NewFIFOSet(config, closer)
   111  }
   112  
   113  func (c *client) newDirectIO(ctx context.Context, fifos *cio.FIFOSet) (*cio.DirectIO, error) {
   114  	return cio.NewDirectIO(ctx, fifos)
   115  }