github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fsimpl/cgroupfs/job.go (about) 1 // Copyright 2021 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 cgroupfs 16 17 import ( 18 "bytes" 19 "fmt" 20 21 "github.com/SagerNet/gvisor/pkg/context" 22 "github.com/SagerNet/gvisor/pkg/sentry/fsimpl/kernfs" 23 "github.com/SagerNet/gvisor/pkg/sentry/kernel/auth" 24 "github.com/SagerNet/gvisor/pkg/usermem" 25 ) 26 27 // +stateify savable 28 type jobController struct { 29 controllerCommon 30 id int64 31 } 32 33 var _ controller = (*jobController)(nil) 34 35 func newJobController(fs *filesystem) *jobController { 36 c := &jobController{} 37 c.controllerCommon.init(controllerJob, fs) 38 return c 39 } 40 41 func (c *jobController) AddControlFiles(ctx context.Context, creds *auth.Credentials, _ *cgroupInode, contents map[string]kernfs.Inode) { 42 contents["job.id"] = c.fs.newControllerWritableFile(ctx, creds, &jobIDData{c: c}) 43 } 44 45 // +stateify savable 46 type jobIDData struct { 47 c *jobController 48 } 49 50 // Generate implements vfs.DynamicBytesSource.Generate. 51 func (d *jobIDData) Generate(ctx context.Context, buf *bytes.Buffer) error { 52 fmt.Fprintf(buf, "%d\n", d.c.id) 53 return nil 54 } 55 56 // Write implements vfs.WritableDynamicBytesSource.Write. 57 func (d *jobIDData) Write(ctx context.Context, src usermem.IOSequence, offset int64) (int64, error) { 58 val, n, err := parseInt64FromString(ctx, src, offset) 59 if err != nil { 60 return n, err 61 } 62 d.c.id = val 63 return n, nil 64 }