Do not use stream encoders

Marshall the raw objects for the sync pipes so that no new line chars
are left behind in the pipe causing errors.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2016-01-25 18:15:44 -08:00
parent 3268a1ea00
commit ddcee3cc2a
7 changed files with 33 additions and 21 deletions

View File

@ -21,6 +21,7 @@ import (
"github.com/opencontainers/runc/libcontainer/cgroups" "github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/criurpc" "github.com/opencontainers/runc/libcontainer/criurpc"
"github.com/opencontainers/runc/libcontainer/utils"
"github.com/vishvananda/netlink/nl" "github.com/vishvananda/netlink/nl"
) )
@ -964,7 +965,7 @@ func (c *linuxContainer) saveState(s *State) error {
return err return err
} }
defer f.Close() defer f.Close()
return json.NewEncoder(f).Encode(s) return utils.WriteJSON(f, s)
} }
func (c *linuxContainer) deleteState() error { func (c *linuxContainer) deleteState() error {

View File

@ -18,6 +18,7 @@ import (
"github.com/opencontainers/runc/libcontainer/cgroups/systemd" "github.com/opencontainers/runc/libcontainer/cgroups/systemd"
"github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/configs/validate" "github.com/opencontainers/runc/libcontainer/configs/validate"
"github.com/opencontainers/runc/libcontainer/utils"
) )
const ( const (
@ -235,15 +236,15 @@ func (l *LinuxFactory) StartInitialization() (err error) {
if err != nil { if err != nil {
if _, ok := i.(*linuxStandardInit); ok { if _, ok := i.(*linuxStandardInit); ok {
// Synchronisation only necessary for standard init. // Synchronisation only necessary for standard init.
if err := json.NewEncoder(pipe).Encode(procError); err != nil { if err := utils.WriteJSON(pipe, syncT{procError}); err != nil {
panic(err) panic(err)
} }
} }
if err := json.NewEncoder(pipe).Encode(newSystemError(err)); err != nil { if err := utils.WriteJSON(pipe, newSystemError(err)); err != nil {
panic(err) panic(err)
} }
} else { } else {
if err := json.NewEncoder(pipe).Encode(procStart); err != nil { if err := utils.WriteJSON(pipe, syncT{procStart}); err != nil {
panic(err) panic(err)
} }
} }

View File

@ -3,7 +3,6 @@
package libcontainer package libcontainer
import ( import (
"encoding/json"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -12,6 +11,7 @@ import (
"github.com/docker/docker/pkg/mount" "github.com/docker/docker/pkg/mount"
"github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/utils"
) )
func newTestRoot() (string, error) { func newTestRoot() (string, error) {
@ -179,5 +179,5 @@ func marshal(path string, v interface{}) error {
return err return err
} }
defer f.Close() defer f.Close()
return json.NewEncoder(f).Encode(v) return utils.WriteJSON(f, v)
} }

View File

@ -18,6 +18,10 @@ const (
procRun procRun
) )
type syncT struct {
Type syncType `json:"type"`
}
var errorTemplate = template.Must(template.New("error").Parse(`Timestamp: {{.Timestamp}} var errorTemplate = template.Must(template.New("error").Parse(`Timestamp: {{.Timestamp}}
Code: {{.ECode}} Code: {{.ECode}}
{{if .Message }} {{if .Message }}

View File

@ -147,17 +147,16 @@ func finalizeNamespace(config *initConfig) error {
// indicate that it is cleared to Exec. // indicate that it is cleared to Exec.
func syncParentReady(pipe io.ReadWriter) error { func syncParentReady(pipe io.ReadWriter) error {
// Tell parent. // Tell parent.
if err := json.NewEncoder(pipe).Encode(procReady); err != nil { if err := utils.WriteJSON(pipe, syncT{procReady}); err != nil {
return err return err
} }
// Wait for parent to give the all-clear. // Wait for parent to give the all-clear.
var procSync syncType var procSync syncT
if err := json.NewDecoder(pipe).Decode(&procSync); err != nil { if err := json.NewDecoder(pipe).Decode(&procSync); err != nil {
if err == io.EOF { if err == io.EOF {
return fmt.Errorf("parent closed synchronisation channel") return fmt.Errorf("parent closed synchronisation channel")
} }
if procSync != procRun { if procSync.Type != procRun {
return fmt.Errorf("invalid synchronisation flag from parent") return fmt.Errorf("invalid synchronisation flag from parent")
} }
} }

View File

@ -16,6 +16,7 @@ import (
"github.com/opencontainers/runc/libcontainer/cgroups" "github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/system" "github.com/opencontainers/runc/libcontainer/system"
"github.com/opencontainers/runc/libcontainer/utils"
) )
type parentProcess interface { type parentProcess interface {
@ -84,7 +85,7 @@ func (p *setnsProcess) start() (err error) {
return newSystemError(err) return newSystemError(err)
} }
} }
if err := json.NewEncoder(p.parentPipe).Encode(p.config); err != nil { if err := utils.WriteJSON(p.parentPipe, p.config); err != nil {
return newSystemError(err) return newSystemError(err)
} }
@ -231,9 +232,8 @@ func (p *initProcess) start() (err error) {
if err := p.sendConfig(); err != nil { if err := p.sendConfig(); err != nil {
return newSystemError(err) return newSystemError(err)
} }
var ( var (
procSync syncType procSync syncT
sentRun bool sentRun bool
ierr *genericError ierr *genericError
) )
@ -246,8 +246,7 @@ loop:
} }
return newSystemError(err) return newSystemError(err)
} }
switch procSync.Type {
switch procSync {
case procStart: case procStart:
break loop break loop
case procReady: case procReady:
@ -255,7 +254,7 @@ loop:
return newSystemError(err) return newSystemError(err)
} }
// Sync with child. // Sync with child.
if err := json.NewEncoder(p.parentPipe).Encode(procRun); err != nil { if err := utils.WriteJSON(p.parentPipe, syncT{procRun}); err != nil {
return newSystemError(err) return newSystemError(err)
} }
sentRun = true sentRun = true
@ -317,10 +316,7 @@ func (p *initProcess) startTime() (string, error) {
func (p *initProcess) sendConfig() error { func (p *initProcess) sendConfig() error {
// send the state to the container's init process then shutdown writes for the parent // send the state to the container's init process then shutdown writes for the parent
if err := json.NewEncoder(p.parentPipe).Encode(p.config); err != nil { return utils.WriteJSON(p.parentPipe, p.config)
return err
}
return nil
} }
func (p *initProcess) createNetworkInterfaces() error { func (p *initProcess) createNetworkInterfaces() error {

View File

@ -3,6 +3,7 @@ package utils
import ( import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"encoding/json"
"io" "io"
"path/filepath" "path/filepath"
"syscall" "syscall"
@ -36,10 +37,20 @@ func ResolveRootfs(uncleanRootfs string) (string, error) {
} }
// ExitStatus returns the correct exit status for a process based on if it // ExitStatus returns the correct exit status for a process based on if it
// was signaled or exited cleanly. // was signaled or exited cleanly
func ExitStatus(status syscall.WaitStatus) int { func ExitStatus(status syscall.WaitStatus) int {
if status.Signaled() { if status.Signaled() {
return exitSignalOffset + int(status.Signal()) return exitSignalOffset + int(status.Signal())
} }
return status.ExitStatus() return status.ExitStatus()
} }
// WriteJSON writes the provided struct v to w using standard json marshaling
func WriteJSON(w io.Writer, v interface{}) error {
data, err := json.Marshal(v)
if err != nil {
return err
}
_, err = w.Write(data)
return err
}