github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/dev/random.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 dev
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    19  	"github.com/SagerNet/gvisor/pkg/context"
    20  	"github.com/SagerNet/gvisor/pkg/rand"
    21  	"github.com/SagerNet/gvisor/pkg/safemem"
    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  	"github.com/SagerNet/gvisor/pkg/waiter"
    26  )
    27  
    28  // +stateify savable
    29  type randomDevice struct {
    30  	fsutil.InodeGenericChecker       `state:"nosave"`
    31  	fsutil.InodeNoExtendedAttributes `state:"nosave"`
    32  	fsutil.InodeNoopAllocate         `state:"nosave"`
    33  	fsutil.InodeNoopRelease          `state:"nosave"`
    34  	fsutil.InodeNoopTruncate         `state:"nosave"`
    35  	fsutil.InodeNoopWriteOut         `state:"nosave"`
    36  	fsutil.InodeNotDirectory         `state:"nosave"`
    37  	fsutil.InodeNotMappable          `state:"nosave"`
    38  	fsutil.InodeNotSocket            `state:"nosave"`
    39  	fsutil.InodeNotSymlink           `state:"nosave"`
    40  	fsutil.InodeVirtual              `state:"nosave"`
    41  
    42  	fsutil.InodeSimpleAttributes
    43  }
    44  
    45  var _ fs.InodeOperations = (*randomDevice)(nil)
    46  
    47  func newRandomDevice(ctx context.Context, owner fs.FileOwner, mode linux.FileMode) *randomDevice {
    48  	r := &randomDevice{
    49  		InodeSimpleAttributes: fsutil.NewInodeSimpleAttributes(ctx, owner, fs.FilePermsFromMode(mode), linux.TMPFS_MAGIC),
    50  	}
    51  	return r
    52  }
    53  
    54  // GetFile implements fs.InodeOperations.GetFile.
    55  func (*randomDevice) GetFile(ctx context.Context, dirent *fs.Dirent, flags fs.FileFlags) (*fs.File, error) {
    56  	return fs.NewFile(ctx, dirent, flags, &randomFileOperations{}), nil
    57  }
    58  
    59  // +stateify savable
    60  type randomFileOperations struct {
    61  	fsutil.FileGenericSeek          `state:"nosave"`
    62  	fsutil.FileNoIoctl              `state:"nosave"`
    63  	fsutil.FileNoMMap               `state:"nosave"`
    64  	fsutil.FileNoSplice             `state:"nosave"`
    65  	fsutil.FileNoopFlush            `state:"nosave"`
    66  	fsutil.FileNoopFsync            `state:"nosave"`
    67  	fsutil.FileNoopRelease          `state:"nosave"`
    68  	fsutil.FileNoopWrite            `state:"nosave"`
    69  	fsutil.FileNotDirReaddir        `state:"nosave"`
    70  	fsutil.FileUseInodeUnstableAttr `state:"nosave"`
    71  	waiter.AlwaysReady              `state:"nosave"`
    72  }
    73  
    74  var _ fs.FileOperations = (*randomFileOperations)(nil)
    75  
    76  // Read implements fs.FileOperations.Read.
    77  func (*randomFileOperations) Read(ctx context.Context, _ *fs.File, dst usermem.IOSequence, _ int64) (int64, error) {
    78  	return dst.CopyOutFrom(ctx, safemem.FromIOReader{rand.Reader})
    79  }