github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/state.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "os" 6 7 "github.com/opencontainers/runc/libcontainer" 8 "github.com/opencontainers/runc/libcontainer/utils" 9 "github.com/urfave/cli" 10 ) 11 12 var stateCommand = cli.Command{ 13 Name: "state", 14 Usage: "output the state of a container", 15 ArgsUsage: `<container-id> 16 17 Where "<container-id>" is your name for the instance of the container.`, 18 Description: `The state command outputs current state information for the 19 instance of a container.`, 20 Action: func(context *cli.Context) error { 21 if err := checkArgs(context, 1, exactArgs); err != nil { 22 return err 23 } 24 container, err := getContainer(context) 25 if err != nil { 26 return err 27 } 28 containerStatus, err := container.Status() 29 if err != nil { 30 return err 31 } 32 state, err := container.State() 33 if err != nil { 34 return err 35 } 36 pid := state.BaseState.InitProcessPid 37 if containerStatus == libcontainer.Stopped { 38 pid = 0 39 } 40 bundle, annotations := utils.Annotations(state.Config.Labels) 41 cs := containerState{ 42 Version: state.BaseState.Config.Version, 43 ID: state.BaseState.ID, 44 InitProcessPid: pid, 45 Status: containerStatus.String(), 46 Bundle: bundle, 47 Rootfs: state.BaseState.Config.Rootfs, 48 Created: state.BaseState.Created, 49 Annotations: annotations, 50 } 51 data, err := json.MarshalIndent(cs, "", " ") 52 if err != nil { 53 return err 54 } 55 os.Stdout.Write(data) 56 return nil 57 }, 58 }