github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/sentry/devices/memdev/null.go (about)

     1  // Copyright 2020 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 memdev
    16  
    17  import (
    18  	"io"
    19  
    20  	"github.com/MerlinKodo/gvisor/pkg/context"
    21  	"github.com/MerlinKodo/gvisor/pkg/sentry/vfs"
    22  	"github.com/MerlinKodo/gvisor/pkg/usermem"
    23  )
    24  
    25  const nullDevMinor = 3
    26  
    27  // nullDevice implements vfs.Device for /dev/null.
    28  //
    29  // +stateify savable
    30  type nullDevice struct{}
    31  
    32  // Open implements vfs.Device.Open.
    33  func (nullDevice) Open(ctx context.Context, mnt *vfs.Mount, vfsd *vfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) {
    34  	fd := &nullFD{}
    35  	if err := fd.vfsfd.Init(fd, opts.Flags, mnt, vfsd, &vfs.FileDescriptionOptions{
    36  		UseDentryMetadata: true,
    37  	}); err != nil {
    38  		return nil, err
    39  	}
    40  	return &fd.vfsfd, nil
    41  }
    42  
    43  // nullFD implements vfs.FileDescriptionImpl for /dev/null.
    44  //
    45  // +stateify savable
    46  type nullFD struct {
    47  	vfsfd vfs.FileDescription
    48  	vfs.FileDescriptionDefaultImpl
    49  	vfs.DentryMetadataFileDescriptionImpl
    50  	vfs.NoLockFD
    51  }
    52  
    53  // Release implements vfs.FileDescriptionImpl.Release.
    54  func (fd *nullFD) Release(context.Context) {
    55  	// noop
    56  }
    57  
    58  // PRead implements vfs.FileDescriptionImpl.PRead.
    59  func (fd *nullFD) PRead(ctx context.Context, dst usermem.IOSequence, offset int64, opts vfs.ReadOptions) (int64, error) {
    60  	return 0, io.EOF
    61  }
    62  
    63  // Read implements vfs.FileDescriptionImpl.Read.
    64  func (fd *nullFD) Read(ctx context.Context, dst usermem.IOSequence, opts vfs.ReadOptions) (int64, error) {
    65  	return 0, io.EOF
    66  }
    67  
    68  // PWrite implements vfs.FileDescriptionImpl.PWrite.
    69  func (fd *nullFD) PWrite(ctx context.Context, src usermem.IOSequence, offset int64, opts vfs.WriteOptions) (int64, error) {
    70  	return src.NumBytes(), nil
    71  }
    72  
    73  // Write implements vfs.FileDescriptionImpl.Write.
    74  func (fd *nullFD) Write(ctx context.Context, src usermem.IOSequence, opts vfs.WriteOptions) (int64, error) {
    75  	return src.NumBytes(), nil
    76  }
    77  
    78  // Seek implements vfs.FileDescriptionImpl.Seek.
    79  func (fd *nullFD) Seek(ctx context.Context, offset int64, whence int32) (int64, error) {
    80  	return 0, nil
    81  }