github.com/circular-dark/docker@v1.7.0/daemon/execdriver/native/exec.go (about)

     1  // +build linux
     2  
     3  package native
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"os/exec"
     9  	"syscall"
    10  
    11  	"github.com/docker/docker/daemon/execdriver"
    12  	"github.com/docker/libcontainer"
    13  	_ "github.com/docker/libcontainer/nsenter"
    14  	"github.com/docker/libcontainer/utils"
    15  )
    16  
    17  // TODO(vishh): Add support for running in privileged mode.
    18  func (d *driver) Exec(c *execdriver.Command, processConfig *execdriver.ProcessConfig, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {
    19  	active := d.activeContainers[c.ID]
    20  	if active == nil {
    21  		return -1, fmt.Errorf("No active container exists with ID %s", c.ID)
    22  	}
    23  
    24  	p := &libcontainer.Process{
    25  		Args: append([]string{processConfig.Entrypoint}, processConfig.Arguments...),
    26  		Env:  c.ProcessConfig.Env,
    27  		Cwd:  c.WorkingDir,
    28  		User: processConfig.User,
    29  	}
    30  
    31  	config := active.Config()
    32  	if err := setupPipes(&config, processConfig, p, pipes); err != nil {
    33  		return -1, err
    34  	}
    35  
    36  	if err := active.Start(p); err != nil {
    37  		return -1, err
    38  	}
    39  
    40  	if startCallback != nil {
    41  		pid, err := p.Pid()
    42  		if err != nil {
    43  			p.Signal(os.Kill)
    44  			p.Wait()
    45  			return -1, err
    46  		}
    47  		startCallback(&c.ProcessConfig, pid)
    48  	}
    49  
    50  	ps, err := p.Wait()
    51  	if err != nil {
    52  		exitErr, ok := err.(*exec.ExitError)
    53  		if !ok {
    54  			return -1, err
    55  		}
    56  		ps = exitErr.ProcessState
    57  	}
    58  	return utils.ExitStatus(ps.Sys().(syscall.WaitStatus)), nil
    59  }