github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/runsc/cmd/ps.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 cmd
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/google/subcommands"
    22  	"github.com/ttpreport/gvisor-ligolo/pkg/sentry/control"
    23  	"github.com/ttpreport/gvisor-ligolo/runsc/cmd/util"
    24  	"github.com/ttpreport/gvisor-ligolo/runsc/config"
    25  	"github.com/ttpreport/gvisor-ligolo/runsc/container"
    26  	"github.com/ttpreport/gvisor-ligolo/runsc/flag"
    27  )
    28  
    29  // PS implements subcommands.Command for the "ps" command.
    30  type PS struct {
    31  	format string
    32  }
    33  
    34  // Name implements subcommands.Command.Name.
    35  func (*PS) Name() string {
    36  	return "ps"
    37  }
    38  
    39  // Synopsis implements subcommands.Command.Synopsis.
    40  func (*PS) Synopsis() string {
    41  	return "ps displays the processes running inside a container"
    42  }
    43  
    44  // Usage implements subcommands.Command.Usage.
    45  func (*PS) Usage() string {
    46  	return "<container-id> [ps options]"
    47  }
    48  
    49  // SetFlags implements subcommands.Command.SetFlags.
    50  func (ps *PS) SetFlags(f *flag.FlagSet) {
    51  	f.StringVar(&ps.format, "format", "table", "output format. Select one of: table or json (default: table)")
    52  }
    53  
    54  // Execute implements subcommands.Command.Execute.
    55  func (ps *PS) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
    56  	if f.NArg() != 1 {
    57  		f.Usage()
    58  		return subcommands.ExitUsageError
    59  	}
    60  
    61  	id := f.Arg(0)
    62  	conf := args[0].(*config.Config)
    63  
    64  	c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{SkipCheck: true})
    65  	if err != nil {
    66  		util.Fatalf("loading sandbox: %v", err)
    67  	}
    68  	pList, err := c.Processes()
    69  	if err != nil {
    70  		util.Fatalf("getting processes for container: %v", err)
    71  	}
    72  
    73  	switch ps.format {
    74  	case "table":
    75  		fmt.Println(control.ProcessListToTable(pList))
    76  	case "json":
    77  		o, err := control.PrintPIDsJSON(pList)
    78  		if err != nil {
    79  			util.Fatalf("generating JSON: %v", err)
    80  		}
    81  		fmt.Println(o)
    82  	default:
    83  		util.Fatalf("unsupported format: %s", ps.format)
    84  	}
    85  
    86  	return subcommands.ExitSuccess
    87  }