-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Rename the files - v1.go: cgroupv1 aka legacy; - v2.go: cgroupv2 aka unified hierarchy; - unsupported.go: when systemd is not available. 2. Move the code that is common between v1 and v2 to common.go Signed-off-by: Kir Kolyshkin <[email protected]>
- Loading branch information
Showing
4 changed files
with
101 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package systemd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"sync" | ||
|
||
systemdDbus "github.com/coreos/go-systemd/v22/dbus" | ||
dbus "github.com/godbus/dbus/v5" | ||
"github.com/opencontainers/runc/libcontainer/configs" | ||
) | ||
|
||
var ( | ||
connOnce sync.Once | ||
connDbus *systemdDbus.Conn | ||
connErr error | ||
) | ||
|
||
// NOTE: This function comes from package github.com/coreos/go-systemd/util | ||
// It was borrowed here to avoid a dependency on cgo. | ||
// | ||
// IsRunningSystemd checks whether the host was booted with systemd as its init | ||
// system. This functions similarly to systemd's `sd_booted(3)`: internally, it | ||
// checks whether /run/systemd/system/ exists and is a directory. | ||
// http://www.freedesktop.org/software/systemd/man/sd_booted.html | ||
func IsRunningSystemd() bool { | ||
fi, err := os.Lstat("/run/systemd/system") | ||
if err != nil { | ||
return false | ||
} | ||
return fi.IsDir() | ||
} | ||
|
||
// systemd represents slice hierarchy using `-`, so we need to follow suit when | ||
// generating the path of slice. Essentially, test-a-b.slice becomes | ||
// /test.slice/test-a.slice/test-a-b.slice. | ||
func ExpandSlice(slice string) (string, error) { | ||
suffix := ".slice" | ||
// Name has to end with ".slice", but can't be just ".slice". | ||
if len(slice) < len(suffix) || !strings.HasSuffix(slice, suffix) { | ||
return "", fmt.Errorf("invalid slice name: %s", slice) | ||
} | ||
|
||
// Path-separators are not allowed. | ||
if strings.Contains(slice, "/") { | ||
return "", fmt.Errorf("invalid slice name: %s", slice) | ||
} | ||
|
||
var path, prefix string | ||
sliceName := strings.TrimSuffix(slice, suffix) | ||
// if input was -.slice, we should just return root now | ||
if sliceName == "-" { | ||
return "/", nil | ||
} | ||
for _, component := range strings.Split(sliceName, "-") { | ||
// test--a.slice isn't permitted, nor is -test.slice. | ||
if component == "" { | ||
return "", fmt.Errorf("invalid slice name: %s", slice) | ||
} | ||
|
||
// Append the component to the path and to the prefix. | ||
path += "/" + prefix + component + suffix | ||
prefix += component + "-" | ||
} | ||
return path, nil | ||
} | ||
|
||
// getDbusConnection lazy initializes systemd dbus connection | ||
// and returns it | ||
func getDbusConnection() (*systemdDbus.Conn, error) { | ||
connOnce.Do(func() { | ||
connDbus, connErr = systemdDbus.New() | ||
}) | ||
return connDbus, connErr | ||
} | ||
|
||
func newProp(name string, units interface{}) systemdDbus.Property { | ||
return systemdDbus.Property{ | ||
Name: name, | ||
Value: dbus.MakeVariant(units), | ||
} | ||
} | ||
|
||
func getUnitName(c *configs.Cgroup) string { | ||
// by default, we create a scope unless the user explicitly asks for a slice. | ||
if !strings.HasSuffix(c.Name, ".slice") { | ||
return fmt.Sprintf("%s-%s.scope", c.ScopePrefix, c.Name) | ||
} | ||
return c.Name | ||
} | ||
|
||
// isUnitExists returns true if the error is that a systemd unit already exists. | ||
func isUnitExists(err error) bool { | ||
if err != nil { | ||
if dbusError, ok := err.(dbus.Error); ok { | ||
return strings.Contains(dbusError.Name, "org.freedesktop.systemd1.UnitExists") | ||
} | ||
} | ||
return false | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.