Merge pull request #2177 from crosbymichael/file-io

Refactor shim to support file logging
This commit is contained in:
Kenfe-Mickaël Laventure 2018-03-05 07:52:45 -08:00 committed by GitHub
commit a1b511fb80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,6 +22,7 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"os"
"sync" "sync"
"syscall" "syscall"
@ -37,44 +38,75 @@ var bufPool = sync.Pool{
} }
func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, wg, cwg *sync.WaitGroup) error { func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, wg, cwg *sync.WaitGroup) error {
for name, dest := range map[string]func(wc io.WriteCloser, rc io.Closer){ var sameFile io.WriteCloser
stdout: func(wc io.WriteCloser, rc io.Closer) { for _, i := range []struct {
wg.Add(1) name string
cwg.Add(1) dest func(wc io.WriteCloser, rc io.Closer)
go func() { }{
cwg.Done() {
p := bufPool.Get().(*[]byte) name: stdout,
defer bufPool.Put(p) dest: func(wc io.WriteCloser, rc io.Closer) {
io.CopyBuffer(wc, rio.Stdout(), *p) wg.Add(1)
wg.Done() cwg.Add(1)
wc.Close() go func() {
rc.Close() cwg.Done()
}() p := bufPool.Get().(*[]byte)
}, defer bufPool.Put(p)
stderr: func(wc io.WriteCloser, rc io.Closer) { io.CopyBuffer(wc, rio.Stdout(), *p)
wg.Add(1) wg.Done()
cwg.Add(1) wc.Close()
go func() { if rc != nil {
cwg.Done() rc.Close()
p := bufPool.Get().(*[]byte) }
defer bufPool.Put(p) }()
},
io.CopyBuffer(wc, rio.Stderr(), *p) }, {
wg.Done() name: stderr,
wc.Close() dest: func(wc io.WriteCloser, rc io.Closer) {
rc.Close() wg.Add(1)
}() cwg.Add(1)
go func() {
cwg.Done()
p := bufPool.Get().(*[]byte)
defer bufPool.Put(p)
io.CopyBuffer(wc, rio.Stderr(), *p)
wg.Done()
wc.Close()
if rc != nil {
rc.Close()
}
}()
},
}, },
} { } {
fw, err := fifo.OpenFifo(ctx, name, syscall.O_WRONLY, 0) ok, err := isFifo(i.name)
if err != nil { if err != nil {
return fmt.Errorf("containerd-shim: opening %s failed: %s", name, err) return err
} }
fr, err := fifo.OpenFifo(ctx, name, syscall.O_RDONLY, 0) var (
if err != nil { fw io.WriteCloser
return fmt.Errorf("containerd-shim: opening %s failed: %s", name, err) fr io.Closer
)
if ok {
if fw, err = fifo.OpenFifo(ctx, i.name, syscall.O_WRONLY, 0); err != nil {
return fmt.Errorf("containerd-shim: opening %s failed: %s", i.name, err)
}
if fr, err = fifo.OpenFifo(ctx, i.name, syscall.O_RDONLY, 0); err != nil {
return fmt.Errorf("containerd-shim: opening %s failed: %s", i.name, err)
}
} else {
if sameFile != nil {
i.dest(sameFile, nil)
continue
}
if fw, err = os.OpenFile(i.name, syscall.O_WRONLY|syscall.O_APPEND, 0); err != nil {
return fmt.Errorf("containerd-shim: opening %s failed: %s", i.name, err)
}
if stdout == stderr {
sameFile = fw
}
} }
dest(fw, fr) i.dest(fw, fr)
} }
if stdin == "" { if stdin == "" {
rio.Stdin().Close() rio.Stdin().Close()
@ -96,3 +128,19 @@ func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, w
}() }()
return nil return nil
} }
// isFifo checks if a file is a fifo
// if the file does not exist then it returns false
func isFifo(path string) (bool, error) {
stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
if stat.Mode()&os.ModeNamedPipe == os.ModeNamedPipe {
return true, nil
}
return false, nil
}