github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/kernel/pipe/reader_writer.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package pipe
    16  
    17  import (
    18  	"io"
    19  
    20  	"github.com/SagerNet/gvisor/pkg/context"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/arch"
    22  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    23  	"github.com/SagerNet/gvisor/pkg/sentry/fs/fsutil"
    24  	"github.com/SagerNet/gvisor/pkg/usermem"
    25  )
    26  
    27  // ReaderWriter satisfies the FileOperations interface and services both
    28  // read and write requests. This should only be used directly for named pipes.
    29  // pipe(2) and pipe2(2) only support unidirectional pipes and should use
    30  // either pipe.Reader or pipe.Writer.
    31  //
    32  // +stateify savable
    33  type ReaderWriter struct {
    34  	fsutil.FilePipeSeek             `state:"nosave"`
    35  	fsutil.FileNotDirReaddir        `state:"nosave"`
    36  	fsutil.FileNoFsync              `state:"nosave"`
    37  	fsutil.FileNoMMap               `state:"nosave"`
    38  	fsutil.FileNoSplice             `state:"nosave"`
    39  	fsutil.FileNoopFlush            `state:"nosave"`
    40  	fsutil.FileUseInodeUnstableAttr `state:"nosave"`
    41  	*Pipe
    42  }
    43  
    44  // Read implements fs.FileOperations.Read.
    45  func (rw *ReaderWriter) Read(ctx context.Context, _ *fs.File, dst usermem.IOSequence, _ int64) (int64, error) {
    46  	return rw.Pipe.Read(ctx, dst)
    47  }
    48  
    49  // WriteTo implements fs.FileOperations.WriteTo.
    50  func (rw *ReaderWriter) WriteTo(ctx context.Context, _ *fs.File, w io.Writer, count int64, dup bool) (int64, error) {
    51  	return rw.Pipe.WriteTo(ctx, w, count, dup)
    52  }
    53  
    54  // Write implements fs.FileOperations.Write.
    55  func (rw *ReaderWriter) Write(ctx context.Context, _ *fs.File, src usermem.IOSequence, _ int64) (int64, error) {
    56  	return rw.Pipe.Write(ctx, src)
    57  }
    58  
    59  // ReadFrom implements fs.FileOperations.WriteTo.
    60  func (rw *ReaderWriter) ReadFrom(ctx context.Context, _ *fs.File, r io.Reader, count int64) (int64, error) {
    61  	return rw.Pipe.ReadFrom(ctx, r, count)
    62  }
    63  
    64  // Ioctl implements fs.FileOperations.Ioctl.
    65  func (rw *ReaderWriter) Ioctl(ctx context.Context, _ *fs.File, io usermem.IO, args arch.SyscallArguments) (uintptr, error) {
    66  	return rw.Pipe.Ioctl(ctx, io, args)
    67  }