Merge pull request #2177 from crosbymichael/file-io
Refactor shim to support file logging
This commit is contained in:
commit
a1b511fb80
@ -22,6 +22,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
@ -37,8 +38,14 @@ 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 {
|
||||||
|
name string
|
||||||
|
dest func(wc io.WriteCloser, rc io.Closer)
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: stdout,
|
||||||
|
dest: func(wc io.WriteCloser, rc io.Closer) {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
cwg.Add(1)
|
cwg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
@ -48,33 +55,58 @@ func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, w
|
|||||||
io.CopyBuffer(wc, rio.Stdout(), *p)
|
io.CopyBuffer(wc, rio.Stdout(), *p)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
wc.Close()
|
wc.Close()
|
||||||
|
if rc != nil {
|
||||||
rc.Close()
|
rc.Close()
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
},
|
},
|
||||||
stderr: func(wc io.WriteCloser, rc io.Closer) {
|
}, {
|
||||||
|
name: stderr,
|
||||||
|
dest: func(wc io.WriteCloser, rc io.Closer) {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
cwg.Add(1)
|
cwg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
cwg.Done()
|
cwg.Done()
|
||||||
p := bufPool.Get().(*[]byte)
|
p := bufPool.Get().(*[]byte)
|
||||||
defer bufPool.Put(p)
|
defer bufPool.Put(p)
|
||||||
|
|
||||||
io.CopyBuffer(wc, rio.Stderr(), *p)
|
io.CopyBuffer(wc, rio.Stderr(), *p)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
wc.Close()
|
wc.Close()
|
||||||
|
if rc != nil {
|
||||||
rc.Close()
|
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)
|
||||||
}
|
}
|
||||||
dest(fw, fr)
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user