github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/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  func (d *driver) Exec(c *execdriver.Command, processConfig *execdriver.ProcessConfig, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {
    18  	active := d.activeContainers[c.ID]
    19  	if active == nil {
    20  		return -1, fmt.Errorf("No active container exists with ID %s", c.ID)
    21  	}
    22  
    23  	p := &libcontainer.Process{
    24  		Args: append([]string{processConfig.Entrypoint}, processConfig.Arguments...),
    25  		Env:  c.ProcessConfig.Env,
    26  		Cwd:  c.WorkingDir,
    27  		User: processConfig.User,
    28  	}
    29  
    30  	if processConfig.Privileged {
    31  		p.Capabilities = execdriver.GetAllCapabilities()
    32  	}
    33  
    34  	config := active.Config()
    35  	if err := setupPipes(&config, processConfig, p, pipes); err != nil {
    36  		return -1, err
    37  	}
    38  
    39  	if err := active.Start(p); err != nil {
    40  		return -1, err
    41  	}
    42  
    43  	if startCallback != nil {
    44  		pid, err := p.Pid()
    45  		if err != nil {
    46  			p.Signal(os.Kill)
    47  			p.Wait()
    48  			return -1, err
    49  		}
    50  		startCallback(&c.ProcessConfig, pid)
    51  	}
    52  
    53  	ps, err := p.Wait()
    54  	if err != nil {
    55  		exitErr, ok := err.(*exec.ExitError)
    56  		if !ok {
    57  			return -1, err
    58  		}
    59  		ps = exitErr.ProcessState
    60  	}
    61  	return utils.ExitStatus(ps.Sys().(syscall.WaitStatus)), nil
    62  }