mirror of
https://github.com/docker/cli.git
synced 2026-01-23 15:21:32 +03:00
Merge pull request #27405 from tonistiigi/fix-fifo2
Fix issues with fifos blocking on open Upstream-commit: 32b541e44385206772b30b2a6f241a7c809aa62f Component: engine
This commit is contained in:
21
components/engine/vendor/src/github.com/tonistiigi/fifo/LICENSE
vendored
Normal file
21
components/engine/vendor/src/github.com/tonistiigi/fifo/LICENSE
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT
|
||||
|
||||
Copyright (C) 2016 Tõnis Tiigi <tonistiigi@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
13
components/engine/vendor/src/github.com/tonistiigi/fifo/Makefile
vendored
Normal file
13
components/engine/vendor/src/github.com/tonistiigi/fifo/Makefile
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
.PHONY: fmt vet test deps
|
||||
|
||||
test: deps
|
||||
go test -v ./...
|
||||
|
||||
deps:
|
||||
go get -d -t ./...
|
||||
|
||||
fmt:
|
||||
gofmt -s -l .
|
||||
|
||||
vet:
|
||||
go vet ./...
|
||||
215
components/engine/vendor/src/github.com/tonistiigi/fifo/fifo.go
vendored
Normal file
215
components/engine/vendor/src/github.com/tonistiigi/fifo/fifo.go
vendored
Normal file
@@ -0,0 +1,215 @@
|
||||
package fifo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type fifo struct {
|
||||
flag int
|
||||
opened chan struct{}
|
||||
closed chan struct{}
|
||||
closing chan struct{}
|
||||
err error
|
||||
file *os.File
|
||||
closingOnce sync.Once // close has been called
|
||||
closedOnce sync.Once // fifo is closed
|
||||
handle *handle
|
||||
}
|
||||
|
||||
var leakCheckWg *sync.WaitGroup
|
||||
|
||||
// OpenFifo opens a fifo. Returns io.ReadWriteCloser.
|
||||
// Context can be used to cancel this function until open(2) has not returned.
|
||||
// Accepted flags:
|
||||
// - syscall.O_CREAT - create new fifo if one doesn't exist
|
||||
// - syscall.O_RDONLY - open fifo only from reader side
|
||||
// - syscall.O_WRONLY - open fifo only from writer side
|
||||
// - syscall.O_RDWR - open fifo from both sides, never block on syscall level
|
||||
// - syscall.O_NONBLOCK - return io.ReadWriteCloser even if other side of the
|
||||
// fifo isn't open. read/write will be connected after the actual fifo is
|
||||
// open or after fifo is closed.
|
||||
func OpenFifo(ctx context.Context, fn string, flag int, perm os.FileMode) (io.ReadWriteCloser, error) {
|
||||
if _, err := os.Stat(fn); err != nil {
|
||||
if os.IsNotExist(err) && flag&syscall.O_CREAT != 0 {
|
||||
if err := syscall.Mkfifo(fn, uint32(perm&os.ModePerm)); err != nil && !os.IsExist(err) {
|
||||
return nil, errors.Wrapf(err, "error creating fifo %v", fn)
|
||||
}
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
block := flag&syscall.O_NONBLOCK == 0 || flag&syscall.O_RDWR != 0
|
||||
|
||||
flag &= ^syscall.O_CREAT
|
||||
flag &= ^syscall.O_NONBLOCK
|
||||
|
||||
h, err := getHandle(fn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f := &fifo{
|
||||
handle: h,
|
||||
flag: flag,
|
||||
opened: make(chan struct{}),
|
||||
closed: make(chan struct{}),
|
||||
closing: make(chan struct{}),
|
||||
}
|
||||
|
||||
wg := leakCheckWg
|
||||
if wg != nil {
|
||||
wg.Add(2)
|
||||
}
|
||||
|
||||
go func() {
|
||||
if wg != nil {
|
||||
defer wg.Done()
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
f.Close()
|
||||
case <-f.opened:
|
||||
case <-f.closed:
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
if wg != nil {
|
||||
defer wg.Done()
|
||||
}
|
||||
var file *os.File
|
||||
fn, err := h.Path()
|
||||
if err == nil {
|
||||
file, err = os.OpenFile(fn, flag, 0)
|
||||
}
|
||||
select {
|
||||
case <-f.closing:
|
||||
if err == nil {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
err = ctx.Err()
|
||||
default:
|
||||
err = errors.Errorf("fifo %v was closed before opening", fn)
|
||||
}
|
||||
if file != nil {
|
||||
file.Close()
|
||||
}
|
||||
}
|
||||
default:
|
||||
}
|
||||
if err != nil {
|
||||
f.closedOnce.Do(func() {
|
||||
f.err = err
|
||||
close(f.closed)
|
||||
})
|
||||
return
|
||||
}
|
||||
f.file = file
|
||||
close(f.opened)
|
||||
}()
|
||||
if block {
|
||||
select {
|
||||
case <-f.opened:
|
||||
case <-f.closed:
|
||||
return nil, f.err
|
||||
}
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// Read from a fifo to a byte array.
|
||||
func (f *fifo) Read(b []byte) (int, error) {
|
||||
if f.flag&syscall.O_WRONLY > 0 {
|
||||
return 0, errors.New("reading from write-only fifo")
|
||||
}
|
||||
select {
|
||||
case <-f.opened:
|
||||
return f.file.Read(b)
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case <-f.opened:
|
||||
return f.file.Read(b)
|
||||
case <-f.closed:
|
||||
return 0, errors.New("reading from a closed fifo")
|
||||
}
|
||||
}
|
||||
|
||||
// Write from byte array to a fifo.
|
||||
func (f *fifo) Write(b []byte) (int, error) {
|
||||
if f.flag&(syscall.O_WRONLY|syscall.O_RDWR) == 0 {
|
||||
return 0, errors.New("writing to read-only fifo")
|
||||
}
|
||||
select {
|
||||
case <-f.opened:
|
||||
return f.file.Write(b)
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case <-f.opened:
|
||||
return f.file.Write(b)
|
||||
case <-f.closed:
|
||||
return 0, errors.New("writing to a closed fifo")
|
||||
}
|
||||
}
|
||||
|
||||
// Close the fifo. Next reads/writes will error. This method can also be used
|
||||
// before open(2) has returned and fifo was never opened.
|
||||
func (f *fifo) Close() error {
|
||||
for {
|
||||
select {
|
||||
case <-f.closed:
|
||||
f.handle.Close()
|
||||
return f.err
|
||||
default:
|
||||
select {
|
||||
case <-f.opened:
|
||||
f.closedOnce.Do(func() {
|
||||
f.err = f.file.Close()
|
||||
close(f.closed)
|
||||
})
|
||||
default:
|
||||
if f.flag&syscall.O_RDWR != 0 {
|
||||
runtime.Gosched()
|
||||
break
|
||||
}
|
||||
f.closingOnce.Do(func() {
|
||||
close(f.closing)
|
||||
})
|
||||
reverseMode := syscall.O_WRONLY
|
||||
if f.flag&syscall.O_WRONLY > 0 {
|
||||
reverseMode = syscall.O_RDONLY
|
||||
}
|
||||
fn, err := f.handle.Path()
|
||||
// if Close() is called concurrently(shouldn't) it may cause error
|
||||
// because handle is closed
|
||||
select {
|
||||
case <-f.closed:
|
||||
default:
|
||||
if err != nil {
|
||||
// Path has become invalid. We will leak a goroutine.
|
||||
// This case should not happen in linux.
|
||||
f.closedOnce.Do(func() {
|
||||
f.err = err
|
||||
close(f.closed)
|
||||
})
|
||||
<-f.closed
|
||||
break
|
||||
}
|
||||
f, err := os.OpenFile(fn, reverseMode|syscall.O_NONBLOCK, 0)
|
||||
if err == nil {
|
||||
f.Close()
|
||||
}
|
||||
runtime.Gosched()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
70
components/engine/vendor/src/github.com/tonistiigi/fifo/handle_linux.go
vendored
Normal file
70
components/engine/vendor/src/github.com/tonistiigi/fifo/handle_linux.go
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
// +build linux
|
||||
|
||||
package fifo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const O_PATH = 010000000
|
||||
|
||||
type handle struct {
|
||||
f *os.File
|
||||
dev uint64
|
||||
ino uint64
|
||||
closeOnce sync.Once
|
||||
}
|
||||
|
||||
func getHandle(fn string) (*handle, error) {
|
||||
f, err := os.OpenFile(fn, O_PATH, 0)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to open %v with O_PATH", fn)
|
||||
}
|
||||
|
||||
var stat syscall.Stat_t
|
||||
if err := syscall.Fstat(int(f.Fd()), &stat); err != nil {
|
||||
f.Close()
|
||||
return nil, errors.Wrapf(err, "failed to stat handle %v", f.Fd())
|
||||
}
|
||||
|
||||
h := &handle{
|
||||
f: f,
|
||||
dev: stat.Dev,
|
||||
ino: stat.Ino,
|
||||
}
|
||||
|
||||
// check /proc just in case
|
||||
if _, err := os.Stat(h.procPath()); err != nil {
|
||||
f.Close()
|
||||
return nil, errors.Wrapf(err, "couldn't stat %v", h.procPath())
|
||||
}
|
||||
|
||||
return h, nil
|
||||
}
|
||||
|
||||
func (h *handle) procPath() string {
|
||||
return fmt.Sprintf("/proc/self/fd/%d", h.f.Fd())
|
||||
}
|
||||
|
||||
func (h *handle) Path() (string, error) {
|
||||
var stat syscall.Stat_t
|
||||
if err := syscall.Stat(h.procPath(), &stat); err != nil {
|
||||
return "", errors.Wrapf(err, "path %v could not be statted", h.procPath())
|
||||
}
|
||||
if stat.Dev != h.dev || stat.Ino != h.ino {
|
||||
return "", errors.Errorf("failed to verify handle %v/%v %v/%v", stat.Dev, h.dev, stat.Ino, h.ino)
|
||||
}
|
||||
return h.procPath(), nil
|
||||
}
|
||||
|
||||
func (h *handle) Close() error {
|
||||
h.closeOnce.Do(func() {
|
||||
h.f.Close()
|
||||
})
|
||||
return nil
|
||||
}
|
||||
45
components/engine/vendor/src/github.com/tonistiigi/fifo/handle_nolinux.go
vendored
Normal file
45
components/engine/vendor/src/github.com/tonistiigi/fifo/handle_nolinux.go
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
// +build !linux
|
||||
|
||||
package fifo
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type handle struct {
|
||||
fn string
|
||||
dev uint64
|
||||
ino uint64
|
||||
}
|
||||
|
||||
func getHandle(fn string) (*handle, error) {
|
||||
var stat syscall.Stat_t
|
||||
if err := syscall.Stat(fn, &stat); err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to stat %v", fn)
|
||||
}
|
||||
|
||||
h := &handle{
|
||||
fn: fn,
|
||||
dev: uint64(stat.Dev),
|
||||
ino: stat.Ino,
|
||||
}
|
||||
|
||||
return h, nil
|
||||
}
|
||||
|
||||
func (h *handle) Path() (string, error) {
|
||||
var stat syscall.Stat_t
|
||||
if err := syscall.Stat(h.fn, &stat); err != nil {
|
||||
return "", errors.Wrapf(err, "path %v could not be statted", h.fn)
|
||||
}
|
||||
if uint64(stat.Dev) != h.dev || stat.Ino != h.ino {
|
||||
return "", errors.Errorf("failed to verify handle %v/%v %v/%v", stat.Dev, h.dev, stat.Ino, h.ino)
|
||||
}
|
||||
return h.fn, nil
|
||||
}
|
||||
|
||||
func (h *handle) Close() error {
|
||||
return nil
|
||||
}
|
||||
30
components/engine/vendor/src/github.com/tonistiigi/fifo/readme.md
vendored
Normal file
30
components/engine/vendor/src/github.com/tonistiigi/fifo/readme.md
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
### fifo
|
||||
|
||||
Go package for handling fifos in a sane way.
|
||||
|
||||
```
|
||||
// OpenFifo opens a fifo. Returns io.ReadWriteCloser.
|
||||
// Context can be used to cancel this function until open(2) has not returned.
|
||||
// Accepted flags:
|
||||
// - syscall.O_CREAT - create new fifo if one doesn't exist
|
||||
// - syscall.O_RDONLY - open fifo only from reader side
|
||||
// - syscall.O_WRONLY - open fifo only from writer side
|
||||
// - syscall.O_RDWR - open fifo from both sides, never block on syscall level
|
||||
// - syscall.O_NONBLOCK - return io.ReadWriteCloser even if other side of the
|
||||
// fifo isn't open. read/write will be connected after the actual fifo is
|
||||
// open or after fifo is closed.
|
||||
func OpenFifo(ctx context.Context, fn string, flag int, perm os.FileMode) (io.ReadWriteCloser, error)
|
||||
|
||||
|
||||
// Read from a fifo to a byte array.
|
||||
func (f *fifo) Read(b []byte) (int, error)
|
||||
|
||||
|
||||
// Write from byte array to a fifo.
|
||||
func (f *fifo) Write(b []byte) (int, error)
|
||||
|
||||
|
||||
// Close the fifo. Next reads/writes will error. This method can also be used
|
||||
// before open(2) has returned and fifo was never opened.
|
||||
func (f *fifo) Close() error
|
||||
```
|
||||
Reference in New Issue
Block a user