|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path/filepath" |
| 6 | + |
| 7 | + "github.com/hyperhq/runv/containerd/api/grpc/types" |
| 8 | + "github.com/hyperhq/runv/lib/linuxsignal" |
| 9 | + "github.com/urfave/cli" |
| 10 | + netcontext "golang.org/x/net/context" |
| 11 | +) |
| 12 | + |
| 13 | +var pauseCommand = cli.Command{ |
| 14 | + Name: "pause", |
| 15 | + Usage: "suspend all processes in the container", |
| 16 | + ArgsUsage: `<container-id>`, |
| 17 | + Action: func(context *cli.Context) error { |
| 18 | + container := context.Args().First() |
| 19 | + if container == "" { |
| 20 | + return cli.NewExitError(fmt.Sprintf("container id cannot be empty"), -1) |
| 21 | + } |
| 22 | + |
| 23 | + c, err := getClient(filepath.Join(context.GlobalString("root"), container, "namespace/namespaced.sock")) |
| 24 | + if err != nil { |
| 25 | + return cli.NewExitError(fmt.Sprintf("failed to get client: %v", err), -1) |
| 26 | + } |
| 27 | + |
| 28 | + plist, err := getProcessList(c, container) |
| 29 | + if err != nil { |
| 30 | + return cli.NewExitError(fmt.Sprintf("can't get process list, %v", err), -1) |
| 31 | + } |
| 32 | + |
| 33 | + for _, p := range plist { |
| 34 | + if _, err := c.Signal(netcontext.Background(), &types.SignalRequest{ |
| 35 | + Id: container, |
| 36 | + Pid: p, |
| 37 | + Signal: uint32(linuxsignal.SIGSTOP), |
| 38 | + }); err != nil { |
| 39 | + return cli.NewExitError(fmt.Sprintf("suspend signal failed, %v", err), -1) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return nil |
| 44 | + }, |
| 45 | +} |
| 46 | + |
| 47 | +var resumeCommand = cli.Command{ |
| 48 | + Name: "resume", |
| 49 | + Usage: "resume all processes in the container", |
| 50 | + ArgsUsage: `<container-id>`, |
| 51 | + Action: func(context *cli.Context) error { |
| 52 | + container := context.Args().First() |
| 53 | + if container == "" { |
| 54 | + return cli.NewExitError(fmt.Sprintf("container id cannot be empty"), -1) |
| 55 | + } |
| 56 | + |
| 57 | + c, err := getClient(filepath.Join(context.GlobalString("root"), container, "namespace/namespaced.sock")) |
| 58 | + if err != nil { |
| 59 | + return cli.NewExitError(fmt.Sprintf("failed to get client: %v", err), -1) |
| 60 | + } |
| 61 | + |
| 62 | + plist, err := getProcessList(c, container) |
| 63 | + if err != nil { |
| 64 | + return cli.NewExitError(fmt.Sprintf("can't get process list, %v", err), -1) |
| 65 | + } |
| 66 | + |
| 67 | + for _, p := range plist { |
| 68 | + if _, err := c.Signal(netcontext.Background(), &types.SignalRequest{ |
| 69 | + Id: container, |
| 70 | + Pid: p, |
| 71 | + Signal: uint32(linuxsignal.SIGCONT), |
| 72 | + }); err != nil { |
| 73 | + return cli.NewExitError(fmt.Sprintf("resume signal failed, %v", err), -1) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | + }, |
| 79 | +} |
0 commit comments