github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/devices/memdev/memdev.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 implements "mem" character devices, as implemented in Linux
    16  // by drivers/char/mem.c and drivers/char/random.c.
    17  package memdev
    18  
    19  import (
    20  	"github.com/metacubex/gvisor/pkg/abi/linux"
    21  	"github.com/metacubex/gvisor/pkg/sentry/vfs"
    22  )
    23  
    24  // Register registers all devices implemented by this package in vfsObj.
    25  func Register(vfsObj *vfs.VirtualFilesystem) error {
    26  	for minor, spec := range map[uint32]struct {
    27  		dev      vfs.Device
    28  		pathname string
    29  	}{
    30  		nullDevMinor:    {nullDevice{}, "null"},
    31  		zeroDevMinor:    {zeroDevice{}, "zero"},
    32  		fullDevMinor:    {fullDevice{}, "full"},
    33  		randomDevMinor:  {randomDevice{}, "random"},
    34  		urandomDevMinor: {randomDevice{}, "urandom"},
    35  	} {
    36  		if err := vfsObj.RegisterDevice(vfs.CharDevice, linux.MEM_MAJOR, minor, spec.dev, &vfs.RegisterDeviceOptions{
    37  			GroupName: "mem",
    38  			Pathname:  spec.pathname,
    39  			FilePerms: 0666,
    40  		}); err != nil {
    41  			return err
    42  		}
    43  	}
    44  	return nil
    45  }