github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/sys/sys.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 sys implements a sysfs filesystem.
    16  package sys
    17  
    18  import (
    19  	"github.com/SagerNet/gvisor/pkg/context"
    20  	"github.com/SagerNet/gvisor/pkg/hostarch"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    22  	"github.com/SagerNet/gvisor/pkg/sentry/fs/ramfs"
    23  )
    24  
    25  func newFile(ctx context.Context, node fs.InodeOperations, msrc *fs.MountSource) *fs.Inode {
    26  	sattr := fs.StableAttr{
    27  		DeviceID:  sysfsDevice.DeviceID(),
    28  		InodeID:   sysfsDevice.NextIno(),
    29  		BlockSize: hostarch.PageSize,
    30  		Type:      fs.SpecialFile,
    31  	}
    32  	return fs.NewInode(ctx, node, msrc, sattr)
    33  }
    34  
    35  func newDir(ctx context.Context, msrc *fs.MountSource, contents map[string]*fs.Inode) *fs.Inode {
    36  	d := ramfs.NewDir(ctx, contents, fs.RootOwner, fs.FilePermsFromMode(0555))
    37  	return fs.NewInode(ctx, d, msrc, fs.StableAttr{
    38  		DeviceID:  sysfsDevice.DeviceID(),
    39  		InodeID:   sysfsDevice.NextIno(),
    40  		BlockSize: hostarch.PageSize,
    41  		Type:      fs.SpecialDirectory,
    42  	})
    43  }
    44  
    45  // New returns the root node of a partial simple sysfs.
    46  func New(ctx context.Context, msrc *fs.MountSource) *fs.Inode {
    47  	return newDir(ctx, msrc, map[string]*fs.Inode{
    48  		// Add a basic set of top-level directories. In Linux, these
    49  		// are dynamically added depending on the KConfig. Here we just
    50  		// add the most common ones.
    51  		"block": newDir(ctx, msrc, nil),
    52  		"bus":   newDir(ctx, msrc, nil),
    53  		"class": newDir(ctx, msrc, map[string]*fs.Inode{
    54  			"power_supply": newDir(ctx, msrc, nil),
    55  		}),
    56  		"dev":      newDir(ctx, msrc, nil),
    57  		"devices":  newDevicesDir(ctx, msrc),
    58  		"firmware": newDir(ctx, msrc, nil),
    59  		"fs":       newDir(ctx, msrc, nil),
    60  		"kernel":   newDir(ctx, msrc, nil),
    61  		"module":   newDir(ctx, msrc, nil),
    62  		"power":    newDir(ctx, msrc, nil),
    63  	})
    64  }