merge branch 'pr-1317'

Closes #1317
LGTMs: @cyphar @crosbymichael
This commit is contained in:
Aleksa Sarai 2017-02-24 08:21:58 +11:00
commit 02141ce862
No known key found for this signature in database
GPG Key ID: 9E18AA267DDB8DB4
2 changed files with 49 additions and 21 deletions

View File

@ -191,17 +191,29 @@ func (c *linuxContainer) Start(process *Process) error {
if err != nil { if err != nil {
return err return err
} }
return c.start(process, status == Stopped) if status == Stopped {
if err := c.createExecFifo(); err != nil {
return err
}
}
if err := c.start(process, status == Stopped); err != nil {
if status == Stopped {
c.deleteExecFifo()
}
return err
}
return nil
} }
func (c *linuxContainer) Run(process *Process) error { func (c *linuxContainer) Run(process *Process) error {
c.m.Lock() c.m.Lock()
defer c.m.Unlock()
status, err := c.currentStatus() status, err := c.currentStatus()
if err != nil { if err != nil {
c.m.Unlock()
return err return err
} }
if err := c.start(process, status == Stopped); err != nil { c.m.Unlock()
if err := c.Start(process); err != nil {
return err return err
} }
if status == Stopped { if status == Stopped {
@ -291,6 +303,37 @@ func (c *linuxContainer) Signal(s os.Signal, all bool) error {
return nil return nil
} }
func (c *linuxContainer) createExecFifo() error {
rootuid, err := c.Config().HostUID()
if err != nil {
return err
}
rootgid, err := c.Config().HostGID()
if err != nil {
return err
}
fifoName := filepath.Join(c.root, execFifoFilename)
if _, err := os.Stat(fifoName); err == nil {
return fmt.Errorf("exec fifo %s already exists", fifoName)
}
oldMask := syscall.Umask(0000)
if err := syscall.Mkfifo(fifoName, 0622); err != nil {
syscall.Umask(oldMask)
return err
}
syscall.Umask(oldMask)
if err := os.Chown(fifoName, rootuid, rootgid); err != nil {
return err
}
return nil
}
func (c *linuxContainer) deleteExecFifo() {
fifoName := filepath.Join(c.root, execFifoFilename)
os.Remove(fifoName)
}
func (c *linuxContainer) newParentProcess(p *Process, doInit bool) (parentProcess, error) { func (c *linuxContainer) newParentProcess(p *Process, doInit bool) (parentProcess, error) {
parentPipe, childPipe, err := newPipe() parentPipe, childPipe, err := newPipe()
if err != nil { if err != nil {
@ -1216,14 +1259,9 @@ func (c *linuxContainer) runType() (Status, error) {
if !exist || err != nil { if !exist || err != nil {
return Stopped, err return Stopped, err
} }
// check if the process that is running is the init process or the user's process. // We'll create exec fifo and blocking on it after container is created,
// this is the difference between the container Running and Created. // and delete it after start container.
environ, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/environ", pid)) if _, err := os.Stat(filepath.Join(c.root, execFifoFilename)); err == nil {
if err != nil {
return Stopped, newSystemErrorWithCausef(err, "reading /proc/%d/environ", pid)
}
check := []byte("_LIBCONTAINER")
if bytes.Contains(environ, check) {
return Created, nil return Created, nil
} }
return Running, nil return Running, nil

View File

@ -169,16 +169,6 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
if err := os.Chown(containerRoot, uid, gid); err != nil { if err := os.Chown(containerRoot, uid, gid); err != nil {
return nil, newGenericError(err, SystemError) return nil, newGenericError(err, SystemError)
} }
fifoName := filepath.Join(containerRoot, execFifoFilename)
oldMask := syscall.Umask(0000)
if err := syscall.Mkfifo(fifoName, 0622); err != nil {
syscall.Umask(oldMask)
return nil, newGenericError(err, SystemError)
}
syscall.Umask(oldMask)
if err := os.Chown(fifoName, uid, gid); err != nil {
return nil, newGenericError(err, SystemError)
}
c := &linuxContainer{ c := &linuxContainer{
id: id, id: id,
root: containerRoot, root: containerRoot,