github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/runtime/v2/shim/util_unix.go (about) 1 // +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 shim 20 21 import ( 22 "context" 23 "crypto/sha256" 24 "fmt" 25 "net" 26 "os" 27 "path/filepath" 28 "strings" 29 "syscall" 30 "time" 31 32 "github.com/containerd/containerd/namespaces" 33 "github.com/containerd/containerd/pkg/dialer" 34 "github.com/containerd/containerd/sys" 35 "github.com/pkg/errors" 36 ) 37 38 const shimBinaryFormat = "containerd-shim-%s-%s" 39 40 func getSysProcAttr() *syscall.SysProcAttr { 41 return &syscall.SysProcAttr{ 42 Setpgid: true, 43 } 44 } 45 46 // SetScore sets the oom score for a process 47 func SetScore(pid int) error { 48 return sys.SetOOMScore(pid, sys.OOMScoreMaxKillable) 49 } 50 51 // AdjustOOMScore sets the OOM score for the process to the parents OOM score +1 52 // to ensure that they parent has a lower* score than the shim 53 func AdjustOOMScore(pid int) error { 54 parent := os.Getppid() 55 score, err := sys.GetOOMScoreAdj(parent) 56 if err != nil { 57 return errors.Wrap(err, "get parent OOM score") 58 } 59 shimScore := score + 1 60 if err := sys.SetOOMScore(pid, shimScore); err != nil { 61 return errors.Wrap(err, "set shim OOM score") 62 } 63 return nil 64 } 65 66 // SocketAddress returns an abstract socket address 67 func SocketAddress(ctx context.Context, id string) (string, error) { 68 ns, err := namespaces.NamespaceRequired(ctx) 69 if err != nil { 70 return "", err 71 } 72 d := sha256.Sum256([]byte(filepath.Join(ns, id))) 73 return filepath.Join(string(filepath.Separator), "containerd-shim", fmt.Sprintf("%x.sock", d)), nil 74 } 75 76 // AnonDialer returns a dialer for an abstract socket 77 func AnonDialer(address string, timeout time.Duration) (net.Conn, error) { 78 address = strings.TrimPrefix(address, "unix://") 79 return dialer.Dialer("\x00"+address, timeout) 80 } 81 82 func AnonReconnectDialer(address string, timeout time.Duration) (net.Conn, error) { 83 return AnonDialer(address, timeout) 84 } 85 86 // NewSocket returns a new socket 87 func NewSocket(address string) (*net.UnixListener, error) { 88 if len(address) > 106 { 89 return nil, errors.Errorf("%q: unix socket path too long (> 106)", address) 90 } 91 l, err := net.Listen("unix", "\x00"+address) 92 if err != nil { 93 return nil, errors.Wrapf(err, "failed to listen to abstract unix socket %q", address) 94 } 95 return l.(*net.UnixListener), nil 96 }