github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/cgroups/systemd.go (about)

     1  package cgroups
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	systemdDbus "github.com/coreos/go-systemd/v22/dbus"
     9  	"github.com/godbus/dbus/v5"
    10  )
    11  
    12  func systemdCreate(path string, c *systemdDbus.Conn) error {
    13  	slice, name := filepath.Split(path)
    14  	slice = strings.TrimSuffix(slice, "/")
    15  
    16  	var lastError error
    17  	for i := 0; i < 2; i++ {
    18  		properties := []systemdDbus.Property{
    19  			systemdDbus.PropDescription(fmt.Sprintf("cgroup %s", name)),
    20  			systemdDbus.PropWants(slice),
    21  		}
    22  		pMap := map[string]bool{
    23  			"DefaultDependencies": false,
    24  			"MemoryAccounting":    true,
    25  			"CPUAccounting":       true,
    26  			"BlockIOAccounting":   true,
    27  		}
    28  		if i == 0 {
    29  			pMap["Delegate"] = true
    30  		}
    31  		for k, v := range pMap {
    32  			p := systemdDbus.Property{
    33  				Name:  k,
    34  				Value: dbus.MakeVariant(v),
    35  			}
    36  			properties = append(properties, p)
    37  		}
    38  
    39  		ch := make(chan string)
    40  		_, err := c.StartTransientUnit(name, "replace", properties, ch)
    41  		if err != nil {
    42  			lastError = err
    43  			continue
    44  		}
    45  		<-ch
    46  		return nil
    47  	}
    48  	return lastError
    49  }
    50  
    51  /*
    52     systemdDestroyConn is copied from containerd/cgroups/systemd.go file, that
    53     has the following license:
    54  
    55     Copyright The containerd Authors.
    56  
    57     Licensed under the Apache License, Version 2.0 (the "License");
    58     you may not use this file except in compliance with the License.
    59     You may obtain a copy of the License at
    60  
    61         http://www.apache.org/licenses/LICENSE-2.0
    62  
    63     Unless required by applicable law or agreed to in writing, software
    64     distributed under the License is distributed on an "AS IS" BASIS,
    65     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    66     See the License for the specific language governing permissions and
    67     limitations under the License.
    68  */
    69  func systemdDestroyConn(path string, c *systemdDbus.Conn) error {
    70  	name := filepath.Base(path)
    71  
    72  	ch := make(chan string)
    73  	_, err := c.StopUnit(name, "replace", ch)
    74  	if err != nil {
    75  		return err
    76  	}
    77  	<-ch
    78  	return nil
    79  }