| 1 |
//go:build linux |
| 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 runc |
| 20 |
|
| 21 |
import ( |
| 22 |
"context" |
| 23 |
"encoding/json" |
| 24 |
"os" |
| 25 |
"path/filepath" |
| 26 |
|
| 27 |
"github.com/containerd/log" |
| 28 |
"github.com/opencontainers/runtime-spec/specs-go" |
| 29 |
) |
| 30 |
|
| 31 |
// ShouldKillAllOnExit reads the bundle's OCI spec and returns true if |
| 32 |
// there is an error reading the spec or if the container has a private PID namespace |
| 33 |
func ShouldKillAllOnExit(ctx context.Context, bundlePath string) bool { |
| 34 |
spec, err := readSpec(bundlePath) |
| 35 |
if err != nil { |
| 36 |
log.G(ctx).WithError(err).Error("shouldKillAllOnExit: failed to read config.json") |
| 37 |
return true |
| 38 |
} |
| 39 |
|
| 40 |
if spec.Linux != nil { |
| 41 |
for _, ns := range spec.Linux.Namespaces { |
| 42 |
if ns.Type == specs.PIDNamespace && ns.Path == "" { |
| 43 |
return false |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
return true |
| 48 |
} |
| 49 |
|
| 50 |
func readSpec(p string) (*specs.Spec, error) { |
| 51 |
const configFileName = "config.json" |
| 52 |
f, err := os.Open(filepath.Join(p, configFileName)) |
| 53 |
if err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
defer f.Close() |
| 57 |
var s specs.Spec |
| 58 |
if err := json.NewDecoder(f).Decode(&s); err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
return &s, nil |
| 62 |
} |