github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/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/MerlinKodo/gvisor/pkg/abi/linux" 21 "github.com/MerlinKodo/gvisor/pkg/context" 22 "github.com/MerlinKodo/gvisor/pkg/sentry/fsimpl/devtmpfs" 23 "github.com/MerlinKodo/gvisor/pkg/sentry/vfs" 24 ) 25 26 // Register registers all devices implemented by this package in vfsObj. 27 func Register(vfsObj *vfs.VirtualFilesystem) error { 28 for minor, dev := range map[uint32]vfs.Device{ 29 nullDevMinor: nullDevice{}, 30 zeroDevMinor: zeroDevice{}, 31 fullDevMinor: fullDevice{}, 32 randomDevMinor: randomDevice{}, 33 urandomDevMinor: randomDevice{}, 34 } { 35 if err := vfsObj.RegisterDevice(vfs.CharDevice, linux.MEM_MAJOR, minor, dev, &vfs.RegisterDeviceOptions{ 36 GroupName: "mem", 37 }); err != nil { 38 return err 39 } 40 } 41 return nil 42 } 43 44 // CreateDevtmpfsFiles creates device special files in dev representing all 45 // devices implemented by this package. 46 func CreateDevtmpfsFiles(ctx context.Context, dev *devtmpfs.Accessor) error { 47 for minor, name := range map[uint32]string{ 48 nullDevMinor: "null", 49 zeroDevMinor: "zero", 50 fullDevMinor: "full", 51 randomDevMinor: "random", 52 urandomDevMinor: "urandom", 53 } { 54 if err := dev.CreateDeviceFile(ctx, name, vfs.CharDevice, linux.MEM_MAJOR, minor, 0666 /* mode */); err != nil { 55 return err 56 } 57 } 58 return nil 59 }