github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/runsc/cmd/pause.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  
    20  	"github.com/MerlinKodo/gvisor/runsc/cmd/util"
    21  	"github.com/MerlinKodo/gvisor/runsc/config"
    22  	"github.com/MerlinKodo/gvisor/runsc/container"
    23  	"github.com/MerlinKodo/gvisor/runsc/flag"
    24  	"github.com/google/subcommands"
    25  )
    26  
    27  // Pause implements subcommands.Command for the "pause" command.
    28  type Pause struct{}
    29  
    30  // Name implements subcommands.Command.Name.
    31  func (*Pause) Name() string {
    32  	return "pause"
    33  }
    34  
    35  // Synopsis implements subcommands.Command.Synopsis.
    36  func (*Pause) Synopsis() string {
    37  	return "pause suspends all processes in a container"
    38  }
    39  
    40  // Usage implements subcommands.Command.Usage.
    41  func (*Pause) Usage() string {
    42  	return `pause <container id> - pause process in instance of container.`
    43  }
    44  
    45  // SetFlags implements subcommands.Command.SetFlags.
    46  func (*Pause) SetFlags(*flag.FlagSet) {
    47  }
    48  
    49  // Execute implements subcommands.Command.Execute.
    50  func (*Pause) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
    51  	if f.NArg() != 1 {
    52  		f.Usage()
    53  		return subcommands.ExitUsageError
    54  	}
    55  
    56  	id := f.Arg(0)
    57  	conf := args[0].(*config.Config)
    58  
    59  	cont, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{})
    60  	if err != nil {
    61  		util.Fatalf("loading container: %v", err)
    62  	}
    63  
    64  	if err := cont.Pause(); err != nil {
    65  		util.Fatalf("pause failed: %v", err)
    66  	}
    67  
    68  	return subcommands.ExitSuccess
    69  }