github.com/shishir-a412ed/docker@v1.3.2-0.20180103180333-fda904911d87/libcontainerd/client_daemon_linux.go (about)

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