github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/runsc/cmd/wait.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 "encoding/json" 20 "os" 21 22 "github.com/MerlinKodo/gvisor/runsc/cmd/util" 23 "github.com/MerlinKodo/gvisor/runsc/config" 24 "github.com/MerlinKodo/gvisor/runsc/container" 25 "github.com/MerlinKodo/gvisor/runsc/flag" 26 "github.com/google/subcommands" 27 "golang.org/x/sys/unix" 28 ) 29 30 const ( 31 unsetPID = -1 32 ) 33 34 // Wait implements subcommands.Command for the "wait" command. 35 type Wait struct { 36 rootPID int 37 pid int 38 } 39 40 // Name implements subcommands.Command.Name. 41 func (*Wait) Name() string { 42 return "wait" 43 } 44 45 // Synopsis implements subcommands.Command.Synopsis. 46 func (*Wait) Synopsis() string { 47 return "wait on a process inside a container" 48 } 49 50 // Usage implements subcommands.Command.Usage. 51 func (*Wait) Usage() string { 52 return `wait [flags] <container id>` 53 } 54 55 // SetFlags implements subcommands.Command.SetFlags. 56 func (wt *Wait) SetFlags(f *flag.FlagSet) { 57 f.IntVar(&wt.rootPID, "rootpid", unsetPID, "select a PID in the sandbox root PID namespace to wait on instead of the container's root process") 58 f.IntVar(&wt.pid, "pid", unsetPID, "select a PID in the container's PID namespace to wait on instead of the container's root process") 59 } 60 61 // Execute implements subcommands.Command.Execute. It waits for a process in a 62 // container to exit before returning. 63 func (wt *Wait) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus { 64 if f.NArg() != 1 { 65 f.Usage() 66 return subcommands.ExitUsageError 67 } 68 // You can't specify both -pid and -rootpid. 69 if wt.rootPID != unsetPID && wt.pid != unsetPID { 70 util.Fatalf("only one of -pid and -rootPid can be set") 71 } 72 73 id := f.Arg(0) 74 conf := args[0].(*config.Config) 75 76 c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{}) 77 if err != nil { 78 util.Fatalf("loading container: %v", err) 79 } 80 81 var waitStatus unix.WaitStatus 82 switch { 83 // Wait on the whole container. 84 case wt.rootPID == unsetPID && wt.pid == unsetPID: 85 ws, err := c.Wait() 86 if err != nil { 87 util.Fatalf("waiting on container %q: %v", c.ID, err) 88 } 89 waitStatus = ws 90 // Wait on a PID in the root PID namespace. 91 case wt.rootPID != unsetPID: 92 ws, err := c.WaitRootPID(int32(wt.rootPID)) 93 if err != nil { 94 util.Fatalf("waiting on PID in root PID namespace %d in container %q: %v", wt.rootPID, c.ID, err) 95 } 96 waitStatus = ws 97 // Wait on a PID in the container's PID namespace. 98 case wt.pid != unsetPID: 99 ws, err := c.WaitPID(int32(wt.pid)) 100 if err != nil { 101 util.Fatalf("waiting on PID %d in container %q: %v", wt.pid, c.ID, err) 102 } 103 waitStatus = ws 104 } 105 result := waitResult{ 106 ID: id, 107 ExitStatus: exitStatus(waitStatus), 108 } 109 // Write json-encoded wait result directly to stdout. 110 if err := json.NewEncoder(os.Stdout).Encode(result); err != nil { 111 util.Fatalf("marshaling wait result: %v", err) 112 } 113 return subcommands.ExitSuccess 114 } 115 116 type waitResult struct { 117 ID string `json:"id"` 118 ExitStatus int `json:"exitStatus"` 119 } 120 121 // exitStatus returns the correct exit status for a process based on if it 122 // was signaled or exited cleanly. 123 func exitStatus(status unix.WaitStatus) int { 124 if status.Signaled() { 125 return 128 + int(status.Signal()) 126 } 127 return status.ExitStatus() 128 }