github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/runc/state.go (about)

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