2019-04-29 21:25:05 +00:00
|
|
|
// Copyright 2018 The gVisor Authors.
|
2018-04-27 17:37:02 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package pipe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2019-06-13 23:49:09 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/abi/linux"
|
|
|
|
"gvisor.dev/gvisor/pkg/sentry/context"
|
|
|
|
"gvisor.dev/gvisor/pkg/sentry/fs"
|
|
|
|
"gvisor.dev/gvisor/pkg/sentry/fs/fsutil"
|
|
|
|
"gvisor.dev/gvisor/pkg/syserror"
|
2018-04-27 17:37:02 +00:00
|
|
|
)
|
|
|
|
|
2019-01-15 04:33:29 +00:00
|
|
|
// inodeOperations implements fs.InodeOperations for pipes.
|
2018-08-02 17:41:44 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2018-04-27 17:37:02 +00:00
|
|
|
type inodeOperations struct {
|
2019-01-15 04:33:29 +00:00
|
|
|
fsutil.InodeGenericChecker `state:"nosave"`
|
|
|
|
fsutil.InodeNoExtendedAttributes `state:"nosave"`
|
|
|
|
fsutil.InodeNoopRelease `state:"nosave"`
|
|
|
|
fsutil.InodeNoopTruncate `state:"nosave"`
|
|
|
|
fsutil.InodeNoopWriteOut `state:"nosave"`
|
|
|
|
fsutil.InodeNotDirectory `state:"nosave"`
|
|
|
|
fsutil.InodeNotMappable `state:"nosave"`
|
|
|
|
fsutil.InodeNotSocket `state:"nosave"`
|
|
|
|
fsutil.InodeNotSymlink `state:"nosave"`
|
2019-06-04 19:57:41 +00:00
|
|
|
|
|
|
|
// Marking pipe inodes as virtual allows them to be saved and restored
|
|
|
|
// even if they have been unlinked. We can get away with this because
|
|
|
|
// their state exists entirely within the sentry.
|
|
|
|
fsutil.InodeVirtual `state:"nosave"`
|
2019-01-15 04:33:29 +00:00
|
|
|
|
|
|
|
fsutil.InodeSimpleAttributes
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
// mu protects the fields below.
|
|
|
|
mu sync.Mutex `state:"nosave"`
|
|
|
|
|
|
|
|
// p is the underlying Pipe object representing this fifo.
|
|
|
|
p *Pipe
|
|
|
|
|
|
|
|
// Channels for synchronizing the creation of new readers and writers of
|
|
|
|
// this fifo. See waitFor and newHandleLocked.
|
|
|
|
//
|
|
|
|
// These are not saved/restored because all waiters are unblocked on save,
|
|
|
|
// and either automatically restart (via ERESTARTSYS) or return EINTR on
|
|
|
|
// resume. On restarts via ERESTARTSYS, the appropriate channel will be
|
|
|
|
// recreated.
|
|
|
|
rWakeup chan struct{} `state:"nosave"`
|
|
|
|
wWakeup chan struct{} `state:"nosave"`
|
|
|
|
}
|
|
|
|
|
2019-01-15 04:33:29 +00:00
|
|
|
var _ fs.InodeOperations = (*inodeOperations)(nil)
|
|
|
|
|
|
|
|
// NewInodeOperations returns a new fs.InodeOperations for a given pipe.
|
|
|
|
func NewInodeOperations(ctx context.Context, perms fs.FilePermissions, p *Pipe) *inodeOperations {
|
2018-04-27 17:37:02 +00:00
|
|
|
return &inodeOperations{
|
2019-01-15 04:33:29 +00:00
|
|
|
InodeSimpleAttributes: fsutil.NewInodeSimpleAttributes(ctx, fs.FileOwnerFromContext(ctx), perms, linux.PIPEFS_MAGIC),
|
|
|
|
p: p,
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFile implements fs.InodeOperations.GetFile. Named pipes have special blocking
|
|
|
|
// semantics during open:
|
|
|
|
//
|
|
|
|
// "Normally, opening the FIFO blocks until the other end is opened also. A
|
|
|
|
// process can open a FIFO in nonblocking mode. In this case, opening for
|
|
|
|
// read-only will succeed even if no-one has opened on the write side yet,
|
|
|
|
// opening for write-only will fail with ENXIO (no such device or address)
|
|
|
|
// unless the other end has already been opened. Under Linux, opening a FIFO
|
|
|
|
// for read and write will succeed both in blocking and nonblocking mode. POSIX
|
|
|
|
// leaves this behavior undefined. This can be used to open a FIFO for writing
|
|
|
|
// while there are no readers available." - fifo(7)
|
|
|
|
func (i *inodeOperations) GetFile(ctx context.Context, d *fs.Dirent, flags fs.FileFlags) (*fs.File, error) {
|
|
|
|
i.mu.Lock()
|
|
|
|
defer i.mu.Unlock()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case flags.Read && !flags.Write: // O_RDONLY.
|
2019-06-04 19:57:41 +00:00
|
|
|
r := i.p.Open(ctx, d, flags)
|
2019-10-17 20:08:27 +00:00
|
|
|
newHandleLocked(&i.rWakeup)
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
if i.p.isNamed && !flags.NonBlocking && !i.p.HasWriters() {
|
2019-10-17 20:08:27 +00:00
|
|
|
if !waitFor(&i.mu, &i.wWakeup, ctx) {
|
2018-04-27 17:37:02 +00:00
|
|
|
r.DecRef()
|
|
|
|
return nil, syserror.ErrInterrupted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// By now, either we're doing a nonblocking open or we have a writer. On
|
|
|
|
// a nonblocking read-only open, the open succeeds even if no-one has
|
|
|
|
// opened the write side yet.
|
|
|
|
return r, nil
|
|
|
|
|
|
|
|
case flags.Write && !flags.Read: // O_WRONLY.
|
2019-06-04 19:57:41 +00:00
|
|
|
w := i.p.Open(ctx, d, flags)
|
2019-10-17 20:08:27 +00:00
|
|
|
newHandleLocked(&i.wWakeup)
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
if i.p.isNamed && !i.p.HasReaders() {
|
|
|
|
// On a nonblocking, write-only open, the open fails with ENXIO if the
|
|
|
|
// read side isn't open yet.
|
|
|
|
if flags.NonBlocking {
|
|
|
|
w.DecRef()
|
|
|
|
return nil, syserror.ENXIO
|
|
|
|
}
|
|
|
|
|
2019-10-17 20:08:27 +00:00
|
|
|
if !waitFor(&i.mu, &i.rWakeup, ctx) {
|
2018-04-27 17:37:02 +00:00
|
|
|
w.DecRef()
|
|
|
|
return nil, syserror.ErrInterrupted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w, nil
|
|
|
|
|
|
|
|
case flags.Read && flags.Write: // O_RDWR.
|
|
|
|
// Pipes opened for read-write always succeeds without blocking.
|
2019-06-04 19:57:41 +00:00
|
|
|
rw := i.p.Open(ctx, d, flags)
|
2019-10-17 20:08:27 +00:00
|
|
|
newHandleLocked(&i.rWakeup)
|
|
|
|
newHandleLocked(&i.wWakeup)
|
2018-04-27 17:37:02 +00:00
|
|
|
return rw, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, syserror.EINVAL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:34:44 +00:00
|
|
|
func (*inodeOperations) Allocate(_ context.Context, _ *fs.Inode, _, _ int64) error {
|
|
|
|
return syserror.EPIPE
|
|
|
|
}
|