| 1 |
//go:build !windows |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright The containerd Authors. |
| 5 |
|
| 6 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
you may not use this file except in compliance with the License. |
| 8 |
You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package process |
| 20 |
|
| 21 |
import ( |
| 22 |
"context" |
| 23 |
"encoding/json" |
| 24 |
"fmt" |
| 25 |
"io" |
| 26 |
"os" |
| 27 |
"path/filepath" |
| 28 |
"strings" |
| 29 |
"sync" |
| 30 |
"time" |
| 31 |
|
| 32 |
"github.com/containerd/errdefs" |
| 33 |
runc "github.com/containerd/go-runc" |
| 34 |
"golang.org/x/sys/unix" |
| 35 |
) |
| 36 |
|
| 37 |
const ( |
| 38 |
// RuncRoot is the path to the root runc state directory |
| 39 |
RuncRoot = "/run/containerd/runc" |
| 40 |
// InitPidFile name of the file that contains the init pid |
| 41 |
InitPidFile = "init.pid" |
| 42 |
) |
| 43 |
|
| 44 |
// safePid is a thread safe wrapper for pid. |
| 45 |
type safePid struct { |
| 46 |
sync.Mutex |
| 47 |
pid int |
| 48 |
} |
| 49 |
|
| 50 |
func (s *safePid) get() int { |
| 51 |
s.Lock() |
| 52 |
defer s.Unlock() |
| 53 |
return s.pid |
| 54 |
} |
| 55 |
|
| 56 |
// TODO(mlaventure): move to runc package? |
| 57 |
func getLastRuntimeError(r *runc.Runc) (string, error) { |
| 58 |
if r.Log == "" { |
| 59 |
return "", nil |
| 60 |
} |
| 61 |
|
| 62 |
f, err := os.OpenFile(r.Log, os.O_RDONLY, 0400) |
| 63 |
if err != nil { |
| 64 |
return "", err |
| 65 |
} |
| 66 |
defer f.Close() |
| 67 |
|
| 68 |
var ( |
| 69 |
errMsg string |
| 70 |
log struct { |
| 71 |
Level string |
| 72 |
Msg string |
| 73 |
Time time.Time |
| 74 |
} |
| 75 |
) |
| 76 |
|
| 77 |
dec := json.NewDecoder(f) |
| 78 |
for err = nil; err == nil; { |
| 79 |
if err = dec.Decode(&log); err != nil && err != io.EOF { |
| 80 |
return "", err |
| 81 |
} |
| 82 |
if log.Level == "error" { |
| 83 |
errMsg = strings.TrimSpace(log.Msg) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return errMsg, nil |
| 88 |
} |
| 89 |
|
| 90 |
// criuError returns only the first line of the error message from criu |
| 91 |
// it tries to add an invalid dump log location when returning the message |
| 92 |
func criuError(err error) string { |
| 93 |
parts := strings.Split(err.Error(), "\n") |
| 94 |
return parts[0] |
| 95 |
} |
| 96 |
|
| 97 |
func copyFile(to, from string) error { |
| 98 |
ff, err := os.Open(from) |
| 99 |
if err != nil { |
| 100 |
return err |
| 101 |
} |
| 102 |
defer ff.Close() |
| 103 |
tt, err := os.Create(to) |
| 104 |
if err != nil { |
| 105 |
return err |
| 106 |
} |
| 107 |
defer tt.Close() |
| 108 |
|
| 109 |
p := bufPool.Get().(*[]byte) |
| 110 |
defer bufPool.Put(p) |
| 111 |
_, err = io.CopyBuffer(tt, ff, *p) |
| 112 |
return err |
| 113 |
} |
| 114 |
|
| 115 |
func checkKillError(err error) error { |
| 116 |
if err == nil { |
| 117 |
return nil |
| 118 |
} |
| 119 |
if strings.Contains(err.Error(), "os: process already finished") || |
| 120 |
strings.Contains(err.Error(), "container not running") || |
| 121 |
strings.Contains(strings.ToLower(err.Error()), "no such process") || |
| 122 |
err == unix.ESRCH { |
| 123 |
return fmt.Errorf("process already finished: %w", errdefs.ErrNotFound) |
| 124 |
} else if strings.Contains(err.Error(), "does not exist") { |
| 125 |
return fmt.Errorf("no such container: %w", errdefs.ErrNotFound) |
| 126 |
} |
| 127 |
return fmt.Errorf("unknown error after kill: %w", err) |
| 128 |
} |
| 129 |
|
| 130 |
func newPidFile(bundle string) *pidFile { |
| 131 |
return &pidFile{ |
| 132 |
path: filepath.Join(bundle, InitPidFile), |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func newExecPidFile(bundle, id string) *pidFile { |
| 137 |
return &pidFile{ |
| 138 |
path: filepath.Join(bundle, fmt.Sprintf("%s.pid", id)), |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
type pidFile struct { |
| 143 |
path string |
| 144 |
} |
| 145 |
|
| 146 |
func (p *pidFile) Path() string { |
| 147 |
return p.path |
| 148 |
} |
| 149 |
|
| 150 |
func (p *pidFile) Read() (int, error) { |
| 151 |
return runc.ReadPidFile(p.path) |
| 152 |
} |
| 153 |
|
| 154 |
// waitTimeout handles waiting on a waitgroup with a specified timeout. |
| 155 |
// this is commonly used for waiting on IO to finish after a process has exited |
| 156 |
func waitTimeout(ctx context.Context, wg *sync.WaitGroup, timeout time.Duration) error { |
| 157 |
ctx, cancel := context.WithTimeout(ctx, timeout) |
| 158 |
defer cancel() |
| 159 |
done := make(chan struct{}) |
| 160 |
go func() { |
| 161 |
wg.Wait() |
| 162 |
close(done) |
| 163 |
}() |
| 164 |
select { |
| 165 |
case <-done: |
| 166 |
return nil |
| 167 |
case <-ctx.Done(): |
| 168 |
return ctx.Err() |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
func stateName(v interface{}) string { |
| 173 |
switch v.(type) { |
| 174 |
case *runningState, *execRunningState: |
| 175 |
return "running" |
| 176 |
case *createdState, *execCreatedState, *createdCheckpointState: |
| 177 |
return "created" |
| 178 |
case *pausedState: |
| 179 |
return "paused" |
| 180 |
case *deletedState: |
| 181 |
return "deleted" |
| 182 |
case *stoppedState: |
| 183 |
return "stopped" |
| 184 |
} |
| 185 |
panic(fmt.Errorf("invalid state %v", v)) |
| 186 |
} |