github.com/demonoid81/containerd@v1.3.4/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  	"github.com/containerd/containerd/log"
    25  	srvconfig "github.com/containerd/containerd/services/server/config"
    26  	"github.com/containerd/containerd/sys"
    27  	"github.com/containerd/ttrpc"
    28  	specs "github.com/opencontainers/runtime-spec/specs-go"
    29  )
    30  
    31  // apply sets config settings on the server process
    32  func apply(ctx context.Context, config *srvconfig.Config) error {
    33  	if config.OOMScore != 0 {
    34  		log.G(ctx).Debugf("changing OOM score to %d", config.OOMScore)
    35  		if err := sys.SetOOMScore(os.Getpid(), config.OOMScore); err != nil {
    36  			log.G(ctx).WithError(err).Errorf("failed to change OOM score to %d", config.OOMScore)
    37  		}
    38  	}
    39  	if config.Cgroup.Path != "" {
    40  		cg, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(config.Cgroup.Path))
    41  		if err != nil {
    42  			if err != cgroups.ErrCgroupDeleted {
    43  				return err
    44  			}
    45  			if cg, err = cgroups.New(cgroups.V1, cgroups.StaticPath(config.Cgroup.Path), &specs.LinuxResources{}); err != nil {
    46  				return err
    47  			}
    48  		}
    49  		if err := cg.Add(cgroups.Process{
    50  			Pid: os.Getpid(),
    51  		}); err != nil {
    52  			return err
    53  		}
    54  	}
    55  	return nil
    56  }
    57  
    58  func newTTRPCServer() (*ttrpc.Server, error) {
    59  	return ttrpc.NewServer(ttrpc.WithServerHandshaker(ttrpc.UnixSocketRequireSameUser()))
    60  }