github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/runsc/cmd/read_control.go (about) 1 // Copyright 2022 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 cmd 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/google/subcommands" 22 "github.com/metacubex/gvisor/pkg/sentry/control" 23 "github.com/metacubex/gvisor/runsc/cmd/util" 24 "github.com/metacubex/gvisor/runsc/config" 25 "github.com/metacubex/gvisor/runsc/container" 26 "github.com/metacubex/gvisor/runsc/flag" 27 ) 28 29 // ReadControl implements subcommands.Command for the "read-control" command. 30 type ReadControl struct{} 31 32 // Name implements subcommands.Command.Name. 33 func (*ReadControl) Name() string { 34 return "read-control" 35 } 36 37 // Synopsis implements subcommands.Command.Synopsis. 38 func (*ReadControl) Synopsis() string { 39 return "read a cgroups control value inside the container" 40 } 41 42 // Usage implements subcommands.Command.Usage. 43 func (*ReadControl) Usage() string { 44 return `read-control <container-id> <controller> <cgroup-path> <control-value-name> 45 46 Where "<container-id>" is the name for the instance of the container, 47 "<controller>" is the name of an active cgroupv1 controller, <cgroup-path> is 48 the path to the cgroup to read and <control-value-name> is the name of the 49 control file to read. 50 51 EXAMPLE: 52 # runsc read-control <container-id> cpuacct / cpuacct.usage 53 ` 54 } 55 56 // SetFlags implements subcommands.Command.SetFlags. 57 func (r *ReadControl) SetFlags(f *flag.FlagSet) {} 58 59 // Execute implements subcommands.Command.Execute. 60 func (r *ReadControl) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus { 61 if f.NArg() < 4 { 62 f.Usage() 63 return subcommands.ExitUsageError 64 } 65 66 id := f.Arg(0) 67 conf := args[0].(*config.Config) 68 69 c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{SkipCheck: true}) 70 if err != nil { 71 util.Fatalf("loading sandbox: %v", err) 72 } 73 74 out, err := c.Sandbox.CgroupsReadControlFile(control.CgroupControlFile{ 75 Controller: f.Arg(1), 76 Path: f.Arg(2), 77 Name: f.Arg(3), 78 }) 79 if err != nil { 80 fmt.Printf("ERROR: %s\n", err) 81 return subcommands.ExitFailure 82 } 83 fmt.Printf("%s\n", out) 84 return subcommands.ExitSuccess 85 }