github.com/containerd/Containerd@v1.4.13/services/server/server_linux.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package server
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  
    23  	"github.com/containerd/cgroups"
    24  	cgroupsv2 "github.com/containerd/cgroups/v2"
    25  	"github.com/containerd/containerd/log"
    26  	srvconfig "github.com/containerd/containerd/services/server/config"
    27  	"github.com/containerd/containerd/sys"
    28  	"github.com/containerd/ttrpc"
    29  	specs "github.com/opencontainers/runtime-spec/specs-go"
    30  )
    31  
    32  // apply sets config settings on the server process
    33  func apply(ctx context.Context, config *srvconfig.Config) error {
    34  	if config.OOMScore != 0 {
    35  		log.G(ctx).Debugf("changing OOM score to %d", config.OOMScore)
    36  		if err := sys.SetOOMScore(os.Getpid(), config.OOMScore); err != nil {
    37  			log.G(ctx).WithError(err).Errorf("failed to change OOM score to %d", config.OOMScore)
    38  		}
    39  	}
    40  	if config.Cgroup.Path != "" {
    41  		if cgroups.Mode() == cgroups.Unified {
    42  			cg, err := cgroupsv2.LoadManager("/sys/fs/cgroup", config.Cgroup.Path)
    43  			if err != nil {
    44  				if err != cgroupsv2.ErrCgroupDeleted {
    45  					return err
    46  				}
    47  				if cg, err = cgroupsv2.NewManager("/sys/fs/cgroup", config.Cgroup.Path, nil); err != nil {
    48  					return err
    49  				}
    50  			}
    51  			if err := cg.AddProc(uint64(os.Getpid())); err != nil {
    52  				return err
    53  			}
    54  		} else {
    55  			cg, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(config.Cgroup.Path))
    56  			if err != nil {
    57  				if err != cgroups.ErrCgroupDeleted {
    58  					return err
    59  				}
    60  				if cg, err = cgroups.New(cgroups.V1, cgroups.StaticPath(config.Cgroup.Path), &specs.LinuxResources{}); err != nil {
    61  					return err
    62  				}
    63  			}
    64  			if err := cg.Add(cgroups.Process{
    65  				Pid: os.Getpid(),
    66  			}); err != nil {
    67  				return err
    68  			}
    69  		}
    70  	}
    71  	return nil
    72  }
    73  
    74  func newTTRPCServer() (*ttrpc.Server, error) {
    75  	return ttrpc.NewServer(ttrpc.WithServerHandshaker(ttrpc.UnixSocketRequireSameUser()))
    76  }