github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/proc/uptime.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 proc 16 17 import ( 18 "fmt" 19 "io" 20 21 "github.com/SagerNet/gvisor/pkg/abi/linux" 22 "github.com/SagerNet/gvisor/pkg/context" 23 "github.com/SagerNet/gvisor/pkg/errors/linuxerr" 24 "github.com/SagerNet/gvisor/pkg/sentry/fs" 25 "github.com/SagerNet/gvisor/pkg/sentry/fs/fsutil" 26 ktime "github.com/SagerNet/gvisor/pkg/sentry/kernel/time" 27 "github.com/SagerNet/gvisor/pkg/usermem" 28 "github.com/SagerNet/gvisor/pkg/waiter" 29 ) 30 31 // LINT.IfChange 32 33 // uptime is a file containing the system uptime. 34 // 35 // +stateify savable 36 type uptime struct { 37 fsutil.SimpleFileInode 38 39 // The "start time" of the sandbox. 40 startTime ktime.Time 41 } 42 43 // newUptime returns a new uptime file. 44 func newUptime(ctx context.Context, msrc *fs.MountSource) *fs.Inode { 45 u := &uptime{ 46 SimpleFileInode: *fsutil.NewSimpleFileInode(ctx, fs.RootOwner, fs.FilePermsFromMode(0444), linux.PROC_SUPER_MAGIC), 47 startTime: ktime.NowFromContext(ctx), 48 } 49 return newProcInode(ctx, u, msrc, fs.SpecialFile, nil) 50 } 51 52 // GetFile implements fs.InodeOperations.GetFile. 53 func (u *uptime) GetFile(ctx context.Context, dirent *fs.Dirent, flags fs.FileFlags) (*fs.File, error) { 54 return fs.NewFile(ctx, dirent, flags, &uptimeFile{startTime: u.startTime}), nil 55 } 56 57 // +stateify savable 58 type uptimeFile struct { 59 fsutil.FileGenericSeek `state:"nosave"` 60 fsutil.FileNoIoctl `state:"nosave"` 61 fsutil.FileNoMMap `state:"nosave"` 62 fsutil.FileNoSplice `state:"nosave"` 63 fsutil.FileNoWrite `state:"nosave"` 64 fsutil.FileNoopFlush `state:"nosave"` 65 fsutil.FileNoopFsync `state:"nosave"` 66 fsutil.FileNoopRelease `state:"nosave"` 67 fsutil.FileNotDirReaddir `state:"nosave"` 68 fsutil.FileUseInodeUnstableAttr `state:"nosave"` 69 waiter.AlwaysReady `state:"nosave"` 70 71 startTime ktime.Time 72 } 73 74 // Read implements fs.FileOperations.Read. 75 func (f *uptimeFile) Read(ctx context.Context, _ *fs.File, dst usermem.IOSequence, offset int64) (int64, error) { 76 if offset < 0 { 77 return 0, linuxerr.EINVAL 78 } 79 80 now := ktime.NowFromContext(ctx) 81 // Pretend that we've spent zero time sleeping (second number). 82 s := []byte(fmt.Sprintf("%.2f 0.00\n", now.Sub(f.startTime).Seconds())) 83 if offset >= int64(len(s)) { 84 return 0, io.EOF 85 } 86 87 n, err := dst.CopyOut(ctx, s[offset:]) 88 return int64(n), err 89 } 90 91 // LINT.ThenChange(../../fsimpl/proc/tasks_files.go)