-
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.
Tested with both Podman (master) and Moby (master), on Ubuntu 19.10 . $ podman --cgroup-manager=systemd run -it --rm --runtime=runc \ --cgroupns=host --memory 42m --cpus 0.42 --pids-limit 42 alpine / # cat /proc/self/cgroup 0::/user.slice/user-1001.slice/user@1001.service/user.slice/libpod-132ff0d72245e6f13a3bbc6cdc5376886897b60ac59eaa8dea1df7ab959cbf1c.scope / # cat /sys/fs/cgroup/user.slice/user-1001.slice/user@1001.service/user.slice/libpod-132ff0d72245e6f13a3bbc6cdc5376886897b60ac59eaa8dea1df7ab959cbf1c.scope/memory.max 44040192 / # cat /sys/fs/cgroup/user.slice/user-1001.slice/user@1001.service/user.slice/libpod-132ff0d72245e6f13a3bbc6cdc5376886897b60ac59eaa8dea1df7ab959cbf1c.scope/cpu.max 42000 100000 / # cat /sys/fs/cgroup/user.slice/user-1001.slice/user@1001.service/user.slice/libpod-132ff0d72245e6f13a3bbc6cdc5376886897b60ac59eaa8dea1df7ab959cbf1c.scope/pids.max 42 Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
- Loading branch information
1 parent
e3e26ca
commit 159eecf
Showing
7 changed files
with
163 additions
and
20 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
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
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
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,103 @@ | ||
// +build linux | ||
|
||
package systemd | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
systemdDbus "github.com/coreos/go-systemd/v22/dbus" | ||
dbus "github.com/godbus/dbus/v5" | ||
"github.com/opencontainers/runc/libcontainer/system" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// NewUserSystemdDbus creates a connection for systemd user-instance. | ||
func NewUserSystemdDbus() (*systemdDbus.Conn, error) { | ||
addr, err := DetectUserDbusSessionBusAddress() | ||
if err != nil { | ||
return nil, err | ||
} | ||
uid, err := DetectUID() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return systemdDbus.NewConnection(func() (*dbus.Conn, error) { | ||
conn, err := dbus.Dial(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
methods := []dbus.Auth{dbus.AuthExternal(strconv.Itoa(uid))} | ||
err = conn.Auth(methods) | ||
if err != nil { | ||
conn.Close() | ||
return nil, err | ||
} | ||
if err = conn.Hello(); err != nil { | ||
conn.Close() | ||
return nil, err | ||
} | ||
return conn, nil | ||
}) | ||
} | ||
|
||
// DetectUID detects UID from the OwnerUID field of `busctl --user status` | ||
// if running in userNS. The value corresponds to sd_bus_creds_get_owner_uid(3) . | ||
// | ||
// Otherwise returns os.Getuid() . | ||
func DetectUID() (int, error) { | ||
if !system.RunningInUserNS() { | ||
return os.Getuid(), nil | ||
} | ||
b, err := exec.Command("busctl", "--user", "--no-pager", "status").CombinedOutput() | ||
if err != nil { | ||
return -1, errors.Wrap(err, "could not execute `busctl --user --no-pager status`") | ||
} | ||
scanner := bufio.NewScanner(bytes.NewReader(b)) | ||
for scanner.Scan() { | ||
s := strings.TrimSpace(scanner.Text()) | ||
if strings.HasPrefix(s, "OwnerUID=") { | ||
uidStr := strings.TrimPrefix(s, "OwnerUID=") | ||
i, err := strconv.Atoi(uidStr) | ||
if err != nil { | ||
return -1, errors.Wrapf(err, "could not detect the OwnerUID: %s", s) | ||
} | ||
return i, nil | ||
} | ||
} | ||
return 0, nil | ||
} | ||
|
||
// DetectUserDbusSessionBusAddress returns $DBUS_SESSION_BUS_ADDRESS if set. | ||
// Otherwise returns "unix:path=$XDG_RUNTIME_DIR/bus" if $XDG_RUNTIME_DIR/bus exists. | ||
// Otheriwe parses the value from `systemctl --user show-environment` . | ||
func DetectUserDbusSessionBusAddress() (string, error) { | ||
if env := os.Getenv("DBUS_SESSION_BUS_ADDRESS"); env != "" { | ||
return env, nil | ||
} | ||
if xdr := os.Getenv("XDG_RUNTIME_DIR"); xdr != "" { | ||
busPath := filepath.Join(xdr, "bus") | ||
if _, err := os.Stat(busPath); err == nil { | ||
busAddress := "unix:path=" + busPath | ||
return busAddress, nil | ||
} | ||
} | ||
b, err := exec.Command("systemctl", "--user", "--no-pager", "show-environment").CombinedOutput() | ||
if err != nil { | ||
return "", errors.Wrap(err, "could not execute `systemctl --user --no-pager show-environment`") | ||
} | ||
scanner := bufio.NewScanner(bytes.NewReader(b)) | ||
for scanner.Scan() { | ||
s := strings.TrimSpace(scanner.Text()) | ||
if strings.HasPrefix(s, "DBUS_SESSION_BUS_ADDRESS=") { | ||
return strings.TrimPrefix(s, "DBUS_SESSION_BUS_ADDRESS="), nil | ||
} | ||
} | ||
return "", errors.New("could not detect DBUS_SESSION_BUS_ADDRESS from `systemctl --user --no-pager show-environment`") | ||
} |
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
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
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