summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/sys/sys.go
blob: b1c3d48eb49d4feb41b118c2848ee29be47765cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package sys implements a sysfs filesystem.
package sys

import (
	"gvisor.googlesource.com/gvisor/pkg/sentry/context"
	"gvisor.googlesource.com/gvisor/pkg/sentry/fs"
	"gvisor.googlesource.com/gvisor/pkg/sentry/fs/ramfs"
	"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
)

// sys is a root sys node.
//
// +stateify savable
type sys struct {
	ramfs.Dir
}

func newFile(node fs.InodeOperations, msrc *fs.MountSource) *fs.Inode {
	sattr := fs.StableAttr{
		DeviceID:  sysfsDevice.DeviceID(),
		InodeID:   sysfsDevice.NextIno(),
		BlockSize: usermem.PageSize,
		Type:      fs.SpecialFile,
	}
	return fs.NewInode(node, msrc, sattr)
}

func newDir(ctx context.Context, msrc *fs.MountSource, contents map[string]*fs.Inode) *fs.Inode {
	d := &sys{}
	d.InitDir(ctx, contents, fs.RootOwner, fs.FilePermsFromMode(0555))
	return fs.NewInode(d, msrc, fs.StableAttr{
		DeviceID:  sysfsDevice.DeviceID(),
		InodeID:   sysfsDevice.NextIno(),
		BlockSize: usermem.PageSize,
		Type:      fs.SpecialDirectory,
	})
}

// New returns the root node of a partial simple sysfs.
func New(ctx context.Context, msrc *fs.MountSource) *fs.Inode {
	return newDir(ctx, msrc, map[string]*fs.Inode{
		// Add a basic set of top-level directories. In Linux, these
		// are dynamically added depending on the KConfig. Here we just
		// add the most common ones.
		"block":    newDir(ctx, msrc, nil),
		"bus":      newDir(ctx, msrc, nil),
		"class":    newDir(ctx, msrc, nil),
		"dev":      newDir(ctx, msrc, nil),
		"devices":  newDevicesDir(ctx, msrc),
		"firmware": newDir(ctx, msrc, nil),
		"fs":       newDir(ctx, msrc, nil),
		"kernel":   newDir(ctx, msrc, nil),
		"module":   newDir(ctx, msrc, nil),
		"power":    newDir(ctx, msrc, nil),
	})
}