github.com/ggreg80/ketos@v0.0.0-20171109040536-049616f51ddb/cmd/chr/libc_engine.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/setekhid/ketos/pkg/metadata"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"strings"
     9  )
    10  
    11  func newLibcChrootExecutor(env []string) (ChrootExecutor, error) {
    12  
    13  	libcHookPath := os.Getenv("KETOS_LIBC_HOOKER")
    14  	if len(libcHookPath) <= 0 {
    15  		libcHookPath = "/usr/local/lib/libketos-hookroot.so"
    16  	}
    17  
    18  	executor := func(repoPath, tagName string, userCommand []string) error {
    19  
    20  		meta, err := metadata.GetMetadatas(repoPath)
    21  		if err != nil {
    22  			return err
    23  		}
    24  		manifest, err := meta.GetManifest(tagName)
    25  		if err != nil {
    26  			return err
    27  		}
    28  
    29  		rootfsLayers := []string{}
    30  		for _, layer := range manifest.FSLayers {
    31  			layerPath := meta.LayerPath(layer.BlobSum)
    32  			rootfsLayers = append(rootfsLayers, layerPath)
    33  		}
    34  		rootfsLayers = append(rootfsLayers, meta.ContainerPath())
    35  
    36  		exe := exec.Command(userCommand[0], userCommand[1:]...)
    37  		exe.Env = append(exe.Env,
    38  			"LD_PRELOAD="+libcHookPath,
    39  			"KETOS_ROOTPATH_LAYERS="+
    40  				strings.Join(rootfsLayers, string(filepath.ListSeparator)))
    41  		exe.Stdin = os.Stdin
    42  		exe.Stdout = os.Stdout
    43  		exe.Stderr = os.Stderr
    44  
    45  		return exe.Run()
    46  	}
    47  
    48  	return ExecutorFunc(executor), nil
    49  }
    50  
    51  func init() { AddExecutor("libc", newLibcChrootExecutor) }